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"
69 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img || return 1
73 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root /usr/lib/systemd/systemd
76 setup_basic_environment() {
77 # create the basic filesystem layout
81 install_missing_libraries
97 generate_module_dependencies
99 ln -fs /proc/self/mounts $initdir/etc/mtab
103 instmods dm_crypt =crypto
104 type -P dmeventd >/dev/null && dracut_install dmeventd
105 inst_libdir_file "libdevmapper-event.so*"
106 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
110 # install compiled files
111 (cd $TEST_BASE_DIR/..; make DESTDIR=$initdir install)
112 # remove unneeded documentation
113 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
114 # we strip binaries since debug symbols increase binaries size a lot
115 # and it could fill the available space
119 install_missing_libraries() {
120 # install possible missing libraries
121 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
126 create_empty_image() {
127 rm -f "$TESTDIR/rootdisk.img"
128 # Create the blank file to use as a root filesystem
129 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=200
130 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
131 [ -b "$LOOPDEV" ] || return 1
132 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
133 sfdisk -C 6400 -H 2 -S 32 -L "$LOOPDEV" <<EOF
138 mkfs.ext3 -L systemd "${LOOPDEV}p1"
141 check_result_nspawn() {
143 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
144 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
145 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
146 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
147 ls -l $TESTDIR/journal/*/*.journal
148 test -s $TESTDIR/failed && ret=$(($ret+1))
153 ddebug "Strip binaries"
154 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
158 mkdir -p $initdir/etc/rc.d
159 cat >$initdir/etc/rc.d/rc.local <<EOF
163 chmod 0755 $initdir/etc/rc.d/rc.local
167 # install any Execs from the service files
168 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
170 i=${i##Exec*=}; i=${i##-}
175 generate_module_dependencies() {
176 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
177 ! depmod -a -b "$initdir" $KERNEL_VER; then
178 dfatal "\"depmod -a $KERNEL_VER\" failed."
183 install_depmod_files() {
184 inst /lib/modules/$KERNEL_VER/modules.order
185 inst /lib/modules/$KERNEL_VER/modules.builtin
189 # install plymouth, if found... else remove plymouth service files
190 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
191 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
192 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
193 # dracut_install plymouth plymouthd
195 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
199 install_ld_so_conf() {
200 cp -a /etc/ld.so.conf* $initdir/etc
201 ldconfig -r "$initdir"
204 install_config_files() {
205 inst /etc/sysconfig/init
210 inst /etc/nsswitch.conf
215 # we want an empty environment
216 > $initdir/etc/environment
217 > $initdir/etc/machine-id
219 echo systemd-testsuite > $initdir/etc/hostname
221 cat >$initdir/etc/fstab <<EOF
222 LABEL=systemd / ext3 rw 0 1
226 install_basic_tools() {
227 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
230 install_debug_tools() {
231 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
235 # install libnss_files for login
236 inst_libdir_file "libnss_files*"
240 inst /usr/lib/systemd/system/dbus.socket
241 inst /usr/lib/systemd/system/dbus.service
244 /etc/dbus-1 -xtype f \
245 | while read file; do
255 /lib/security -xtype f \
256 | while read file; do
263 /usr/lib/kbd/keymaps/include/* \
264 /usr/lib/kbd/keymaps/i386/include/* \
265 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
266 [[ -f $i ]] || continue
273 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
274 [[ -f $i ]] || continue
280 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
281 [ -f ${_terminfodir}/l/linux ] && break
283 dracut_install -o ${_terminfodir}/l/linux
287 cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
289 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
290 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
291 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
293 # make the testsuite the default target
294 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
297 setup_nspawn_root() {
298 rm -fr $TESTDIR/nspawn-root
299 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
300 cp -ar $initdir $TESTDIR/nspawn-root
301 # we don't mount in the nspawn root
302 rm -f $TESTDIR/nspawn-root/etc/fstab
306 mkdir -p $initdir/run
307 mkdir -p $initdir/etc/systemd/system
308 mkdir -p $initdir/var/log/journal
310 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
311 if [ -L "/$d" ]; then
318 ln -sfn /run "$initdir/var/run"
319 ln -sfn /run/lock "$initdir/var/lock"
324 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
327 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
328 [[ $_line = 'not a dynamic executable' ]] && break
330 if [[ $_line =~ $_so_regex ]]; then
331 _file=${BASH_REMATCH[1]}
332 [[ -e ${initdir}/$_file ]] && continue
333 inst_library "$_file"
337 if [[ $_line =~ not\ found ]]; then
338 dfatal "Missing a shared library required by $_bin."
339 dfatal "Run \"ldd $_bin\" to find out what it is."
341 dfatal "dracut cannot create an initrd."
349 [[ -e $STATEFILE ]] && . $STATEFILE
350 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
351 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
352 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
358 initdir=$TESTDIR/root
362 ## @brief Converts numeric logging level to the first letter of level name.
364 # @param lvl Numeric logging level in range from 1 to 6.
365 # @retval 1 if @a lvl is out of range.
366 # @retval 0 if @a lvl is correct.
367 # @result Echoes first letter of level name.
380 ## @brief Internal helper function for _do_dlog()
382 # @param lvl Numeric logging level.
383 # @param msg Message.
384 # @retval 0 It's always returned, even if logging failed.
386 # @note This function is not supposed to be called manually. Please use
387 # dtrace(), ddebug(), or others instead which wrap this one.
389 # This function calls _do_dlog() either with parameter msg, or if
390 # none is given, it will read standard input and will use every line as
394 # dwarn "This is a warning"
395 # echo "This is a warning" | dwarn
399 [ -z "$LOG_LEVEL" ] && return 0
400 [ $1 -le $LOG_LEVEL ] || return 0
401 local lvl="$1"; shift
402 local lvlc=$(_lvl2char "$lvl") || return 0
404 if [ $# -ge 1 ]; then
408 echo "$lvlc: " "$line"
413 ## @brief Logs message at TRACE level (6)
415 # @param msg Message.
416 # @retval 0 It's always returned, even if logging failed.
420 [ -n "$debug" ] && set -x || :
423 ## @brief Logs message at DEBUG level (5)
425 # @param msg Message.
426 # @retval 0 It's always returned, even if logging failed.
430 # [ -n "$debug" ] && set -x || :
433 ## @brief Logs message at INFO level (4)
435 # @param msg Message.
436 # @retval 0 It's always returned, even if logging failed.
440 [ -n "$debug" ] && set -x || :
443 ## @brief Logs message at WARN level (3)
445 # @param msg Message.
446 # @retval 0 It's always returned, even if logging failed.
450 [ -n "$debug" ] && set -x || :
453 ## @brief Logs message at ERROR level (2)
455 # @param msg Message.
456 # @retval 0 It's always returned, even if logging failed.
460 # [ -n "$debug" ] && set -x || :
463 ## @brief Logs message at FATAL level (1)
465 # @param msg Message.
466 # @retval 0 It's always returned, even if logging failed.
470 [ -n "$debug" ] && set -x || :
474 # Generic substring function. If $2 is in $1, return 0.
475 strstr() { [ "${1#*$2*}" != "$1" ]; }
477 # normalize_path <path>
478 # Prints the normalized path, where it removes any duplicated
479 # and trailing slashes.
481 # $ normalize_path ///test/test//
485 set -- "${1//+(\/)//}"
490 # convert_abs_rel <from> <to>
491 # Prints the relative path, when creating a symlink to <to> from <from>.
493 # $ convert_abs_rel /usr/bin/test /bin/test-2
495 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
497 local __current __absolute __abssize __cursize __newpath
500 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
502 # corner case #1 - self looping link
503 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
505 # corner case #2 - own dir link
506 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
508 IFS="/" __current=($1)
509 IFS="/" __absolute=($2)
511 __abssize=${#__absolute[@]}
512 __cursize=${#__current[@]}
514 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
517 if (( __level > __abssize || __level > __cursize ))
523 for ((__i = __level; __i < __cursize-1; __i++))
527 __newpath=$__newpath"/"
529 __newpath=$__newpath".."
532 for ((__i = __level; __i < __abssize; __i++))
534 if [[ -n $__newpath ]]
536 __newpath=$__newpath"/"
538 __newpath=$__newpath${__absolute[__i]}
545 # Install a directory, keeping symlinks as on the original system.
546 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
547 # will create ${initdir}/lib64, ${initdir}/lib64/file,
548 # and a symlink ${initdir}/lib -> lib64.
550 [[ -e ${initdir}/"$1" ]] && return 0 # already there
552 local _dir="$1" _part="${1%/*}" _file
553 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
558 # iterate over parent directories
559 for _file in $_dir; do
560 [[ -e "${initdir}/$_file" ]] && continue
561 if [[ -L $_file ]]; then
562 inst_symlink "$_file"
565 mkdir -m 0755 -p "${initdir}/$_file" || return 1
566 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
567 chmod u+w "${initdir}/$_file"
572 # $1 = file to copy to ramdisk
573 # $2 (optional) Name for the file on the ramdisk
574 # Location of the image dir is assumed to be $initdir
575 # We never overwrite the target if it exists.
577 [[ -f "$1" ]] || return 1
578 strstr "$1" "/" || return 1
580 local _src=$1 target="${2:-$1}"
581 if ! [[ -d ${initdir}/$target ]]; then
582 [[ -e ${initdir}/$target ]] && return 0
583 [[ -L ${initdir}/$target ]] && return 0
584 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
586 # install checksum files also
587 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
588 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
590 ddebug "Installing $_src"
591 cp --sparse=always -pfL "$_src" "${initdir}/$target"
594 # find symlinks linked to given library file
596 # Function searches for symlinks by stripping version numbers appended to
597 # library filename, checks if it points to the same target and finally
598 # prints the list of symlinks to stdout.
601 # rev_lib_symlinks libfoo.so.8.1
602 # output: libfoo.so.8 libfoo.so
603 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
605 [[ ! $1 ]] && return 0
607 local fn="$1" orig="$(readlink -f "$1")" links=''
609 [[ ${fn} =~ .*\.so\..* ]] || return 1
611 until [[ ${fn##*.} == so ]]; do
613 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
619 # Same as above, but specialized to handle dynamic libraries.
620 # It handles making symlinks according to how the original library
623 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
624 strstr "$1" "/" || return 1
625 [[ -e $initdir/$_dest ]] && return 0
626 if [[ -L $_src ]]; then
627 # install checksum files also
628 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
629 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
631 _reallib=$(readlink -f "$_src")
632 inst_simple "$_reallib" "$_reallib"
633 inst_dir "${_dest%/*}"
634 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
635 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
637 inst_simple "$_src" "$_dest"
640 # Create additional symlinks. See rev_symlinks description.
641 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
642 [[ ! -e $initdir/$_symlink ]] && {
643 ddebug "Creating extra symlink: $_symlink"
644 inst_symlink $_symlink
649 # find a binary. If we were not passed the full path directly,
650 # search in the usual places to find the binary.
652 if [[ -z ${1##/*} ]]; then
653 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
662 # Same as above, but specialized to install binary executables.
663 # Install binary executable, and all shared library dependencies, if any.
666 _bin=$(find_binary "$1") || return 1
668 [[ -e $initdir/$_target ]] && return 0
669 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
671 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
673 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
674 [[ $_line = 'not a dynamic executable' ]] && break
676 if [[ $_line =~ $_so_regex ]]; then
677 _file=${BASH_REMATCH[1]}
678 [[ -e ${initdir}/$_file ]] && continue
679 inst_library "$_file"
683 if [[ $_line =~ not\ found ]]; then
684 dfatal "Missing a shared library required by $_bin."
685 dfatal "Run \"ldd $_bin\" to find out what it is."
687 dfatal "dracut cannot create an initrd."
691 inst_simple "$_bin" "$_target"
694 # same as above, except for shell scripts.
695 # If your shell script does not start with shebang, it is not a shell script.
698 _bin=$(find_binary "$1") || return 1
700 local _line _shebang_regex
701 read -r -n 80 _line <"$_bin"
702 # If debug is set, clean unprintable chars to prevent messing up the term
703 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
704 _shebang_regex='(#! *)(/[^ ]+).*'
705 [[ $_line =~ $_shebang_regex ]] || return 1
706 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
709 # same as above, but specialized for symlinks
711 local _src=$1 _target=${2:-$1} _realsrc
712 strstr "$1" "/" || return 1
713 [[ -L $1 ]] || return 1
714 [[ -L $initdir/$_target ]] && return 0
715 _realsrc=$(readlink -f "$_src")
716 if ! [[ -e $initdir/$_realsrc ]]; then
717 if [[ -d $_realsrc ]]; then
723 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
724 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
725 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
728 # attempt to install any programs specified in a udev rule
729 inst_rule_programs() {
732 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
733 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
734 if [ -x /lib/udev/$_prog ]; then
735 _bin=/lib/udev/$_prog
737 _bin=$(find_binary "$_prog") || {
738 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
743 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
744 dracut_install "$_bin"
749 # udev rules always get installed in the same place, so
750 # create a function to install them to make life simpler.
752 local _target=/etc/udev/rules.d _rule _found
754 inst_dir "/lib/udev/rules.d"
756 for _rule in "$@"; do
757 if [ "${rule#/}" = "$rule" ]; then
758 for r in /lib/udev/rules.d /etc/udev/rules.d; do
759 if [[ -f $r/$_rule ]]; then
761 inst_simple "$_found"
762 inst_rule_programs "$_found"
766 for r in '' ./ $dracutbasedir/rules.d/; do
767 if [[ -f ${r}$_rule ]]; then
769 inst_simple "$_found" "$_target/${_found##*/}"
770 inst_rule_programs "$_found"
773 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
777 # general purpose installation function
778 # Same args as above.
784 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
785 [[ $initdir = $2 ]] && set $1;;
786 3) [[ -z $initdir ]] && export initdir=$2
788 *) dfatal "inst only takes 1 or 2 or 3 arguments"
791 for _x in inst_symlink inst_script inst_binary inst_simple; do
797 # install any of listed files
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.
806 # inst_any -d /bin/foo /bin/bar /bin/baz
808 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
813 [[ $1 = '-d' ]] && to="$2" && shift 2
817 [[ $to ]] && inst "$f" "$to" && return 0
818 inst "$f" && return 0
825 # dracut_install [-o ] <file> [<file> ... ]
826 # Install <file> to the initramfs image
827 # -o optionally install the <file> and don't fail, if it is not there
830 if [[ $1 = '-o' ]]; then
835 if ! inst "$1" ; then
836 if [[ $_optional = yes ]]; then
837 dinfo "Skipping program $1 as it cannot be found and is" \
838 "flagged to be optional"
840 dfatal "Failed to install $1"
848 # Install a single kernel module along with any firmware it may require.
849 # $1 = full path to kernel module to install
850 install_kmod_with_fw() {
851 # no need to go further if the module is already installed
853 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
856 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
858 if [[ $omit_drivers ]]; then
862 if [[ "$_kmod" =~ $omit_drivers ]]; then
863 dinfo "Omitting driver $_kmod"
866 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
867 dinfo "Omitting driver $_kmod"
872 [ -d "$initdir/.kernelmodseen" ] && \
873 > "$initdir/.kernelmodseen/${1##*/}"
875 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
878 local _modname=${1##*/} _fwdir _found _fw
879 _modname=${_modname%.ko*}
880 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
882 for _fwdir in $fw_dir; do
883 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
884 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
888 if [[ $_found != yes ]]; then
889 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
890 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
893 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
901 # Do something with all the dependencies of a kernel module.
902 # Note that kernel modules depend on themselves using the technique we use
903 # $1 = function to call for each dependency we find
904 # It will be passed the full path to the found kernel module
905 # $2 = module to get dependencies for
906 # rest of args = arguments to modprobe
907 # _fderr specifies FD passed from surrounding scope
908 for_each_kmod_dep() {
909 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
911 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
912 while read _cmd _modpath _options; do
913 [[ $_cmd = insmod ]] || continue
914 $_func ${_modpath} || exit $?
917 [[ $_found -eq 0 ]] && exit 1
922 # filter kernel modules to install certain modules that meet specific
924 # $1 = search only in subdirectory of /kernel/$1
925 # $2 = function to call with module name to filter.
926 # This function will be passed the full path to the module to test.
927 # The behavior of this function can vary depending on whether $hostonly is set.
928 # If it is, we will only look at modules that are already in memory.
929 # If it is not, we will look at all kernel modules
930 # This function returns the full filenames of modules that match $1
931 filter_kernel_modules_by_path () (
932 local _modname _filtercmd
933 if ! [[ $hostonly ]]; then
934 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
935 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
936 _filtercmd+=' -o -name "*.ko.xz"'
937 _filtercmd+=' 2>/dev/null'
939 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
940 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
942 for _modname in $(eval $_filtercmd); do
944 *.ko) "$2" "$_modname" && echo "$_modname";;
945 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
946 $2 $initdir/$$.ko && echo "$_modname"
949 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
950 $2 $initdir/$$.ko && echo "$_modname"
956 find_kernel_modules_by_path () (
957 if ! [[ $hostonly ]]; then
958 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
959 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
961 cut -d " " -f 1 </proc/modules \
962 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
966 filter_kernel_modules () {
967 filter_kernel_modules_by_path drivers "$1"
970 find_kernel_modules () {
971 find_kernel_modules_by_path drivers
974 # instmods [-c] <kernel module> [<kernel module> ... ]
975 # instmods [-c] <kernel subsystem>
976 # install kernel modules along with all their dependencies.
977 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
979 [[ $no_kernel = yes ]] && return
980 # called [sub]functions inherit _fderr
983 if [[ $1 = '-c' ]]; then
988 function inst1mod() {
989 local _ret=0 _mod="$1"
992 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
993 ( [[ "$_mpargs" ]] && echo $_mpargs
994 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
997 ( [[ "$_mpargs" ]] && echo $_mpargs
998 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1002 --*) _mpargs+=" $_mod" ;;
1003 i2o_scsi) return ;; # Do not load this diagnostic-only module
1006 # if we are already installed, skip this module and go on
1008 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1010 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1011 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1014 # If we are building a host-specific initramfs and this
1015 # module is not already loaded, move on to the next one.
1016 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1017 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1020 # We use '-d' option in modprobe only if modules prefix path
1021 # differs from default '/'. This allows us to use Dracut with
1022 # old version of modprobe which doesn't have '-d' option.
1023 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1024 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1026 # ok, load the module, all its dependencies, and any firmware
1028 for_each_kmod_dep install_kmod_with_fw $_mod \
1029 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1036 function instmods_1() {
1038 if (($# == 0)); then # filenames from stdin
1040 inst1mod "${_mod%.ko*}" || {
1041 if [ "$_check" = "yes" ]; then
1042 dfatal "Failed to install $_mod"
1048 while (($# > 0)); do # filenames as arguments
1049 inst1mod ${1%.ko*} || {
1050 if [ "$_check" = "yes" ]; then
1051 dfatal "Failed to install $1"
1060 local _ret _filter_not_found='FATAL: Module .* not found.'
1062 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1063 # redirections, but that would make dracut require bash4 at least.
1064 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1065 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1071 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1072 # Install a <file> located on a lib directory to the initramfs image
1073 # -n <pattern> install non-matching files
1074 inst_libdir_file() {
1075 if [[ "$1" == "-n" ]]; then
1078 for _dir in $libdirs; do
1080 for _f in "$_dir"/$_i; do
1081 [[ "$_i" =~ $_pattern ]] || continue
1082 [[ -e "$_i" ]] && dracut_install "$_i"
1087 for _dir in $libdirs; do
1089 for _f in "$_dir"/$_i; do
1090 [[ -e "$_f" ]] && dracut_install "$_f"
1098 [[ -d /sys/fs/cgroup/systemd ]]
1103 if [[ $UID != "0" ]]; then
1104 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1109 [[ $libdir ]] || for libdir in /lib64 /lib; do
1110 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1113 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1114 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1120 while (($# > 0)); do
1123 echo "TEST RUN: $TEST_DESCRIPTION"
1126 if [ $ret -eq 0 ]; then
1127 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1129 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1133 echo "TEST SETUP: $TEST_DESCRIPTION"
1137 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1143 echo -n "TEST: $TEST_DESCRIPTION ";
1145 test_setup && test_run
1151 ) </dev/null >test.log 2>&1
1153 if [ $ret -eq 0 ]; then
1158 echo "see $(pwd)/test.log"