2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 PATH=/sbin:/bin:/usr/sbin:/usr/bin
7 KERNEL_VER=${KERNEL_VER-$(uname -r)}
8 KERNEL_MODS="/lib/modules/$KERNEL_VER/"
11 for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do
19 ln -sfn /run "$initdir/var/run"
20 ln -sfn /run/lock "$initdir/var/lock"
25 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
28 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
29 [[ $_line = 'not a dynamic executable' ]] && break
31 if [[ $_line =~ $_so_regex ]]; then
32 _file=${BASH_REMATCH[1]}
33 [[ -e ${initdir}/$_file ]] && continue
38 if [[ $_line =~ not\ found ]]; then
39 dfatal "Missing a shared library required by $_bin."
40 dfatal "Run \"ldd $_bin\" to find out what it is."
42 dfatal "dracut cannot create an initrd."
50 [[ -e $STATEFILE ]] && . $STATEFILE
51 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
52 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
53 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
58 ## @brief Converts numeric logging level to the first letter of level name.
60 # @param lvl Numeric logging level in range from 1 to 6.
61 # @retval 1 if @a lvl is out of range.
62 # @retval 0 if @a lvl is correct.
63 # @result Echoes first letter of level name.
76 ## @brief Internal helper function for _do_dlog()
78 # @param lvl Numeric logging level.
80 # @retval 0 It's always returned, even if logging failed.
82 # @note This function is not supposed to be called manually. Please use
83 # dtrace(), ddebug(), or others instead which wrap this one.
85 # This function calls _do_dlog() either with parameter msg, or if
86 # none is given, it will read standard input and will use every line as
90 # dwarn "This is a warning"
91 # echo "This is a warning" | dwarn
95 [ -z "$LOG_LEVEL" ] && return 0
96 [ $1 -le $LOG_LEVEL ] || return 0
98 local lvlc=$(_lvl2char "$lvl") || return 0
100 if [ $# -ge 1 ]; then
104 echo "$lvlc: " "$line"
109 ## @brief Logs message at TRACE level (6)
111 # @param msg Message.
112 # @retval 0 It's always returned, even if logging failed.
116 [ -n "$debug" ] && set -x || :
119 ## @brief Logs message at DEBUG level (5)
121 # @param msg Message.
122 # @retval 0 It's always returned, even if logging failed.
126 # [ -n "$debug" ] && set -x || :
129 ## @brief Logs message at INFO level (4)
131 # @param msg Message.
132 # @retval 0 It's always returned, even if logging failed.
136 [ -n "$debug" ] && set -x || :
139 ## @brief Logs message at WARN level (3)
141 # @param msg Message.
142 # @retval 0 It's always returned, even if logging failed.
146 [ -n "$debug" ] && set -x || :
149 ## @brief Logs message at ERROR level (2)
151 # @param msg Message.
152 # @retval 0 It's always returned, even if logging failed.
156 # [ -n "$debug" ] && set -x || :
159 ## @brief Logs message at FATAL level (1)
161 # @param msg Message.
162 # @retval 0 It's always returned, even if logging failed.
166 [ -n "$debug" ] && set -x || :
170 # Generic substring function. If $2 is in $1, return 0.
171 strstr() { [ "${1#*$2*}" != "$1" ]; }
173 # normalize_path <path>
174 # Prints the normalized path, where it removes any duplicated
175 # and trailing slashes.
177 # $ normalize_path ///test/test//
181 set -- "${1//+(\/)//}"
186 # convert_abs_rel <from> <to>
187 # Prints the relative path, when creating a symlink to <to> from <from>.
189 # $ convert_abs_rel /usr/bin/test /bin/test-2
191 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
193 local __current __absolute __abssize __cursize __newpath
196 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
198 # corner case #1 - self looping link
199 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
201 # corner case #2 - own dir link
202 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
204 IFS="/" __current=($1)
205 IFS="/" __absolute=($2)
207 __abssize=${#__absolute[@]}
208 __cursize=${#__current[@]}
210 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
213 if (( __level > __abssize || __level > __cursize ))
219 for ((__i = __level; __i < __cursize-1; __i++))
223 __newpath=$__newpath"/"
225 __newpath=$__newpath".."
228 for ((__i = __level; __i < __abssize; __i++))
230 if [[ -n $__newpath ]]
232 __newpath=$__newpath"/"
234 __newpath=$__newpath${__absolute[__i]}
241 # Install a directory, keeping symlinks as on the original system.
242 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
243 # will create ${initdir}/lib64, ${initdir}/lib64/file,
244 # and a symlink ${initdir}/lib -> lib64.
246 [[ -e ${initdir}/"$1" ]] && return 0 # already there
248 local _dir="$1" _part="${1%/*}" _file
249 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
254 # iterate over parent directories
255 for _file in $_dir; do
256 [[ -e "${initdir}/$_file" ]] && continue
257 if [[ -L $_file ]]; then
258 inst_symlink "$_file"
261 mkdir -m 0755 -p "${initdir}/$_file" || return 1
262 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
263 chmod u+w "${initdir}/$_file"
268 # $1 = file to copy to ramdisk
269 # $2 (optional) Name for the file on the ramdisk
270 # Location of the image dir is assumed to be $initdir
271 # We never overwrite the target if it exists.
273 [[ -f "$1" ]] || return 1
274 strstr "$1" "/" || return 1
276 local _src=$1 target="${2:-$1}"
277 if ! [[ -d ${initdir}/$target ]]; then
278 [[ -e ${initdir}/$target ]] && return 0
279 [[ -L ${initdir}/$target ]] && return 0
280 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
282 # install checksum files also
283 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
284 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
286 ddebug "Installing $_src"
287 cp --sparse=always -pfL "$_src" "${initdir}/$target"
290 # find symlinks linked to given library file
292 # Function searches for symlinks by stripping version numbers appended to
293 # library filename, checks if it points to the same target and finally
294 # prints the list of symlinks to stdout.
297 # rev_lib_symlinks libfoo.so.8.1
298 # output: libfoo.so.8 libfoo.so
299 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
301 [[ ! $1 ]] && return 0
303 local fn="$1" orig="$(readlink -f "$1")" links=''
305 [[ ${fn} =~ .*\.so\..* ]] || return 1
307 until [[ ${fn##*.} == so ]]; do
309 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
315 # Same as above, but specialized to handle dynamic libraries.
316 # It handles making symlinks according to how the original library
319 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
320 strstr "$1" "/" || return 1
321 [[ -e $initdir/$_dest ]] && return 0
322 if [[ -L $_src ]]; then
323 # install checksum files also
324 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
325 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
327 _reallib=$(readlink -f "$_src")
328 inst_simple "$_reallib" "$_reallib"
329 inst_dir "${_dest%/*}"
330 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
331 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
333 inst_simple "$_src" "$_dest"
336 # Create additional symlinks. See rev_symlinks description.
337 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
338 [[ ! -e $initdir/$_symlink ]] && {
339 ddebug "Creating extra symlink: $_symlink"
340 inst_symlink $_symlink
345 # find a binary. If we were not passed the full path directly,
346 # search in the usual places to find the binary.
348 if [[ -z ${1##/*} ]]; then
349 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
358 # Same as above, but specialized to install binary executables.
359 # Install binary executable, and all shared library dependencies, if any.
362 _bin=$(find_binary "$1") || return 1
364 [[ -e $initdir/$_target ]] && return 0
365 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
367 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
369 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
370 [[ $_line = 'not a dynamic executable' ]] && break
372 if [[ $_line =~ $_so_regex ]]; then
373 _file=${BASH_REMATCH[1]}
374 [[ -e ${initdir}/$_file ]] && continue
375 inst_library "$_file"
379 if [[ $_line =~ not\ found ]]; then
380 dfatal "Missing a shared library required by $_bin."
381 dfatal "Run \"ldd $_bin\" to find out what it is."
383 dfatal "dracut cannot create an initrd."
387 inst_simple "$_bin" "$_target"
390 # same as above, except for shell scripts.
391 # If your shell script does not start with shebang, it is not a shell script.
394 _bin=$(find_binary "$1") || return 1
396 local _line _shebang_regex
397 read -r -n 80 _line <"$_bin"
398 # If debug is set, clean unprintable chars to prevent messing up the term
399 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
400 _shebang_regex='(#! *)(/[^ ]+).*'
401 [[ $_line =~ $_shebang_regex ]] || return 1
402 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
405 # same as above, but specialized for symlinks
407 local _src=$1 _target=${2:-$1} _realsrc
408 strstr "$1" "/" || return 1
409 [[ -L $1 ]] || return 1
410 [[ -L $initdir/$_target ]] && return 0
411 _realsrc=$(readlink -f "$_src")
412 if ! [[ -e $initdir/$_realsrc ]]; then
413 if [[ -d $_realsrc ]]; then
419 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
420 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
421 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
424 # attempt to install any programs specified in a udev rule
425 inst_rule_programs() {
428 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
429 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
430 if [ -x /lib/udev/$_prog ]; then
431 _bin=/lib/udev/$_prog
433 _bin=$(find_binary "$_prog") || {
434 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
439 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
440 dracut_install "$_bin"
445 # udev rules always get installed in the same place, so
446 # create a function to install them to make life simpler.
448 local _target=/etc/udev/rules.d _rule _found
450 inst_dir "/lib/udev/rules.d"
452 for _rule in "$@"; do
453 if [ "${rule#/}" = "$rule" ]; then
454 for r in /lib/udev/rules.d /etc/udev/rules.d; do
455 if [[ -f $r/$_rule ]]; then
457 inst_simple "$_found"
458 inst_rule_programs "$_found"
462 for r in '' ./ $dracutbasedir/rules.d/; do
463 if [[ -f ${r}$_rule ]]; then
465 inst_simple "$_found" "$_target/${_found##*/}"
466 inst_rule_programs "$_found"
469 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
473 # general purpose installation function
474 # Same args as above.
480 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
481 [[ $initdir = $2 ]] && set $1;;
482 3) [[ -z $initdir ]] && export initdir=$2
484 *) dfatal "inst only takes 1 or 2 or 3 arguments"
487 for _x in inst_symlink inst_script inst_binary inst_simple; do
493 # install any of listed files
495 # If first argument is '-d' and second some destination path, first accessible
496 # source is installed into this path, otherwise it will installed in the same
497 # path as source. If none of listed files was installed, function return 1.
498 # On first successful installation it returns with 0 status.
502 # inst_any -d /bin/foo /bin/bar /bin/baz
504 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
509 [[ $1 = '-d' ]] && to="$2" && shift 2
513 [[ $to ]] && inst "$f" "$to" && return 0
514 inst "$f" && return 0
521 # dracut_install [-o ] <file> [<file> ... ]
522 # Install <file> to the initramfs image
523 # -o optionally install the <file> and don't fail, if it is not there
526 if [[ $1 = '-o' ]]; then
531 if ! inst "$1" ; then
532 if [[ $_optional = yes ]]; then
533 dinfo "Skipping program $1 as it cannot be found and is" \
534 "flagged to be optional"
536 dfatal "Failed to install $1"
544 # Install a single kernel module along with any firmware it may require.
545 # $1 = full path to kernel module to install
546 install_kmod_with_fw() {
547 # no need to go further if the module is already installed
549 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
552 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
554 if [[ $omit_drivers ]]; then
558 if [[ "$_kmod" =~ $omit_drivers ]]; then
559 dinfo "Omitting driver $_kmod"
562 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
563 dinfo "Omitting driver $_kmod"
568 [ -d "$initdir/.kernelmodseen" ] && \
569 > "$initdir/.kernelmodseen/${1##*/}"
571 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
574 local _modname=${1##*/} _fwdir _found _fw
575 _modname=${_modname%.ko*}
576 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
578 for _fwdir in $fw_dir; do
579 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
580 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
584 if [[ $_found != yes ]]; then
585 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
586 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
589 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
597 # Do something with all the dependencies of a kernel module.
598 # Note that kernel modules depend on themselves using the technique we use
599 # $1 = function to call for each dependency we find
600 # It will be passed the full path to the found kernel module
601 # $2 = module to get dependencies for
602 # rest of args = arguments to modprobe
603 # _fderr specifies FD passed from surrounding scope
604 for_each_kmod_dep() {
605 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
607 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
608 while read _cmd _modpath _options; do
609 [[ $_cmd = insmod ]] || continue
610 $_func ${_modpath} || exit $?
613 [[ $_found -eq 0 ]] && exit 1
618 # filter kernel modules to install certain modules that meet specific
620 # $1 = search only in subdirectory of /kernel/$1
621 # $2 = function to call with module name to filter.
622 # This function will be passed the full path to the module to test.
623 # The behavior of this function can vary depending on whether $hostonly is set.
624 # If it is, we will only look at modules that are already in memory.
625 # If it is not, we will look at all kernel modules
626 # This function returns the full filenames of modules that match $1
627 filter_kernel_modules_by_path () (
628 local _modname _filtercmd
629 if ! [[ $hostonly ]]; then
630 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
631 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
632 _filtercmd+=' -o -name "*.ko.xz"'
633 _filtercmd+=' 2>/dev/null'
635 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
636 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
638 for _modname in $(eval $_filtercmd); do
640 *.ko) "$2" "$_modname" && echo "$_modname";;
641 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
642 $2 $initdir/$$.ko && echo "$_modname"
645 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
646 $2 $initdir/$$.ko && echo "$_modname"
652 find_kernel_modules_by_path () (
653 if ! [[ $hostonly ]]; then
654 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
655 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
657 cut -d " " -f 1 </proc/modules \
658 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
662 filter_kernel_modules () {
663 filter_kernel_modules_by_path drivers "$1"
666 find_kernel_modules () {
667 find_kernel_modules_by_path drivers
670 # instmods [-c] <kernel module> [<kernel module> ... ]
671 # instmods [-c] <kernel subsystem>
672 # install kernel modules along with all their dependencies.
673 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
675 [[ $no_kernel = yes ]] && return
676 # called [sub]functions inherit _fderr
679 if [[ $1 = '-c' ]]; then
684 function inst1mod() {
685 local _ret=0 _mod="$1"
688 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
689 ( [[ "$_mpargs" ]] && echo $_mpargs
690 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
693 ( [[ "$_mpargs" ]] && echo $_mpargs
694 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
698 --*) _mpargs+=" $_mod" ;;
699 i2o_scsi) return ;; # Do not load this diagnostic-only module
702 # if we are already installed, skip this module and go on
704 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
706 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
707 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
710 # If we are building a host-specific initramfs and this
711 # module is not already loaded, move on to the next one.
712 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
713 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
716 # We use '-d' option in modprobe only if modules prefix path
717 # differs from default '/'. This allows us to use Dracut with
718 # old version of modprobe which doesn't have '-d' option.
719 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
720 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
722 # ok, load the module, all its dependencies, and any firmware
724 for_each_kmod_dep install_kmod_with_fw $_mod \
725 --set-version $KERNEL_VER ${_moddirname} $_mpargs
732 function instmods_1() {
734 if (($# == 0)); then # filenames from stdin
736 inst1mod "${_mod%.ko*}" || {
737 if [ "$_check" = "yes" ]; then
738 dfatal "Failed to install $_mod"
744 while (($# > 0)); do # filenames as arguments
745 inst1mod ${1%.ko*} || {
746 if [ "$_check" = "yes" ]; then
747 dfatal "Failed to install $1"
756 local _ret _filter_not_found='FATAL: Module .* not found.'
758 # Capture all stderr from modprobe to _fderr. We could use {var}>...
759 # redirections, but that would make dracut require bash4 at least.
760 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
761 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
767 # inst_libdir_file [-n <pattern>] <file> [<file>...]
768 # Install a <file> located on a lib directory to the initramfs image
769 # -n <pattern> install non-matching files
771 if [[ "$1" == "-n" ]]; then
774 for _dir in $libdirs; do
776 for _f in "$_dir"/$_i; do
777 [[ "$_i" =~ $_pattern ]] || continue
778 [[ -e "$_i" ]] && dracut_install "$_i"
783 for _dir in $libdirs; do
785 for _f in "$_dir"/$_i; do
786 [[ -e "$_f" ]] && dracut_install "$_f"
794 command -v qemu-kvm &>/dev/null && [[ -c /dev/kvm ]]
798 [[ -d /sys/fs/cgroup/systemd ]]
803 if [[ $UID != "0" ]]; then
804 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
809 [[ $libdir ]] || for libdir in /lib64 /lib; do
810 [[ -d $libdir ]] && libdirs+=" $libdir" && break
813 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
814 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
822 echo "TEST RUN: $TEST_DESCRIPTION"
825 if [ $ret -eq 0 ]; then
826 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
828 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
832 echo "TEST SETUP: $TEST_DESCRIPTION"
836 echo "TEST CLEANUP: $TEST_DESCRIPTION"
842 echo -n "TEST: $TEST_DESCRIPTION ";
844 test_setup && test_run
850 ) </dev/null >test.log 2>&1
852 if [ $ret -eq 0 ]; then
857 echo "see $(pwd)/test.log"