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 if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
11 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
12 ROOTLIBDIR=/usr/lib/systemd
15 BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
16 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort"
18 function find_qemu_bin() {
19 # SUSE and Red Hat call the binary qemu-kvm
20 # Debian and Gentoo call it kvm
21 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
23 [ "$ARCH" ] || ARCH=$(uname -m)
26 # QEMU's own build system calls it qemu-system-x86_64
27 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
30 # new i386 version of QEMU
31 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
33 # i386 version of QEMU
34 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
38 if [ ! -e "$QEMU_BIN" ]; then
39 echo "Could not find a suitable QEMU binary" >&2
45 [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
46 [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
47 [ "$QEMU_SMP" ] || QEMU_SMP=1
49 find_qemu_bin || return 1
51 KERNEL_APPEND="root=/dev/sda1 \
52 systemd.log_level=debug \
55 init=$ROOTLIBDIR/systemd \
62 QEMU_OPTIONS="-machine accel=kvm:tcg \
70 if [ "$INITRD" ]; then
71 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
75 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
80 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
83 setup_basic_environment() {
84 # create the basic filesystem layout
88 install_missing_libraries
104 generate_module_dependencies
106 ln -fs /proc/self/mounts $initdir/etc/mtab
110 instmods dm_crypt =crypto
111 type -P dmeventd >/dev/null && dracut_install dmeventd
112 inst_libdir_file "libdevmapper-event.so*"
113 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
117 # install compiled files
118 (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
119 # remove unneeded documentation
120 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
121 # we strip binaries since debug symbols increase binaries size a lot
122 # and it could fill the available space
126 install_missing_libraries() {
127 # install possible missing libraries
128 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
133 create_empty_image() {
134 rm -f "$TESTDIR/rootdisk.img"
135 # Create the blank file to use as a root filesystem
136 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
137 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
138 [ -b "$LOOPDEV" ] || return 1
139 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
140 sfdisk -C 9600 -H 2 -S 32 -L "$LOOPDEV" <<EOF
145 mkfs.ext3 -L systemd "${LOOPDEV}p1"
148 check_result_nspawn() {
150 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
151 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
152 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
153 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
154 ls -l $TESTDIR/journal/*/*.journal
155 test -s $TESTDIR/failed && ret=$(($ret+1))
160 ddebug "Strip binaries"
161 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
165 mkdir -p $initdir/etc/rc.d
166 cat >$initdir/etc/rc.d/rc.local <<EOF
170 chmod 0755 $initdir/etc/rc.d/rc.local
174 # install any Execs from the service files
175 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
177 i=${i##Exec*=}; i=${i##-}
182 generate_module_dependencies() {
183 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
184 ! depmod -a -b "$initdir" $KERNEL_VER; then
185 dfatal "\"depmod -a $KERNEL_VER\" failed."
190 install_depmod_files() {
191 inst /lib/modules/$KERNEL_VER/modules.order
192 inst /lib/modules/$KERNEL_VER/modules.builtin
196 # install plymouth, if found... else remove plymouth service files
197 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
198 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
199 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
200 # dracut_install plymouth plymouthd
202 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
206 install_ld_so_conf() {
207 cp -a /etc/ld.so.conf* $initdir/etc
208 ldconfig -r "$initdir"
211 install_config_files() {
212 inst /etc/sysconfig/init
217 inst /etc/nsswitch.conf
222 # we want an empty environment
223 > $initdir/etc/environment
224 > $initdir/etc/machine-id
226 echo systemd-testsuite > $initdir/etc/hostname
228 cat >$initdir/etc/fstab <<EOF
229 LABEL=systemd / ext3 rw 0 1
233 install_basic_tools() {
234 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
235 dracut_install -o sushell
238 install_debug_tools() {
239 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
243 # install libnss_files for login
244 inst_libdir_file "libnss_files*"
248 inst $ROOTLIBDIR/system/dbus.socket
249 inst $ROOTLIBDIR/system/dbus.service
252 /etc/dbus-1 -xtype f \
253 | while read file; do
263 /lib/security -xtype f \
264 | while read file; do
271 /usr/lib/kbd/keymaps/include/* \
272 /usr/lib/kbd/keymaps/i386/include/* \
273 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
274 [[ -f $i ]] || continue
281 /usr/lib/kbd/consolefonts/eurlatgr* \
282 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
283 [[ -f $i ]] || continue
289 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
290 [ -f ${_terminfodir}/l/linux ] && break
292 dracut_install -o ${_terminfodir}/l/linux
296 cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
298 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
299 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
300 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
302 # make the testsuite the default target
303 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
306 setup_nspawn_root() {
307 rm -fr $TESTDIR/nspawn-root
308 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
309 cp -ar $initdir $TESTDIR/nspawn-root
310 # we don't mount in the nspawn root
311 rm -f $TESTDIR/nspawn-root/etc/fstab
315 mkdir -p $initdir/run
316 mkdir -p $initdir/etc/systemd/system
317 mkdir -p $initdir/var/log/journal
319 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
320 if [ -L "/$d" ]; then
327 ln -sfn /run "$initdir/var/run"
328 ln -sfn /run/lock "$initdir/var/lock"
333 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
336 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
337 [[ $_line = 'not a dynamic executable' ]] && break
339 if [[ $_line =~ $_so_regex ]]; then
340 _file=${BASH_REMATCH[1]}
341 [[ -e ${initdir}/$_file ]] && continue
342 inst_library "$_file"
346 if [[ $_line =~ not\ found ]]; then
347 dfatal "Missing a shared library required by $_bin."
348 dfatal "Run \"ldd $_bin\" to find out what it is."
350 dfatal "dracut cannot create an initrd."
358 [[ -e $STATEFILE ]] && . $STATEFILE
359 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
360 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
361 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
367 initdir=$TESTDIR/root
371 ## @brief Converts numeric logging level to the first letter of level name.
373 # @param lvl Numeric logging level in range from 1 to 6.
374 # @retval 1 if @a lvl is out of range.
375 # @retval 0 if @a lvl is correct.
376 # @result Echoes first letter of level name.
389 ## @brief Internal helper function for _do_dlog()
391 # @param lvl Numeric logging level.
392 # @param msg Message.
393 # @retval 0 It's always returned, even if logging failed.
395 # @note This function is not supposed to be called manually. Please use
396 # dtrace(), ddebug(), or others instead which wrap this one.
398 # This function calls _do_dlog() either with parameter msg, or if
399 # none is given, it will read standard input and will use every line as
403 # dwarn "This is a warning"
404 # echo "This is a warning" | dwarn
408 [ -z "$LOG_LEVEL" ] && return 0
409 [ $1 -le $LOG_LEVEL ] || return 0
410 local lvl="$1"; shift
411 local lvlc=$(_lvl2char "$lvl") || return 0
413 if [ $# -ge 1 ]; then
417 echo "$lvlc: " "$line"
422 ## @brief Logs message at TRACE level (6)
424 # @param msg Message.
425 # @retval 0 It's always returned, even if logging failed.
429 [ -n "$debug" ] && set -x || :
432 ## @brief Logs message at DEBUG level (5)
434 # @param msg Message.
435 # @retval 0 It's always returned, even if logging failed.
439 # [ -n "$debug" ] && set -x || :
442 ## @brief Logs message at INFO level (4)
444 # @param msg Message.
445 # @retval 0 It's always returned, even if logging failed.
449 [ -n "$debug" ] && set -x || :
452 ## @brief Logs message at WARN level (3)
454 # @param msg Message.
455 # @retval 0 It's always returned, even if logging failed.
459 [ -n "$debug" ] && set -x || :
462 ## @brief Logs message at ERROR level (2)
464 # @param msg Message.
465 # @retval 0 It's always returned, even if logging failed.
469 # [ -n "$debug" ] && set -x || :
472 ## @brief Logs message at FATAL level (1)
474 # @param msg Message.
475 # @retval 0 It's always returned, even if logging failed.
479 [ -n "$debug" ] && set -x || :
483 # Generic substring function. If $2 is in $1, return 0.
484 strstr() { [ "${1#*$2*}" != "$1" ]; }
486 # normalize_path <path>
487 # Prints the normalized path, where it removes any duplicated
488 # and trailing slashes.
490 # $ normalize_path ///test/test//
494 set -- "${1//+(\/)//}"
499 # convert_abs_rel <from> <to>
500 # Prints the relative path, when creating a symlink to <to> from <from>.
502 # $ convert_abs_rel /usr/bin/test /bin/test-2
504 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
506 local __current __absolute __abssize __cursize __newpath
509 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
511 # corner case #1 - self looping link
512 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
514 # corner case #2 - own dir link
515 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
517 IFS="/" __current=($1)
518 IFS="/" __absolute=($2)
520 __abssize=${#__absolute[@]}
521 __cursize=${#__current[@]}
523 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
526 if (( __level > __abssize || __level > __cursize ))
532 for ((__i = __level; __i < __cursize-1; __i++))
536 __newpath=$__newpath"/"
538 __newpath=$__newpath".."
541 for ((__i = __level; __i < __abssize; __i++))
543 if [[ -n $__newpath ]]
545 __newpath=$__newpath"/"
547 __newpath=$__newpath${__absolute[__i]}
554 # Install a directory, keeping symlinks as on the original system.
555 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
556 # will create ${initdir}/lib64, ${initdir}/lib64/file,
557 # and a symlink ${initdir}/lib -> lib64.
559 [[ -e ${initdir}/"$1" ]] && return 0 # already there
561 local _dir="$1" _part="${1%/*}" _file
562 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
567 # iterate over parent directories
568 for _file in $_dir; do
569 [[ -e "${initdir}/$_file" ]] && continue
570 if [[ -L $_file ]]; then
571 inst_symlink "$_file"
574 mkdir -m 0755 -p "${initdir}/$_file" || return 1
575 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
576 chmod u+w "${initdir}/$_file"
581 # $1 = file to copy to ramdisk
582 # $2 (optional) Name for the file on the ramdisk
583 # Location of the image dir is assumed to be $initdir
584 # We never overwrite the target if it exists.
586 [[ -f "$1" ]] || return 1
587 strstr "$1" "/" || return 1
589 local _src=$1 target="${2:-$1}"
590 if ! [[ -d ${initdir}/$target ]]; then
591 [[ -e ${initdir}/$target ]] && return 0
592 [[ -L ${initdir}/$target ]] && return 0
593 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
595 # install checksum files also
596 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
597 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
599 ddebug "Installing $_src"
600 cp --sparse=always -pfL "$_src" "${initdir}/$target"
603 # find symlinks linked to given library file
605 # Function searches for symlinks by stripping version numbers appended to
606 # library filename, checks if it points to the same target and finally
607 # prints the list of symlinks to stdout.
610 # rev_lib_symlinks libfoo.so.8.1
611 # output: libfoo.so.8 libfoo.so
612 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
614 [[ ! $1 ]] && return 0
616 local fn="$1" orig="$(readlink -f "$1")" links=''
618 [[ ${fn} =~ .*\.so\..* ]] || return 1
620 until [[ ${fn##*.} == so ]]; do
622 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
628 # Same as above, but specialized to handle dynamic libraries.
629 # It handles making symlinks according to how the original library
632 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
633 strstr "$1" "/" || return 1
634 [[ -e $initdir/$_dest ]] && return 0
635 if [[ -L $_src ]]; then
636 # install checksum files also
637 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
638 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
640 _reallib=$(readlink -f "$_src")
641 inst_simple "$_reallib" "$_reallib"
642 inst_dir "${_dest%/*}"
643 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
644 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
646 inst_simple "$_src" "$_dest"
649 # Create additional symlinks. See rev_symlinks description.
650 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
651 [[ ! -e $initdir/$_symlink ]] && {
652 ddebug "Creating extra symlink: $_symlink"
653 inst_symlink $_symlink
658 # find a binary. If we were not passed the full path directly,
659 # search in the usual places to find the binary.
661 if [[ -z ${1##/*} ]]; then
662 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
671 # Same as above, but specialized to install binary executables.
672 # Install binary executable, and all shared library dependencies, if any.
675 _bin=$(find_binary "$1") || return 1
677 [[ -e $initdir/$_target ]] && return 0
678 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
680 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
682 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
683 [[ $_line = 'not a dynamic executable' ]] && break
685 if [[ $_line =~ $_so_regex ]]; then
686 _file=${BASH_REMATCH[1]}
687 [[ -e ${initdir}/$_file ]] && continue
688 inst_library "$_file"
692 if [[ $_line =~ not\ found ]]; then
693 dfatal "Missing a shared library required by $_bin."
694 dfatal "Run \"ldd $_bin\" to find out what it is."
696 dfatal "dracut cannot create an initrd."
700 inst_simple "$_bin" "$_target"
703 # same as above, except for shell scripts.
704 # If your shell script does not start with shebang, it is not a shell script.
707 _bin=$(find_binary "$1") || return 1
709 local _line _shebang_regex
710 read -r -n 80 _line <"$_bin"
711 # If debug is set, clean unprintable chars to prevent messing up the term
712 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
713 _shebang_regex='(#! *)(/[^ ]+).*'
714 [[ $_line =~ $_shebang_regex ]] || return 1
715 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
718 # same as above, but specialized for symlinks
720 local _src=$1 _target=${2:-$1} _realsrc
721 strstr "$1" "/" || return 1
722 [[ -L $1 ]] || return 1
723 [[ -L $initdir/$_target ]] && return 0
724 _realsrc=$(readlink -f "$_src")
725 if ! [[ -e $initdir/$_realsrc ]]; then
726 if [[ -d $_realsrc ]]; then
732 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
733 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
734 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
737 # attempt to install any programs specified in a udev rule
738 inst_rule_programs() {
741 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
742 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
743 if [ -x /lib/udev/$_prog ]; then
744 _bin=/lib/udev/$_prog
746 _bin=$(find_binary "$_prog") || {
747 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
752 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
753 dracut_install "$_bin"
758 # udev rules always get installed in the same place, so
759 # create a function to install them to make life simpler.
761 local _target=/etc/udev/rules.d _rule _found
763 inst_dir "/lib/udev/rules.d"
765 for _rule in "$@"; do
766 if [ "${rule#/}" = "$rule" ]; then
767 for r in /lib/udev/rules.d /etc/udev/rules.d; do
768 if [[ -f $r/$_rule ]]; then
770 inst_simple "$_found"
771 inst_rule_programs "$_found"
775 for r in '' ./ $dracutbasedir/rules.d/; do
776 if [[ -f ${r}$_rule ]]; then
778 inst_simple "$_found" "$_target/${_found##*/}"
779 inst_rule_programs "$_found"
782 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
786 # general purpose installation function
787 # Same args as above.
793 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
794 [[ $initdir = $2 ]] && set $1;;
795 3) [[ -z $initdir ]] && export initdir=$2
797 *) dfatal "inst only takes 1 or 2 or 3 arguments"
800 for _x in inst_symlink inst_script inst_binary inst_simple; do
806 # install any of listed files
808 # If first argument is '-d' and second some destination path, first accessible
809 # source is installed into this path, otherwise it will installed in the same
810 # path as source. If none of listed files was installed, function return 1.
811 # On first successful installation it returns with 0 status.
815 # inst_any -d /bin/foo /bin/bar /bin/baz
817 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
822 [[ $1 = '-d' ]] && to="$2" && shift 2
826 [[ $to ]] && inst "$f" "$to" && return 0
827 inst "$f" && return 0
834 # dracut_install [-o ] <file> [<file> ... ]
835 # Install <file> to the initramfs image
836 # -o optionally install the <file> and don't fail, if it is not there
839 if [[ $1 = '-o' ]]; then
844 if ! inst "$1" ; then
845 if [[ $_optional = yes ]]; then
846 dinfo "Skipping program $1 as it cannot be found and is" \
847 "flagged to be optional"
849 dfatal "Failed to install $1"
857 # Install a single kernel module along with any firmware it may require.
858 # $1 = full path to kernel module to install
859 install_kmod_with_fw() {
860 # no need to go further if the module is already installed
862 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
865 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
867 if [[ $omit_drivers ]]; then
871 if [[ "$_kmod" =~ $omit_drivers ]]; then
872 dinfo "Omitting driver $_kmod"
875 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
876 dinfo "Omitting driver $_kmod"
881 [ -d "$initdir/.kernelmodseen" ] && \
882 > "$initdir/.kernelmodseen/${1##*/}"
884 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
887 local _modname=${1##*/} _fwdir _found _fw
888 _modname=${_modname%.ko*}
889 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
891 for _fwdir in $fw_dir; do
892 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
893 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
897 if [[ $_found != yes ]]; then
898 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
899 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
902 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
910 # Do something with all the dependencies of a kernel module.
911 # Note that kernel modules depend on themselves using the technique we use
912 # $1 = function to call for each dependency we find
913 # It will be passed the full path to the found kernel module
914 # $2 = module to get dependencies for
915 # rest of args = arguments to modprobe
916 # _fderr specifies FD passed from surrounding scope
917 for_each_kmod_dep() {
918 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
920 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
921 while read _cmd _modpath _options; do
922 [[ $_cmd = insmod ]] || continue
923 $_func ${_modpath} || exit $?
926 [[ $_found -eq 0 ]] && exit 1
931 # filter kernel modules to install certain modules that meet specific
933 # $1 = search only in subdirectory of /kernel/$1
934 # $2 = function to call with module name to filter.
935 # This function will be passed the full path to the module to test.
936 # The behavior of this function can vary depending on whether $hostonly is set.
937 # If it is, we will only look at modules that are already in memory.
938 # If it is not, we will look at all kernel modules
939 # This function returns the full filenames of modules that match $1
940 filter_kernel_modules_by_path () (
941 local _modname _filtercmd
942 if ! [[ $hostonly ]]; then
943 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
944 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
945 _filtercmd+=' -o -name "*.ko.xz"'
946 _filtercmd+=' 2>/dev/null'
948 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
949 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
951 for _modname in $(eval $_filtercmd); do
953 *.ko) "$2" "$_modname" && echo "$_modname";;
954 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
955 $2 $initdir/$$.ko && echo "$_modname"
958 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
959 $2 $initdir/$$.ko && echo "$_modname"
965 find_kernel_modules_by_path () (
966 if ! [[ $hostonly ]]; then
967 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
968 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
970 cut -d " " -f 1 </proc/modules \
971 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
975 filter_kernel_modules () {
976 filter_kernel_modules_by_path drivers "$1"
979 find_kernel_modules () {
980 find_kernel_modules_by_path drivers
983 # instmods [-c] <kernel module> [<kernel module> ... ]
984 # instmods [-c] <kernel subsystem>
985 # install kernel modules along with all their dependencies.
986 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
988 [[ $no_kernel = yes ]] && return
989 # called [sub]functions inherit _fderr
992 if [[ $1 = '-c' ]]; then
997 function inst1mod() {
998 local _ret=0 _mod="$1"
1001 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1002 ( [[ "$_mpargs" ]] && echo $_mpargs
1003 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1006 ( [[ "$_mpargs" ]] && echo $_mpargs
1007 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1011 --*) _mpargs+=" $_mod" ;;
1012 i2o_scsi) return ;; # Do not load this diagnostic-only module
1015 # if we are already installed, skip this module and go on
1017 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1019 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1020 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1023 # If we are building a host-specific initramfs and this
1024 # module is not already loaded, move on to the next one.
1025 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1026 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1029 # We use '-d' option in modprobe only if modules prefix path
1030 # differs from default '/'. This allows us to use Dracut with
1031 # old version of modprobe which doesn't have '-d' option.
1032 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1033 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1035 # ok, load the module, all its dependencies, and any firmware
1037 for_each_kmod_dep install_kmod_with_fw $_mod \
1038 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1045 function instmods_1() {
1047 if (($# == 0)); then # filenames from stdin
1049 inst1mod "${_mod%.ko*}" || {
1050 if [ "$_check" = "yes" ]; then
1051 dfatal "Failed to install $_mod"
1057 while (($# > 0)); do # filenames as arguments
1058 inst1mod ${1%.ko*} || {
1059 if [ "$_check" = "yes" ]; then
1060 dfatal "Failed to install $1"
1069 local _ret _filter_not_found='FATAL: Module .* not found.'
1071 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1072 # redirections, but that would make dracut require bash4 at least.
1073 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1074 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1080 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1081 # Install a <file> located on a lib directory to the initramfs image
1082 # -n <pattern> install non-matching files
1083 inst_libdir_file() {
1084 if [[ "$1" == "-n" ]]; then
1087 for _dir in $libdirs; do
1089 for _f in "$_dir"/$_i; do
1090 [[ "$_i" =~ $_pattern ]] || continue
1091 [[ -e "$_i" ]] && dracut_install "$_i"
1096 for _dir in $libdirs; do
1098 for _f in "$_dir"/$_i; do
1099 [[ -e "$_f" ]] && dracut_install "$_f"
1107 [[ -d /sys/fs/cgroup/systemd ]]
1112 if [[ $UID != "0" ]]; then
1113 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1118 [[ $libdir ]] || for libdir in /lib64 /lib; do
1119 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1122 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1123 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1129 while (($# > 0)); do
1132 echo "TEST RUN: $TEST_DESCRIPTION"
1135 if [ $ret -eq 0 ]; then
1136 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1138 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1142 echo "TEST SETUP: $TEST_DESCRIPTION"
1146 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1152 echo -n "TEST: $TEST_DESCRIPTION ";
1154 test_setup && test_run
1160 ) </dev/null >test.log 2>&1
1162 if [ $ret -eq 0 ]; then
1167 echo "see $(pwd)/test.log"