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