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 $initdir/etc/systemd/system/
297 sed "s#@SYSTEMCTL@#$(type -P systemctl)#g" $TEST_BASE_DIR/end.service.in > $initdir/etc/systemd/system/end.service
299 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
300 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
301 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
303 # make the testsuite the default target
304 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
307 setup_nspawn_root() {
308 rm -fr $TESTDIR/nspawn-root
309 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
310 cp -ar $initdir $TESTDIR/nspawn-root
311 # we don't mount in the nspawn root
312 rm -f $TESTDIR/nspawn-root/etc/fstab
316 mkdir -p $initdir/run
317 mkdir -p $initdir/etc/systemd/system
318 mkdir -p $initdir/var/log/journal
320 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
321 if [ -L "/$d" ]; then
328 ln -sfn /run "$initdir/var/run"
329 ln -sfn /run/lock "$initdir/var/lock"
334 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
337 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
338 [[ $_line = 'not a dynamic executable' ]] && break
340 if [[ $_line =~ $_so_regex ]]; then
341 _file=${BASH_REMATCH[1]}
342 [[ -e ${initdir}/$_file ]] && continue
343 inst_library "$_file"
347 if [[ $_line =~ not\ found ]]; then
348 dfatal "Missing a shared library required by $_bin."
349 dfatal "Run \"ldd $_bin\" to find out what it is."
351 dfatal "dracut cannot create an initrd."
359 [[ -e $STATEFILE ]] && . $STATEFILE
360 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
361 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
362 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
368 initdir=$TESTDIR/root
372 ## @brief Converts numeric logging level to the first letter of level name.
374 # @param lvl Numeric logging level in range from 1 to 6.
375 # @retval 1 if @a lvl is out of range.
376 # @retval 0 if @a lvl is correct.
377 # @result Echoes first letter of level name.
390 ## @brief Internal helper function for _do_dlog()
392 # @param lvl Numeric logging level.
393 # @param msg Message.
394 # @retval 0 It's always returned, even if logging failed.
396 # @note This function is not supposed to be called manually. Please use
397 # dtrace(), ddebug(), or others instead which wrap this one.
399 # This function calls _do_dlog() either with parameter msg, or if
400 # none is given, it will read standard input and will use every line as
404 # dwarn "This is a warning"
405 # echo "This is a warning" | dwarn
409 [ -z "$LOG_LEVEL" ] && return 0
410 [ $1 -le $LOG_LEVEL ] || return 0
411 local lvl="$1"; shift
412 local lvlc=$(_lvl2char "$lvl") || return 0
414 if [ $# -ge 1 ]; then
418 echo "$lvlc: " "$line"
423 ## @brief Logs message at TRACE level (6)
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 DEBUG level (5)
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 INFO level (4)
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 WARN level (3)
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 ERROR level (2)
465 # @param msg Message.
466 # @retval 0 It's always returned, even if logging failed.
470 # [ -n "$debug" ] && set -x || :
473 ## @brief Logs message at FATAL level (1)
475 # @param msg Message.
476 # @retval 0 It's always returned, even if logging failed.
480 [ -n "$debug" ] && set -x || :
484 # Generic substring function. If $2 is in $1, return 0.
485 strstr() { [ "${1#*$2*}" != "$1" ]; }
487 # normalize_path <path>
488 # Prints the normalized path, where it removes any duplicated
489 # and trailing slashes.
491 # $ normalize_path ///test/test//
495 set -- "${1//+(\/)//}"
500 # convert_abs_rel <from> <to>
501 # Prints the relative path, when creating a symlink to <to> from <from>.
503 # $ convert_abs_rel /usr/bin/test /bin/test-2
505 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
507 local __current __absolute __abssize __cursize __newpath
510 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
512 # corner case #1 - self looping link
513 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
515 # corner case #2 - own dir link
516 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
518 IFS="/" __current=($1)
519 IFS="/" __absolute=($2)
521 __abssize=${#__absolute[@]}
522 __cursize=${#__current[@]}
524 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
527 if (( __level > __abssize || __level > __cursize ))
533 for ((__i = __level; __i < __cursize-1; __i++))
537 __newpath=$__newpath"/"
539 __newpath=$__newpath".."
542 for ((__i = __level; __i < __abssize; __i++))
544 if [[ -n $__newpath ]]
546 __newpath=$__newpath"/"
548 __newpath=$__newpath${__absolute[__i]}
555 # Install a directory, keeping symlinks as on the original system.
556 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
557 # will create ${initdir}/lib64, ${initdir}/lib64/file,
558 # and a symlink ${initdir}/lib -> lib64.
560 [[ -e ${initdir}/"$1" ]] && return 0 # already there
562 local _dir="$1" _part="${1%/*}" _file
563 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
568 # iterate over parent directories
569 for _file in $_dir; do
570 [[ -e "${initdir}/$_file" ]] && continue
571 if [[ -L $_file ]]; then
572 inst_symlink "$_file"
575 mkdir -m 0755 -p "${initdir}/$_file" || return 1
576 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
577 chmod u+w "${initdir}/$_file"
582 # $1 = file to copy to ramdisk
583 # $2 (optional) Name for the file on the ramdisk
584 # Location of the image dir is assumed to be $initdir
585 # We never overwrite the target if it exists.
587 [[ -f "$1" ]] || return 1
588 strstr "$1" "/" || return 1
590 local _src=$1 target="${2:-$1}"
591 if ! [[ -d ${initdir}/$target ]]; then
592 [[ -e ${initdir}/$target ]] && return 0
593 [[ -L ${initdir}/$target ]] && return 0
594 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
596 # install checksum files also
597 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
598 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
600 ddebug "Installing $_src"
601 cp --sparse=always -pfL "$_src" "${initdir}/$target"
604 # find symlinks linked to given library file
606 # Function searches for symlinks by stripping version numbers appended to
607 # library filename, checks if it points to the same target and finally
608 # prints the list of symlinks to stdout.
611 # rev_lib_symlinks libfoo.so.8.1
612 # output: libfoo.so.8 libfoo.so
613 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
615 [[ ! $1 ]] && return 0
617 local fn="$1" orig="$(readlink -f "$1")" links=''
619 [[ ${fn} =~ .*\.so\..* ]] || return 1
621 until [[ ${fn##*.} == so ]]; do
623 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
629 # Same as above, but specialized to handle dynamic libraries.
630 # It handles making symlinks according to how the original library
633 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
634 strstr "$1" "/" || return 1
635 [[ -e $initdir/$_dest ]] && return 0
636 if [[ -L $_src ]]; then
637 # install checksum files also
638 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
639 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
641 _reallib=$(readlink -f "$_src")
642 inst_simple "$_reallib" "$_reallib"
643 inst_dir "${_dest%/*}"
644 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
645 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
647 inst_simple "$_src" "$_dest"
650 # Create additional symlinks. See rev_symlinks description.
651 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
652 [[ ! -e $initdir/$_symlink ]] && {
653 ddebug "Creating extra symlink: $_symlink"
654 inst_symlink $_symlink
659 # find a binary. If we were not passed the full path directly,
660 # search in the usual places to find the binary.
662 if [[ -z ${1##/*} ]]; then
663 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
672 # Same as above, but specialized to install binary executables.
673 # Install binary executable, and all shared library dependencies, if any.
676 _bin=$(find_binary "$1") || return 1
678 [[ -e $initdir/$_target ]] && return 0
679 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
681 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
683 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
684 [[ $_line = 'not a dynamic executable' ]] && break
686 if [[ $_line =~ $_so_regex ]]; then
687 _file=${BASH_REMATCH[1]}
688 [[ -e ${initdir}/$_file ]] && continue
689 inst_library "$_file"
693 if [[ $_line =~ not\ found ]]; then
694 dfatal "Missing a shared library required by $_bin."
695 dfatal "Run \"ldd $_bin\" to find out what it is."
697 dfatal "dracut cannot create an initrd."
701 inst_simple "$_bin" "$_target"
704 # same as above, except for shell scripts.
705 # If your shell script does not start with shebang, it is not a shell script.
708 _bin=$(find_binary "$1") || return 1
710 local _line _shebang_regex
711 read -r -n 80 _line <"$_bin"
712 # If debug is set, clean unprintable chars to prevent messing up the term
713 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
714 _shebang_regex='(#! *)(/[^ ]+).*'
715 [[ $_line =~ $_shebang_regex ]] || return 1
716 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
719 # same as above, but specialized for symlinks
721 local _src=$1 _target=${2:-$1} _realsrc
722 strstr "$1" "/" || return 1
723 [[ -L $1 ]] || return 1
724 [[ -L $initdir/$_target ]] && return 0
725 _realsrc=$(readlink -f "$_src")
726 if ! [[ -e $initdir/$_realsrc ]]; then
727 if [[ -d $_realsrc ]]; then
733 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
734 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
735 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
738 # attempt to install any programs specified in a udev rule
739 inst_rule_programs() {
742 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
743 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
744 if [ -x /lib/udev/$_prog ]; then
745 _bin=/lib/udev/$_prog
747 _bin=$(find_binary "$_prog") || {
748 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
753 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
754 dracut_install "$_bin"
759 # udev rules always get installed in the same place, so
760 # create a function to install them to make life simpler.
762 local _target=/etc/udev/rules.d _rule _found
764 inst_dir "/lib/udev/rules.d"
766 for _rule in "$@"; do
767 if [ "${rule#/}" = "$rule" ]; then
768 for r in /lib/udev/rules.d /etc/udev/rules.d; do
769 if [[ -f $r/$_rule ]]; then
771 inst_simple "$_found"
772 inst_rule_programs "$_found"
776 for r in '' ./ $dracutbasedir/rules.d/; do
777 if [[ -f ${r}$_rule ]]; then
779 inst_simple "$_found" "$_target/${_found##*/}"
780 inst_rule_programs "$_found"
783 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
787 # general purpose installation function
788 # Same args as above.
794 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
795 [[ $initdir = $2 ]] && set $1;;
796 3) [[ -z $initdir ]] && export initdir=$2
798 *) dfatal "inst only takes 1 or 2 or 3 arguments"
801 for _x in inst_symlink inst_script inst_binary inst_simple; do
807 # install any of listed files
809 # If first argument is '-d' and second some destination path, first accessible
810 # source is installed into this path, otherwise it will installed in the same
811 # path as source. If none of listed files was installed, function return 1.
812 # On first successful installation it returns with 0 status.
816 # inst_any -d /bin/foo /bin/bar /bin/baz
818 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
823 [[ $1 = '-d' ]] && to="$2" && shift 2
827 [[ $to ]] && inst "$f" "$to" && return 0
828 inst "$f" && return 0
835 # dracut_install [-o ] <file> [<file> ... ]
836 # Install <file> to the initramfs image
837 # -o optionally install the <file> and don't fail, if it is not there
840 if [[ $1 = '-o' ]]; then
845 if ! inst "$1" ; then
846 if [[ $_optional = yes ]]; then
847 dinfo "Skipping program $1 as it cannot be found and is" \
848 "flagged to be optional"
850 dfatal "Failed to install $1"
858 # Install a single kernel module along with any firmware it may require.
859 # $1 = full path to kernel module to install
860 install_kmod_with_fw() {
861 # no need to go further if the module is already installed
863 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
866 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
868 if [[ $omit_drivers ]]; then
872 if [[ "$_kmod" =~ $omit_drivers ]]; then
873 dinfo "Omitting driver $_kmod"
876 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
877 dinfo "Omitting driver $_kmod"
882 [ -d "$initdir/.kernelmodseen" ] && \
883 > "$initdir/.kernelmodseen/${1##*/}"
885 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
888 local _modname=${1##*/} _fwdir _found _fw
889 _modname=${_modname%.ko*}
890 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
892 for _fwdir in $fw_dir; do
893 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
894 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
898 if [[ $_found != yes ]]; then
899 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
900 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
903 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
911 # Do something with all the dependencies of a kernel module.
912 # Note that kernel modules depend on themselves using the technique we use
913 # $1 = function to call for each dependency we find
914 # It will be passed the full path to the found kernel module
915 # $2 = module to get dependencies for
916 # rest of args = arguments to modprobe
917 # _fderr specifies FD passed from surrounding scope
918 for_each_kmod_dep() {
919 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
921 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
922 while read _cmd _modpath _options; do
923 [[ $_cmd = insmod ]] || continue
924 $_func ${_modpath} || exit $?
927 [[ $_found -eq 0 ]] && exit 1
932 # filter kernel modules to install certain modules that meet specific
934 # $1 = search only in subdirectory of /kernel/$1
935 # $2 = function to call with module name to filter.
936 # This function will be passed the full path to the module to test.
937 # The behavior of this function can vary depending on whether $hostonly is set.
938 # If it is, we will only look at modules that are already in memory.
939 # If it is not, we will look at all kernel modules
940 # This function returns the full filenames of modules that match $1
941 filter_kernel_modules_by_path () (
942 local _modname _filtercmd
943 if ! [[ $hostonly ]]; then
944 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
945 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
946 _filtercmd+=' -o -name "*.ko.xz"'
947 _filtercmd+=' 2>/dev/null'
949 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
950 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
952 for _modname in $(eval $_filtercmd); do
954 *.ko) "$2" "$_modname" && echo "$_modname";;
955 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
956 $2 $initdir/$$.ko && echo "$_modname"
959 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
960 $2 $initdir/$$.ko && echo "$_modname"
966 find_kernel_modules_by_path () (
967 if ! [[ $hostonly ]]; then
968 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
969 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
971 cut -d " " -f 1 </proc/modules \
972 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
976 filter_kernel_modules () {
977 filter_kernel_modules_by_path drivers "$1"
980 find_kernel_modules () {
981 find_kernel_modules_by_path drivers
984 # instmods [-c] <kernel module> [<kernel module> ... ]
985 # instmods [-c] <kernel subsystem>
986 # install kernel modules along with all their dependencies.
987 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
989 [[ $no_kernel = yes ]] && return
990 # called [sub]functions inherit _fderr
993 if [[ $1 = '-c' ]]; then
998 function inst1mod() {
999 local _ret=0 _mod="$1"
1002 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1003 ( [[ "$_mpargs" ]] && echo $_mpargs
1004 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1007 ( [[ "$_mpargs" ]] && echo $_mpargs
1008 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1012 --*) _mpargs+=" $_mod" ;;
1013 i2o_scsi) return ;; # Do not load this diagnostic-only module
1016 # if we are already installed, skip this module and go on
1018 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1020 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1021 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1024 # If we are building a host-specific initramfs and this
1025 # module is not already loaded, move on to the next one.
1026 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1027 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1030 # We use '-d' option in modprobe only if modules prefix path
1031 # differs from default '/'. This allows us to use Dracut with
1032 # old version of modprobe which doesn't have '-d' option.
1033 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1034 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1036 # ok, load the module, all its dependencies, and any firmware
1038 for_each_kmod_dep install_kmod_with_fw $_mod \
1039 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1046 function instmods_1() {
1048 if (($# == 0)); then # filenames from stdin
1050 inst1mod "${_mod%.ko*}" || {
1051 if [ "$_check" = "yes" ]; then
1052 dfatal "Failed to install $_mod"
1058 while (($# > 0)); do # filenames as arguments
1059 inst1mod ${1%.ko*} || {
1060 if [ "$_check" = "yes" ]; then
1061 dfatal "Failed to install $1"
1070 local _ret _filter_not_found='FATAL: Module .* not found.'
1072 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1073 # redirections, but that would make dracut require bash4 at least.
1074 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1075 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1081 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1082 # Install a <file> located on a lib directory to the initramfs image
1083 # -n <pattern> install non-matching files
1084 inst_libdir_file() {
1085 if [[ "$1" == "-n" ]]; then
1088 for _dir in $libdirs; do
1090 for _f in "$_dir"/$_i; do
1091 [[ "$_i" =~ $_pattern ]] || continue
1092 [[ -e "$_i" ]] && dracut_install "$_i"
1097 for _dir in $libdirs; do
1099 for _f in "$_dir"/$_i; do
1100 [[ -e "$_f" ]] && dracut_install "$_f"
1108 [[ -d /sys/fs/cgroup/systemd ]]
1113 if [[ $UID != "0" ]]; then
1114 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1119 [[ $libdir ]] || for libdir in /lib64 /lib; do
1120 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1123 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1124 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1130 while (($# > 0)); do
1133 echo "TEST RUN: $TEST_DESCRIPTION"
1136 if [ $ret -eq 0 ]; then
1137 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1139 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1143 echo "TEST SETUP: $TEST_DESCRIPTION"
1147 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1153 echo -n "TEST: $TEST_DESCRIPTION ";
1155 test_setup && test_run
1161 ) </dev/null >test.log 2>&1
1163 if [ $ret -eq 0 ]; then
1168 echo "see $(pwd)/test.log"