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"
15 -hda $TESTDIR/rootdisk.img \
17 -net none -kernel /boot/vmlinuz-$KERNEL_VER \
18 -append "root=/dev/sda1 systemd.log_level=debug raid=noautodetect loglevel=2 init=/usr/lib/systemd/systemd ro console=ttyS0,115200n81 selinux=0 $DEBUGFAIL" || return 1
22 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root /usr/lib/systemd/systemd
25 setup_basic_environment() {
26 # create the basic filesystem layout
30 install_missing_libraries
46 generate_module_dependencies
48 ln -fs /proc/self/mounts $initdir/etc/mtab
52 instmods dm_crypt =crypto
53 type -P dmeventd >/dev/null && dracut_install dmeventd
54 inst_libdir_file "libdevmapper-event.so*"
55 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
59 # install compiled files
60 (cd $TEST_BASE_DIR/..; make DESTDIR=$initdir install)
61 # remove unneeded documentation
62 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
63 # we strip binaries since debug symbols increase binaries size a lot
64 # and it could fill the available space
68 install_missing_libraries() {
69 # install possible missing libraries
70 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
75 create_empty_image() {
76 rm -f $TESTDIR/rootdisk.img
77 # Create the blank file to use as a root filesystem
78 dd if=/dev/null of=$TESTDIR/rootdisk.img bs=1M seek=200
79 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
80 [ -b $LOOPDEV ] || return 1
81 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
82 sfdisk -C 6400 -H 2 -S 32 -L $LOOPDEV <<EOF
87 mkfs.ext3 -L systemd ${LOOPDEV}p1
90 check_result_nspawn() {
92 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
93 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
94 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
95 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
96 ls -l $TESTDIR/journal/*/*.journal
97 test -s $TESTDIR/failed && ret=$(($ret+1))
102 ddebug "Strip binaries"
103 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
107 mkdir -p $initdir/etc/rc.d
108 cat >$initdir/etc/rc.d/rc.local <<EOF
112 chmod 0755 $initdir/etc/rc.d/rc.local
116 # install any Execs from the service files
117 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
119 i=${i##Exec*=}; i=${i##-}
124 generate_module_dependencies() {
125 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
126 ! depmod -a -b "$initdir" $KERNEL_VER; then
127 dfatal "\"depmod -a $KERNEL_VER\" failed."
132 install_depmod_files() {
133 inst /lib/modules/$KERNEL_VER/modules.order
134 inst /lib/modules/$KERNEL_VER/modules.builtin
138 # install plymouth, if found... else remove plymouth service files
139 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
140 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
141 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
142 # dracut_install plymouth plymouthd
144 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
148 install_ld_so_conf() {
149 cp -a /etc/ld.so.conf* $initdir/etc
150 ldconfig -r "$initdir"
153 install_config_files() {
154 inst /etc/sysconfig/init
159 inst /etc/nsswitch.conf
164 # we want an empty environment
165 > $initdir/etc/environment
166 > $initdir/etc/machine-id
168 echo systemd-testsuite > $initdir/etc/hostname
170 cat >$initdir/etc/fstab <<EOF
171 LABEL=systemd / ext3 rw 0 1
175 install_basic_tools() {
176 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
179 install_debug_tools() {
180 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
184 # install libnss_files for login
185 inst_libdir_file "libnss_files*"
189 inst /usr/lib/systemd/system/dbus.socket
190 inst /usr/lib/systemd/system/dbus.service
193 /etc/dbus-1 -xtype f \
194 | while read file; do
204 /lib/security -xtype f \
205 | while read file; do
212 /usr/lib/kbd/keymaps/include/* \
213 /usr/lib/kbd/keymaps/i386/include/* \
214 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
215 [[ -f $i ]] || continue
222 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
223 [[ -f $i ]] || continue
229 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
230 [ -f ${_terminfodir}/l/linux ] && break
232 dracut_install -o ${_terminfodir}/l/linux
236 cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
238 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
239 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
240 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
242 # make the testsuite the default target
243 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
246 setup_nspawn_root() {
247 rm -fr $TESTDIR/nspawn-root
248 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
249 cp -ar $initdir $TESTDIR/nspawn-root
250 # we don't mount in the nspawn root
251 rm -f $TESTDIR/nspawn-root/etc/fstab
255 mkdir -p $initdir/run
256 mkdir -p $initdir/etc/systemd/system
257 mkdir -p $initdir/var/log/journal
259 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
260 if [ -L "/$d" ]; then
267 ln -sfn /run "$initdir/var/run"
268 ln -sfn /run/lock "$initdir/var/lock"
273 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
276 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
277 [[ $_line = 'not a dynamic executable' ]] && break
279 if [[ $_line =~ $_so_regex ]]; then
280 _file=${BASH_REMATCH[1]}
281 [[ -e ${initdir}/$_file ]] && continue
282 inst_library "$_file"
286 if [[ $_line =~ not\ found ]]; then
287 dfatal "Missing a shared library required by $_bin."
288 dfatal "Run \"ldd $_bin\" to find out what it is."
290 dfatal "dracut cannot create an initrd."
298 [[ -e $STATEFILE ]] && . $STATEFILE
299 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
300 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
301 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
307 initdir=$TESTDIR/root
311 ## @brief Converts numeric logging level to the first letter of level name.
313 # @param lvl Numeric logging level in range from 1 to 6.
314 # @retval 1 if @a lvl is out of range.
315 # @retval 0 if @a lvl is correct.
316 # @result Echoes first letter of level name.
329 ## @brief Internal helper function for _do_dlog()
331 # @param lvl Numeric logging level.
332 # @param msg Message.
333 # @retval 0 It's always returned, even if logging failed.
335 # @note This function is not supposed to be called manually. Please use
336 # dtrace(), ddebug(), or others instead which wrap this one.
338 # This function calls _do_dlog() either with parameter msg, or if
339 # none is given, it will read standard input and will use every line as
343 # dwarn "This is a warning"
344 # echo "This is a warning" | dwarn
348 [ -z "$LOG_LEVEL" ] && return 0
349 [ $1 -le $LOG_LEVEL ] || return 0
350 local lvl="$1"; shift
351 local lvlc=$(_lvl2char "$lvl") || return 0
353 if [ $# -ge 1 ]; then
357 echo "$lvlc: " "$line"
362 ## @brief Logs message at TRACE level (6)
364 # @param msg Message.
365 # @retval 0 It's always returned, even if logging failed.
369 [ -n "$debug" ] && set -x || :
372 ## @brief Logs message at DEBUG level (5)
374 # @param msg Message.
375 # @retval 0 It's always returned, even if logging failed.
379 # [ -n "$debug" ] && set -x || :
382 ## @brief Logs message at INFO level (4)
384 # @param msg Message.
385 # @retval 0 It's always returned, even if logging failed.
389 [ -n "$debug" ] && set -x || :
392 ## @brief Logs message at WARN level (3)
394 # @param msg Message.
395 # @retval 0 It's always returned, even if logging failed.
399 [ -n "$debug" ] && set -x || :
402 ## @brief Logs message at ERROR level (2)
404 # @param msg Message.
405 # @retval 0 It's always returned, even if logging failed.
409 # [ -n "$debug" ] && set -x || :
412 ## @brief Logs message at FATAL level (1)
414 # @param msg Message.
415 # @retval 0 It's always returned, even if logging failed.
419 [ -n "$debug" ] && set -x || :
423 # Generic substring function. If $2 is in $1, return 0.
424 strstr() { [ "${1#*$2*}" != "$1" ]; }
426 # normalize_path <path>
427 # Prints the normalized path, where it removes any duplicated
428 # and trailing slashes.
430 # $ normalize_path ///test/test//
434 set -- "${1//+(\/)//}"
439 # convert_abs_rel <from> <to>
440 # Prints the relative path, when creating a symlink to <to> from <from>.
442 # $ convert_abs_rel /usr/bin/test /bin/test-2
444 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
446 local __current __absolute __abssize __cursize __newpath
449 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
451 # corner case #1 - self looping link
452 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
454 # corner case #2 - own dir link
455 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
457 IFS="/" __current=($1)
458 IFS="/" __absolute=($2)
460 __abssize=${#__absolute[@]}
461 __cursize=${#__current[@]}
463 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
466 if (( __level > __abssize || __level > __cursize ))
472 for ((__i = __level; __i < __cursize-1; __i++))
476 __newpath=$__newpath"/"
478 __newpath=$__newpath".."
481 for ((__i = __level; __i < __abssize; __i++))
483 if [[ -n $__newpath ]]
485 __newpath=$__newpath"/"
487 __newpath=$__newpath${__absolute[__i]}
494 # Install a directory, keeping symlinks as on the original system.
495 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
496 # will create ${initdir}/lib64, ${initdir}/lib64/file,
497 # and a symlink ${initdir}/lib -> lib64.
499 [[ -e ${initdir}/"$1" ]] && return 0 # already there
501 local _dir="$1" _part="${1%/*}" _file
502 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
507 # iterate over parent directories
508 for _file in $_dir; do
509 [[ -e "${initdir}/$_file" ]] && continue
510 if [[ -L $_file ]]; then
511 inst_symlink "$_file"
514 mkdir -m 0755 -p "${initdir}/$_file" || return 1
515 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
516 chmod u+w "${initdir}/$_file"
521 # $1 = file to copy to ramdisk
522 # $2 (optional) Name for the file on the ramdisk
523 # Location of the image dir is assumed to be $initdir
524 # We never overwrite the target if it exists.
526 [[ -f "$1" ]] || return 1
527 strstr "$1" "/" || return 1
529 local _src=$1 target="${2:-$1}"
530 if ! [[ -d ${initdir}/$target ]]; then
531 [[ -e ${initdir}/$target ]] && return 0
532 [[ -L ${initdir}/$target ]] && return 0
533 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
535 # install checksum files also
536 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
537 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
539 ddebug "Installing $_src"
540 cp --sparse=always -pfL "$_src" "${initdir}/$target"
543 # find symlinks linked to given library file
545 # Function searches for symlinks by stripping version numbers appended to
546 # library filename, checks if it points to the same target and finally
547 # prints the list of symlinks to stdout.
550 # rev_lib_symlinks libfoo.so.8.1
551 # output: libfoo.so.8 libfoo.so
552 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
554 [[ ! $1 ]] && return 0
556 local fn="$1" orig="$(readlink -f "$1")" links=''
558 [[ ${fn} =~ .*\.so\..* ]] || return 1
560 until [[ ${fn##*.} == so ]]; do
562 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
568 # Same as above, but specialized to handle dynamic libraries.
569 # It handles making symlinks according to how the original library
572 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
573 strstr "$1" "/" || return 1
574 [[ -e $initdir/$_dest ]] && return 0
575 if [[ -L $_src ]]; then
576 # install checksum files also
577 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
578 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
580 _reallib=$(readlink -f "$_src")
581 inst_simple "$_reallib" "$_reallib"
582 inst_dir "${_dest%/*}"
583 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
584 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
586 inst_simple "$_src" "$_dest"
589 # Create additional symlinks. See rev_symlinks description.
590 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
591 [[ ! -e $initdir/$_symlink ]] && {
592 ddebug "Creating extra symlink: $_symlink"
593 inst_symlink $_symlink
598 # find a binary. If we were not passed the full path directly,
599 # search in the usual places to find the binary.
601 if [[ -z ${1##/*} ]]; then
602 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
611 # Same as above, but specialized to install binary executables.
612 # Install binary executable, and all shared library dependencies, if any.
615 _bin=$(find_binary "$1") || return 1
617 [[ -e $initdir/$_target ]] && return 0
618 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
620 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
622 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
623 [[ $_line = 'not a dynamic executable' ]] && break
625 if [[ $_line =~ $_so_regex ]]; then
626 _file=${BASH_REMATCH[1]}
627 [[ -e ${initdir}/$_file ]] && continue
628 inst_library "$_file"
632 if [[ $_line =~ not\ found ]]; then
633 dfatal "Missing a shared library required by $_bin."
634 dfatal "Run \"ldd $_bin\" to find out what it is."
636 dfatal "dracut cannot create an initrd."
640 inst_simple "$_bin" "$_target"
643 # same as above, except for shell scripts.
644 # If your shell script does not start with shebang, it is not a shell script.
647 _bin=$(find_binary "$1") || return 1
649 local _line _shebang_regex
650 read -r -n 80 _line <"$_bin"
651 # If debug is set, clean unprintable chars to prevent messing up the term
652 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
653 _shebang_regex='(#! *)(/[^ ]+).*'
654 [[ $_line =~ $_shebang_regex ]] || return 1
655 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
658 # same as above, but specialized for symlinks
660 local _src=$1 _target=${2:-$1} _realsrc
661 strstr "$1" "/" || return 1
662 [[ -L $1 ]] || return 1
663 [[ -L $initdir/$_target ]] && return 0
664 _realsrc=$(readlink -f "$_src")
665 if ! [[ -e $initdir/$_realsrc ]]; then
666 if [[ -d $_realsrc ]]; then
672 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
673 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
674 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
677 # attempt to install any programs specified in a udev rule
678 inst_rule_programs() {
681 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
682 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
683 if [ -x /lib/udev/$_prog ]; then
684 _bin=/lib/udev/$_prog
686 _bin=$(find_binary "$_prog") || {
687 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
692 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
693 dracut_install "$_bin"
698 # udev rules always get installed in the same place, so
699 # create a function to install them to make life simpler.
701 local _target=/etc/udev/rules.d _rule _found
703 inst_dir "/lib/udev/rules.d"
705 for _rule in "$@"; do
706 if [ "${rule#/}" = "$rule" ]; then
707 for r in /lib/udev/rules.d /etc/udev/rules.d; do
708 if [[ -f $r/$_rule ]]; then
710 inst_simple "$_found"
711 inst_rule_programs "$_found"
715 for r in '' ./ $dracutbasedir/rules.d/; do
716 if [[ -f ${r}$_rule ]]; then
718 inst_simple "$_found" "$_target/${_found##*/}"
719 inst_rule_programs "$_found"
722 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
726 # general purpose installation function
727 # Same args as above.
733 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
734 [[ $initdir = $2 ]] && set $1;;
735 3) [[ -z $initdir ]] && export initdir=$2
737 *) dfatal "inst only takes 1 or 2 or 3 arguments"
740 for _x in inst_symlink inst_script inst_binary inst_simple; do
746 # install any of listed files
748 # If first argument is '-d' and second some destination path, first accessible
749 # source is installed into this path, otherwise it will installed in the same
750 # path as source. If none of listed files was installed, function return 1.
751 # On first successful installation it returns with 0 status.
755 # inst_any -d /bin/foo /bin/bar /bin/baz
757 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
762 [[ $1 = '-d' ]] && to="$2" && shift 2
766 [[ $to ]] && inst "$f" "$to" && return 0
767 inst "$f" && return 0
774 # dracut_install [-o ] <file> [<file> ... ]
775 # Install <file> to the initramfs image
776 # -o optionally install the <file> and don't fail, if it is not there
779 if [[ $1 = '-o' ]]; then
784 if ! inst "$1" ; then
785 if [[ $_optional = yes ]]; then
786 dinfo "Skipping program $1 as it cannot be found and is" \
787 "flagged to be optional"
789 dfatal "Failed to install $1"
797 # Install a single kernel module along with any firmware it may require.
798 # $1 = full path to kernel module to install
799 install_kmod_with_fw() {
800 # no need to go further if the module is already installed
802 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
805 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
807 if [[ $omit_drivers ]]; then
811 if [[ "$_kmod" =~ $omit_drivers ]]; then
812 dinfo "Omitting driver $_kmod"
815 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
816 dinfo "Omitting driver $_kmod"
821 [ -d "$initdir/.kernelmodseen" ] && \
822 > "$initdir/.kernelmodseen/${1##*/}"
824 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
827 local _modname=${1##*/} _fwdir _found _fw
828 _modname=${_modname%.ko*}
829 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
831 for _fwdir in $fw_dir; do
832 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
833 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
837 if [[ $_found != yes ]]; then
838 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
839 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
842 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
850 # Do something with all the dependencies of a kernel module.
851 # Note that kernel modules depend on themselves using the technique we use
852 # $1 = function to call for each dependency we find
853 # It will be passed the full path to the found kernel module
854 # $2 = module to get dependencies for
855 # rest of args = arguments to modprobe
856 # _fderr specifies FD passed from surrounding scope
857 for_each_kmod_dep() {
858 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
860 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
861 while read _cmd _modpath _options; do
862 [[ $_cmd = insmod ]] || continue
863 $_func ${_modpath} || exit $?
866 [[ $_found -eq 0 ]] && exit 1
871 # filter kernel modules to install certain modules that meet specific
873 # $1 = search only in subdirectory of /kernel/$1
874 # $2 = function to call with module name to filter.
875 # This function will be passed the full path to the module to test.
876 # The behavior of this function can vary depending on whether $hostonly is set.
877 # If it is, we will only look at modules that are already in memory.
878 # If it is not, we will look at all kernel modules
879 # This function returns the full filenames of modules that match $1
880 filter_kernel_modules_by_path () (
881 local _modname _filtercmd
882 if ! [[ $hostonly ]]; then
883 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
884 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
885 _filtercmd+=' -o -name "*.ko.xz"'
886 _filtercmd+=' 2>/dev/null'
888 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
889 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
891 for _modname in $(eval $_filtercmd); do
893 *.ko) "$2" "$_modname" && echo "$_modname";;
894 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
895 $2 $initdir/$$.ko && echo "$_modname"
898 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
899 $2 $initdir/$$.ko && echo "$_modname"
905 find_kernel_modules_by_path () (
906 if ! [[ $hostonly ]]; then
907 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
908 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
910 cut -d " " -f 1 </proc/modules \
911 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
915 filter_kernel_modules () {
916 filter_kernel_modules_by_path drivers "$1"
919 find_kernel_modules () {
920 find_kernel_modules_by_path drivers
923 # instmods [-c] <kernel module> [<kernel module> ... ]
924 # instmods [-c] <kernel subsystem>
925 # install kernel modules along with all their dependencies.
926 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
928 [[ $no_kernel = yes ]] && return
929 # called [sub]functions inherit _fderr
932 if [[ $1 = '-c' ]]; then
937 function inst1mod() {
938 local _ret=0 _mod="$1"
941 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
942 ( [[ "$_mpargs" ]] && echo $_mpargs
943 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
946 ( [[ "$_mpargs" ]] && echo $_mpargs
947 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
951 --*) _mpargs+=" $_mod" ;;
952 i2o_scsi) return ;; # Do not load this diagnostic-only module
955 # if we are already installed, skip this module and go on
957 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
959 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
960 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
963 # If we are building a host-specific initramfs and this
964 # module is not already loaded, move on to the next one.
965 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
966 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
969 # We use '-d' option in modprobe only if modules prefix path
970 # differs from default '/'. This allows us to use Dracut with
971 # old version of modprobe which doesn't have '-d' option.
972 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
973 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
975 # ok, load the module, all its dependencies, and any firmware
977 for_each_kmod_dep install_kmod_with_fw $_mod \
978 --set-version $KERNEL_VER ${_moddirname} $_mpargs
985 function instmods_1() {
987 if (($# == 0)); then # filenames from stdin
989 inst1mod "${_mod%.ko*}" || {
990 if [ "$_check" = "yes" ]; then
991 dfatal "Failed to install $_mod"
997 while (($# > 0)); do # filenames as arguments
998 inst1mod ${1%.ko*} || {
999 if [ "$_check" = "yes" ]; then
1000 dfatal "Failed to install $1"
1009 local _ret _filter_not_found='FATAL: Module .* not found.'
1011 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1012 # redirections, but that would make dracut require bash4 at least.
1013 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1014 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1020 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1021 # Install a <file> located on a lib directory to the initramfs image
1022 # -n <pattern> install non-matching files
1023 inst_libdir_file() {
1024 if [[ "$1" == "-n" ]]; then
1027 for _dir in $libdirs; do
1029 for _f in "$_dir"/$_i; do
1030 [[ "$_i" =~ $_pattern ]] || continue
1031 [[ -e "$_i" ]] && dracut_install "$_i"
1036 for _dir in $libdirs; do
1038 for _f in "$_dir"/$_i; do
1039 [[ -e "$_f" ]] && dracut_install "$_f"
1047 command -v qemu-kvm &>/dev/null && [[ -c /dev/kvm ]]
1051 [[ -d /sys/fs/cgroup/systemd ]]
1056 if [[ $UID != "0" ]]; then
1057 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1062 [[ $libdir ]] || for libdir in /lib64 /lib; do
1063 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1066 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1067 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1073 while (($# > 0)); do
1076 echo "TEST RUN: $TEST_DESCRIPTION"
1079 if [ $ret -eq 0 ]; then
1080 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1082 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1086 echo "TEST SETUP: $TEST_DESCRIPTION"
1090 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1096 echo -n "TEST: $TEST_DESCRIPTION ";
1098 test_setup && test_run
1104 ) </dev/null >test.log 2>&1
1106 if [ $ret -eq 0 ]; then
1111 echo "see $(pwd)/test.log"