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