]>
Commit | Line | Data |
---|---|---|
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 | # |
04b56f3a | 22 | |
0874654c HH |
23 | if ! [[ $dracutbasedir ]]; then |
24 | dracutbasedir=${BASH_SOURCE[0]%/*} | |
25 | [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="." | |
26 | [[ $dracutbasedir ]] || dracutbasedir="." | |
27 | dracutbasedir="$(readlink -f $dracutbasedir)" | |
28 | fi | |
29 | ||
d6d53f60 | 30 | if ! type dinfo >/dev/null 2>&1; then |
552ecca6 | 31 | . "$dracutbasedir/dracut-logger.sh" |
e103615b AŻ |
32 | dlog_init |
33 | fi | |
34 | ||
7e2bca48 HH |
35 | # export standard hookdirs |
36 | [[ $hookdirs ]] || { | |
4fed3ddf | 37 | hookdirs="cmdline pre-udev pre-trigger netroot " |
6e3cc00f | 38 | hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout " |
4fed3ddf HH |
39 | hookdirs+="pre-mount pre-pivot mount " |
40 | hookdirs+="emergency shutdown-emergency shutdown cleanup " | |
7e2bca48 HH |
41 | export hookdirs |
42 | } | |
43 | ||
3198f171 | 44 | # Generic substring function. If $2 is in $1, return 0. |
43dfbeec | 45 | strstr() { [ "${1#*$2*}" != "$1" ]; } |
f04dc5f3 | 46 | |
87122afc AŻ |
47 | # Create all subdirectories for given path without creating the last element. |
48 | # $1 = path | |
94f49230 | 49 | mksubdirs() { mkdir -m 0755 -p ${1%/*}; } |
87122afc | 50 | |
22ecea45 AŻ |
51 | # Version comparision function. Assumes Linux style version scheme. |
52 | # $1 = version a | |
53 | # $2 = comparision op (gt, ge, eq, le, lt, ne) | |
54 | # $3 = version b | |
ecf42850 | 55 | vercmp() { |
29b10e65 | 56 | local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res |
ecf42850 | 57 | |
29b10e65 | 58 | for ((_i=0; ; _i++)) |
22ecea45 | 59 | do |
29b10e65 HH |
60 | if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0 |
61 | elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1 | |
62 | elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2 | |
22ecea45 AŻ |
63 | else continue |
64 | fi | |
65 | break | |
ecf42850 | 66 | done |
ecf42850 | 67 | |
29b10e65 HH |
68 | case $_op in |
69 | gt) ((_res == 1));; | |
70 | ge) ((_res != 2));; | |
71 | eq) ((_res == 0));; | |
72 | le) ((_res != 1));; | |
73 | lt) ((_res == 2));; | |
74 | ne) ((_res != 0));; | |
22ecea45 | 75 | esac |
ecf42850 AŻ |
76 | } |
77 | ||
7e2bca48 HH |
78 | # is_func <command> |
79 | # Check whether $1 is a function. | |
95d2dabc | 80 | is_func() { |
0b706743 | 81 | [[ $(type -t $1) = "function" ]] |
95d2dabc HH |
82 | } |
83 | ||
87122afc AŻ |
84 | # Function prints global variables in format name=value line by line. |
85 | # $@ = list of global variables' name | |
86 | print_vars() { | |
29b10e65 | 87 | local _var _value |
87122afc | 88 | |
29b10e65 | 89 | for _var in $@ |
87122afc | 90 | do |
29b10e65 HH |
91 | _value=$(eval echo \$$_var) |
92 | [[ ${_value} ]] && echo "${_var}=\"${_value}\"" | |
87122afc AŻ |
93 | done |
94 | } | |
95 | ||
7e2bca48 HH |
96 | # normalize_path <path> |
97 | # Prints the normalized path, where it removes any duplicated | |
98 | # and trailing slashes. | |
99 | # Example: | |
100 | # $ normalize_path ///test/test// | |
101 | # /test/test | |
626d9eba | 102 | normalize_path() { |
c44e3cb4 MS |
103 | shopt -q -s extglob |
104 | set -- "${1//+(\/)//}" | |
105 | shopt -q -u extglob | |
106 | echo "${1%/}" | |
626d9eba | 107 | } |
d4bb4316 | 108 | |
7e2bca48 HH |
109 | # convert_abs_rel <from> <to> |
110 | # Prints the relative path, when creating a symlink to <to> from <from>. | |
111 | # Example: | |
112 | # $ convert_abs_rel /usr/bin/test /bin/test-2 | |
113 | # ../../bin/test-2 | |
114 | # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test | |
d4bb4316 | 115 | convert_abs_rel() { |
6d2a7942 | 116 | local __current __absolute __abssize __cursize __newpath |
c44e3cb4 | 117 | local -i __i __level |
d4bb4316 | 118 | |
c44e3cb4 MS |
119 | set -- "$(normalize_path "$1")" "$(normalize_path "$2")" |
120 | ||
c1609dd4 MS |
121 | # corner case #1 - self looping link |
122 | [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; } | |
123 | ||
124 | # corner case #2 - own dir link | |
125 | [[ "${1%/*}" == "$2" ]] && { echo "."; return; } | |
126 | ||
6d2a7942 HH |
127 | IFS="/" __current=($1) |
128 | IFS="/" __absolute=($2) | |
d4bb4316 HH |
129 | |
130 | __abssize=${#__absolute[@]} | |
131 | __cursize=${#__current[@]} | |
132 | ||
133 | while [[ ${__absolute[__level]} == ${__current[__level]} ]] | |
134 | do | |
135 | (( __level++ )) | |
136 | if (( __level > __abssize || __level > __cursize )) | |
137 | then | |
138 | break | |
139 | fi | |
140 | done | |
141 | ||
142 | for ((__i = __level; __i < __cursize-1; __i++)) | |
143 | do | |
144 | if ((__i > __level)) | |
145 | then | |
146 | __newpath=$__newpath"/" | |
147 | fi | |
148 | __newpath=$__newpath".." | |
149 | done | |
150 | ||
151 | for ((__i = __level; __i < __abssize; __i++)) | |
152 | do | |
153 | if [[ -n $__newpath ]] | |
154 | then | |
155 | __newpath=$__newpath"/" | |
156 | fi | |
157 | __newpath=$__newpath${__absolute[__i]} | |
158 | done | |
159 | ||
160 | echo "$__newpath" | |
161 | } | |
162 | ||
7e2bca48 HH |
163 | # get_fs_env <device> |
164 | # Get and set the ID_FS_TYPE and ID_FS_UUID variable from udev for a device. | |
165 | # Example: | |
166 | # $ get_fs_env /dev/sda2; echo $ID_FS_TYPE; echo $ID_FS_UUID | |
167 | # ext4 | |
168 | # 551a39aa-4ae9-4e70-a262-ef665cadb574 | |
9ede1929 | 169 | get_fs_env() { |
7c179686 | 170 | [[ $1 ]] || return |
29b10e65 HH |
171 | unset ID_FS_TYPE |
172 | unset ID_FS_UUID | |
07f3e4f1 HH |
173 | eval $(udevadm info --query=env --name=$1 \ |
174 | | while read line; do | |
2f6fe2ec | 175 | [[ "$line" =~ ID_FS_(TYPE|UUID)= ]] && echo $line; |
07f3e4f1 | 176 | done) |
e03c77cb | 177 | [[ $ID_FS_TYPE ]] && return 0 |
d8b9844c | 178 | |
59c88f49 | 179 | if [[ -x /lib/udev/vol_id ]]; then |
cc02093d | 180 | eval $(/lib/udev/vol_id --export $1) |
9ede1929 | 181 | elif find_binary blkid >/dev/null; then |
cc02093d | 182 | eval $(blkid -o udev $1) |
59c88f49 | 183 | else |
cc02093d | 184 | return 1 |
59c88f49 | 185 | fi |
9ede1929 VL |
186 | } |
187 | ||
7e2bca48 HH |
188 | # get_fs_uuid <device> |
189 | # Prints the filesystem UUID for a device. | |
190 | # Example: | |
191 | # $ get_fs_uuid /dev/sda2 | |
192 | # 551a39aa-4ae9-4e70-a262-ef665cadb574 | |
193 | get_fs_uuid() ( | |
194 | get_fs_env $1 || return | |
195 | echo $ID_FS_UUID | |
196 | ) | |
197 | ||
198 | # get_fs_type <device> | |
199 | # Prints the filesystem type for a device. | |
200 | # Example: | |
201 | # $ get_fs_type /dev/sda1 | |
202 | # ext4 | |
9ede1929 | 203 | get_fs_type() ( |
7c179686 HH |
204 | [[ $1 ]] || return |
205 | if [[ $1 != ${1#/dev/block/nfs:} ]] \ | |
cc02093d HH |
206 | || [[ $1 != ${1#/dev/block/nfs3:} ]] \ |
207 | || [[ $1 != ${1#/dev/block/nfs4:} ]]; then | |
208 | echo "nfs" | |
e03c77cb | 209 | return 0 |
7c179686 | 210 | fi |
7ae5d9d1 HH |
211 | if get_fs_env $1; then |
212 | echo $ID_FS_TYPE | |
e03c77cb | 213 | return 0 |
7ae5d9d1 HH |
214 | fi |
215 | find_dev_fstype $1 | |
9ede1929 VL |
216 | ) |
217 | ||
7e2bca48 HH |
218 | # get_maj_min <device> |
219 | # Prints the major and minor of a device node. | |
220 | # Example: | |
221 | # $ get_maj_min /dev/sda2 | |
222 | # 8:2 | |
480d772f HH |
223 | get_maj_min() { |
224 | local _dev | |
74132a10 | 225 | _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null) |
480d772f HH |
226 | _dev=$(eval "echo $_dev") |
227 | echo $_dev | |
228 | } | |
229 | ||
7e2bca48 HH |
230 | # find_block_device <mountpoint> |
231 | # Prints the major and minor number of the block device | |
232 | # for a given mountpoint. | |
233 | # Unless $use_fstab is set to "yes" the functions | |
234 | # uses /proc/self/mountinfo as the primary source of the | |
235 | # information and only falls back to /etc/fstab, if the mountpoint | |
236 | # is not found there. | |
237 | # Example: | |
238 | # $ find_block_device /usr | |
239 | # 8:4 | |
f76ef3aa | 240 | find_block_device() { |
29b10e65 | 241 | local _x _mpt _majmin _dev _fs _maj _min |
0b706743 | 242 | if [[ $use_fstab != yes ]]; then |
29b10e65 | 243 | while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do |
7ae5d9d1 | 244 | [[ $_mpt = $1 ]] || continue |
29b10e65 HH |
245 | [[ $_fs = nfs ]] && { echo $_dev; return 0;} |
246 | [[ $_fs = nfs3 ]] && { echo $_dev; return 0;} | |
247 | [[ $_fs = nfs4 ]] && { echo $_dev; return 0;} | |
248 | [[ $_fs = btrfs ]] && { | |
480d772f HH |
249 | get_maj_min $_dev |
250 | return 0; | |
51c977d1 | 251 | } |
7ae5d9d1 | 252 | if [[ ${_majmin#0:} = $_majmin ]]; then |
29b10e65 | 253 | echo $_majmin |
cc02093d HH |
254 | return 0 # we have a winner! |
255 | fi | |
0b706743 | 256 | done < /proc/self/mountinfo |
7c179686 | 257 | fi |
0b706743 | 258 | # fall back to /etc/fstab |
29b10e65 | 259 | while read _dev _mpt _fs _x; do |
85f8bb16 HH |
260 | [ "${_dev%%#*}" != "$_dev" ] && continue |
261 | ||
29b10e65 HH |
262 | if [[ $_mpt = $1 ]]; then |
263 | [[ $_fs = nfs ]] && { echo $_dev; return 0;} | |
264 | [[ $_fs = nfs3 ]] && { echo $_dev; return 0;} | |
265 | [[ $_fs = nfs4 ]] && { echo $_dev; return 0;} | |
266 | [[ $_dev != ${_dev#UUID=} ]] && _dev=/dev/disk/by-uuid/${_dev#UUID=} | |
267 | [[ $_dev != ${_dev#LABEL=} ]] && _dev=/dev/disk/by-label/${_dev#LABEL=} | |
268 | [[ -b $_dev ]] || return 1 # oops, not a block device. | |
c4e48eae | 269 | get_maj_min "$_dev" && return 0 |
cc02093d | 270 | fi |
7c179686 | 271 | done < /etc/fstab |
0b706743 AŻ |
272 | |
273 | return 1 | |
17829e94 VL |
274 | } |
275 | ||
7e2bca48 HH |
276 | # find_dev_fstype <device> |
277 | # Echo the filesystem type for a given device. | |
278 | # /proc/self/mountinfo is taken as the primary source of information | |
279 | # and /etc/fstab is used as a fallback. | |
280 | # No newline is appended! | |
281 | # Example: | |
282 | # $ find_dev_fstype /dev/sda2;echo | |
283 | # ext4 | |
7ae5d9d1 HH |
284 | find_dev_fstype() { |
285 | local _x _mpt _majmin _dev _fs _maj _min | |
286 | while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do | |
287 | [[ $_dev = $1 ]] || continue | |
288 | echo -n $_fs; | |
289 | return 0; | |
290 | done < /proc/self/mountinfo | |
291 | ||
292 | # fall back to /etc/fstab | |
293 | while read _dev _mpt _fs _x; do | |
294 | [[ $_dev = $1 ]] || continue | |
295 | echo -n $_fs; | |
296 | return 0; | |
297 | done < /etc/fstab | |
298 | ||
299 | return 1 | |
300 | } | |
301 | ||
302 | # finds the major:minor of the block device backing the root filesystem. | |
f76ef3aa VL |
303 | find_root_block_device() { find_block_device /; } |
304 | ||
7e2bca48 HH |
305 | # for_each_host_dev_fs <func> |
306 | # Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found | |
307 | # in ${host_fs_types[@]} | |
480d772f HH |
308 | for_each_host_dev_fs() |
309 | { | |
310 | local _func="$1" | |
d0096de7 AW |
311 | local _dev |
312 | local _fs | |
2efa546f | 313 | local _ret=1 |
480d772f HH |
314 | for f in ${host_fs_types[@]}; do |
315 | OLDIFS="$IFS" | |
316 | IFS="|" | |
317 | set -- $f | |
318 | IFS="$OLDIFS" | |
d0096de7 AW |
319 | _dev="$1" |
320 | [[ -b "$_dev" ]] || continue | |
321 | _fs="$2" | |
2efa546f | 322 | $_func $_dev $_fs && _ret=0 |
480d772f | 323 | done |
2efa546f | 324 | return $_ret |
480d772f HH |
325 | } |
326 | ||
17829e94 VL |
327 | # Walk all the slave relationships for a given block device. |
328 | # Stop when our helper function returns success | |
329 | # $1 = function to call on every found block device | |
330 | # $2 = block device in major:minor format | |
0b706743 | 331 | check_block_and_slaves() { |
29b10e65 | 332 | local _x |
17829e94 VL |
333 | [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry. |
334 | "$1" $2 && return | |
533d7dc4 | 335 | check_vol_slaves "$@" && return 0 |
3478b314 | 336 | if [[ -f /sys/dev/block/$2/../dev ]]; then |
0b706743 | 337 | check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0 |
a3afcf2a | 338 | fi |
17829e94 | 339 | [[ -d /sys/dev/block/$2/slaves ]] || return 1 |
29b10e65 HH |
340 | for _x in /sys/dev/block/$2/slaves/*/dev; do |
341 | [[ -f $_x ]] || continue | |
342 | check_block_and_slaves $1 $(cat "$_x") && return 0 | |
17829e94 VL |
343 | done |
344 | return 1 | |
345 | } | |
346 | ||
533d7dc4 HH |
347 | # ugly workaround for the lvm design |
348 | # There is no volume group device, | |
349 | # so, there are no slave devices for volume groups. | |
350 | # Logical volumes only have the slave devices they really live on, | |
351 | # but you cannot create the logical volume without the volume group. | |
95bde758 | 352 | # And the volume group might be bigger than the devices the LV needs. |
533d7dc4 | 353 | check_vol_slaves() { |
29b10e65 | 354 | local _lv _vg _pv |
0b706743 | 355 | for i in /dev/mapper/*; do |
c4e48eae | 356 | _lv=$(get_maj_min $i) |
29b10e65 HH |
357 | if [[ $_lv = $2 ]]; then |
358 | _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null) | |
cc02093d | 359 | # strip space |
29b10e65 HH |
360 | _vg=$(echo $_vg) |
361 | if [[ $_vg ]]; then | |
362 | for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null) | |
0b706743 | 363 | do |
c4e48eae | 364 | check_block_and_slaves $1 $(get_maj_min $_pv) && return 0 |
cc02093d HH |
365 | done |
366 | fi | |
367 | fi | |
533d7dc4 HH |
368 | done |
369 | return 1 | |
370 | } | |
371 | ||
19612483 | 372 | # Install a directory, keeping symlinks as on the original system. |
3f590c78 | 373 | # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file" |
19612483 LA |
374 | # will create ${initdir}/lib64, ${initdir}/lib64/file, |
375 | # and a symlink ${initdir}/lib -> lib64. | |
376 | inst_dir() { | |
4637c5c2 | 377 | [[ -e ${initdir}/"$1" ]] && return 0 # already there |
a76dc278 | 378 | |
9e103df4 | 379 | local _dir="$1" _part="${1%/*}" _file |
4637c5c2 | 380 | while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do |
a76dc278 HH |
381 | _dir="$_part $_dir" |
382 | _part=${_part%/*} | |
383 | done | |
19612483 | 384 | |
a76dc278 HH |
385 | # iterate over parent directories |
386 | for _file in $_dir; do | |
3249c257 | 387 | [[ -e "${initdir}/$_file" ]] && continue |
29b10e65 | 388 | if [[ -L $_file ]]; then |
d4bb4316 | 389 | inst_symlink "$_file" |
19612483 LA |
390 | else |
391 | # create directory | |
3249c257 HH |
392 | mkdir -m 0755 -p "${initdir}/$_file" || return 1 |
393 | [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file" | |
394 | chmod u+w "${initdir}/$_file" | |
19612483 LA |
395 | fi |
396 | done | |
397 | } | |
398 | ||
36b24d7c VL |
399 | # $1 = file to copy to ramdisk |
400 | # $2 (optional) Name for the file on the ramdisk | |
401 | # Location of the image dir is assumed to be $initdir | |
3198f171 | 402 | # We never overwrite the target if it exists. |
36b24d7c | 403 | inst_simple() { |
4637c5c2 HH |
404 | [[ -f "$1" ]] || return 1 |
405 | strstr "$1" "/" || return 1 | |
3f590c78 JR |
406 | |
407 | local _src=$1 target="${2:-$1}" | |
4637c5c2 HH |
408 | if ! [[ -d ${initdir}/$target ]]; then |
409 | [[ -e ${initdir}/$target ]] && return 0 | |
cc227886 HH |
410 | [[ -L ${initdir}/$target ]] && return 0 |
411 | [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}" | |
3c56f372 | 412 | fi |
5242d8fb | 413 | # install checksum files also |
29b10e65 | 414 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then |
5f06f0c3 | 415 | inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" |
5242d8fb | 416 | fi |
29b10e65 | 417 | ddebug "Installing $_src" |
7fffc9f1 | 418 | cp --sparse=always -pfL "$_src" "${initdir}/$target" |
36b24d7c VL |
419 | } |
420 | ||
a10a1416 AŻ |
421 | # find symlinks linked to given library file |
422 | # $1 = library file | |
423 | # Function searches for symlinks by stripping version numbers appended to | |
424 | # library filename, checks if it points to the same target and finally | |
425 | # prints the list of symlinks to stdout. | |
426 | # | |
427 | # Example: | |
428 | # rev_lib_symlinks libfoo.so.8.1 | |
429 | # output: libfoo.so.8 libfoo.so | |
430 | # (Only if libfoo.so.8 and libfoo.so exists on host system.) | |
431 | rev_lib_symlinks() { | |
432 | [[ ! $1 ]] && return 0 | |
433 | ||
434 | local fn="$1" orig="$(readlink -f "$1")" links='' | |
435 | ||
436 | [[ ${fn} =~ .*\.so\..* ]] || return 1 | |
437 | ||
438 | until [[ ${fn##*.} == so ]]; do | |
439 | fn="${fn%.*}" | |
440 | [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}" | |
441 | done | |
442 | ||
0b706743 | 443 | echo "${links}" |
a10a1416 AŻ |
444 | } |
445 | ||
95bde758 | 446 | # Same as above, but specialized to handle dynamic libraries. |
3198f171 VL |
447 | # It handles making symlinks according to how the original library |
448 | # is referenced. | |
36b24d7c | 449 | inst_library() { |
4637c5c2 HH |
450 | local _src="$1" _dest=${2:-$1} _lib _reallib _symlink |
451 | strstr "$1" "/" || return 1 | |
452 | [[ -e $initdir/$_dest ]] && return 0 | |
29b10e65 | 453 | if [[ -L $_src ]]; then |
0b706743 | 454 | # install checksum files also |
29b10e65 | 455 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then |
5f06f0c3 | 456 | inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac" |
f90fd5b3 | 457 | fi |
29b10e65 | 458 | _reallib=$(readlink -f "$_src") |
29b10e65 HH |
459 | inst_simple "$_reallib" "$_reallib" |
460 | inst_dir "${_dest%/*}" | |
9cb8447c | 461 | [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/} |
4637c5c2 | 462 | ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}" |
36b24d7c | 463 | else |
29b10e65 | 464 | inst_simple "$_src" "$_dest" |
36b24d7c | 465 | fi |
a10a1416 AŻ |
466 | |
467 | # Create additional symlinks. See rev_symlinks description. | |
29b10e65 | 468 | for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do |
4637c5c2 | 469 | [[ ! -e $initdir/$_symlink ]] && { |
29b10e65 HH |
470 | ddebug "Creating extra symlink: $_symlink" |
471 | inst_symlink $_symlink | |
a10a1416 AŻ |
472 | } |
473 | done | |
36b24d7c | 474 | } |
3198f171 VL |
475 | |
476 | # find a binary. If we were not passed the full path directly, | |
477 | # search in the usual places to find the binary. | |
6b25d71a | 478 | find_binary() { |
81c6e7fb | 479 | if [[ -z ${1##/*} ]]; then |
86bf239e | 480 | if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then |
81c6e7fb HH |
481 | echo $1 |
482 | return 0 | |
483 | fi | |
484 | fi | |
1610a566 | 485 | |
e29d0b8b | 486 | type -P $1 |
3359c8da | 487 | } |
36b24d7c | 488 | |
3198f171 VL |
489 | # Same as above, but specialized to install binary executables. |
490 | # Install binary executable, and all shared library dependencies, if any. | |
36b24d7c | 491 | inst_binary() { |
d18bc907 | 492 | local _bin _target |
29b10e65 HH |
493 | _bin=$(find_binary "$1") || return 1 |
494 | _target=${2:-$_bin} | |
4637c5c2 | 495 | [[ -e $initdir/$_target ]] && return 0 |
d18bc907 HH |
496 | [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0 |
497 | local _file _line | |
498 | local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' | |
8667f2b7 | 499 | # I love bash! |
d18bc907 HH |
500 | LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do |
501 | [[ $_line = 'not a dynamic executable' ]] && break | |
502 | ||
503 | if [[ $_line =~ $_so_regex ]]; then | |
504 | _file=${BASH_REMATCH[1]} | |
505 | [[ -e ${initdir}/$_file ]] && continue | |
506 | inst_library "$_file" | |
507 | continue | |
508 | fi | |
509 | ||
29b10e65 HH |
510 | if [[ $_line =~ not\ found ]]; then |
511 | dfatal "Missing a shared library required by $_bin." | |
512 | dfatal "Run \"ldd $_bin\" to find out what it is." | |
d18bc907 | 513 | dfatal "$_line" |
e27770e1 | 514 | dfatal "dracut cannot create an initrd." |
cc02093d HH |
515 | exit 1 |
516 | fi | |
5c862533 | 517 | done |
d18bc907 | 518 | inst_simple "$_bin" "$_target" |
36b24d7c | 519 | } |
04b56f3a | 520 | |
36b24d7c VL |
521 | # same as above, except for shell scripts. |
522 | # If your shell script does not start with shebang, it is not a shell script. | |
523 | inst_script() { | |
4637c5c2 HH |
524 | local _bin |
525 | _bin=$(find_binary "$1") || return 1 | |
f60995ad | 526 | shift |
29b10e65 | 527 | local _line _shebang_regex |
4637c5c2 | 528 | read -r -n 80 _line <"$_bin" |
00531568 | 529 | # If debug is set, clean unprintable chars to prevent messing up the term |
29b10e65 HH |
530 | [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]') |
531 | _shebang_regex='(#! *)(/[^ ]+).*' | |
532 | [[ $_line =~ $_shebang_regex ]] || return 1 | |
03cec388 | 533 | inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@" |
04b56f3a JK |
534 | } |
535 | ||
36b24d7c VL |
536 | # same as above, but specialized for symlinks |
537 | inst_symlink() { | |
76a80dff | 538 | local _src=$1 _target=${2:-$1} _realsrc |
4637c5c2 | 539 | strstr "$1" "/" || return 1 |
36b24d7c | 540 | [[ -L $1 ]] || return 1 |
76a80dff | 541 | [[ -L $initdir/$_target ]] && return 0 |
29b10e65 | 542 | _realsrc=$(readlink -f "$_src") |
3249c257 HH |
543 | if ! [[ -e $initdir/$_realsrc ]]; then |
544 | if [[ -d $_realsrc ]]; then | |
545 | inst_dir "$_realsrc" | |
546 | else | |
76a80dff | 547 | inst "$_realsrc" |
3249c257 | 548 | fi |
c3b0d83d | 549 | fi |
e7da9734 | 550 | [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}" |
3249c257 HH |
551 | [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/} |
552 | ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target" | |
36b24d7c VL |
553 | } |
554 | ||
62073c30 CG |
555 | # attempt to install any programs specified in a udev rule |
556 | inst_rule_programs() { | |
557 | local _prog _bin | |
558 | ||
559 | if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then | |
560 | for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do | |
561 | if [ -x /lib/udev/$_prog ]; then | |
562 | _bin=/lib/udev/$_prog | |
563 | else | |
564 | _bin=$(find_binary "$_prog") || { | |
565 | dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" | |
566 | continue; | |
567 | } | |
568 | fi | |
569 | ||
570 | #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" | |
571 | dracut_install "$_bin" | |
572 | done | |
573 | fi | |
574 | } | |
575 | ||
f04dc5f3 VL |
576 | # udev rules always get installed in the same place, so |
577 | # create a function to install them to make life simpler. | |
0b706743 | 578 | inst_rules() { |
29b10e65 | 579 | local _target=/etc/udev/rules.d _rule _found |
fd2312e0 | 580 | |
19612483 | 581 | inst_dir "/lib/udev/rules.d" |
29b10e65 HH |
582 | inst_dir "$_target" |
583 | for _rule in "$@"; do | |
08769b7f | 584 | if [ "${rule#/}" = "$rule" ]; then |
76f5fa54 HH |
585 | for r in /lib/udev/rules.d /etc/udev/rules.d; do |
586 | if [[ -f $r/$_rule ]]; then | |
587 | _found="$r/$_rule" | |
588 | inst_simple "$_found" | |
62073c30 | 589 | inst_rule_programs "$_found" |
76f5fa54 HH |
590 | fi |
591 | done | |
592 | fi | |
c97e1a76 | 593 | for r in '' ./ $dracutbasedir/rules.d/; do |
29b10e65 HH |
594 | if [[ -f ${r}$_rule ]]; then |
595 | _found="${r}$_rule" | |
596 | inst_simple "$_found" "$_target/${_found##*/}" | |
62073c30 | 597 | inst_rule_programs "$_found" |
c97e1a76 HH |
598 | fi |
599 | done | |
29b10e65 | 600 | [[ $_found ]] || dinfo "Skipping udev rule: $_rule" |
f04dc5f3 VL |
601 | done |
602 | } | |
603 | ||
36b24d7c VL |
604 | # general purpose installation function |
605 | # Same args as above. | |
6b24de99 | 606 | inst() { |
29b10e65 HH |
607 | local _x |
608 | ||
94dcc5b8 HH |
609 | case $# in |
610 | 1) ;; | |
3478b314 | 611 | 2) [[ ! $initdir && -d $2 ]] && export initdir=$2 |
cc02093d | 612 | [[ $initdir = $2 ]] && set $1;; |
0b706743 | 613 | 3) [[ -z $initdir ]] && export initdir=$2 |
cc02093d | 614 | set $1 $3;; |
e27770e1 | 615 | *) dfatal "inst only takes 1 or 2 or 3 arguments" |
cc02093d | 616 | exit 1;; |
94dcc5b8 | 617 | esac |
29b10e65 HH |
618 | for _x in inst_symlink inst_script inst_binary inst_simple; do |
619 | $_x "$@" && return 0 | |
36b24d7c VL |
620 | done |
621 | return 1 | |
04b56f3a JK |
622 | } |
623 | ||
53f95456 VL |
624 | # install function specialized for hooks |
625 | # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook | |
626 | # All hooks should be POSIX/SuS compliant, they will be sourced by init. | |
627 | inst_hook() { | |
cdad82fd | 628 | if ! [[ -f $3 ]]; then |
e27770e1 AŻ |
629 | dfatal "Cannot install a hook ($3) that does not exist." |
630 | dfatal "Aborting initrd creation." | |
cc02093d | 631 | exit 1 |
cdad82fd | 632 | elif ! strstr "$hookdirs" "$1"; then |
3b403b32 | 633 | dfatal "No such hook type $1. Aborting initrd creation." |
cc02093d | 634 | exit 1 |
cdad82fd | 635 | fi |
0b53ca70 | 636 | inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}" |
53f95456 VL |
637 | } |
638 | ||
3378a54f AŻ |
639 | # install any of listed files |
640 | # | |
641 | # If first argument is '-d' and second some destination path, first accessible | |
642 | # source is installed into this path, otherwise it will installed in the same | |
643 | # path as source. If none of listed files was installed, function return 1. | |
644 | # On first successful installation it returns with 0 status. | |
645 | # | |
646 | # Example: | |
647 | # | |
648 | # inst_any -d /bin/foo /bin/bar /bin/baz | |
649 | # | |
650 | # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in | |
651 | # initramfs. | |
652 | inst_any() { | |
653 | local to f | |
654 | ||
655 | [[ $1 = '-d' ]] && to="$2" && shift 2 | |
656 | ||
657 | for f in "$@"; do | |
658 | if [[ -e $f ]]; then | |
659 | [[ $to ]] && inst "$f" "$to" && return 0 | |
660 | inst "$f" && return 0 | |
661 | fi | |
662 | done | |
663 | ||
664 | return 1 | |
665 | } | |
666 | ||
7e2bca48 HH |
667 | # dracut_install [-o ] <file> [<file> ... ] |
668 | # Install <file> to the initramfs image | |
669 | # -o optionally install the <file> and don't fail, if it is not there | |
f4fff04e | 670 | dracut_install() { |
29b10e65 | 671 | local _optional=no |
0b706743 | 672 | if [[ $1 = '-o' ]]; then |
29b10e65 | 673 | _optional=yes |
cc02093d | 674 | shift |
bd4c4bcb | 675 | fi |
f4fff04e | 676 | while (($# > 0)); do |
cc02093d | 677 | if ! inst "$1" ; then |
29b10e65 | 678 | if [[ $_optional = yes ]]; then |
a4c235ed | 679 | dinfo "Skipping program $1 as it cannot be found and is" \ |
e27770e1 | 680 | "flagged to be optional" |
cc02093d | 681 | else |
e27770e1 | 682 | dfatal "Failed to install $1" |
cc02093d | 683 | exit 1 |
bd4c4bcb | 684 | fi |
cc02093d HH |
685 | fi |
686 | shift | |
f4fff04e VL |
687 | done |
688 | } | |
689 | ||
87122afc AŻ |
690 | # install function decompressing the target and handling symlinks |
691 | # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files | |
692 | # | |
693 | # Function install targets in the same paths inside overlay but decompressed | |
694 | # and without extensions (.gz, .bz2). | |
695 | inst_decompress() { | |
29b10e65 | 696 | local _src _dst _realsrc _realdst _cmd |
87122afc | 697 | |
29b10e65 | 698 | for _src in $@ |
87122afc | 699 | do |
29b10e65 HH |
700 | case ${_src} in |
701 | *.gz) _cmd='gzip -d' ;; | |
702 | *.bz2) _cmd='bzip2 -d' ;; | |
87122afc AŻ |
703 | *) return 1 ;; |
704 | esac | |
705 | ||
29b10e65 | 706 | if [[ -L ${_src} ]] |
87122afc | 707 | then |
29b10e65 HH |
708 | _realsrc="$(readlink -f ${_src})" # symlink target with extension |
709 | _dst="${_src%.*}" # symlink without extension | |
710 | _realdst="${_realsrc%.*}" # symlink target without extension | |
711 | mksubdirs "${initdir}/${_src}" | |
87122afc | 712 | # Create symlink without extension to target without extension. |
29b10e65 | 713 | ln -sfn "${_realdst}" "${initdir}/${_dst}" |
87122afc AŻ |
714 | fi |
715 | ||
716 | # If the source is symlink we operate on its target. | |
29b10e65 HH |
717 | [[ ${_realsrc} ]] && _src=${_realsrc} |
718 | inst ${_src} | |
87122afc AŻ |
719 | # Decompress with chosen tool. We assume that tool changes name e.g. |
720 | # from 'name.gz' to 'name'. | |
29b10e65 | 721 | ${_cmd} "${initdir}${_src}" |
87122afc AŻ |
722 | done |
723 | } | |
724 | ||
725 | # It's similar to above, but if file is not compressed, performs standard | |
726 | # install. | |
727 | # $@ = list of files | |
728 | inst_opt_decompress() { | |
29b10e65 | 729 | local _src |
87122afc | 730 | |
29b10e65 | 731 | for _src in $@ |
87122afc | 732 | do |
29b10e65 | 733 | inst_decompress "${_src}" || inst "${_src}" |
87122afc AŻ |
734 | done |
735 | } | |
736 | ||
7e2bca48 HH |
737 | # module_check <dracut module> |
738 | # execute the check() function of module-setup.sh of <dracut module> | |
739 | # or the "check" script, if module-setup.sh is not found | |
740 | # "check $hostonly" is called | |
95d2dabc | 741 | module_check() { |
29b10e65 HH |
742 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) |
743 | local _ret | |
31f1c02d AW |
744 | local _forced=0 |
745 | local _hostonly=$hostonly | |
746 | [ $# -eq 2 ] && _forced=$2 | |
29b10e65 HH |
747 | [[ -d $_moddir ]] || return 1 |
748 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
95d2dabc | 749 | # if we do not have a check script, we are unconditionally included |
29b10e65 | 750 | [[ -x $_moddir/check ]] || return 0 |
31f1c02d | 751 | [ $_forced -ne 0 ] && unset hostonly |
29b10e65 | 752 | $_moddir/check $hostonly |
31f1c02d | 753 | _ret=$? |
95d2dabc HH |
754 | else |
755 | unset check depends install installkernel | |
29b10e65 | 756 | . $_moddir/module-setup.sh |
95d2dabc | 757 | is_func check || return 0 |
31f1c02d AW |
758 | [ $_forced -ne 0 ] && unset hostonly |
759 | check $hostonly | |
29b10e65 | 760 | _ret=$? |
95d2dabc | 761 | unset check depends install installkernel |
0c2e3d12 | 762 | fi |
31f1c02d AW |
763 | hostonly=$_hostonly |
764 | return $_ret | |
0c2e3d12 VL |
765 | } |
766 | ||
7e2bca48 HH |
767 | # module_check_mount <dracut module> |
768 | # execute the check() function of module-setup.sh of <dracut module> | |
769 | # or the "check" script, if module-setup.sh is not found | |
770 | # "mount_needs=1 check 0" is called | |
771 | module_check_mount() { | |
772 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
773 | local _ret | |
774 | mount_needs=1 | |
775 | [[ -d $_moddir ]] || return 1 | |
776 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
777 | # if we do not have a check script, we are unconditionally included | |
778 | [[ -x $_moddir/check ]] || return 0 | |
779 | mount_needs=1 $_moddir/check 0 | |
780 | _ret=$? | |
781 | else | |
782 | unset check depends install installkernel | |
783 | . $_moddir/module-setup.sh | |
784 | is_func check || return 1 | |
785 | check 0 | |
786 | _ret=$? | |
787 | unset check depends install installkernel | |
788 | fi | |
789 | unset mount_needs | |
790 | return $_ret | |
791 | } | |
792 | ||
793 | # module_depends <dracut module> | |
794 | # execute the depends() function of module-setup.sh of <dracut module> | |
795 | # or the "depends" script, if module-setup.sh is not found | |
95d2dabc | 796 | module_depends() { |
29b10e65 HH |
797 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) |
798 | local _ret | |
799 | [[ -d $_moddir ]] || return 1 | |
800 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
95d2dabc | 801 | # if we do not have a check script, we have no deps |
29b10e65 HH |
802 | [[ -x $_moddir/check ]] || return 0 |
803 | $_moddir/check -d | |
95d2dabc HH |
804 | return $? |
805 | else | |
806 | unset check depends install installkernel | |
29b10e65 | 807 | . $_moddir/module-setup.sh |
95d2dabc HH |
808 | is_func depends || return 0 |
809 | depends | |
29b10e65 | 810 | _ret=$? |
95d2dabc | 811 | unset check depends install installkernel |
29b10e65 | 812 | return $_ret |
33ee031c | 813 | fi |
0c2e3d12 VL |
814 | } |
815 | ||
7e2bca48 HH |
816 | # module_install <dracut module> |
817 | # execute the install() function of module-setup.sh of <dracut module> | |
818 | # or the "install" script, if module-setup.sh is not found | |
95d2dabc | 819 | module_install() { |
29b10e65 HH |
820 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) |
821 | local _ret | |
822 | [[ -d $_moddir ]] || return 1 | |
823 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
824 | [[ -x $_moddir/install ]] && . "$_moddir/install" | |
95d2dabc HH |
825 | return $? |
826 | else | |
827 | unset check depends install installkernel | |
29b10e65 | 828 | . $_moddir/module-setup.sh |
95d2dabc HH |
829 | is_func install || return 0 |
830 | install | |
29b10e65 | 831 | _ret=$? |
95d2dabc | 832 | unset check depends install installkernel |
29b10e65 | 833 | return $_ret |
95d2dabc HH |
834 | fi |
835 | } | |
836 | ||
7e2bca48 HH |
837 | # module_installkernel <dracut module> |
838 | # execute the installkernel() function of module-setup.sh of <dracut module> | |
839 | # or the "installkernel" script, if module-setup.sh is not found | |
95d2dabc | 840 | module_installkernel() { |
29b10e65 HH |
841 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) |
842 | local _ret | |
843 | [[ -d $_moddir ]] || return 1 | |
844 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
845 | [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel" | |
95d2dabc HH |
846 | return $? |
847 | else | |
848 | unset check depends install installkernel | |
29b10e65 | 849 | . $_moddir/module-setup.sh |
95d2dabc HH |
850 | is_func installkernel || return 0 |
851 | installkernel | |
29b10e65 | 852 | _ret=$? |
95d2dabc | 853 | unset check depends install installkernel |
29b10e65 | 854 | return $_ret |
95d2dabc HH |
855 | fi |
856 | } | |
857 | ||
7e2bca48 HH |
858 | # check_mount <dracut module> |
859 | # check_mount checks, if a dracut module is needed for the given | |
860 | # device and filesystem types in "${host_fs_types[@]}" | |
1b7fd0fa AW |
861 | check_mount() { |
862 | local _mod=$1 | |
863 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
864 | local _ret | |
865 | local _moddep | |
866 | # If we are already scheduled to be loaded, no need to check again. | |
867 | strstr " $mods_to_load " " $_mod " && return 0 | |
868 | strstr " $mods_checked_as_dep " " $_mod " && return 1 | |
869 | ||
870 | # This should never happen, but... | |
871 | [[ -d $_moddir ]] || return 1 | |
872 | ||
873 | [[ $2 ]] || mods_checked_as_dep+=" $_mod " | |
874 | ||
875 | strstr " $omit_dracutmodules " " $_mod " && return 1 | |
876 | ||
877 | if [ "${#host_fs_types[*]}" -gt 0 ]; then | |
878 | module_check_mount $_mod || return 1 | |
879 | else | |
880 | # skip this module | |
881 | return 1 | |
882 | fi | |
883 | ||
884 | for _moddep in $(module_depends $_mod); do | |
885 | # handle deps as if they were manually added | |
886 | strstr " $add_dracutmodules " " $_moddep " || \ | |
887 | add_dracutmodules+=" $_moddep " | |
888 | strstr " $force_add_dracutmodules " " $_moddep " || \ | |
889 | force_add_dracutmodules+=" $_moddep " | |
890 | # if a module we depend on fail, fail also | |
891 | check_module $_moddep || return 1 | |
892 | done | |
893 | ||
894 | strstr " $mods_to_load " " $_mod " || \ | |
895 | mods_to_load+=" $_mod " | |
896 | ||
897 | return 0 | |
898 | } | |
899 | ||
7e2bca48 HH |
900 | # check_module <dracut module> [<use_as_dep>] |
901 | # check if a dracut module is to be used in the initramfs process | |
902 | # if <use_as_dep> is set, then the process also keeps track | |
903 | # that the modules were checked for the dependency tracking process | |
95d2dabc | 904 | check_module() { |
29b10e65 HH |
905 | local _mod=$1 |
906 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
907 | local _ret | |
908 | local _moddep | |
95d2dabc | 909 | # If we are already scheduled to be loaded, no need to check again. |
29b10e65 HH |
910 | strstr " $mods_to_load " " $_mod " && return 0 |
911 | strstr " $mods_checked_as_dep " " $_mod " && return 1 | |
95d2dabc HH |
912 | |
913 | # This should never happen, but... | |
29b10e65 | 914 | [[ -d $_moddir ]] || return 1 |
95d2dabc | 915 | |
29b10e65 | 916 | [[ $2 ]] || mods_checked_as_dep+=" $_mod " |
95d2dabc | 917 | |
29b10e65 | 918 | strstr " $omit_dracutmodules " " $_mod " && return 1 |
95d2dabc | 919 | |
31f1c02d AW |
920 | if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then |
921 | if strstr " $force_add_dracutmodules" " $_mod"; then | |
922 | module_check $_mod 1; ret=$? | |
923 | else | |
924 | module_check $_mod 0; ret=$? | |
925 | fi | |
95d2dabc HH |
926 | # explicit module, so also accept ret=255 |
927 | [[ $ret = 0 || $ret = 255 ]] || return 1 | |
928 | else | |
929 | # module not in our list | |
930 | if [[ $dracutmodules = all ]]; then | |
931 | # check, if we can and should install this module | |
29b10e65 | 932 | module_check $_mod || return 1 |
cc02093d | 933 | else |
95d2dabc HH |
934 | # skip this module |
935 | return 1 | |
970e646b | 936 | fi |
95d2dabc | 937 | fi |
bb764545 | 938 | |
29b10e65 | 939 | for _moddep in $(module_depends $_mod); do |
95d2dabc | 940 | # handle deps as if they were manually added |
29b10e65 HH |
941 | strstr " $add_dracutmodules " " $_moddep " || \ |
942 | add_dracutmodules+=" $_moddep " | |
31f1c02d AW |
943 | strstr " $force_add_dracutmodules " " $_moddep " || \ |
944 | force_add_dracutmodules+=" $_moddep " | |
95d2dabc | 945 | # if a module we depend on fail, fail also |
29b10e65 | 946 | check_module $_moddep || return 1 |
0c2e3d12 | 947 | done |
70503db4 | 948 | |
29b10e65 HH |
949 | strstr " $mods_to_load " " $_mod " || \ |
950 | mods_to_load+=" $_mod " | |
95d2dabc HH |
951 | |
952 | return 0 | |
953 | } | |
954 | ||
7e2bca48 HH |
955 | # for_each_module_dir <func> |
956 | # execute "<func> <dracut module> 1" | |
1b7fd0fa | 957 | for_each_module_dir() { |
29b10e65 HH |
958 | local _modcheck |
959 | local _mod | |
960 | local _moddir | |
1b7fd0fa AW |
961 | local _func |
962 | _func=$1 | |
29b10e65 HH |
963 | for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do |
964 | _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]} | |
1b7fd0fa | 965 | $_func $_mod 1 |
70503db4 | 966 | done |
724b87a6 HH |
967 | |
968 | # Report any missing dracut modules, the user has specified | |
31f1c02d | 969 | _modcheck="$add_dracutmodules $force_add_dracutmodules" |
29b10e65 HH |
970 | [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules" |
971 | for _mod in $_modcheck; do | |
972 | strstr "$mods_to_load" "$_mod" && continue | |
973 | strstr "$omit_dracutmodules" "$_mod" && continue | |
974 | derror "Dracut module \"$_mod\" cannot be found." | |
724b87a6 | 975 | done |
0c2e3d12 VL |
976 | } |
977 | ||
ddfd1d10 VL |
978 | # Install a single kernel module along with any firmware it may require. |
979 | # $1 = full path to kernel module to install | |
980 | install_kmod_with_fw() { | |
0b440844 | 981 | # no need to go further if the module is already installed |
379c34d2 | 982 | |
0b706743 AŻ |
983 | [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \ |
984 | && return 0 | |
fcbcb252 | 985 | |
379c34d2 HH |
986 | [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0 |
987 | ||
fcbcb252 HH |
988 | if [[ $omit_drivers ]]; then |
989 | local _kmod=${1##*/} | |
990 | _kmod=${_kmod%.ko} | |
991 | _kmod=${_kmod/-/_} | |
34248c92 HH |
992 | if [[ "$_kmod" =~ $omit_drivers ]]; then |
993 | dinfo "Omitting driver $_kmod" | |
994 | return 1 | |
995 | fi | |
996 | if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then | |
fcbcb252 HH |
997 | dinfo "Omitting driver $_kmod" |
998 | return 1 | |
999 | fi | |
1000 | fi | |
1001 | ||
6a2c23d1 HH |
1002 | [ -d "$initdir/.kernelmodseen" ] && \ |
1003 | > "$initdir/.kernelmodseen/${1##*/}" | |
e12c1a8d | 1004 | |
0b706743 AŻ |
1005 | inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" \ |
1006 | || return $? | |
e6024e00 JR |
1007 | |
1008 | local _modname=${1##*/} _fwdir _found _fw | |
1009 | _modname=${_modname%.ko*} | |
29b10e65 HH |
1010 | for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do |
1011 | _found='' | |
1012 | for _fwdir in $fw_dir; do | |
1013 | if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then | |
1014 | inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw" | |
1015 | _found=yes | |
cc02093d HH |
1016 | fi |
1017 | done | |
29b10e65 HH |
1018 | if [[ $_found != yes ]]; then |
1019 | if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then | |
1020 | dinfo "Possible missing firmware \"${_fw}\" for kernel module" \ | |
1021 | "\"${_modname}.ko\"" | |
e2cdb570 | 1022 | else |
29b10e65 HH |
1023 | dwarn "Possible missing firmware \"${_fw}\" for kernel module" \ |
1024 | "\"${_modname}.ko\"" | |
e2cdb570 | 1025 | fi |
cc02093d | 1026 | fi |
ddfd1d10 | 1027 | done |
0b440844 | 1028 | return 0 |
ddfd1d10 VL |
1029 | } |
1030 | ||
1031 | # Do something with all the dependencies of a kernel module. | |
1032 | # Note that kernel modules depend on themselves using the technique we use | |
0b706743 AŻ |
1033 | # $1 = function to call for each dependency we find |
1034 | # It will be passed the full path to the found kernel module | |
ddfd1d10 VL |
1035 | # $2 = module to get dependencies for |
1036 | # rest of args = arguments to modprobe | |
c32bda6b | 1037 | # _fderr specifies FD passed from surrounding scope |
ddfd1d10 | 1038 | for_each_kmod_dep() { |
29b10e65 | 1039 | local _func=$1 _kmod=$2 _cmd _modpath _options _found=0 |
ddfd1d10 | 1040 | shift 2 |
c32bda6b | 1041 | modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | ( |
29b10e65 HH |
1042 | while read _cmd _modpath _options; do |
1043 | [[ $_cmd = insmod ]] || continue | |
1044 | $_func ${_modpath} || exit $? | |
1045 | _found=1 | |
0b706743 | 1046 | done |
29b10e65 | 1047 | [[ $_found -eq 0 ]] && exit 1 |
0b706743 AŻ |
1048 | exit 0 |
1049 | ) | |
ddfd1d10 VL |
1050 | } |
1051 | ||
4073c815 HH |
1052 | # filter kernel modules to install certain modules that meet specific |
1053 | # requirements. | |
1054 | # $1 = search only in subdirectory of /kernel/$1 | |
1055 | # $2 = function to call with module name to filter. | |
1056 | # This function will be passed the full path to the module to test. | |
1057 | # The behaviour of this function can vary depending on whether $hostonly is set. | |
1058 | # If it is, we will only look at modules that are already in memory. | |
1059 | # If it is not, we will look at all kernel modules | |
1060 | # This function returns the full filenames of modules that match $1 | |
1061 | filter_kernel_modules_by_path () ( | |
1062 | local _modname _filtercmd | |
1063 | if ! [[ $hostonly ]]; then | |
1064 | _filtercmd='find "$srcmods/kernel/$1" "$srcmods/extra"' | |
1065 | _filtercmd+=' "$srcmods/weak-updates" -name "*.ko" -o -name "*.ko.gz"' | |
7fffc9f1 | 1066 | _filtercmd+=' -o -name "*.ko.xz"' |
4073c815 HH |
1067 | _filtercmd+=' 2>/dev/null' |
1068 | else | |
1069 | _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename ' | |
1070 | _filtercmd+='-k $kernel 2>/dev/null' | |
1071 | fi | |
1072 | for _modname in $(eval $_filtercmd); do | |
1073 | case $_modname in | |
1074 | *.ko) "$2" "$_modname" && echo "$_modname";; | |
1075 | *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko | |
1076 | $2 $initdir/$$.ko && echo "$_modname" | |
1077 | rm -f $initdir/$$.ko | |
1078 | ;; | |
7fffc9f1 JB |
1079 | *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko |
1080 | $2 $initdir/$$.ko && echo "$_modname" | |
1081 | rm -f $initdir/$$.ko | |
1082 | ;; | |
4073c815 HH |
1083 | esac |
1084 | done | |
1085 | ) | |
881eda69 JR |
1086 | find_kernel_modules_by_path () ( |
1087 | if ! [[ $hostonly ]]; then | |
1088 | find "$srcmods/kernel/$1" "$srcmods/extra" "$srcmods/weak-updates" \ | |
7fffc9f1 | 1089 | -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null |
881eda69 JR |
1090 | else |
1091 | cut -d " " -f 1 </proc/modules \ | |
1092 | | xargs modinfo -F filename -k $kernel 2>/dev/null | |
1093 | fi | |
1094 | ) | |
4073c815 | 1095 | |
ceebd9ac JR |
1096 | filter_kernel_modules () { |
1097 | filter_kernel_modules_by_path drivers "$1" | |
1098 | } | |
240cc7c4 | 1099 | |
881eda69 JR |
1100 | find_kernel_modules () { |
1101 | find_kernel_modules_by_path drivers | |
1102 | } | |
1103 | ||
7e2bca48 HH |
1104 | # instmods <kernel module> [<kernel module> ... ] |
1105 | # instmods <kernel subsystem> | |
ddfd1d10 | 1106 | # install kernel modules along with all their dependencies. |
7e2bca48 | 1107 | # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage" |
6fa0c3d6 | 1108 | instmods() { |
0c1a8ebc | 1109 | [[ $no_kernel = yes ]] && return |
c32bda6b MS |
1110 | # called [sub]functions inherit _fderr |
1111 | local _fderr=9 | |
881eda69 JR |
1112 | |
1113 | function inst1mod() { | |
1114 | local _mod="$1" | |
29b10e65 | 1115 | case $_mod in |
0b706743 | 1116 | =*) |
edea870c | 1117 | if [ -f $srcmods/modules.${_mod#=} ]; then |
0024702f JR |
1118 | ( [[ "$_mpargs" ]] && echo $_mpargs |
1119 | cat "${srcmods}/modules.${_mod#=}" ) \ | |
881eda69 | 1120 | | instmods |
cc02093d | 1121 | else |
0024702f | 1122 | ( [[ "$_mpargs" ]] && echo $_mpargs |
334cc283 | 1123 | find "$srcmods" -path "*/${_mod#=}/*" -printf '%f\n' ) \ |
881eda69 | 1124 | | instmods |
cc02093d HH |
1125 | fi |
1126 | ;; | |
0024702f | 1127 | --*) _mpargs+=" $_mod" ;; |
881eda69 | 1128 | i2o_scsi) return ;; # Do not load this diagnostic-only module |
34248c92 | 1129 | *) |
379c34d2 | 1130 | _mod=${_mod##*/} |
6568d86a | 1131 | # if we are already installed, skip this module and go on |
cc02093d | 1132 | # to the next one. |
379c34d2 | 1133 | [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return |
881eda69 | 1134 | |
34248c92 HH |
1135 | if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then |
1136 | dinfo "Omitting driver ${_mod##$srcmods}" | |
1137 | return | |
1138 | fi | |
cc02093d HH |
1139 | # If we are building a host-specific initramfs and this |
1140 | # module is not already loaded, move on to the next one. | |
29b10e65 | 1141 | [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \ |
881eda69 JR |
1142 | && ! echo $add_drivers | grep -qe "\<${_mod}\>" \ |
1143 | && return | |
04b56f3a | 1144 | |
cc02093d HH |
1145 | # We use '-d' option in modprobe only if modules prefix path |
1146 | # differs from default '/'. This allows us to use Dracut with | |
1147 | # old version of modprobe which doesn't have '-d' option. | |
881eda69 | 1148 | local _moddirname=${srcmods%%/lib/modules/*} |
29b10e65 | 1149 | [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/" |
0b706743 | 1150 | |
cc02093d HH |
1151 | # ok, load the module, all its dependencies, and any firmware |
1152 | # it may require | |
29b10e65 | 1153 | for_each_kmod_dep install_kmod_with_fw $_mod \ |
f4ca564b | 1154 | --set-version $kernel ${_moddirname} $_mpargs |
29b10e65 | 1155 | ((_ret+=$?)) |
cc02093d | 1156 | ;; |
0b706743 | 1157 | esac |
881eda69 JR |
1158 | } |
1159 | ||
f9708da2 JR |
1160 | function instmods_1() { |
1161 | local _ret=0 _mod _mpargs | |
1162 | if (($# == 0)); then # filenames from stdin | |
1163 | while read _mod; do | |
1164 | inst1mod "${_mod%.ko*}" | |
1165 | done | |
1166 | fi | |
1167 | while (($# > 0)); do # filenames as arguments | |
1168 | inst1mod ${1%.ko*} | |
1169 | shift | |
881eda69 | 1170 | done |
f9708da2 JR |
1171 | return $_ret |
1172 | } | |
1173 | ||
86191581 | 1174 | local _filter_not_found='FATAL: Module .* not found.' |
c32bda6b MS |
1175 | # Capture all stderr from modprobe to _fderr. We could use {var}>... |
1176 | # redirections, but that would make dracut require bash4 at least. | |
1177 | eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \ | |
86191581 | 1178 | | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror |
f9708da2 | 1179 | return $? |
0b706743 | 1180 | } |