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/"
10 BASICTOOLS="sh bash setsid loadkeys setfont login sushell sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
11 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort"
13 function find_qemu_bin() {
14 # SUSE and Red Hat call the binary qemu-kvm
15 # Debian and Gentoo call it kvm
16 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
18 [ "$ARCH" ] || ARCH=$(uname -m)
21 # QEMU's own build system calls it qemu-system-x86_64
22 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
25 # new i386 version of QEMU
26 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
28 # i386 version of QEMU
29 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
33 if [ ! -e "$QEMU_BIN" ]; then
34 echo "Could not find a suitable QEMU binary" >&2
40 [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
41 [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
42 [ "$QEMU_SMP" ] || QEMU_SMP=1
44 find_qemu_bin || return 1
46 KERNEL_APPEND="root=/dev/sda1 \
47 systemd.log_level=debug \
50 init=/usr/lib/systemd/systemd \
57 QEMU_OPTIONS="-machine accel=kvm:tcg \
65 if [ "$INITRD" ]; then
66 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
70 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
75 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root /usr/lib/systemd/systemd $KERNEL_APPEND
78 setup_basic_environment() {
79 # create the basic filesystem layout
83 install_missing_libraries
99 generate_module_dependencies
101 ln -fs /proc/self/mounts $initdir/etc/mtab
105 instmods dm_crypt =crypto
106 type -P dmeventd >/dev/null && dracut_install dmeventd
107 inst_libdir_file "libdevmapper-event.so*"
108 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
112 # install compiled files
113 (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
114 # remove unneeded documentation
115 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
116 # we strip binaries since debug symbols increase binaries size a lot
117 # and it could fill the available space
121 install_missing_libraries() {
122 # install possible missing libraries
123 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
128 create_empty_image() {
129 rm -f "$TESTDIR/rootdisk.img"
130 # Create the blank file to use as a root filesystem
131 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
132 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
133 [ -b "$LOOPDEV" ] || return 1
134 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
135 sfdisk -C 9600 -H 2 -S 32 -L "$LOOPDEV" <<EOF
140 mkfs.ext3 -L systemd "${LOOPDEV}p1"
143 check_result_nspawn() {
145 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
146 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
147 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
148 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
149 ls -l $TESTDIR/journal/*/*.journal
150 test -s $TESTDIR/failed && ret=$(($ret+1))
155 ddebug "Strip binaries"
156 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
160 mkdir -p $initdir/etc/rc.d
161 cat >$initdir/etc/rc.d/rc.local <<EOF
165 chmod 0755 $initdir/etc/rc.d/rc.local
169 # install any Execs from the service files
170 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
172 i=${i##Exec*=}; i=${i##-}
177 generate_module_dependencies() {
178 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
179 ! depmod -a -b "$initdir" $KERNEL_VER; then
180 dfatal "\"depmod -a $KERNEL_VER\" failed."
185 install_depmod_files() {
186 inst /lib/modules/$KERNEL_VER/modules.order
187 inst /lib/modules/$KERNEL_VER/modules.builtin
191 # install plymouth, if found... else remove plymouth service files
192 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
193 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
194 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
195 # dracut_install plymouth plymouthd
197 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
201 install_ld_so_conf() {
202 cp -a /etc/ld.so.conf* $initdir/etc
203 ldconfig -r "$initdir"
206 install_config_files() {
207 inst /etc/sysconfig/init
212 inst /etc/nsswitch.conf
217 # we want an empty environment
218 > $initdir/etc/environment
219 > $initdir/etc/machine-id
221 echo systemd-testsuite > $initdir/etc/hostname
223 cat >$initdir/etc/fstab <<EOF
224 LABEL=systemd / ext3 rw 0 1
228 install_basic_tools() {
229 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
232 install_debug_tools() {
233 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
237 # install libnss_files for login
238 inst_libdir_file "libnss_files*"
242 inst /usr/lib/systemd/system/dbus.socket
243 inst /usr/lib/systemd/system/dbus.service
246 /etc/dbus-1 -xtype f \
247 | while read file; do
257 /lib/security -xtype f \
258 | while read file; do
265 /usr/lib/kbd/keymaps/include/* \
266 /usr/lib/kbd/keymaps/i386/include/* \
267 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
268 [[ -f $i ]] || continue
275 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
276 [[ -f $i ]] || continue
282 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
283 [ -f ${_terminfodir}/l/linux ] && break
285 dracut_install -o ${_terminfodir}/l/linux
289 cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
291 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
292 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
293 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
295 # make the testsuite the default target
296 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
299 setup_nspawn_root() {
300 rm -fr $TESTDIR/nspawn-root
301 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
302 cp -ar $initdir $TESTDIR/nspawn-root
303 # we don't mount in the nspawn root
304 rm -f $TESTDIR/nspawn-root/etc/fstab
308 mkdir -p $initdir/run
309 mkdir -p $initdir/etc/systemd/system
310 mkdir -p $initdir/var/log/journal
312 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
313 if [ -L "/$d" ]; then
320 ln -sfn /run "$initdir/var/run"
321 ln -sfn /run/lock "$initdir/var/lock"
326 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
329 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
330 [[ $_line = 'not a dynamic executable' ]] && break
332 if [[ $_line =~ $_so_regex ]]; then
333 _file=${BASH_REMATCH[1]}
334 [[ -e ${initdir}/$_file ]] && continue
335 inst_library "$_file"
339 if [[ $_line =~ not\ found ]]; then
340 dfatal "Missing a shared library required by $_bin."
341 dfatal "Run \"ldd $_bin\" to find out what it is."
343 dfatal "dracut cannot create an initrd."
351 [[ -e $STATEFILE ]] && . $STATEFILE
352 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
353 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
354 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
360 initdir=$TESTDIR/root
364 ## @brief Converts numeric logging level to the first letter of level name.
366 # @param lvl Numeric logging level in range from 1 to 6.
367 # @retval 1 if @a lvl is out of range.
368 # @retval 0 if @a lvl is correct.
369 # @result Echoes first letter of level name.
382 ## @brief Internal helper function for _do_dlog()
384 # @param lvl Numeric logging level.
385 # @param msg Message.
386 # @retval 0 It's always returned, even if logging failed.
388 # @note This function is not supposed to be called manually. Please use
389 # dtrace(), ddebug(), or others instead which wrap this one.
391 # This function calls _do_dlog() either with parameter msg, or if
392 # none is given, it will read standard input and will use every line as
396 # dwarn "This is a warning"
397 # echo "This is a warning" | dwarn
401 [ -z "$LOG_LEVEL" ] && return 0
402 [ $1 -le $LOG_LEVEL ] || return 0
403 local lvl="$1"; shift
404 local lvlc=$(_lvl2char "$lvl") || return 0
406 if [ $# -ge 1 ]; then
410 echo "$lvlc: " "$line"
415 ## @brief Logs message at TRACE level (6)
417 # @param msg Message.
418 # @retval 0 It's always returned, even if logging failed.
422 [ -n "$debug" ] && set -x || :
425 ## @brief Logs message at DEBUG level (5)
427 # @param msg Message.
428 # @retval 0 It's always returned, even if logging failed.
432 # [ -n "$debug" ] && set -x || :
435 ## @brief Logs message at INFO level (4)
437 # @param msg Message.
438 # @retval 0 It's always returned, even if logging failed.
442 [ -n "$debug" ] && set -x || :
445 ## @brief Logs message at WARN level (3)
447 # @param msg Message.
448 # @retval 0 It's always returned, even if logging failed.
452 [ -n "$debug" ] && set -x || :
455 ## @brief Logs message at ERROR level (2)
457 # @param msg Message.
458 # @retval 0 It's always returned, even if logging failed.
462 # [ -n "$debug" ] && set -x || :
465 ## @brief Logs message at FATAL level (1)
467 # @param msg Message.
468 # @retval 0 It's always returned, even if logging failed.
472 [ -n "$debug" ] && set -x || :
476 # Generic substring function. If $2 is in $1, return 0.
477 strstr() { [ "${1#*$2*}" != "$1" ]; }
479 # normalize_path <path>
480 # Prints the normalized path, where it removes any duplicated
481 # and trailing slashes.
483 # $ normalize_path ///test/test//
487 set -- "${1//+(\/)//}"
492 # convert_abs_rel <from> <to>
493 # Prints the relative path, when creating a symlink to <to> from <from>.
495 # $ convert_abs_rel /usr/bin/test /bin/test-2
497 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
499 local __current __absolute __abssize __cursize __newpath
502 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
504 # corner case #1 - self looping link
505 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
507 # corner case #2 - own dir link
508 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
510 IFS="/" __current=($1)
511 IFS="/" __absolute=($2)
513 __abssize=${#__absolute[@]}
514 __cursize=${#__current[@]}
516 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
519 if (( __level > __abssize || __level > __cursize ))
525 for ((__i = __level; __i < __cursize-1; __i++))
529 __newpath=$__newpath"/"
531 __newpath=$__newpath".."
534 for ((__i = __level; __i < __abssize; __i++))
536 if [[ -n $__newpath ]]
538 __newpath=$__newpath"/"
540 __newpath=$__newpath${__absolute[__i]}
547 # Install a directory, keeping symlinks as on the original system.
548 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
549 # will create ${initdir}/lib64, ${initdir}/lib64/file,
550 # and a symlink ${initdir}/lib -> lib64.
552 [[ -e ${initdir}/"$1" ]] && return 0 # already there
554 local _dir="$1" _part="${1%/*}" _file
555 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
560 # iterate over parent directories
561 for _file in $_dir; do
562 [[ -e "${initdir}/$_file" ]] && continue
563 if [[ -L $_file ]]; then
564 inst_symlink "$_file"
567 mkdir -m 0755 -p "${initdir}/$_file" || return 1
568 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
569 chmod u+w "${initdir}/$_file"
574 # $1 = file to copy to ramdisk
575 # $2 (optional) Name for the file on the ramdisk
576 # Location of the image dir is assumed to be $initdir
577 # We never overwrite the target if it exists.
579 [[ -f "$1" ]] || return 1
580 strstr "$1" "/" || return 1
582 local _src=$1 target="${2:-$1}"
583 if ! [[ -d ${initdir}/$target ]]; then
584 [[ -e ${initdir}/$target ]] && return 0
585 [[ -L ${initdir}/$target ]] && return 0
586 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
588 # install checksum files also
589 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
590 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
592 ddebug "Installing $_src"
593 cp --sparse=always -pfL "$_src" "${initdir}/$target"
596 # find symlinks linked to given library file
598 # Function searches for symlinks by stripping version numbers appended to
599 # library filename, checks if it points to the same target and finally
600 # prints the list of symlinks to stdout.
603 # rev_lib_symlinks libfoo.so.8.1
604 # output: libfoo.so.8 libfoo.so
605 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
607 [[ ! $1 ]] && return 0
609 local fn="$1" orig="$(readlink -f "$1")" links=''
611 [[ ${fn} =~ .*\.so\..* ]] || return 1
613 until [[ ${fn##*.} == so ]]; do
615 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
621 # Same as above, but specialized to handle dynamic libraries.
622 # It handles making symlinks according to how the original library
625 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
626 strstr "$1" "/" || return 1
627 [[ -e $initdir/$_dest ]] && return 0
628 if [[ -L $_src ]]; then
629 # install checksum files also
630 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
631 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
633 _reallib=$(readlink -f "$_src")
634 inst_simple "$_reallib" "$_reallib"
635 inst_dir "${_dest%/*}"
636 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
637 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
639 inst_simple "$_src" "$_dest"
642 # Create additional symlinks. See rev_symlinks description.
643 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
644 [[ ! -e $initdir/$_symlink ]] && {
645 ddebug "Creating extra symlink: $_symlink"
646 inst_symlink $_symlink
651 # find a binary. If we were not passed the full path directly,
652 # search in the usual places to find the binary.
654 if [[ -z ${1##/*} ]]; then
655 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
664 # Same as above, but specialized to install binary executables.
665 # Install binary executable, and all shared library dependencies, if any.
668 _bin=$(find_binary "$1") || return 1
670 [[ -e $initdir/$_target ]] && return 0
671 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
673 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
675 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
676 [[ $_line = 'not a dynamic executable' ]] && break
678 if [[ $_line =~ $_so_regex ]]; then
679 _file=${BASH_REMATCH[1]}
680 [[ -e ${initdir}/$_file ]] && continue
681 inst_library "$_file"
685 if [[ $_line =~ not\ found ]]; then
686 dfatal "Missing a shared library required by $_bin."
687 dfatal "Run \"ldd $_bin\" to find out what it is."
689 dfatal "dracut cannot create an initrd."
693 inst_simple "$_bin" "$_target"
696 # same as above, except for shell scripts.
697 # If your shell script does not start with shebang, it is not a shell script.
700 _bin=$(find_binary "$1") || return 1
702 local _line _shebang_regex
703 read -r -n 80 _line <"$_bin"
704 # If debug is set, clean unprintable chars to prevent messing up the term
705 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
706 _shebang_regex='(#! *)(/[^ ]+).*'
707 [[ $_line =~ $_shebang_regex ]] || return 1
708 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
711 # same as above, but specialized for symlinks
713 local _src=$1 _target=${2:-$1} _realsrc
714 strstr "$1" "/" || return 1
715 [[ -L $1 ]] || return 1
716 [[ -L $initdir/$_target ]] && return 0
717 _realsrc=$(readlink -f "$_src")
718 if ! [[ -e $initdir/$_realsrc ]]; then
719 if [[ -d $_realsrc ]]; then
725 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
726 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
727 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
730 # attempt to install any programs specified in a udev rule
731 inst_rule_programs() {
734 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
735 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
736 if [ -x /lib/udev/$_prog ]; then
737 _bin=/lib/udev/$_prog
739 _bin=$(find_binary "$_prog") || {
740 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
745 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
746 dracut_install "$_bin"
751 # udev rules always get installed in the same place, so
752 # create a function to install them to make life simpler.
754 local _target=/etc/udev/rules.d _rule _found
756 inst_dir "/lib/udev/rules.d"
758 for _rule in "$@"; do
759 if [ "${rule#/}" = "$rule" ]; then
760 for r in /lib/udev/rules.d /etc/udev/rules.d; do
761 if [[ -f $r/$_rule ]]; then
763 inst_simple "$_found"
764 inst_rule_programs "$_found"
768 for r in '' ./ $dracutbasedir/rules.d/; do
769 if [[ -f ${r}$_rule ]]; then
771 inst_simple "$_found" "$_target/${_found##*/}"
772 inst_rule_programs "$_found"
775 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
779 # general purpose installation function
780 # Same args as above.
786 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
787 [[ $initdir = $2 ]] && set $1;;
788 3) [[ -z $initdir ]] && export initdir=$2
790 *) dfatal "inst only takes 1 or 2 or 3 arguments"
793 for _x in inst_symlink inst_script inst_binary inst_simple; do
799 # install any of listed files
801 # If first argument is '-d' and second some destination path, first accessible
802 # source is installed into this path, otherwise it will installed in the same
803 # path as source. If none of listed files was installed, function return 1.
804 # On first successful installation it returns with 0 status.
808 # inst_any -d /bin/foo /bin/bar /bin/baz
810 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
815 [[ $1 = '-d' ]] && to="$2" && shift 2
819 [[ $to ]] && inst "$f" "$to" && return 0
820 inst "$f" && return 0
827 # dracut_install [-o ] <file> [<file> ... ]
828 # Install <file> to the initramfs image
829 # -o optionally install the <file> and don't fail, if it is not there
832 if [[ $1 = '-o' ]]; then
837 if ! inst "$1" ; then
838 if [[ $_optional = yes ]]; then
839 dinfo "Skipping program $1 as it cannot be found and is" \
840 "flagged to be optional"
842 dfatal "Failed to install $1"
850 # Install a single kernel module along with any firmware it may require.
851 # $1 = full path to kernel module to install
852 install_kmod_with_fw() {
853 # no need to go further if the module is already installed
855 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
858 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
860 if [[ $omit_drivers ]]; then
864 if [[ "$_kmod" =~ $omit_drivers ]]; then
865 dinfo "Omitting driver $_kmod"
868 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
869 dinfo "Omitting driver $_kmod"
874 [ -d "$initdir/.kernelmodseen" ] && \
875 > "$initdir/.kernelmodseen/${1##*/}"
877 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
880 local _modname=${1##*/} _fwdir _found _fw
881 _modname=${_modname%.ko*}
882 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
884 for _fwdir in $fw_dir; do
885 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
886 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
890 if [[ $_found != yes ]]; then
891 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
892 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
895 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
903 # Do something with all the dependencies of a kernel module.
904 # Note that kernel modules depend on themselves using the technique we use
905 # $1 = function to call for each dependency we find
906 # It will be passed the full path to the found kernel module
907 # $2 = module to get dependencies for
908 # rest of args = arguments to modprobe
909 # _fderr specifies FD passed from surrounding scope
910 for_each_kmod_dep() {
911 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
913 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
914 while read _cmd _modpath _options; do
915 [[ $_cmd = insmod ]] || continue
916 $_func ${_modpath} || exit $?
919 [[ $_found -eq 0 ]] && exit 1
924 # filter kernel modules to install certain modules that meet specific
926 # $1 = search only in subdirectory of /kernel/$1
927 # $2 = function to call with module name to filter.
928 # This function will be passed the full path to the module to test.
929 # The behavior of this function can vary depending on whether $hostonly is set.
930 # If it is, we will only look at modules that are already in memory.
931 # If it is not, we will look at all kernel modules
932 # This function returns the full filenames of modules that match $1
933 filter_kernel_modules_by_path () (
934 local _modname _filtercmd
935 if ! [[ $hostonly ]]; then
936 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
937 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
938 _filtercmd+=' -o -name "*.ko.xz"'
939 _filtercmd+=' 2>/dev/null'
941 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
942 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
944 for _modname in $(eval $_filtercmd); do
946 *.ko) "$2" "$_modname" && echo "$_modname";;
947 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
948 $2 $initdir/$$.ko && echo "$_modname"
951 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
952 $2 $initdir/$$.ko && echo "$_modname"
958 find_kernel_modules_by_path () (
959 if ! [[ $hostonly ]]; then
960 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
961 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
963 cut -d " " -f 1 </proc/modules \
964 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
968 filter_kernel_modules () {
969 filter_kernel_modules_by_path drivers "$1"
972 find_kernel_modules () {
973 find_kernel_modules_by_path drivers
976 # instmods [-c] <kernel module> [<kernel module> ... ]
977 # instmods [-c] <kernel subsystem>
978 # install kernel modules along with all their dependencies.
979 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
981 [[ $no_kernel = yes ]] && return
982 # called [sub]functions inherit _fderr
985 if [[ $1 = '-c' ]]; then
990 function inst1mod() {
991 local _ret=0 _mod="$1"
994 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
995 ( [[ "$_mpargs" ]] && echo $_mpargs
996 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
999 ( [[ "$_mpargs" ]] && echo $_mpargs
1000 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1004 --*) _mpargs+=" $_mod" ;;
1005 i2o_scsi) return ;; # Do not load this diagnostic-only module
1008 # if we are already installed, skip this module and go on
1010 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1012 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1013 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1016 # If we are building a host-specific initramfs and this
1017 # module is not already loaded, move on to the next one.
1018 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1019 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1022 # We use '-d' option in modprobe only if modules prefix path
1023 # differs from default '/'. This allows us to use Dracut with
1024 # old version of modprobe which doesn't have '-d' option.
1025 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1026 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1028 # ok, load the module, all its dependencies, and any firmware
1030 for_each_kmod_dep install_kmod_with_fw $_mod \
1031 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1038 function instmods_1() {
1040 if (($# == 0)); then # filenames from stdin
1042 inst1mod "${_mod%.ko*}" || {
1043 if [ "$_check" = "yes" ]; then
1044 dfatal "Failed to install $_mod"
1050 while (($# > 0)); do # filenames as arguments
1051 inst1mod ${1%.ko*} || {
1052 if [ "$_check" = "yes" ]; then
1053 dfatal "Failed to install $1"
1062 local _ret _filter_not_found='FATAL: Module .* not found.'
1064 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1065 # redirections, but that would make dracut require bash4 at least.
1066 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1067 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1073 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1074 # Install a <file> located on a lib directory to the initramfs image
1075 # -n <pattern> install non-matching files
1076 inst_libdir_file() {
1077 if [[ "$1" == "-n" ]]; then
1080 for _dir in $libdirs; do
1082 for _f in "$_dir"/$_i; do
1083 [[ "$_i" =~ $_pattern ]] || continue
1084 [[ -e "$_i" ]] && dracut_install "$_i"
1089 for _dir in $libdirs; do
1091 for _f in "$_dir"/$_i; do
1092 [[ -e "$_f" ]] && dracut_install "$_f"
1100 [[ -d /sys/fs/cgroup/systemd ]]
1105 if [[ $UID != "0" ]]; then
1106 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1111 [[ $libdir ]] || for libdir in /lib64 /lib; do
1112 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1115 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1116 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1122 while (($# > 0)); do
1125 echo "TEST RUN: $TEST_DESCRIPTION"
1128 if [ $ret -eq 0 ]; then
1129 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1131 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1135 echo "TEST SETUP: $TEST_DESCRIPTION"
1139 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1145 echo -n "TEST: $TEST_DESCRIPTION ";
1147 test_setup && test_run
1153 ) </dev/null >test.log 2>&1
1155 if [ $ret -eq 0 ]; then
1160 echo "see $(pwd)/test.log"