]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
removed freenode.txt
[thirdparty/dracut.git] / dracut-functions.sh
CommitLineData
04b56f3a 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
04b56f3a 4#
33ee031c 5# functions used by dracut and other tools.
04b56f3a 6#
33ee031c 7# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
0f9c78c1 20# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 21#
258de828 22export LC_MESSAGES=C
f4031e8a
HH
23
24if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
25 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
26 mkdir -p "$initdir/.kernelmodseen"
27 fi
28 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
29fi
ec1aa3bc 30
eaa924b6
HH
31if [[ $initdir ]] && ! [[ -d $initdir ]]; then
32 mkdir -p "$initdir"
33fi
34
8d95b8b3 35# Generic substring function. If $2 is in $1, return 0.
520047c3 36strstr() { [[ $1 = *$2* ]]; }
8d95b8b3 37
f4031e8a
HH
38# find a binary. If we were not passed the full path directly,
39# search in the usual places to find the binary.
40find_binary() {
41 if [[ -z ${1##/*} ]]; then
b093aa2d
HH
42 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
43 printf "%s\n" "$1"
f4031e8a
HH
44 return 0
45 fi
46 fi
47
b093aa2d 48 type -P "${1##*/}"
f4031e8a
HH
49}
50
0874654c
HH
51if ! [[ $dracutbasedir ]]; then
52 dracutbasedir=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
54 [[ $dracutbasedir ]] || dracutbasedir="."
55 dracutbasedir="$(readlink -f $dracutbasedir)"
56fi
57
4fe1bdd4
HH
58ldconfig_paths()
59{
60 local a i
61 declare -A a
62 for i in $(
63 ldconfig -pN 2>/dev/null | while read a b c d; do
64 [[ "$c" != "=>" ]] && continue
466a5998 65 printf "%s\n" ${d%/*};
4fe1bdd4
HH
66 done
67 ); do
68 a["$i"]=1;
69 done;
70 printf "%s\n" ${!a[@]}
71}
72
8d95b8b3
HH
73# Detect lib paths
74if ! [[ $libdirs ]] ; then
b093aa2d 75 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
8d95b8b3
HH
76 && [[ -d /lib64 ]]; then
77 libdirs+=" /lib64"
78 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
79 else
80 libdirs+=" /lib"
81 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
82 fi
4fe1bdd4
HH
83
84 libdirs+="$(ldconfig_paths)"
85
8d95b8b3
HH
86 export libdirs
87fi
88
89if ! [[ $kernel ]]; then
90 kernel=$(uname -r)
91 export kernel
92fi
93
f06c2b58
HH
94# Version comparision function. Assumes Linux style version scheme.
95# $1 = version a
96# $2 = comparision op (gt, ge, eq, le, lt, ne)
97# $3 = version b
98vercmp() {
99 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
100
101 for ((_i=0; ; _i++))
102 do
103 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
104 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
105 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
106 else continue
107 fi
108 break
109 done
110
111 case $_op in
112 gt) ((_res == 1));;
113 ge) ((_res != 2));;
114 eq) ((_res == 0));;
115 le) ((_res != 1));;
116 lt) ((_res == 2));;
117 ne) ((_res != 0));;
118 esac
119}
120
8d95b8b3 121srcmods="/lib/modules/$kernel/"
f06c2b58 122
8d95b8b3 123[[ $drivers_dir ]] && {
f06c2b58 124 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
8d95b8b3
HH
125 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
126 exit 1
127 fi
128 srcmods="$drivers_dir"
129}
130export srcmods
131
d6d53f60 132if ! type dinfo >/dev/null 2>&1; then
552ecca6 133 . "$dracutbasedir/dracut-logger.sh"
e103615b
134 dlog_init
135fi
136
593b315c
HH
137if ! [[ $initdir ]]; then
138 dfatal "initdir not set"
139 exit 1
140fi
141
7e2bca48
HH
142# export standard hookdirs
143[[ $hookdirs ]] || {
4fed3ddf 144 hookdirs="cmdline pre-udev pre-trigger netroot "
6e3cc00f 145 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
eef7649e 146 hookdirs+="pre-mount pre-pivot cleanup mount "
2b6dd6a1 147 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
7e2bca48
HH
148 export hookdirs
149}
150
8d85429a
HH
151dracut_need_initqueue() {
152 >"$initdir/lib/dracut/need-initqueue"
153}
154
155dracut_module_included() {
50411d30 156 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
8d85429a
HH
157}
158
87122afc
159# Create all subdirectories for given path without creating the last element.
160# $1 = path
b093aa2d
HH
161mksubdirs() {
162 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
163}
87122afc 164
7e2bca48
HH
165# is_func <command>
166# Check whether $1 is a function.
95d2dabc 167is_func() {
b093aa2d 168 [[ "$(type -t "$1")" = "function" ]]
95d2dabc
HH
169}
170
87122afc
171# Function prints global variables in format name=value line by line.
172# $@ = list of global variables' name
173print_vars() {
29b10e65 174 local _var _value
87122afc 175
b093aa2d 176 for _var in "$@"
87122afc 177 do
b093aa2d
HH
178 eval printf -v _value "%s" "\$$_var"
179 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
87122afc
180 done
181}
182
7e2bca48
HH
183# normalize_path <path>
184# Prints the normalized path, where it removes any duplicated
185# and trailing slashes.
186# Example:
187# $ normalize_path ///test/test//
188# /test/test
626d9eba 189normalize_path() {
c44e3cb4
MS
190 shopt -q -s extglob
191 set -- "${1//+(\/)//}"
192 shopt -q -u extglob
466a5998 193 printf "%s\n" "${1%/}"
626d9eba 194}
d4bb4316 195
7e2bca48
HH
196# convert_abs_rel <from> <to>
197# Prints the relative path, when creating a symlink to <to> from <from>.
198# Example:
199# $ convert_abs_rel /usr/bin/test /bin/test-2
200# ../../bin/test-2
201# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
d4bb4316 202convert_abs_rel() {
6d2a7942 203 local __current __absolute __abssize __cursize __newpath
c44e3cb4 204 local -i __i __level
d4bb4316 205
c44e3cb4
MS
206 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
207
c1609dd4 208 # corner case #1 - self looping link
466a5998 209 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
c1609dd4
MS
210
211 # corner case #2 - own dir link
466a5998 212 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
c1609dd4 213
6d2a7942
HH
214 IFS="/" __current=($1)
215 IFS="/" __absolute=($2)
d4bb4316
HH
216
217 __abssize=${#__absolute[@]}
218 __cursize=${#__current[@]}
219
b093aa2d 220 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
d4bb4316
HH
221 do
222 (( __level++ ))
223 if (( __level > __abssize || __level > __cursize ))
224 then
225 break
226 fi
227 done
228
229 for ((__i = __level; __i < __cursize-1; __i++))
230 do
231 if ((__i > __level))
232 then
233 __newpath=$__newpath"/"
234 fi
235 __newpath=$__newpath".."
236 done
237
238 for ((__i = __level; __i < __abssize; __i++))
239 do
240 if [[ -n $__newpath ]]
241 then
242 __newpath=$__newpath"/"
243 fi
244 __newpath=$__newpath${__absolute[__i]}
245 done
246
466a5998 247 printf "%s\n" "$__newpath"
d4bb4316
HH
248}
249
b093aa2d 250if [[ "$(ln --help)" == *--relative* ]]; then
a20d24de
HH
251 ln_r() {
252 ln -sfnr "${initdir}/$1" "${initdir}/$2"
253 }
254else
255 ln_r() {
256 local _source=$1
257 local _dest=$2
258 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
b093aa2d 259 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
a20d24de
HH
260 }
261fi
262
7e2bca48 263# get_fs_env <device>
97af51db 264# Get and the ID_FS_TYPE variable from udev for a device.
7e2bca48 265# Example:
97af51db 266# $ get_fs_env /dev/sda2
7e2bca48 267# ext4
9ede1929 268get_fs_env() {
0e979d48
HH
269 local evalstr
270 local found
271
7c179686 272 [[ $1 ]] || return
29b10e65 273 unset ID_FS_TYPE
69f7ed96
HH
274 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
275 | while read line; do
276 if [[ "$line" == TYPE\=* ]]; then
277 printf "%s" "${line#TYPE=}";
278 exit 0;
279 fi
280 done)
281 if [[ $ID_FS_TYPE ]]; then
282 printf "%s" "$ID_FS_TYPE"
283 return 0
7c179686 284 fi
a5cde2dd
HH
285 return 1
286}
9ede1929 287
7e2bca48
HH
288# get_maj_min <device>
289# Prints the major and minor of a device node.
290# Example:
291# $ get_maj_min /dev/sda2
292# 8:2
480d772f 293get_maj_min() {
dba20559
HH
294 local _maj _min _majmin
295 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
296 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
480d772f
HH
297}
298
69f7ed96
HH
299# get a persistent path from a device
300get_persistent_dev() {
301 local i _tmp _dev
302
303 _dev=$(get_maj_min "$1")
304 [ -z "$_dev" ] && return
305
324ea606
HH
306 for i in \
307 /dev/mapper/* \
3411ad66 308 /dev/disk/${persistent_policy:-by-uuid}/* \
324ea606
HH
309 /dev/disk/by-uuid/* \
310 /dev/disk/by-label/* \
311 /dev/disk/by-partuuid/* \
312 /dev/disk/by-partlabel/* \
313 /dev/disk/by-id/* \
314 /dev/disk/by-path/* \
315 ; do
1743473b 316 [[ $i == /dev/mapper/control ]] && continue
69f7ed96
HH
317 [[ $i == /dev/mapper/mpath* ]] && continue
318 _tmp=$(get_maj_min "$i")
319 if [ "$_tmp" = "$_dev" ]; then
320 printf -- "%s" "$i"
321 return
322 fi
323 done
324}
325
c82a1133
HH
326expand_persistent_dev() {
327 local _dev=$1
328
329 case "$_dev" in
330 LABEL=*)
331 _dev="/dev/disk/by-label/${_dev#LABEL=}"
332 ;;
333 UUID=*)
334 _dev="${_dev#UUID=}"
335 _dev="$(tr "[:upper:]" "[:lower:]" <<< "$_dev" )"
336 _dev="/dev/disk/by-uuid/${_dev}"
337 ;;
338 PARTUUID=*)
339 _dev="${_dev#PARTUUID=}"
340 _dev="$(tr "[:upper:]" "[:lower:]" <<< "$_dev" )"
341 _dev="/dev/disk/by-partuuid/${_dev}"
342 ;;
343 PARTLABEL=*)
344 _dev="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
345 ;;
346 esac
347 printf "%s" "$_dev"
348}
349
324ea606 350shorten_persistent_dev() {
c82a1133
HH
351 local _dev="$1"
352 case "$_dev" in
324ea606 353 /dev/disk/by-uuid/*)
c82a1133 354 printf "%s" "UUID=${_dev##*/}";;
324ea606 355 /dev/disk/by-label/*)
c82a1133 356 printf "%s" "LABEL=${_dev##*/}";;
324ea606 357 /dev/disk/by-partuuid/*)
c82a1133 358 printf "%s" "PARTUUID=${_dev##*/}";;
324ea606 359 /dev/disk/by-partlabel/*)
c82a1133 360 printf "%s" "PARTLABEL=${_dev##*/}";;
324ea606 361 *)
c82a1133 362 printf "%s" "$_dev";;
324ea606
HH
363 esac
364}
365
7e2bca48
HH
366# find_block_device <mountpoint>
367# Prints the major and minor number of the block device
368# for a given mountpoint.
369# Unless $use_fstab is set to "yes" the functions
370# uses /proc/self/mountinfo as the primary source of the
371# information and only falls back to /etc/fstab, if the mountpoint
372# is not found there.
373# Example:
374# $ find_block_device /usr
375# 8:4
f76ef3aa 376find_block_device() {
9d36d4fb 377 local _majmin _dev _majmin _find_mpt
c8d685c9 378 _find_mpt="$1"
0b706743 379 if [[ $use_fstab != yes ]]; then
9d36d4fb 380 [[ -d $_find_mpt/. ]]
dba20559
HH
381 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
382 while read _majmin _dev; do
383 if [[ -b $_dev ]]; then
384 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
385 _majmin=$(get_maj_min $_dev)
386 fi
387 if [[ $_majmin ]]; then
466a5998 388 printf "%s\n" "$_majmin"
dba20559 389 else
466a5998 390 printf "%s\n" "$_dev"
dba20559
HH
391 fi
392 return 0
393 fi
394 if [[ $_dev = *:* ]]; then
466a5998 395 printf "%s\n" "$_dev"
dba20559
HH
396 return 0
397 fi
398 done; return 1; } && return 0
399 fi
400 # fall back to /etc/fstab
401
402 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
9d36d4fb 403 while read _majmin _dev; do
dba20559
HH
404 if ! [[ $_dev ]]; then
405 _dev="$_majmin"
406 unset _majmin
407 fi
9d36d4fb 408 if [[ -b $_dev ]]; then
dba20559 409 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
9d36d4fb 410 if [[ $_majmin ]]; then
466a5998 411 printf "%s\n" "$_majmin"
9d36d4fb 412 else
466a5998 413 printf "%s\n" "$_dev"
9d36d4fb
HH
414 fi
415 return 0
416 fi
417 if [[ $_dev = *:* ]]; then
466a5998 418 printf "%s\n" "$_dev"
9d36d4fb 419 return 0
cc02093d 420 fi
dba20559 421 done; return 1; } && return 0
0b706743
422
423 return 1
17829e94
VL
424}
425
9d36d4fb
HH
426# find_mp_fstype <mountpoint>
427# Echo the filesystem type for a given mountpoint.
7e2bca48
HH
428# /proc/self/mountinfo is taken as the primary source of information
429# and /etc/fstab is used as a fallback.
430# No newline is appended!
431# Example:
9d36d4fb 432# $ find_mp_fstype /;echo
7e2bca48 433# ext4
9d36d4fb
HH
434find_mp_fstype() {
435 local _fs
7ae5d9d1 436
9d36d4fb 437 if [[ $use_fstab != yes ]]; then
dba20559
HH
438 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
439 while read _fs; do
440 [[ $_fs ]] || continue
441 [[ $_fs = "autofs" ]] && continue
466a5998 442 printf "%s" "$_fs"
dba20559
HH
443 return 0
444 done; return 1; } && return 0
445 fi
446
447 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
9d36d4fb
HH
448 while read _fs; do
449 [[ $_fs ]] || continue
450 [[ $_fs = "autofs" ]] && continue
466a5998 451 printf "%s" "$_fs"
9d36d4fb 452 return 0
dba20559 453 done; return 1; } && return 0
7ae5d9d1
HH
454
455 return 1
456}
457
9d36d4fb
HH
458# find_dev_fstype <device>
459# Echo the filesystem type for a given device.
81672479
HH
460# /proc/self/mountinfo is taken as the primary source of information
461# and /etc/fstab is used as a fallback.
462# No newline is appended!
463# Example:
9d36d4fb 464# $ find_dev_fstype /dev/sda2;echo
81672479 465# ext4
9d36d4fb 466find_dev_fstype() {
5e601454 467 local _find_dev _fs
9d36d4fb 468 _find_dev="$1"
a4f7b504
HH
469 if ! [[ "$_find_dev" = /dev* ]]; then
470 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
471 fi
5e601454
HH
472
473 if [[ $use_fstab != yes ]]; then
dba20559
HH
474 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
475 while read _fs; do
476 [[ $_fs ]] || continue
477 [[ $_fs = "autofs" ]] && continue
466a5998 478 printf "%s" "$_fs"
dba20559
HH
479 return 0
480 done; return 1; } && return 0
481 fi
482
483 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
5e601454
HH
484 while read _fs; do
485 [[ $_fs ]] || continue
486 [[ $_fs = "autofs" ]] && continue
466a5998 487 printf "%s" "$_fs"
5e601454 488 return 0
dba20559 489 done; return 1; } && return 0
5e601454
HH
490
491 return 1
97af51db 492}
5e601454 493
bbc9bfe1
HH
494# find_mp_fsopts <mountpoint>
495# Echo the filesystem options for a given mountpoint.
496# /proc/self/mountinfo is taken as the primary source of information
497# and /etc/fstab is used as a fallback.
498# No newline is appended!
499# Example:
500# $ find_mp_fsopts /;echo
501# rw,relatime,discard,data=ordered
502find_mp_fsopts() {
503 if [[ $use_fstab != yes ]]; then
504 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2>/dev/null && return 0
505 fi
506
507 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
508}
509
97af51db
HH
510# find_dev_fsopts <device>
511# Echo the filesystem options for a given device.
512# /proc/self/mountinfo is taken as the primary source of information
513# and /etc/fstab is used as a fallback.
514# Example:
515# $ find_dev_fsopts /dev/sda2
516# rw,relatime,discard,data=ordered
517find_dev_fsopts() {
518 local _find_dev _opts
519 _find_dev="$1"
520 if ! [[ "$_find_dev" = /dev* ]]; then
521 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
522 fi
523
524 if [[ $use_fstab != yes ]]; then
525 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
526 fi
527
528 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
81672479
HH
529}
530
97af51db 531
7ae5d9d1 532# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
533find_root_block_device() { find_block_device /; }
534
7e2bca48 535# for_each_host_dev_fs <func>
d351541e 536# Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
7e2bca48 537# in ${host_fs_types[@]}
480d772f
HH
538for_each_host_dev_fs()
539{
540 local _func="$1"
d0096de7 541 local _dev
2efa546f 542 local _ret=1
fd191a7b
HH
543
544 [[ "${!host_fs_types[@]}" ]] || return 0
545
d351541e
HH
546 for _dev in "${!host_fs_types[@]}"; do
547 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
480d772f 548 done
2efa546f 549 return $_ret
480d772f
HH
550}
551
fd191a7b
HH
552host_fs_all()
553{
466a5998 554 printf "%s\n" "${host_fs_types[@]}"
fd191a7b
HH
555}
556
17829e94
VL
557# Walk all the slave relationships for a given block device.
558# Stop when our helper function returns success
559# $1 = function to call on every found block device
560# $2 = block device in major:minor format
0b706743 561check_block_and_slaves() {
29b10e65 562 local _x
17829e94
VL
563 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
564 "$1" $2 && return
533d7dc4 565 check_vol_slaves "$@" && return 0
3478b314 566 if [[ -f /sys/dev/block/$2/../dev ]]; then
dba20559 567 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
a3afcf2a 568 fi
17829e94 569 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65 570 for _x in /sys/dev/block/$2/slaves/*/dev; do
d82e0d7d 571 [[ -f $_x ]] || continue
dba20559 572 check_block_and_slaves $1 $(<"$_x") && return 0
17829e94
VL
573 done
574 return 1
575}
576
83e0dc7a
DY
577check_block_and_slaves_all() {
578 local _x _ret=1
579 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
580 if "$1" $2; then
dba20559 581 _ret=0
83e0dc7a
DY
582 fi
583 check_vol_slaves "$@" && return 0
584 if [[ -f /sys/dev/block/$2/../dev ]]; then
dba20559 585 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
83e0dc7a
DY
586 fi
587 [[ -d /sys/dev/block/$2/slaves ]] || return 1
588 for _x in /sys/dev/block/$2/slaves/*/dev; do
589 [[ -f $_x ]] || continue
dba20559 590 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
83e0dc7a
DY
591 done
592 return $_ret
593}
594# for_each_host_dev_and_slaves <func>
595# Execute "<func> <dev>" for every "<dev>" found
596# in ${host_devs[@]} and their slaves
597for_each_host_dev_and_slaves_all()
598{
599 local _func="$1"
600 local _dev
601 local _ret=1
fd191a7b
HH
602
603 [[ "${host_devs[@]}" ]] || return 0
604
83e0dc7a
DY
605 for _dev in ${host_devs[@]}; do
606 [[ -b "$_dev" ]] || continue
83e0dc7a 607 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
dba20559 608 _ret=0
83e0dc7a
DY
609 fi
610 done
611 return $_ret
612}
613
614for_each_host_dev_and_slaves()
615{
616 local _func="$1"
617 local _dev
fd191a7b
HH
618
619 [[ "${host_devs[@]}" ]] || return 0
620
83e0dc7a
DY
621 for _dev in ${host_devs[@]}; do
622 [[ -b "$_dev" ]] || continue
a999414e 623 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
83e0dc7a
DY
624 done
625 return 1
626}
627
533d7dc4
HH
628# ugly workaround for the lvm design
629# There is no volume group device,
630# so, there are no slave devices for volume groups.
631# Logical volumes only have the slave devices they really live on,
632# but you cannot create the logical volume without the volume group.
95bde758 633# And the volume group might be bigger than the devices the LV needs.
533d7dc4 634check_vol_slaves() {
29b10e65 635 local _lv _vg _pv
0b706743 636 for i in /dev/mapper/*; do
1743473b 637 [[ $i == /dev/mapper/control ]] && continue
c4e48eae 638 _lv=$(get_maj_min $i)
29b10e65
HH
639 if [[ $_lv = $2 ]]; then
640 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 641 # strip space
466a5998 642 _vg=$(printf "%s\n" "$_vg")
29b10e65
HH
643 if [[ $_vg ]]; then
644 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 645 do
c4e48eae 646 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
647 done
648 fi
649 fi
533d7dc4
HH
650 done
651 return 1
652}
653
8aa99268
HH
654# fs_get_option <filesystem options> <search for option>
655# search for a specific option in a bunch of filesystem options
656# and return the value
657fs_get_option() {
658 local _fsopts=$1
659 local _option=$2
660 local OLDIFS="$IFS"
661 IFS=,
662 set -- $_fsopts
663 IFS="$OLDIFS"
664 while [ $# -gt 0 ]; do
665 case $1 in
666 $_option=*)
667 echo ${1#${_option}=}
668 break
669 esac
670 shift
671 done
672}
673
89d44e72 674
55a0b3ab
HH
675if ! [[ $DRACUT_INSTALL ]]; then
676 DRACUT_INSTALL=$(find_binary dracut-install)
677fi
19612483 678
55a0b3ab
HH
679if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
680 DRACUT_INSTALL=$dracutbasedir/dracut-install
681fi
3f590c78 682
55a0b3ab
HH
683if ! [[ -x $DRACUT_INSTALL ]]; then
684 dfatal "dracut-install not found!"
685 exit 10
686fi
22048b44 687
55a0b3ab
HH
688[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
689inst_dir() {
690 [[ -e ${initdir}/"$1" ]] && return 0 # already there
691 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
692 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
693}
22048b44 694
55a0b3ab
HH
695inst() {
696 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
697 #dinfo "$DRACUT_INSTALL -l $@"
698 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
699 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
700}
89d44e72 701
55a0b3ab
HH
702inst_simple() {
703 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
704 [[ -e $1 ]] || return 1 # no source
705 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
706 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
707}
89d44e72 708
55a0b3ab
HH
709inst_symlink() {
710 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
711 [[ -L $1 ]] || return 1
712 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
713 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
714}
89d44e72 715
af119460 716inst_multiple() {
55a0b3ab
HH
717 local ret
718 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
719 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
720 ret=$?
721 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
722 return $ret
723}
89d44e72 724
af119460
HH
725dracut_install() {
726 inst_multiple "$@"
727}
728
55a0b3ab
HH
729inst_library() {
730 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
731 [[ -e $1 ]] || return 1 # no source
732 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
733 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
734}
89d44e72 735
55a0b3ab
HH
736inst_binary() {
737 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
738 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
739}
89d44e72 740
55a0b3ab
HH
741inst_script() {
742 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
743 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
744}
36b24d7c 745
a10a1416
746# find symlinks linked to given library file
747# $1 = library file
748# Function searches for symlinks by stripping version numbers appended to
749# library filename, checks if it points to the same target and finally
750# prints the list of symlinks to stdout.
751#
752# Example:
753# rev_lib_symlinks libfoo.so.8.1
754# output: libfoo.so.8 libfoo.so
755# (Only if libfoo.so.8 and libfoo.so exists on host system.)
756rev_lib_symlinks() {
757 [[ ! $1 ]] && return 0
758
759 local fn="$1" orig="$(readlink -f "$1")" links=''
760
b093aa2d 761 [[ ${fn} == *.so.* ]] || return 1
a10a1416
762
763 until [[ ${fn##*.} == so ]]; do
764 fn="${fn%.*}"
765 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
766 done
767
0b706743 768 echo "${links}"
a10a1416
769}
770
62073c30
CG
771# attempt to install any programs specified in a udev rule
772inst_rule_programs() {
773 local _prog _bin
774
775 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
776 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
76d5c79e 777 _bin=""
604d1eb2
HH
778 if [ -x ${udevdir}/$_prog ]; then
779 _bin=${udevdir}/$_prog
34bd2be2 780 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
604d1eb2 781 _bin=$(find_binary "$_prog") || {
cc218087 782 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
783 continue;
784 }
785 fi
786
af119460 787 [[ $_bin ]] && inst_binary "$_bin"
604d1eb2
HH
788 done
789 fi
da9c5181
HH
790 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
791 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
76d5c79e 792 _bin=""
604d1eb2
HH
793 if [ -x ${udevdir}/$_prog ]; then
794 _bin=${udevdir}/$_prog
34bd2be2 795 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
604d1eb2 796 _bin=$(find_binary "$_prog") || {
cc218087 797 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
798 continue;
799 }
800 fi
801
af119460 802 [[ $_bin ]] && inst_binary "$_bin"
604d1eb2
HH
803 done
804 fi
65d1a8a4
HH
805 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
806 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
76d5c79e 807 _bin=""
604d1eb2
HH
808 if [ -x ${udevdir}/$_prog ]; then
809 _bin=${udevdir}/$_prog
34bd2be2 810 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
62073c30 811 _bin=$(find_binary "$_prog") || {
cc218087 812 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
62073c30
CG
813 continue;
814 }
815 fi
816
76d5c79e 817 [[ $_bin ]] && dracut_install "$_bin"
62073c30
CG
818 done
819 fi
820}
821
e0a39336
HH
822# attempt to install any programs specified in a udev rule
823inst_rule_group_owner() {
824 local i
825
826 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
827 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
828 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
829 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
830 fi
831 done
832 fi
833 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
834 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
835 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
836 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
837 fi
838 done
839 fi
840}
841
8d85429a
HH
842inst_rule_initqueue() {
843 if grep -q -F initqueue "$1"; then
844 dracut_need_initqueue
845 fi
846}
847
f04dc5f3
VL
848# udev rules always get installed in the same place, so
849# create a function to install them to make life simpler.
0b706743 850inst_rules() {
29b10e65 851 local _target=/etc/udev/rules.d _rule _found
fd2312e0 852
604d1eb2 853 inst_dir "${udevdir}/rules.d"
29b10e65
HH
854 inst_dir "$_target"
855 for _rule in "$@"; do
256cba18 856 if [ "${_rule#/}" = "$_rule" ]; then
604d1eb2 857 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
858 if [[ -f $r/$_rule ]]; then
859 _found="$r/$_rule"
62073c30 860 inst_rule_programs "$_found"
e0a39336 861 inst_rule_group_owner "$_found"
8d85429a 862 inst_rule_initqueue "$_found"
604d1eb2 863 inst_simple "$_found"
76f5fa54
HH
864 fi
865 done
866 fi
c97e1a76 867 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
868 if [[ -f ${r}$_rule ]]; then
869 _found="${r}$_rule"
62073c30 870 inst_rule_programs "$_found"
e0a39336 871 inst_rule_group_owner "$_found"
8d85429a 872 inst_rule_initqueue "$_found"
604d1eb2 873 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
874 fi
875 done
29b10e65 876 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
877 done
878}
879
c46cda82
HH
880prepare_udev_rules() {
881 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
882
883 for f in "$@"; do
884 f="${initdir}/etc/udev/rules.d/$f"
885 [ -e "$f" ] || continue
886 while read line; do
887 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
888 if [ $UDEVVERSION -ge 174 ]; then
889 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
890 else
891 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
892 fi
893 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
894 if [ $UDEVVERSION -ge 176 ]; then
895 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
896 else
897 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
898 fi
899 else
900 echo "$line"
901 fi
902 done < "${f}" > "${f}.new"
903 mv "${f}.new" "$f"
904 done
905}
906
53f95456
VL
907# install function specialized for hooks
908# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
909# All hooks should be POSIX/SuS compliant, they will be sourced by init.
910inst_hook() {
cdad82fd 911 if ! [[ -f $3 ]]; then
e27770e1
912 dfatal "Cannot install a hook ($3) that does not exist."
913 dfatal "Aborting initrd creation."
cc02093d 914 exit 1
b093aa2d 915 elif ! [[ "$hookdirs" == *$1* ]]; then
3b403b32 916 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 917 exit 1
cdad82fd 918 fi
5a84ac3f 919 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
53f95456
VL
920}
921
3378a54f
922# install any of listed files
923#
924# If first argument is '-d' and second some destination path, first accessible
925# source is installed into this path, otherwise it will installed in the same
926# path as source. If none of listed files was installed, function return 1.
927# On first successful installation it returns with 0 status.
928#
929# Example:
930#
931# inst_any -d /bin/foo /bin/bar /bin/baz
932#
933# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
934# initramfs.
935inst_any() {
936 local to f
937
938 [[ $1 = '-d' ]] && to="$2" && shift 2
939
940 for f in "$@"; do
941 if [[ -e $f ]]; then
942 [[ $to ]] && inst "$f" "$to" && return 0
943 inst "$f" && return 0
944 fi
945 done
946
947 return 1
948}
949
c9143a63
JAH
950
951# inst_libdir_file [-n <pattern>] <file> [<file>...]
952# Install a <file> located on a lib directory to the initramfs image
cacaa90c 953# -n <pattern> install matching files
c9143a63 954inst_libdir_file() {
0c6565c8 955 local _files
c9143a63 956 if [[ "$1" == "-n" ]]; then
cacaa90c 957 local _pattern=$2
c9143a63
JAH
958 shift 2
959 for _dir in $libdirs; do
7828692f 960 for _i in "$@"; do
c9143a63 961 for _f in "$_dir"/$_i; do
7828692f 962 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 963 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
964 done
965 done
966 done
967 else
968 for _dir in $libdirs; do
7828692f 969 for _i in "$@"; do
c9143a63 970 for _f in "$_dir"/$_i; do
0c6565c8 971 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
972 done
973 done
974 done
975 fi
af119460 976 [[ $_files ]] && inst_multiple $_files
c9143a63
JAH
977}
978
979
87122afc
980# install function decompressing the target and handling symlinks
981# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
982#
983# Function install targets in the same paths inside overlay but decompressed
984# and without extensions (.gz, .bz2).
985inst_decompress() {
b19c5d51 986 local _src _cmd
87122afc 987
29b10e65 988 for _src in $@
87122afc 989 do
29b10e65 990 case ${_src} in
c62b9461 991 *.gz) _cmd='gzip -f -d' ;;
29b10e65 992 *.bz2) _cmd='bzip2 -d' ;;
87122afc
993 *) return 1 ;;
994 esac
b19c5d51 995 inst_simple ${_src}
87122afc
996 # Decompress with chosen tool. We assume that tool changes name e.g.
997 # from 'name.gz' to 'name'.
29b10e65 998 ${_cmd} "${initdir}${_src}"
87122afc
999 done
1000}
1001
1002# It's similar to above, but if file is not compressed, performs standard
1003# install.
1004# $@ = list of files
1005inst_opt_decompress() {
29b10e65 1006 local _src
87122afc 1007
29b10e65 1008 for _src in $@
87122afc 1009 do
29b10e65 1010 inst_decompress "${_src}" || inst "${_src}"
87122afc
1011 done
1012}
1013
7e2bca48
HH
1014# module_check <dracut module>
1015# execute the check() function of module-setup.sh of <dracut module>
1016# or the "check" script, if module-setup.sh is not found
1017# "check $hostonly" is called
95d2dabc 1018module_check() {
29b10e65
HH
1019 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1020 local _ret
31f1c02d
AW
1021 local _forced=0
1022 local _hostonly=$hostonly
1023 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
1024 [[ -d $_moddir ]] || return 1
1025 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1026 # if we do not have a check script, we are unconditionally included
29b10e65 1027 [[ -x $_moddir/check ]] || return 0
31f1c02d 1028 [ $_forced -ne 0 ] && unset hostonly
29b10e65 1029 $_moddir/check $hostonly
31f1c02d 1030 _ret=$?
95d2dabc 1031 else
1743473b 1032 unset check depends cmdline install installkernel
b093aa2d 1033 check() { true; }
29b10e65 1034 . $_moddir/module-setup.sh
95d2dabc 1035 is_func check || return 0
31f1c02d
AW
1036 [ $_forced -ne 0 ] && unset hostonly
1037 check $hostonly
29b10e65 1038 _ret=$?
1743473b 1039 unset check depends cmdline install installkernel
0c2e3d12 1040 fi
31f1c02d
AW
1041 hostonly=$_hostonly
1042 return $_ret
0c2e3d12
VL
1043}
1044
7e2bca48
HH
1045# module_check_mount <dracut module>
1046# execute the check() function of module-setup.sh of <dracut module>
1047# or the "check" script, if module-setup.sh is not found
1048# "mount_needs=1 check 0" is called
1049module_check_mount() {
1050 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1051 local _ret
1052 mount_needs=1
1053 [[ -d $_moddir ]] || return 1
1054 if [[ ! -f $_moddir/module-setup.sh ]]; then
1055 # if we do not have a check script, we are unconditionally included
1056 [[ -x $_moddir/check ]] || return 0
1057 mount_needs=1 $_moddir/check 0
1058 _ret=$?
1059 else
1743473b 1060 unset check depends cmdline install installkernel
b093aa2d 1061 check() { false; }
7e2bca48 1062 . $_moddir/module-setup.sh
7e2bca48
HH
1063 check 0
1064 _ret=$?
1743473b 1065 unset check depends cmdline install installkernel
7e2bca48
HH
1066 fi
1067 unset mount_needs
1068 return $_ret
1069}
1070
1071# module_depends <dracut module>
1072# execute the depends() function of module-setup.sh of <dracut module>
1073# or the "depends" script, if module-setup.sh is not found
95d2dabc 1074module_depends() {
29b10e65
HH
1075 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1076 local _ret
1077 [[ -d $_moddir ]] || return 1
1078 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1079 # if we do not have a check script, we have no deps
29b10e65
HH
1080 [[ -x $_moddir/check ]] || return 0
1081 $_moddir/check -d
95d2dabc
HH
1082 return $?
1083 else
1743473b 1084 unset check depends cmdline install installkernel
b093aa2d 1085 depends() { true; }
29b10e65 1086 . $_moddir/module-setup.sh
95d2dabc 1087 depends
29b10e65 1088 _ret=$?
1743473b
HH
1089 unset check depends cmdline install installkernel
1090 return $_ret
1091 fi
1092}
1093
1094# module_cmdline <dracut module>
1095# execute the cmdline() function of module-setup.sh of <dracut module>
1096# or the "cmdline" script, if module-setup.sh is not found
1097module_cmdline() {
1098 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1099 local _ret
1100 [[ -d $_moddir ]] || return 1
1101 if [[ ! -f $_moddir/module-setup.sh ]]; then
1102 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1103 return $?
1104 else
1105 unset check depends cmdline install installkernel
1106 cmdline() { true; }
1107 . $_moddir/module-setup.sh
1108 cmdline
1109 _ret=$?
1110 unset check depends cmdline install installkernel
29b10e65 1111 return $_ret
33ee031c 1112 fi
0c2e3d12
VL
1113}
1114
7e2bca48
HH
1115# module_install <dracut module>
1116# execute the install() function of module-setup.sh of <dracut module>
1117# or the "install" script, if module-setup.sh is not found
95d2dabc 1118module_install() {
29b10e65
HH
1119 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1120 local _ret
1121 [[ -d $_moddir ]] || return 1
1122 if [[ ! -f $_moddir/module-setup.sh ]]; then
1123 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
1124 return $?
1125 else
1743473b 1126 unset check depends cmdline install installkernel
b093aa2d 1127 install() { true; }
29b10e65 1128 . $_moddir/module-setup.sh
95d2dabc 1129 install
29b10e65 1130 _ret=$?
1743473b 1131 unset check depends cmdline install installkernel
29b10e65 1132 return $_ret
95d2dabc
HH
1133 fi
1134}
1135
7e2bca48
HH
1136# module_installkernel <dracut module>
1137# execute the installkernel() function of module-setup.sh of <dracut module>
1138# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 1139module_installkernel() {
29b10e65
HH
1140 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1141 local _ret
1142 [[ -d $_moddir ]] || return 1
1143 if [[ ! -f $_moddir/module-setup.sh ]]; then
1144 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
1145 return $?
1146 else
1743473b 1147 unset check depends cmdline install installkernel
b093aa2d 1148 installkernel() { true; }
dba20559 1149 . $_moddir/module-setup.sh
95d2dabc 1150 installkernel
29b10e65 1151 _ret=$?
1743473b 1152 unset check depends cmdline install installkernel
29b10e65 1153 return $_ret
95d2dabc
HH
1154 fi
1155}
1156
7e2bca48
HH
1157# check_mount <dracut module>
1158# check_mount checks, if a dracut module is needed for the given
1159# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1160check_mount() {
1161 local _mod=$1
1162 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1163 local _ret
1164 local _moddep
ecfa02bd
HH
1165
1166 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1167
1b7fd0fa 1168 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1169 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1170 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1b7fd0fa
AW
1171
1172 # This should never happen, but...
1173 [[ -d $_moddir ]] || return 1
1174
1175 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1176
b093aa2d 1177 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
0f283709
HH
1178 return 1
1179 fi
1b7fd0fa 1180
b093aa2d 1181 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
ecfa02bd
HH
1182 module_check_mount $_mod; ret=$?
1183
1184 # explicit module, so also accept ret=255
1185 [[ $ret = 0 || $ret = 255 ]] || return 1
1b7fd0fa 1186 else
ecfa02bd
HH
1187 # module not in our list
1188 if [[ $dracutmodules = all ]]; then
1189 # check, if we can and should install this module
1190 module_check_mount $_mod || return 1
1191 else
1192 # skip this module
1193 return 1
1194 fi
1b7fd0fa
AW
1195 fi
1196
ecfa02bd 1197
1b7fd0fa
AW
1198 for _moddep in $(module_depends $_mod); do
1199 # handle deps as if they were manually added
b093aa2d 1200 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa 1201 add_dracutmodules+=" $_moddep "
b093aa2d 1202 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa
AW
1203 force_add_dracutmodules+=" $_moddep "
1204 # if a module we depend on fail, fail also
0f283709 1205 if ! check_module $_moddep; then
600c8769 1206 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1207 return 1
1208 fi
1b7fd0fa
AW
1209 done
1210
b093aa2d 1211 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1b7fd0fa
AW
1212 mods_to_load+=" $_mod "
1213
1214 return 0
1215}
1216
7e2bca48
HH
1217# check_module <dracut module> [<use_as_dep>]
1218# check if a dracut module is to be used in the initramfs process
1219# if <use_as_dep> is set, then the process also keeps track
1220# that the modules were checked for the dependency tracking process
95d2dabc 1221check_module() {
29b10e65
HH
1222 local _mod=$1
1223 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1224 local _ret
1225 local _moddep
95d2dabc 1226 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1227 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1228 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
95d2dabc
HH
1229
1230 # This should never happen, but...
29b10e65 1231 [[ -d $_moddir ]] || return 1
95d2dabc 1232
29b10e65 1233 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1234
b093aa2d 1235 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
600c8769 1236 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
0f283709
HH
1237 return 1
1238 fi
95d2dabc 1239
b093aa2d
HH
1240 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1241 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
31f1c02d
AW
1242 module_check $_mod 1; ret=$?
1243 else
1244 module_check $_mod 0; ret=$?
1245 fi
95d2dabc
HH
1246 # explicit module, so also accept ret=255
1247 [[ $ret = 0 || $ret = 255 ]] || return 1
1248 else
1249 # module not in our list
1250 if [[ $dracutmodules = all ]]; then
1251 # check, if we can and should install this module
29b10e65 1252 module_check $_mod || return 1
cc02093d 1253 else
95d2dabc
HH
1254 # skip this module
1255 return 1
970e646b 1256 fi
95d2dabc 1257 fi
bb764545 1258
29b10e65 1259 for _moddep in $(module_depends $_mod); do
95d2dabc 1260 # handle deps as if they were manually added
b093aa2d 1261 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
29b10e65 1262 add_dracutmodules+=" $_moddep "
b093aa2d 1263 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
31f1c02d 1264 force_add_dracutmodules+=" $_moddep "
95d2dabc 1265 # if a module we depend on fail, fail also
0f283709 1266 if ! check_module $_moddep; then
600c8769 1267 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1268 return 1
1269 fi
0c2e3d12 1270 done
70503db4 1271
b093aa2d 1272 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
29b10e65 1273 mods_to_load+=" $_mod "
95d2dabc
HH
1274
1275 return 0
1276}
1277
7e2bca48
HH
1278# for_each_module_dir <func>
1279# execute "<func> <dracut module> 1"
1b7fd0fa 1280for_each_module_dir() {
29b10e65
HH
1281 local _modcheck
1282 local _mod
1283 local _moddir
1b7fd0fa
AW
1284 local _func
1285 _func=$1
29b10e65
HH
1286 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1287 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1288 $_func $_mod 1
70503db4 1289 done
724b87a6
HH
1290
1291 # Report any missing dracut modules, the user has specified
31f1c02d 1292 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1293 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1294 for _mod in $_modcheck; do
b093aa2d
HH
1295 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1296 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
600c8769 1297 derror "dracut module '$_mod' cannot be found or installed."
724b87a6 1298 done
0c2e3d12
VL
1299}
1300
ddfd1d10
VL
1301# Install a single kernel module along with any firmware it may require.
1302# $1 = full path to kernel module to install
1303install_kmod_with_fw() {
0b440844 1304 # no need to go further if the module is already installed
379c34d2 1305
0b706743
1306 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1307 && return 0
fcbcb252 1308
f4031e8a
HH
1309 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1310 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1311 return $ret
1312 fi
379c34d2 1313
fcbcb252
HH
1314 if [[ $omit_drivers ]]; then
1315 local _kmod=${1##*/}
1316 _kmod=${_kmod%.ko}
1317 _kmod=${_kmod/-/_}
34248c92
HH
1318 if [[ "$_kmod" =~ $omit_drivers ]]; then
1319 dinfo "Omitting driver $_kmod"
7abd4264 1320 return 0
34248c92
HH
1321 fi
1322 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1323 dinfo "Omitting driver $_kmod"
7abd4264 1324 return 0
fcbcb252
HH
1325 fi
1326 fi
1327
868f935e
HH
1328 if [[ $silent_omit_drivers ]]; then
1329 local _kmod=${1##*/}
1330 _kmod=${_kmod%.ko}
1331 _kmod=${_kmod/-/_}
1332 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1333 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1334 fi
1335
d8aeb3a7
HH
1336 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1337 ret=$?
f4031e8a
HH
1338 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1339 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1340 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1341 (($ret != 0)) && return $ret
e6024e00
JR
1342
1343 local _modname=${1##*/} _fwdir _found _fw
1344 _modname=${_modname%.ko*}
29b10e65
HH
1345 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1346 _found=''
1347 for _fwdir in $fw_dir; do
1348 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1349 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1350 _found=yes
cc02093d
HH
1351 fi
1352 done
29b10e65 1353 if [[ $_found != yes ]]; then
fe1484f3 1354 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1355 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1356 "\"${_modname}.ko\""
e2cdb570 1357 else
29b10e65
HH
1358 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1359 "\"${_modname}.ko\""
e2cdb570 1360 fi
cc02093d 1361 fi
ddfd1d10 1362 done
0b440844 1363 return 0
ddfd1d10
VL
1364}
1365
1366# Do something with all the dependencies of a kernel module.
1367# Note that kernel modules depend on themselves using the technique we use
0b706743
1368# $1 = function to call for each dependency we find
1369# It will be passed the full path to the found kernel module
ddfd1d10
VL
1370# $2 = module to get dependencies for
1371# rest of args = arguments to modprobe
c32bda6b 1372# _fderr specifies FD passed from surrounding scope
ddfd1d10 1373for_each_kmod_dep() {
7abd4264 1374 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1375 shift 2
c32bda6b 1376 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1377 while read _cmd _modpath _options; do
1378 [[ $_cmd = insmod ]] || continue
1379 $_func ${_modpath} || exit $?
0b706743 1380 done
0b706743 1381 )
ddfd1d10
VL
1382}
1383
70c6b773 1384dracut_kernel_post() {
d8aeb3a7 1385 local _moddirname=${srcmods%%/lib/modules/*}
3c4bfd68 1386 local _pid
d8aeb3a7 1387
f4031e8a 1388 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1389 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1390 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1391 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1392 | sort -u \
1393 | while read _cmd _modpath _options; do
1394 [[ $_cmd = insmod ]] || continue
1395 echo "$_modpath"
f4031e8a 1396 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1397
89d44e72 1398 (
73575f11
HH
1399 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1400 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1401 else
1402 while read _modpath; do
1403 local _destpath=$_modpath
1404 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1405 _destpath=${_destpath##*/lib/modules/$kernel/}
1406 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1407 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1408 fi
1409 ) &
dba20559
HH
1410 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1411 _pid=${_pid##*:}
d8aeb3a7 1412
73575f11
HH
1413 if [[ $DRACUT_INSTALL ]]; then
1414 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1415 | while read line; do
1416 for _fwdir in $fw_dir; do
1417 echo $_fwdir/$line;
1418 done;
73575f11 1419 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1420 else
73575f11 1421 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1422 for _fwdir in $fw_dir; do
1423 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1424 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1425 break
1426 fi
1427 done
1428 done
1429 fi
d8aeb3a7 1430
3c4bfd68 1431 wait $_pid
89d44e72 1432 fi
70c6b773
HH
1433
1434 for _f in modules.builtin.bin modules.builtin; do
1435 [[ $srcmods/$_f ]] && break
1436 done || {
1437 dfatal "No modules.builtin.bin and modules.builtin found!"
1438 return 1
1439 }
1440
1441 for _f in modules.builtin.bin modules.builtin modules.order; do
1442 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1443 done
1444
1445 # generate module dependencies for the initrd
1446 if [[ -d $initdir/lib/modules/$kernel ]] && \
1447 ! depmod -a -b "$initdir" $kernel; then
1448 dfatal "\"depmod -a $kernel\" failed."
1449 exit 1
1450 fi
1451
32bd2fbb 1452 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1453}
fe1484f3 1454
dba20559 1455module_is_host_only() {
080d1b71
HH
1456 local _mod=$1
1457 _mod=${_mod##*/}
1458 _mod=${_mod%.ko}
1459
b093aa2d 1460 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
080d1b71
HH
1461
1462 # check if module is loaded
b093aa2d
HH
1463 for i in /sys/module/${_mod//-/_}; do
1464 [[ -d $i ]] && return 0
1465 done
080d1b71
HH
1466
1467 # check if module is loadable on the current kernel
1468 # this covers the case, where a new module is introduced
1469 # or a module was renamed
1470 # or a module changed from builtin to a module
1471 modinfo -F filename "$_mod" &>/dev/null || return 0
1472
1473 return 1
b093aa2d 1474}
080d1b71 1475
b093aa2d 1476find_kernel_modules_by_path () {
74405477 1477 local _OLDIFS
5db6ca5a 1478
b093aa2d 1479 [[ -f "$srcmods/modules.dep" ]] || return 0
5db6ca5a
HH
1480
1481 _OLDIFS=$IFS
1482 IFS=:
1483 while read a rest; do
1484 [[ $a = */$1/* ]] || continue
b093aa2d
HH
1485 printf "%s\n" "$srcmods/$a"
1486 done < "$srcmods/modules.dep"
5db6ca5a 1487 IFS=$_OLDIFS
74405477 1488 return 0
b093aa2d 1489}
4073c815 1490
881eda69
JR
1491find_kernel_modules () {
1492 find_kernel_modules_by_path drivers
1493}
1494
338b43cd
HH
1495# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1496# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1497# install kernel modules along with all their dependencies.
7e2bca48 1498# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1499instmods() {
0c1a8ebc 1500 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1501 # called [sub]functions inherit _fderr
1502 local _fderr=9
a6d3be9d 1503 local _check=no
338b43cd 1504 local _silent=no
a6d3be9d
AW
1505 if [[ $1 = '-c' ]]; then
1506 _check=yes
1507 shift
1508 fi
881eda69 1509
338b43cd
HH
1510 if [[ $1 = '-s' ]]; then
1511 _silent=yes
1512 shift
1513 fi
1514
881eda69 1515 function inst1mod() {
a6d3be9d 1516 local _ret=0 _mod="$1"
29b10e65 1517 case $_mod in
0b706743 1518 =*)
d8aeb3a7
HH
1519 ( [[ "$_mpargs" ]] && echo $_mpargs
1520 find_kernel_modules_by_path "${_mod#=}" ) \
1521 | instmods
1522 ((_ret+=$?))
cc02093d 1523 ;;
0024702f 1524 --*) _mpargs+=" $_mod" ;;
34248c92 1525 *)
379c34d2 1526 _mod=${_mod##*/}
6568d86a 1527 # if we are already installed, skip this module and go on
cc02093d 1528 # to the next one.
f4031e8a
HH
1529 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1530 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1531 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1532 return $_ret
1533 fi
881eda69 1534
beb26a08
DY
1535 _mod=${_mod/-/_}
1536 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
34248c92 1537 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1538 return 0
34248c92 1539 fi
080d1b71 1540
cc02093d
HH
1541 # If we are building a host-specific initramfs and this
1542 # module is not already loaded, move on to the next one.
fe1484f3 1543 [[ $hostonly ]] \
080d1b71 1544 && ! module_is_host_only "$_mod" \
7abd4264 1545 && return 0
04b56f3a 1546
f4031e8a 1547 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7 1548 # We use '-d' option in modprobe only if modules prefix path
600c8769 1549 # differs from default '/'. This allows us to use dracut with
d8aeb3a7
HH
1550 # old version of modprobe which doesn't have '-d' option.
1551 local _moddirname=${srcmods%%/lib/modules/*}
1552 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1553
1554 # ok, load the module, all its dependencies, and any firmware
1555 # it may require
1556 for_each_kmod_dep install_kmod_with_fw $_mod \
1557 --set-version $kernel ${_moddirname} $_mpargs
1558 ((_ret+=$?))
1559 else
f4031e8a
HH
1560 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1561 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1562 fi
cc02093d 1563 ;;
0b706743 1564 esac
a6d3be9d 1565 return $_ret
881eda69
JR
1566 }
1567
f9708da2 1568 function instmods_1() {
a6d3be9d 1569 local _mod _mpargs
f9708da2
JR
1570 if (($# == 0)); then # filenames from stdin
1571 while read _mod; do
a6d3be9d 1572 inst1mod "${_mod%.ko*}" || {
338b43cd 1573 if [[ "$_check" == "yes" ]]; then
19b5c109 1574 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
a6d3be9d
AW
1575 return 1
1576 fi
1577 }
f9708da2
JR
1578 done
1579 fi
1580 while (($# > 0)); do # filenames as arguments
a6d3be9d 1581 inst1mod ${1%.ko*} || {
338b43cd 1582 if [[ "$_check" == "yes" ]]; then
19b5c109 1583 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
a6d3be9d
AW
1584 return 1
1585 fi
1586 }
f9708da2 1587 shift
881eda69 1588 done
a6d3be9d 1589 return 0
f9708da2
JR
1590 }
1591
a6d3be9d 1592 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1593 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1594 # redirections, but that would make dracut require bash4 at least.
1595 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
dba20559 1596 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1597 _ret=$?
a6d3be9d 1598 return $_ret
0b706743 1599}
5f2c30d9
KRW
1600# get_cpu_vendor
1601# Only two values are returned: AMD or Intel
1602get_cpu_vendor ()
1603{
1604 if grep -qE AMD /proc/cpuinfo; then
1605 printf "AMD"
1606 fi
1607 if grep -qE Intel /proc/cpuinfo; then
1608 printf "Intel"
1609 fi
1610}
1611
1612# get_host_ucode
1613# Get the hosts' ucode file based on the /proc/cpuinfo
1614get_ucode_file ()
1615{
1616 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1617 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1618 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1619
1620 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1621 # If family greater or equal than 0x15
1622 if [[ $family -ge 21 ]]; then
1623 printf "microcode_amd_fam15h.bin"
1624 else
1625 printf "microcode_amd.bin"
1626 fi
1627 fi
1628 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1629 # The /proc/cpuinfo are in decimal.
1630 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1631 fi
1632}