]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/bash | |
2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- | |
3 | # ex: ts=8 sw=4 sts=4 et filetype=sh | |
4 | # | |
5 | # functions used by dracut and other tools. | |
6 | # | |
7 | # Copyright 2005-2009 Red Hat, Inc. All rights reserved. | |
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 | |
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | # | |
22 | export LC_MESSAGES=C | |
23 | ||
24 | if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then | |
25 | if ! [[ -d "$initdir/.kernelmodseen" ]]; then | |
26 | mkdir -p "$initdir/.kernelmodseen" | |
27 | fi | |
28 | DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen" | |
29 | fi | |
30 | ||
31 | if [[ $initdir ]] && ! [[ -d $initdir ]]; then | |
32 | mkdir -p "$initdir" | |
33 | fi | |
34 | ||
35 | # Generic substring function. If $2 is in $1, return 0. | |
36 | strstr() { [[ $1 = *$2* ]]; } | |
37 | ||
38 | # find a binary. If we were not passed the full path directly, | |
39 | # search in the usual places to find the binary. | |
40 | find_binary() { | |
41 | if [[ -z ${1##/*} ]]; then | |
42 | if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then | |
43 | echo $1 | |
44 | return 0 | |
45 | fi | |
46 | fi | |
47 | ||
48 | type -P $1 | |
49 | } | |
50 | ||
51 | if ! [[ $dracutbasedir ]]; then | |
52 | dracutbasedir=${BASH_SOURCE[0]%/*} | |
53 | [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="." | |
54 | [[ $dracutbasedir ]] || dracutbasedir="." | |
55 | dracutbasedir="$(readlink -f $dracutbasedir)" | |
56 | fi | |
57 | ||
58 | if ! [[ $DRACUT_INSTALL ]]; then | |
59 | DRACUT_INSTALL=$(find_binary dracut-install) | |
60 | fi | |
61 | ||
62 | if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then | |
63 | DRACUT_INSTALL=$dracutbasedir/dracut-install | |
64 | fi | |
65 | ||
66 | # Detect lib paths | |
67 | if ! [[ $libdirs ]] ; then | |
68 | if strstr "$(ldd /bin/sh)" "/lib64/" &>/dev/null \ | |
69 | && [[ -d /lib64 ]]; then | |
70 | libdirs+=" /lib64" | |
71 | [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64" | |
72 | else | |
73 | libdirs+=" /lib" | |
74 | [[ -d /usr/lib ]] && libdirs+=" /usr/lib" | |
75 | fi | |
76 | export libdirs | |
77 | fi | |
78 | ||
79 | if ! [[ $kernel ]]; then | |
80 | kernel=$(uname -r) | |
81 | export kernel | |
82 | fi | |
83 | ||
84 | srcmods="/lib/modules/$kernel/" | |
85 | [[ $drivers_dir ]] && { | |
86 | if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then | |
87 | dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.' | |
88 | exit 1 | |
89 | fi | |
90 | srcmods="$drivers_dir" | |
91 | } | |
92 | export srcmods | |
93 | ||
94 | if ! type dinfo >/dev/null 2>&1; then | |
95 | . "$dracutbasedir/dracut-logger.sh" | |
96 | dlog_init | |
97 | fi | |
98 | ||
99 | if ! [[ $initdir ]]; then | |
100 | dfatal "initdir not set" | |
101 | exit 1 | |
102 | fi | |
103 | ||
104 | # export standard hookdirs | |
105 | [[ $hookdirs ]] || { | |
106 | hookdirs="cmdline pre-udev pre-trigger netroot " | |
107 | hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout " | |
108 | hookdirs+="pre-mount pre-pivot cleanup mount " | |
109 | hookdirs+="emergency shutdown-emergency shutdown " | |
110 | export hookdirs | |
111 | } | |
112 | ||
113 | # Create all subdirectories for given path without creating the last element. | |
114 | # $1 = path | |
115 | mksubdirs() { [[ -e ${1%/*} ]] || mkdir -m 0755 -p ${1%/*}; } | |
116 | ||
117 | # Version comparision function. Assumes Linux style version scheme. | |
118 | # $1 = version a | |
119 | # $2 = comparision op (gt, ge, eq, le, lt, ne) | |
120 | # $3 = version b | |
121 | vercmp() { | |
122 | local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res | |
123 | ||
124 | for ((_i=0; ; _i++)) | |
125 | do | |
126 | if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0 | |
127 | elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1 | |
128 | elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2 | |
129 | else continue | |
130 | fi | |
131 | break | |
132 | done | |
133 | ||
134 | case $_op in | |
135 | gt) ((_res == 1));; | |
136 | ge) ((_res != 2));; | |
137 | eq) ((_res == 0));; | |
138 | le) ((_res != 1));; | |
139 | lt) ((_res == 2));; | |
140 | ne) ((_res != 0));; | |
141 | esac | |
142 | } | |
143 | ||
144 | # is_func <command> | |
145 | # Check whether $1 is a function. | |
146 | is_func() { | |
147 | [[ $(type -t $1) = "function" ]] | |
148 | } | |
149 | ||
150 | # Function prints global variables in format name=value line by line. | |
151 | # $@ = list of global variables' name | |
152 | print_vars() { | |
153 | local _var _value | |
154 | ||
155 | for _var in $@ | |
156 | do | |
157 | _value=$(eval echo \$$_var) | |
158 | [[ ${_value} ]] && echo "${_var}=\"${_value}\"" | |
159 | done | |
160 | } | |
161 | ||
162 | # normalize_path <path> | |
163 | # Prints the normalized path, where it removes any duplicated | |
164 | # and trailing slashes. | |
165 | # Example: | |
166 | # $ normalize_path ///test/test// | |
167 | # /test/test | |
168 | normalize_path() { | |
169 | shopt -q -s extglob | |
170 | set -- "${1//+(\/)//}" | |
171 | shopt -q -u extglob | |
172 | echo "${1%/}" | |
173 | } | |
174 | ||
175 | # convert_abs_rel <from> <to> | |
176 | # Prints the relative path, when creating a symlink to <to> from <from>. | |
177 | # Example: | |
178 | # $ convert_abs_rel /usr/bin/test /bin/test-2 | |
179 | # ../../bin/test-2 | |
180 | # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test | |
181 | convert_abs_rel() { | |
182 | local __current __absolute __abssize __cursize __newpath | |
183 | local -i __i __level | |
184 | ||
185 | set -- "$(normalize_path "$1")" "$(normalize_path "$2")" | |
186 | ||
187 | # corner case #1 - self looping link | |
188 | [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; } | |
189 | ||
190 | # corner case #2 - own dir link | |
191 | [[ "${1%/*}" == "$2" ]] && { echo "."; return; } | |
192 | ||
193 | IFS="/" __current=($1) | |
194 | IFS="/" __absolute=($2) | |
195 | ||
196 | __abssize=${#__absolute[@]} | |
197 | __cursize=${#__current[@]} | |
198 | ||
199 | while [[ ${__absolute[__level]} == ${__current[__level]} ]] | |
200 | do | |
201 | (( __level++ )) | |
202 | if (( __level > __abssize || __level > __cursize )) | |
203 | then | |
204 | break | |
205 | fi | |
206 | done | |
207 | ||
208 | for ((__i = __level; __i < __cursize-1; __i++)) | |
209 | do | |
210 | if ((__i > __level)) | |
211 | then | |
212 | __newpath=$__newpath"/" | |
213 | fi | |
214 | __newpath=$__newpath".." | |
215 | done | |
216 | ||
217 | for ((__i = __level; __i < __abssize; __i++)) | |
218 | do | |
219 | if [[ -n $__newpath ]] | |
220 | then | |
221 | __newpath=$__newpath"/" | |
222 | fi | |
223 | __newpath=$__newpath${__absolute[__i]} | |
224 | done | |
225 | ||
226 | echo "$__newpath" | |
227 | } | |
228 | ||
229 | if strstr "$(ln --help)" "--relative"; then | |
230 | ln_r() { | |
231 | ln -sfnr "${initdir}/$1" "${initdir}/$2" | |
232 | } | |
233 | else | |
234 | ln_r() { | |
235 | local _source=$1 | |
236 | local _dest=$2 | |
237 | [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/} | |
238 | ln -sfn $(convert_abs_rel "${_dest}" "${_source}") "${initdir}/${_dest}" | |
239 | } | |
240 | fi | |
241 | ||
242 | # get_fs_env <device> | |
243 | # Get and set the ID_FS_TYPE and ID_FS_UUID variable from udev for a device. | |
244 | # Example: | |
245 | # $ get_fs_env /dev/sda2; echo $ID_FS_TYPE; echo $ID_FS_UUID | |
246 | # ext4 | |
247 | # 551a39aa-4ae9-4e70-a262-ef665cadb574 | |
248 | get_fs_env() { | |
249 | local evalstr | |
250 | local found | |
251 | ||
252 | [[ $1 ]] || return | |
253 | unset ID_FS_TYPE | |
254 | unset ID_FS_UUID | |
255 | if evalstr=$(udevadm info --query=env --name=$1 \ | |
256 | | { while read line; do | |
257 | strstr "$line" "DEVPATH" && found=1; | |
258 | strstr "$line" "ID_FS_TYPE=" && { echo $line; exit 0;} | |
259 | done; [[ $found ]] && exit 0; exit 1; }) ; then | |
260 | eval $evalstr | |
261 | [[ $ID_FS_TYPE ]] && return 0 | |
262 | return 1 | |
263 | fi | |
264 | ||
265 | # Fallback, if we don't have udev information | |
266 | if find_binary blkid >/dev/null; then | |
267 | eval $(blkid -o udev $1 \ | |
268 | | while read line; do | |
269 | strstr "$line" "ID_FS_TYPE=" && echo $line; | |
270 | done) | |
271 | [[ $ID_FS_TYPE ]] && return 0 | |
272 | fi | |
273 | return 1 | |
274 | } | |
275 | ||
276 | # get_maj_min <device> | |
277 | # Prints the major and minor of a device node. | |
278 | # Example: | |
279 | # $ get_maj_min /dev/sda2 | |
280 | # 8:2 | |
281 | get_maj_min() { | |
282 | local _dev | |
283 | _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null) | |
284 | _dev=$(eval "echo $_dev") | |
285 | echo $_dev | |
286 | } | |
287 | ||
288 | # find_block_device <mountpoint> | |
289 | # Prints the major and minor number of the block device | |
290 | # for a given mountpoint. | |
291 | # Unless $use_fstab is set to "yes" the functions | |
292 | # uses /proc/self/mountinfo as the primary source of the | |
293 | # information and only falls back to /etc/fstab, if the mountpoint | |
294 | # is not found there. | |
295 | # Example: | |
296 | # $ find_block_device /usr | |
297 | # 8:4 | |
298 | find_block_device() { | |
299 | local _x _mpt _majmin _dev _fs _maj _min | |
300 | if [[ $use_fstab != yes ]]; then | |
301 | while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do | |
302 | [[ $_mpt = $1 ]] || continue | |
303 | [[ $_fs = nfs ]] && { echo $_dev; return 0;} | |
304 | [[ $_fs = nfs3 ]] && { echo $_dev; return 0;} | |
305 | [[ $_fs = nfs4 ]] && { echo $_dev; return 0;} | |
306 | [[ $_fs = btrfs ]] && { | |
307 | get_maj_min $_dev | |
308 | return 0; | |
309 | } | |
310 | if [[ ${_majmin#0:} = $_majmin ]]; then | |
311 | echo $_majmin | |
312 | return 0 # we have a winner! | |
313 | fi | |
314 | done < /proc/self/mountinfo | |
315 | fi | |
316 | # fall back to /etc/fstab | |
317 | while read _dev _mpt _fs _x; do | |
318 | [ "${_dev%%#*}" != "$_dev" ] && continue | |
319 | ||
320 | if [[ $_mpt = $1 ]]; then | |
321 | [[ $_fs = nfs ]] && { echo $_dev; return 0;} | |
322 | [[ $_fs = nfs3 ]] && { echo $_dev; return 0;} | |
323 | [[ $_fs = nfs4 ]] && { echo $_dev; return 0;} | |
324 | [[ $_dev != ${_dev#UUID=} ]] && _dev=/dev/disk/by-uuid/${_dev#UUID=} | |
325 | [[ $_dev != ${_dev#LABEL=} ]] && _dev=/dev/disk/by-label/${_dev#LABEL=} | |
326 | [[ -b $_dev ]] || return 1 # oops, not a block device. | |
327 | get_maj_min "$_dev" && return 0 | |
328 | fi | |
329 | done < /etc/fstab | |
330 | ||
331 | return 1 | |
332 | } | |
333 | ||
334 | # find_dev_fstype <device> | |
335 | # Echo the filesystem type for a given device. | |
336 | # /proc/self/mountinfo is taken as the primary source of information | |
337 | # and /etc/fstab is used as a fallback. | |
338 | # No newline is appended! | |
339 | # Example: | |
340 | # $ find_dev_fstype /dev/sda2;echo | |
341 | # ext4 | |
342 | find_dev_fstype() { | |
343 | local _x _mpt _majmin _dev _fs _maj _min | |
344 | while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do | |
345 | [[ $_dev = $1 ]] || continue | |
346 | echo -n $_fs; | |
347 | return 0; | |
348 | done < /proc/self/mountinfo | |
349 | ||
350 | # fall back to /etc/fstab | |
351 | while read _dev _mpt _fs _x; do | |
352 | [[ $_dev = $1 ]] || continue | |
353 | echo -n $_fs; | |
354 | return 0; | |
355 | done < /etc/fstab | |
356 | ||
357 | return 1 | |
358 | } | |
359 | ||
360 | # finds the major:minor of the block device backing the root filesystem. | |
361 | find_root_block_device() { find_block_device /; } | |
362 | ||
363 | # for_each_host_dev_fs <func> | |
364 | # Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found | |
365 | # in ${host_fs_types[@]} | |
366 | for_each_host_dev_fs() | |
367 | { | |
368 | local _func="$1" | |
369 | local _dev | |
370 | local _fs | |
371 | local _ret=1 | |
372 | for f in ${host_fs_types[@]}; do | |
373 | OLDIFS="$IFS" | |
374 | IFS="|" | |
375 | set -- $f | |
376 | IFS="$OLDIFS" | |
377 | _dev="$1" | |
378 | [[ -b "$_dev" ]] || continue | |
379 | _fs="$2" | |
380 | $_func $_dev $_fs && _ret=0 | |
381 | done | |
382 | return $_ret | |
383 | } | |
384 | ||
385 | # Walk all the slave relationships for a given block device. | |
386 | # Stop when our helper function returns success | |
387 | # $1 = function to call on every found block device | |
388 | # $2 = block device in major:minor format | |
389 | check_block_and_slaves() { | |
390 | local _x | |
391 | [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry. | |
392 | "$1" $2 && return | |
393 | check_vol_slaves "$@" && return 0 | |
394 | if [[ -f /sys/dev/block/$2/../dev ]]; then | |
395 | check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0 | |
396 | fi | |
397 | [[ -d /sys/dev/block/$2/slaves ]] || return 1 | |
398 | for _x in /sys/dev/block/$2/slaves/*/dev; do | |
399 | [[ -f $_x ]] || continue | |
400 | check_block_and_slaves $1 $(cat "$_x") && return 0 | |
401 | done | |
402 | return 1 | |
403 | } | |
404 | ||
405 | # ugly workaround for the lvm design | |
406 | # There is no volume group device, | |
407 | # so, there are no slave devices for volume groups. | |
408 | # Logical volumes only have the slave devices they really live on, | |
409 | # but you cannot create the logical volume without the volume group. | |
410 | # And the volume group might be bigger than the devices the LV needs. | |
411 | check_vol_slaves() { | |
412 | local _lv _vg _pv | |
413 | for i in /dev/mapper/*; do | |
414 | _lv=$(get_maj_min $i) | |
415 | if [[ $_lv = $2 ]]; then | |
416 | _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null) | |
417 | # strip space | |
418 | _vg=$(echo $_vg) | |
419 | if [[ $_vg ]]; then | |
420 | for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null) | |
421 | do | |
422 | check_block_and_slaves $1 $(get_maj_min $_pv) && return 0 | |
423 | done | |
424 | fi | |
425 | fi | |
426 | done | |
427 | return 1 | |
428 | } | |
429 | ||
430 | if [[ $DRACUT_INSTALL ]]; then | |
431 | [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1 | |
432 | inst_dir() { | |
433 | [[ -e ${initdir}/"$1" ]] && return 0 # already there | |
434 | $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" | |
435 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || : | |
436 | } | |
437 | ||
438 | inst() { | |
439 | [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there | |
440 | #dinfo "$DRACUT_INSTALL -l $@" | |
441 | $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
442 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
443 | } | |
444 | ||
445 | inst_simple() { | |
446 | [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there | |
447 | [[ -e $1 ]] || return 1 # no source | |
448 | $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" | |
449 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || : | |
450 | } | |
451 | ||
452 | inst_symlink() { | |
453 | [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there | |
454 | [[ -L $1 ]] || return 1 | |
455 | $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
456 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
457 | } | |
458 | ||
459 | dracut_install() { | |
460 | #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@" | |
461 | $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
462 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
463 | } | |
464 | ||
465 | inst_library() { | |
466 | [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there | |
467 | [[ -e $1 ]] || return 1 # no source | |
468 | $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
469 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
470 | } | |
471 | ||
472 | inst_binary() { | |
473 | $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
474 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
475 | } | |
476 | ||
477 | inst_script() { | |
478 | $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" | |
479 | (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || : | |
480 | } | |
481 | ||
482 | else | |
483 | ||
484 | # Install a directory, keeping symlinks as on the original system. | |
485 | # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file" | |
486 | # will create ${initdir}/lib64, ${initdir}/lib64/file, | |
487 | # and a symlink ${initdir}/lib -> lib64. | |
488 | inst_dir() { | |
489 | [[ -e ${initdir}/"$1" ]] && return 0 # already there | |
490 | ||
491 | local _dir="$1" _part="${1%/*}" _file | |
492 | while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do | |
493 | _dir="$_part $_dir" | |
494 | _part=${_part%/*} | |
495 | done | |
496 | ||
497 | # iterate over parent directories | |
498 | for _file in $_dir; do | |
499 | [[ -e "${initdir}/$_file" ]] && continue | |
500 | if [[ -L $_file ]]; then | |
501 | inst_symlink "$_file" | |
502 | else | |
503 | # create directory | |
504 | mkdir -m 0755 -p "${initdir}/$_file" || return 1 | |
505 | [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file" | |
506 | chmod u+w "${initdir}/$_file" | |
507 | fi | |
508 | done | |
509 | } | |
510 | ||
511 | # $1 = file to copy to ramdisk | |
512 | # $2 (optional) Name for the file on the ramdisk | |
513 | # Location of the image dir is assumed to be $initdir | |
514 | # We never overwrite the target if it exists. | |
515 | inst_simple() { | |
516 | [[ -f "$1" ]] || return 1 | |
517 | strstr "$1" "/" || return 1 | |
518 | local _src=$1 _target="${2:-$1}" | |
519 | ||
520 | [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; } | |
521 | ||
522 | if ! [[ -d ${initdir}/$_target ]]; then | |
523 | [[ -e ${initdir}/$_target ]] && return 0 | |
524 | [[ -L ${initdir}/$_target ]] && return 0 | |
525 | [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}" | |
526 | fi | |
527 | if [[ $DRACUT_FIPS_MODE ]]; then | |
528 | # install checksum files also | |
529 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then | |
530 | inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac" | |
531 | fi | |
532 | fi | |
533 | ddebug "Installing $_src" | |
534 | cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target" | |
535 | } | |
536 | ||
537 | # same as above, but specialized for symlinks | |
538 | inst_symlink() { | |
539 | local _src=$1 _target=${2:-$1} _realsrc | |
540 | strstr "$1" "/" || return 1 | |
541 | [[ -L $1 ]] || return 1 | |
542 | [[ -L $initdir/$_target ]] && return 0 | |
543 | _realsrc=$(readlink -f "$_src") | |
544 | if ! [[ -e $initdir/$_realsrc ]]; then | |
545 | if [[ -d $_realsrc ]]; then | |
546 | inst_dir "$_realsrc" | |
547 | else | |
548 | inst "$_realsrc" | |
549 | fi | |
550 | fi | |
551 | [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}" | |
552 | ||
553 | ln_r "${_realsrc}" "${_target}" | |
554 | } | |
555 | ||
556 | # Same as above, but specialized to handle dynamic libraries. | |
557 | # It handles making symlinks according to how the original library | |
558 | # is referenced. | |
559 | inst_library() { | |
560 | local _src="$1" _dest=${2:-$1} _lib _reallib _symlink | |
561 | strstr "$1" "/" || return 1 | |
562 | [[ -e $initdir/$_dest ]] && return 0 | |
563 | if [[ -L $_src ]]; then | |
564 | if [[ $DRACUT_FIPS_MODE ]]; then | |
565 | # install checksum files also | |
566 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then | |
567 | inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac" | |
568 | fi | |
569 | fi | |
570 | _reallib=$(readlink -f "$_src") | |
571 | inst_simple "$_reallib" "$_reallib" | |
572 | inst_dir "${_dest%/*}" | |
573 | ln_r "${_reallib}" "${_dest}" | |
574 | else | |
575 | inst_simple "$_src" "$_dest" | |
576 | fi | |
577 | ||
578 | # Create additional symlinks. See rev_symlinks description. | |
579 | for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do | |
580 | [[ ! -e $initdir/$_symlink ]] && { | |
581 | ddebug "Creating extra symlink: $_symlink" | |
582 | inst_symlink $_symlink | |
583 | } | |
584 | done | |
585 | } | |
586 | ||
587 | # Same as above, but specialized to install binary executables. | |
588 | # Install binary executable, and all shared library dependencies, if any. | |
589 | inst_binary() { | |
590 | local _bin _target | |
591 | _bin=$(find_binary "$1") || return 1 | |
592 | _target=${2:-$_bin} | |
593 | [[ -e $initdir/$_target ]] && return 0 | |
594 | local _file _line | |
595 | local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' | |
596 | # I love bash! | |
597 | LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do | |
598 | [[ $_line = 'not a dynamic executable' ]] && break | |
599 | ||
600 | if [[ $_line =~ $_so_regex ]]; then | |
601 | _file=${BASH_REMATCH[1]} | |
602 | [[ -e ${initdir}/$_file ]] && continue | |
603 | inst_library "$_file" | |
604 | continue | |
605 | fi | |
606 | ||
607 | if [[ $_line =~ not\ found ]]; then | |
608 | dfatal "Missing a shared library required by $_bin." | |
609 | dfatal "Run \"ldd $_bin\" to find out what it is." | |
610 | dfatal "$_line" | |
611 | dfatal "dracut cannot create an initrd." | |
612 | exit 1 | |
613 | fi | |
614 | done | |
615 | inst_simple "$_bin" "$_target" | |
616 | } | |
617 | ||
618 | # same as above, except for shell scripts. | |
619 | # If your shell script does not start with shebang, it is not a shell script. | |
620 | inst_script() { | |
621 | local _bin | |
622 | _bin=$(find_binary "$1") || return 1 | |
623 | shift | |
624 | local _line _shebang_regex | |
625 | read -r -n 80 _line <"$_bin" | |
626 | # If debug is set, clean unprintable chars to prevent messing up the term | |
627 | [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]') | |
628 | _shebang_regex='(#! *)(/[^ ]+).*' | |
629 | [[ $_line =~ $_shebang_regex ]] || return 1 | |
630 | inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@" | |
631 | } | |
632 | ||
633 | # general purpose installation function | |
634 | # Same args as above. | |
635 | inst() { | |
636 | local _x | |
637 | ||
638 | case $# in | |
639 | 1) ;; | |
640 | 2) [[ ! $initdir && -d $2 ]] && export initdir=$2 | |
641 | [[ $initdir = $2 ]] && set $1;; | |
642 | 3) [[ -z $initdir ]] && export initdir=$2 | |
643 | set $1 $3;; | |
644 | *) dfatal "inst only takes 1 or 2 or 3 arguments" | |
645 | exit 1;; | |
646 | esac | |
647 | for _x in inst_symlink inst_script inst_binary inst_simple; do | |
648 | $_x "$@" && return 0 | |
649 | done | |
650 | return 1 | |
651 | } | |
652 | ||
653 | # dracut_install [-o ] <file> [<file> ... ] | |
654 | # Install <file> to the initramfs image | |
655 | # -o optionally install the <file> and don't fail, if it is not there | |
656 | dracut_install() { | |
657 | local _optional=no | |
658 | if [[ $1 = '-o' ]]; then | |
659 | _optional=yes | |
660 | shift | |
661 | fi | |
662 | while (($# > 0)); do | |
663 | if ! inst "$1" ; then | |
664 | if [[ $_optional = yes ]]; then | |
665 | dinfo "Skipping program $1 as it cannot be found and is" \ | |
666 | "flagged to be optional" | |
667 | else | |
668 | dfatal "Failed to install $1" | |
669 | exit 1 | |
670 | fi | |
671 | fi | |
672 | shift | |
673 | done | |
674 | } | |
675 | ||
676 | fi | |
677 | ||
678 | # find symlinks linked to given library file | |
679 | # $1 = library file | |
680 | # Function searches for symlinks by stripping version numbers appended to | |
681 | # library filename, checks if it points to the same target and finally | |
682 | # prints the list of symlinks to stdout. | |
683 | # | |
684 | # Example: | |
685 | # rev_lib_symlinks libfoo.so.8.1 | |
686 | # output: libfoo.so.8 libfoo.so | |
687 | # (Only if libfoo.so.8 and libfoo.so exists on host system.) | |
688 | rev_lib_symlinks() { | |
689 | [[ ! $1 ]] && return 0 | |
690 | ||
691 | local fn="$1" orig="$(readlink -f "$1")" links='' | |
692 | ||
693 | [[ ${fn} =~ .*\.so\..* ]] || return 1 | |
694 | ||
695 | until [[ ${fn##*.} == so ]]; do | |
696 | fn="${fn%.*}" | |
697 | [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}" | |
698 | done | |
699 | ||
700 | echo "${links}" | |
701 | } | |
702 | ||
703 | # attempt to install any programs specified in a udev rule | |
704 | inst_rule_programs() { | |
705 | local _prog _bin | |
706 | ||
707 | if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then | |
708 | for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do | |
709 | if [ -x ${udevdir}/$_prog ]; then | |
710 | _bin=${udevdir}/$_prog | |
711 | else | |
712 | _bin=$(find_binary "$_prog") || { | |
713 | dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" | |
714 | continue; | |
715 | } | |
716 | fi | |
717 | ||
718 | #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" | |
719 | dracut_install "$_bin" | |
720 | done | |
721 | fi | |
722 | if grep -qE 'RUN==?"[^ "]+' "$1"; then | |
723 | for _prog in $(grep -E 'RUN==?"[^ "]+' "$1" | sed -r 's/.*RUN==?"([^ "]+).*/\1/'); do | |
724 | if [ -x ${udevdir}/$_prog ]; then | |
725 | _bin=${udevdir}/$_prog | |
726 | else | |
727 | _bin=$(find_binary "$_prog") || { | |
728 | dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" | |
729 | continue; | |
730 | } | |
731 | fi | |
732 | ||
733 | #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" | |
734 | dracut_install "$_bin" | |
735 | done | |
736 | fi | |
737 | if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then | |
738 | for _prog in $(grep -E 'IMPORT==?"[^ "]+' "$1" | sed -r 's/.*IMPORT==?"([^ "]+).*/\1/'); do | |
739 | if [ -x ${udevdir}/$_prog ]; then | |
740 | _bin=${udevdir}/$_prog | |
741 | else | |
742 | _bin=$(find_binary "$_prog") || { | |
743 | dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" | |
744 | continue; | |
745 | } | |
746 | fi | |
747 | ||
748 | #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" | |
749 | dracut_install "$_bin" | |
750 | done | |
751 | fi | |
752 | } | |
753 | ||
754 | # udev rules always get installed in the same place, so | |
755 | # create a function to install them to make life simpler. | |
756 | inst_rules() { | |
757 | local _target=/etc/udev/rules.d _rule _found | |
758 | ||
759 | inst_dir "${udevdir}/rules.d" | |
760 | inst_dir "$_target" | |
761 | for _rule in "$@"; do | |
762 | if [ "${rule#/}" = "$rule" ]; then | |
763 | for r in ${udevdir}/rules.d /etc/udev/rules.d; do | |
764 | if [[ -f $r/$_rule ]]; then | |
765 | _found="$r/$_rule" | |
766 | inst_rule_programs "$_found" | |
767 | inst_simple "$_found" | |
768 | fi | |
769 | done | |
770 | fi | |
771 | for r in '' ./ $dracutbasedir/rules.d/; do | |
772 | if [[ -f ${r}$_rule ]]; then | |
773 | _found="${r}$_rule" | |
774 | inst_rule_programs "$_found" | |
775 | inst_simple "$_found" "$_target/${_found##*/}" | |
776 | fi | |
777 | done | |
778 | [[ $_found ]] || dinfo "Skipping udev rule: $_rule" | |
779 | done | |
780 | } | |
781 | ||
782 | # install function specialized for hooks | |
783 | # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook | |
784 | # All hooks should be POSIX/SuS compliant, they will be sourced by init. | |
785 | inst_hook() { | |
786 | if ! [[ -f $3 ]]; then | |
787 | dfatal "Cannot install a hook ($3) that does not exist." | |
788 | dfatal "Aborting initrd creation." | |
789 | exit 1 | |
790 | elif ! strstr "$hookdirs" "$1"; then | |
791 | dfatal "No such hook type $1. Aborting initrd creation." | |
792 | exit 1 | |
793 | fi | |
794 | inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}" | |
795 | } | |
796 | ||
797 | # install any of listed files | |
798 | # | |
799 | # If first argument is '-d' and second some destination path, first accessible | |
800 | # source is installed into this path, otherwise it will installed in the same | |
801 | # path as source. If none of listed files was installed, function return 1. | |
802 | # On first successful installation it returns with 0 status. | |
803 | # | |
804 | # Example: | |
805 | # | |
806 | # inst_any -d /bin/foo /bin/bar /bin/baz | |
807 | # | |
808 | # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in | |
809 | # initramfs. | |
810 | inst_any() { | |
811 | local to f | |
812 | ||
813 | [[ $1 = '-d' ]] && to="$2" && shift 2 | |
814 | ||
815 | for f in "$@"; do | |
816 | if [[ -e $f ]]; then | |
817 | [[ $to ]] && inst "$f" "$to" && return 0 | |
818 | inst "$f" && return 0 | |
819 | fi | |
820 | done | |
821 | ||
822 | return 1 | |
823 | } | |
824 | ||
825 | ||
826 | # inst_libdir_file [-n <pattern>] <file> [<file>...] | |
827 | # Install a <file> located on a lib directory to the initramfs image | |
828 | # -n <pattern> install matching files | |
829 | inst_libdir_file() { | |
830 | local _files | |
831 | if [[ "$1" == "-n" ]]; then | |
832 | local _pattern=$2 | |
833 | shift 2 | |
834 | for _dir in $libdirs; do | |
835 | for _i in "$@"; do | |
836 | for _f in "$_dir"/$_i; do | |
837 | [[ "$_f" =~ $_pattern ]] || continue | |
838 | [[ -e "$_f" ]] && _files+="$_f " | |
839 | done | |
840 | done | |
841 | done | |
842 | else | |
843 | for _dir in $libdirs; do | |
844 | for _i in "$@"; do | |
845 | for _f in "$_dir"/$_i; do | |
846 | [[ -e "$_f" ]] && _files+="$_f " | |
847 | done | |
848 | done | |
849 | done | |
850 | fi | |
851 | [[ $_files ]] && dracut_install $_files | |
852 | } | |
853 | ||
854 | ||
855 | # install function decompressing the target and handling symlinks | |
856 | # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files | |
857 | # | |
858 | # Function install targets in the same paths inside overlay but decompressed | |
859 | # and without extensions (.gz, .bz2). | |
860 | inst_decompress() { | |
861 | local _src _cmd | |
862 | ||
863 | for _src in $@ | |
864 | do | |
865 | case ${_src} in | |
866 | *.gz) _cmd='gzip -d' ;; | |
867 | *.bz2) _cmd='bzip2 -d' ;; | |
868 | *) return 1 ;; | |
869 | esac | |
870 | inst_simple ${_src} | |
871 | # Decompress with chosen tool. We assume that tool changes name e.g. | |
872 | # from 'name.gz' to 'name'. | |
873 | ${_cmd} "${initdir}${_src}" | |
874 | done | |
875 | } | |
876 | ||
877 | # It's similar to above, but if file is not compressed, performs standard | |
878 | # install. | |
879 | # $@ = list of files | |
880 | inst_opt_decompress() { | |
881 | local _src | |
882 | ||
883 | for _src in $@ | |
884 | do | |
885 | inst_decompress "${_src}" || inst "${_src}" | |
886 | done | |
887 | } | |
888 | ||
889 | # module_check <dracut module> | |
890 | # execute the check() function of module-setup.sh of <dracut module> | |
891 | # or the "check" script, if module-setup.sh is not found | |
892 | # "check $hostonly" is called | |
893 | module_check() { | |
894 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
895 | local _ret | |
896 | local _forced=0 | |
897 | local _hostonly=$hostonly | |
898 | [ $# -eq 2 ] && _forced=$2 | |
899 | [[ -d $_moddir ]] || return 1 | |
900 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
901 | # if we do not have a check script, we are unconditionally included | |
902 | [[ -x $_moddir/check ]] || return 0 | |
903 | [ $_forced -ne 0 ] && unset hostonly | |
904 | $_moddir/check $hostonly | |
905 | _ret=$? | |
906 | else | |
907 | unset check depends install installkernel | |
908 | . $_moddir/module-setup.sh | |
909 | is_func check || return 0 | |
910 | [ $_forced -ne 0 ] && unset hostonly | |
911 | check $hostonly | |
912 | _ret=$? | |
913 | unset check depends install installkernel | |
914 | fi | |
915 | hostonly=$_hostonly | |
916 | return $_ret | |
917 | } | |
918 | ||
919 | # module_check_mount <dracut module> | |
920 | # execute the check() function of module-setup.sh of <dracut module> | |
921 | # or the "check" script, if module-setup.sh is not found | |
922 | # "mount_needs=1 check 0" is called | |
923 | module_check_mount() { | |
924 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
925 | local _ret | |
926 | mount_needs=1 | |
927 | [[ -d $_moddir ]] || return 1 | |
928 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
929 | # if we do not have a check script, we are unconditionally included | |
930 | [[ -x $_moddir/check ]] || return 0 | |
931 | mount_needs=1 $_moddir/check 0 | |
932 | _ret=$? | |
933 | else | |
934 | unset check depends install installkernel | |
935 | . $_moddir/module-setup.sh | |
936 | is_func check || return 1 | |
937 | check 0 | |
938 | _ret=$? | |
939 | unset check depends install installkernel | |
940 | fi | |
941 | unset mount_needs | |
942 | return $_ret | |
943 | } | |
944 | ||
945 | # module_depends <dracut module> | |
946 | # execute the depends() function of module-setup.sh of <dracut module> | |
947 | # or the "depends" script, if module-setup.sh is not found | |
948 | module_depends() { | |
949 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
950 | local _ret | |
951 | [[ -d $_moddir ]] || return 1 | |
952 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
953 | # if we do not have a check script, we have no deps | |
954 | [[ -x $_moddir/check ]] || return 0 | |
955 | $_moddir/check -d | |
956 | return $? | |
957 | else | |
958 | unset check depends install installkernel | |
959 | . $_moddir/module-setup.sh | |
960 | is_func depends || return 0 | |
961 | depends | |
962 | _ret=$? | |
963 | unset check depends install installkernel | |
964 | return $_ret | |
965 | fi | |
966 | } | |
967 | ||
968 | # module_install <dracut module> | |
969 | # execute the install() function of module-setup.sh of <dracut module> | |
970 | # or the "install" script, if module-setup.sh is not found | |
971 | module_install() { | |
972 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
973 | local _ret | |
974 | [[ -d $_moddir ]] || return 1 | |
975 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
976 | [[ -x $_moddir/install ]] && . "$_moddir/install" | |
977 | return $? | |
978 | else | |
979 | unset check depends install installkernel | |
980 | . $_moddir/module-setup.sh | |
981 | is_func install || return 0 | |
982 | install | |
983 | _ret=$? | |
984 | unset check depends install installkernel | |
985 | return $_ret | |
986 | fi | |
987 | } | |
988 | ||
989 | # module_installkernel <dracut module> | |
990 | # execute the installkernel() function of module-setup.sh of <dracut module> | |
991 | # or the "installkernel" script, if module-setup.sh is not found | |
992 | module_installkernel() { | |
993 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
994 | local _ret | |
995 | [[ -d $_moddir ]] || return 1 | |
996 | if [[ ! -f $_moddir/module-setup.sh ]]; then | |
997 | [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel" | |
998 | return $? | |
999 | else | |
1000 | unset check depends install installkernel | |
1001 | . $_moddir/module-setup.sh | |
1002 | is_func installkernel || return 0 | |
1003 | installkernel | |
1004 | _ret=$? | |
1005 | unset check depends install installkernel | |
1006 | return $_ret | |
1007 | fi | |
1008 | } | |
1009 | ||
1010 | # check_mount <dracut module> | |
1011 | # check_mount checks, if a dracut module is needed for the given | |
1012 | # device and filesystem types in "${host_fs_types[@]}" | |
1013 | check_mount() { | |
1014 | local _mod=$1 | |
1015 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
1016 | local _ret | |
1017 | local _moddep | |
1018 | # If we are already scheduled to be loaded, no need to check again. | |
1019 | strstr " $mods_to_load " " $_mod " && return 0 | |
1020 | strstr " $mods_checked_as_dep " " $_mod " && return 1 | |
1021 | ||
1022 | # This should never happen, but... | |
1023 | [[ -d $_moddir ]] || return 1 | |
1024 | ||
1025 | [[ $2 ]] || mods_checked_as_dep+=" $_mod " | |
1026 | ||
1027 | if strstr " $omit_dracutmodules " " $_mod "; then | |
1028 | dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!" | |
1029 | return 1 | |
1030 | fi | |
1031 | ||
1032 | if [ "${#host_fs_types[*]}" -gt 0 ]; then | |
1033 | module_check_mount $_mod || return 1 | |
1034 | else | |
1035 | # skip this module | |
1036 | return 1 | |
1037 | fi | |
1038 | ||
1039 | for _moddep in $(module_depends $_mod); do | |
1040 | # handle deps as if they were manually added | |
1041 | strstr " $add_dracutmodules " " $_moddep " || \ | |
1042 | add_dracutmodules+=" $_moddep " | |
1043 | strstr " $force_add_dracutmodules " " $_moddep " || \ | |
1044 | force_add_dracutmodules+=" $_moddep " | |
1045 | # if a module we depend on fail, fail also | |
1046 | if ! check_module $_moddep; then | |
1047 | derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed" | |
1048 | return 1 | |
1049 | fi | |
1050 | done | |
1051 | ||
1052 | strstr " $mods_to_load " " $_mod " || \ | |
1053 | mods_to_load+=" $_mod " | |
1054 | ||
1055 | return 0 | |
1056 | } | |
1057 | ||
1058 | # check_module <dracut module> [<use_as_dep>] | |
1059 | # check if a dracut module is to be used in the initramfs process | |
1060 | # if <use_as_dep> is set, then the process also keeps track | |
1061 | # that the modules were checked for the dependency tracking process | |
1062 | check_module() { | |
1063 | local _mod=$1 | |
1064 | local _moddir=$(echo ${dracutbasedir}/modules.d/??${1}) | |
1065 | local _ret | |
1066 | local _moddep | |
1067 | # If we are already scheduled to be loaded, no need to check again. | |
1068 | strstr " $mods_to_load " " $_mod " && return 0 | |
1069 | strstr " $mods_checked_as_dep " " $_mod " && return 1 | |
1070 | ||
1071 | # This should never happen, but... | |
1072 | [[ -d $_moddir ]] || return 1 | |
1073 | ||
1074 | [[ $2 ]] || mods_checked_as_dep+=" $_mod " | |
1075 | ||
1076 | if strstr " $omit_dracutmodules " " $_mod "; then | |
1077 | dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!" | |
1078 | return 1 | |
1079 | fi | |
1080 | ||
1081 | if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then | |
1082 | if strstr " $force_add_dracutmodules" " $_mod"; then | |
1083 | module_check $_mod 1; ret=$? | |
1084 | else | |
1085 | module_check $_mod 0; ret=$? | |
1086 | fi | |
1087 | # explicit module, so also accept ret=255 | |
1088 | [[ $ret = 0 || $ret = 255 ]] || return 1 | |
1089 | else | |
1090 | # module not in our list | |
1091 | if [[ $dracutmodules = all ]]; then | |
1092 | # check, if we can and should install this module | |
1093 | module_check $_mod || return 1 | |
1094 | else | |
1095 | # skip this module | |
1096 | return 1 | |
1097 | fi | |
1098 | fi | |
1099 | ||
1100 | for _moddep in $(module_depends $_mod); do | |
1101 | # handle deps as if they were manually added | |
1102 | strstr " $add_dracutmodules " " $_moddep " || \ | |
1103 | add_dracutmodules+=" $_moddep " | |
1104 | strstr " $force_add_dracutmodules " " $_moddep " || \ | |
1105 | force_add_dracutmodules+=" $_moddep " | |
1106 | # if a module we depend on fail, fail also | |
1107 | if ! check_module $_moddep; then | |
1108 | derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed" | |
1109 | return 1 | |
1110 | fi | |
1111 | done | |
1112 | ||
1113 | strstr " $mods_to_load " " $_mod " || \ | |
1114 | mods_to_load+=" $_mod " | |
1115 | ||
1116 | return 0 | |
1117 | } | |
1118 | ||
1119 | # for_each_module_dir <func> | |
1120 | # execute "<func> <dracut module> 1" | |
1121 | for_each_module_dir() { | |
1122 | local _modcheck | |
1123 | local _mod | |
1124 | local _moddir | |
1125 | local _func | |
1126 | _func=$1 | |
1127 | for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do | |
1128 | _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]} | |
1129 | $_func $_mod 1 | |
1130 | done | |
1131 | ||
1132 | # Report any missing dracut modules, the user has specified | |
1133 | _modcheck="$add_dracutmodules $force_add_dracutmodules" | |
1134 | [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules" | |
1135 | for _mod in $_modcheck; do | |
1136 | strstr "$mods_to_load" "$_mod" && continue | |
1137 | strstr "$omit_dracutmodules" "$_mod" && continue | |
1138 | derror "Dracut module \"$_mod\" cannot be found or installed." | |
1139 | done | |
1140 | } | |
1141 | ||
1142 | # Install a single kernel module along with any firmware it may require. | |
1143 | # $1 = full path to kernel module to install | |
1144 | install_kmod_with_fw() { | |
1145 | # no need to go further if the module is already installed | |
1146 | ||
1147 | [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \ | |
1148 | && return 0 | |
1149 | ||
1150 | if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then | |
1151 | read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" | |
1152 | return $ret | |
1153 | fi | |
1154 | ||
1155 | if [[ $omit_drivers ]]; then | |
1156 | local _kmod=${1##*/} | |
1157 | _kmod=${_kmod%.ko} | |
1158 | _kmod=${_kmod/-/_} | |
1159 | if [[ "$_kmod" =~ $omit_drivers ]]; then | |
1160 | dinfo "Omitting driver $_kmod" | |
1161 | return 0 | |
1162 | fi | |
1163 | if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then | |
1164 | dinfo "Omitting driver $_kmod" | |
1165 | return 0 | |
1166 | fi | |
1167 | fi | |
1168 | ||
1169 | inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" | |
1170 | ret=$? | |
1171 | [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \ | |
1172 | [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \ | |
1173 | echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" | |
1174 | (($ret != 0)) && return $ret | |
1175 | ||
1176 | local _modname=${1##*/} _fwdir _found _fw | |
1177 | _modname=${_modname%.ko*} | |
1178 | for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do | |
1179 | _found='' | |
1180 | for _fwdir in $fw_dir; do | |
1181 | if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then | |
1182 | inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw" | |
1183 | _found=yes | |
1184 | fi | |
1185 | done | |
1186 | if [[ $_found != yes ]]; then | |
1187 | if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then | |
1188 | dinfo "Possible missing firmware \"${_fw}\" for kernel module" \ | |
1189 | "\"${_modname}.ko\"" | |
1190 | else | |
1191 | dwarn "Possible missing firmware \"${_fw}\" for kernel module" \ | |
1192 | "\"${_modname}.ko\"" | |
1193 | fi | |
1194 | fi | |
1195 | done | |
1196 | return 0 | |
1197 | } | |
1198 | ||
1199 | # Do something with all the dependencies of a kernel module. | |
1200 | # Note that kernel modules depend on themselves using the technique we use | |
1201 | # $1 = function to call for each dependency we find | |
1202 | # It will be passed the full path to the found kernel module | |
1203 | # $2 = module to get dependencies for | |
1204 | # rest of args = arguments to modprobe | |
1205 | # _fderr specifies FD passed from surrounding scope | |
1206 | for_each_kmod_dep() { | |
1207 | local _func=$1 _kmod=$2 _cmd _modpath _options | |
1208 | shift 2 | |
1209 | modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | ( | |
1210 | while read _cmd _modpath _options; do | |
1211 | [[ $_cmd = insmod ]] || continue | |
1212 | $_func ${_modpath} || exit $? | |
1213 | done | |
1214 | ) | |
1215 | } | |
1216 | ||
1217 | dracut_kernel_post() { | |
1218 | local _moddirname=${srcmods%%/lib/modules/*} | |
1219 | ||
1220 | if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then | |
1221 | xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \ | |
1222 | --ignore-install --show-depends --set-version $kernel \ | |
1223 | < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \ | |
1224 | | sort -u \ | |
1225 | | while read _cmd _modpath _options; do | |
1226 | [[ $_cmd = insmod ]] || continue | |
1227 | echo "$_modpath" | |
1228 | done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" | |
1229 | ||
1230 | ( | |
1231 | if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then | |
1232 | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" | |
1233 | else | |
1234 | while read _modpath; do | |
1235 | local _destpath=$_modpath | |
1236 | [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/} | |
1237 | _destpath=${_destpath##*/lib/modules/$kernel/} | |
1238 | inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $? | |
1239 | done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" | |
1240 | fi | |
1241 | ) & | |
1242 | ||
1243 | if [[ $DRACUT_INSTALL ]]; then | |
1244 | xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \ | |
1245 | | while read line; do | |
1246 | for _fwdir in $fw_dir; do | |
1247 | echo $_fwdir/$line; | |
1248 | done; | |
1249 | done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o | |
1250 | else | |
1251 | for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do | |
1252 | for _fwdir in $fw_dir; do | |
1253 | if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then | |
1254 | inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw" | |
1255 | break | |
1256 | fi | |
1257 | done | |
1258 | done | |
1259 | fi | |
1260 | ||
1261 | wait | |
1262 | fi | |
1263 | ||
1264 | for _f in modules.builtin.bin modules.builtin; do | |
1265 | [[ $srcmods/$_f ]] && break | |
1266 | done || { | |
1267 | dfatal "No modules.builtin.bin and modules.builtin found!" | |
1268 | return 1 | |
1269 | } | |
1270 | ||
1271 | for _f in modules.builtin.bin modules.builtin modules.order; do | |
1272 | [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f" | |
1273 | done | |
1274 | ||
1275 | # generate module dependencies for the initrd | |
1276 | if [[ -d $initdir/lib/modules/$kernel ]] && \ | |
1277 | ! depmod -a -b "$initdir" $kernel; then | |
1278 | dfatal "\"depmod -a $kernel\" failed." | |
1279 | exit 1 | |
1280 | fi | |
1281 | ||
1282 | [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr "$DRACUT_KERNEL_LAZY_HASHDIR" | |
1283 | } | |
1284 | ||
1285 | find_kernel_modules_by_path () ( | |
1286 | local _OLDIFS | |
1287 | if ! [[ $hostonly ]]; then | |
1288 | _OLDIFS=$IFS | |
1289 | IFS=: | |
1290 | while read a rest; do | |
1291 | [[ $a = kernel*/$1/* ]] || continue | |
1292 | echo $srcmods/$a | |
1293 | done < $srcmods/modules.dep | |
1294 | IFS=$_OLDIFS | |
1295 | else | |
1296 | ( cd /sys/module; echo *; ) \ | |
1297 | | xargs -r modinfo -F filename -k $kernel 2>/dev/null \ | |
1298 | | while read a; do | |
1299 | [[ $a = kernel*/$1/* ]] || continue | |
1300 | echo $srcmods/$a | |
1301 | done | |
1302 | fi | |
1303 | return 0 | |
1304 | ) | |
1305 | ||
1306 | find_kernel_modules () { | |
1307 | find_kernel_modules_by_path drivers | |
1308 | } | |
1309 | ||
1310 | # instmods [-c [-s]] <kernel module> [<kernel module> ... ] | |
1311 | # instmods [-c [-s]] <kernel subsystem> | |
1312 | # install kernel modules along with all their dependencies. | |
1313 | # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage" | |
1314 | instmods() { | |
1315 | [[ $no_kernel = yes ]] && return | |
1316 | # called [sub]functions inherit _fderr | |
1317 | local _fderr=9 | |
1318 | local _check=no | |
1319 | local _silent=no | |
1320 | if [[ $1 = '-c' ]]; then | |
1321 | _check=yes | |
1322 | shift | |
1323 | fi | |
1324 | ||
1325 | if [[ $1 = '-s' ]]; then | |
1326 | _silent=yes | |
1327 | shift | |
1328 | fi | |
1329 | ||
1330 | function inst1mod() { | |
1331 | local _ret=0 _mod="$1" | |
1332 | case $_mod in | |
1333 | =*) | |
1334 | ( [[ "$_mpargs" ]] && echo $_mpargs | |
1335 | find_kernel_modules_by_path "${_mod#=}" ) \ | |
1336 | | instmods | |
1337 | ((_ret+=$?)) | |
1338 | ;; | |
1339 | --*) _mpargs+=" $_mod" ;; | |
1340 | i2o_scsi) return 0;; # Do not load this diagnostic-only module | |
1341 | *) | |
1342 | _mod=${_mod##*/} | |
1343 | # if we are already installed, skip this module and go on | |
1344 | # to the next one. | |
1345 | if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \ | |
1346 | [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then | |
1347 | read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" | |
1348 | return $_ret | |
1349 | fi | |
1350 | ||
1351 | if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then | |
1352 | dinfo "Omitting driver ${_mod##$srcmods}" | |
1353 | return 0 | |
1354 | fi | |
1355 | # If we are building a host-specific initramfs and this | |
1356 | # module is not already loaded, move on to the next one. | |
1357 | [[ $hostonly ]] \ | |
1358 | && ! [[ -d $(echo /sys/module/${_mod//-/_}|{ read a b; echo $a; }) ]] \ | |
1359 | && ! [[ "$add_drivers" =~ " ${_mod} " ]] \ | |
1360 | && return 0 | |
1361 | ||
1362 | if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then | |
1363 | # We use '-d' option in modprobe only if modules prefix path | |
1364 | # differs from default '/'. This allows us to use Dracut with | |
1365 | # old version of modprobe which doesn't have '-d' option. | |
1366 | local _moddirname=${srcmods%%/lib/modules/*} | |
1367 | [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/" | |
1368 | ||
1369 | # ok, load the module, all its dependencies, and any firmware | |
1370 | # it may require | |
1371 | for_each_kmod_dep install_kmod_with_fw $_mod \ | |
1372 | --set-version $kernel ${_moddirname} $_mpargs | |
1373 | ((_ret+=$?)) | |
1374 | else | |
1375 | [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \ | |
1376 | echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" | |
1377 | fi | |
1378 | ;; | |
1379 | esac | |
1380 | return $_ret | |
1381 | } | |
1382 | ||
1383 | function instmods_1() { | |
1384 | local _mod _mpargs | |
1385 | if (($# == 0)); then # filenames from stdin | |
1386 | while read _mod; do | |
1387 | inst1mod "${_mod%.ko*}" || { | |
1388 | if [[ "$_check" == "yes" ]]; then | |
1389 | [[ "$_silent" == "no" ]] && dfatal "Failed to install $_mod" | |
1390 | return 1 | |
1391 | fi | |
1392 | } | |
1393 | done | |
1394 | fi | |
1395 | while (($# > 0)); do # filenames as arguments | |
1396 | inst1mod ${1%.ko*} || { | |
1397 | if [[ "$_check" == "yes" ]]; then | |
1398 | [[ "$_silent" == "no" ]] && dfatal "Failed to install $1" | |
1399 | return 1 | |
1400 | fi | |
1401 | } | |
1402 | shift | |
1403 | done | |
1404 | return 0 | |
1405 | } | |
1406 | ||
1407 | local _ret _filter_not_found='FATAL: Module .* not found.' | |
1408 | # Capture all stderr from modprobe to _fderr. We could use {var}>... | |
1409 | # redirections, but that would make dracut require bash4 at least. | |
1410 | eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \ | |
1411 | | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror | |
1412 | _ret=$? | |
1413 | return $_ret | |
1414 | } |