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