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 sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
11 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort"
13 function find_qemu_bin() {
14 # SUSE and Red Hat call the binary qemu-kvm
15 # Debian and Gentoo call it kvm
16 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
18 [ "$ARCH" ] || ARCH=$(uname -m)
21 # QEMU's own build system calls it qemu-system-x86_64
22 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
25 # new i386 version of QEMU
26 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
28 # i386 version of QEMU
29 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
33 if [ ! -e "$QEMU_BIN" ]; then
34 echo "Could not find a suitable QEMU binary" >&2
40 [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
41 [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
42 [ "$QEMU_SMP" ] || QEMU_SMP=1
44 find_qemu_bin || return 1
46 KERNEL_APPEND="root=/dev/sda1 \
47 systemd.log_level=debug \
50 init=/usr/lib/systemd/systemd \
57 QEMU_OPTIONS="-machine accel=kvm:tcg \
65 if [ "$INITRD" ]; then
66 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
70 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
75 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root /usr/lib/systemd/systemd $KERNEL_APPEND
78 setup_basic_environment() {
79 # create the basic filesystem layout
83 install_missing_libraries
99 generate_module_dependencies
101 ln -fs /proc/self/mounts $initdir/etc/mtab
105 instmods dm_crypt =crypto
106 type -P dmeventd >/dev/null && dracut_install dmeventd
107 inst_libdir_file "libdevmapper-event.so*"
108 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
112 # install compiled files
113 (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
114 # remove unneeded documentation
115 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
116 # we strip binaries since debug symbols increase binaries size a lot
117 # and it could fill the available space
121 install_missing_libraries() {
122 # install possible missing libraries
123 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
128 create_empty_image() {
129 rm -f "$TESTDIR/rootdisk.img"
130 # Create the blank file to use as a root filesystem
131 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
132 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
133 [ -b "$LOOPDEV" ] || return 1
134 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
135 sfdisk -C 9600 -H 2 -S 32 -L "$LOOPDEV" <<EOF
140 mkfs.ext3 -L systemd "${LOOPDEV}p1"
143 check_result_nspawn() {
145 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
146 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
147 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
148 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
149 ls -l $TESTDIR/journal/*/*.journal
150 test -s $TESTDIR/failed && ret=$(($ret+1))
155 ddebug "Strip binaries"
156 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
160 mkdir -p $initdir/etc/rc.d
161 cat >$initdir/etc/rc.d/rc.local <<EOF
165 chmod 0755 $initdir/etc/rc.d/rc.local
169 # install any Execs from the service files
170 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
172 i=${i##Exec*=}; i=${i##-}
177 generate_module_dependencies() {
178 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
179 ! depmod -a -b "$initdir" $KERNEL_VER; then
180 dfatal "\"depmod -a $KERNEL_VER\" failed."
185 install_depmod_files() {
186 inst /lib/modules/$KERNEL_VER/modules.order
187 inst /lib/modules/$KERNEL_VER/modules.builtin
191 # install plymouth, if found... else remove plymouth service files
192 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
193 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
194 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
195 # dracut_install plymouth plymouthd
197 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
201 install_ld_so_conf() {
202 cp -a /etc/ld.so.conf* $initdir/etc
203 ldconfig -r "$initdir"
206 install_config_files() {
207 inst /etc/sysconfig/init
212 inst /etc/nsswitch.conf
217 # we want an empty environment
218 > $initdir/etc/environment
219 > $initdir/etc/machine-id
221 echo systemd-testsuite > $initdir/etc/hostname
223 cat >$initdir/etc/fstab <<EOF
224 LABEL=systemd / ext3 rw 0 1
228 install_basic_tools() {
229 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
230 dracut_install -o sushell
233 install_debug_tools() {
234 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
238 # install libnss_files for login
239 inst_libdir_file "libnss_files*"
243 inst /usr/lib/systemd/system/dbus.socket
244 inst /usr/lib/systemd/system/dbus.service
247 /etc/dbus-1 -xtype f \
248 | while read file; do
258 /lib/security -xtype f \
259 | while read file; do
266 /usr/lib/kbd/keymaps/include/* \
267 /usr/lib/kbd/keymaps/i386/include/* \
268 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
269 [[ -f $i ]] || continue
276 /usr/lib/kbd/consolefonts/eurlatgr* \
277 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
278 [[ -f $i ]] || continue
284 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
285 [ -f ${_terminfodir}/l/linux ] && break
287 dracut_install -o ${_terminfodir}/l/linux
291 cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
293 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
294 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
295 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
297 # make the testsuite the default target
298 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
301 setup_nspawn_root() {
302 rm -fr $TESTDIR/nspawn-root
303 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
304 cp -ar $initdir $TESTDIR/nspawn-root
305 # we don't mount in the nspawn root
306 rm -f $TESTDIR/nspawn-root/etc/fstab
310 mkdir -p $initdir/run
311 mkdir -p $initdir/etc/systemd/system
312 mkdir -p $initdir/var/log/journal
314 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
315 if [ -L "/$d" ]; then
322 ln -sfn /run "$initdir/var/run"
323 ln -sfn /run/lock "$initdir/var/lock"
328 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
331 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
332 [[ $_line = 'not a dynamic executable' ]] && break
334 if [[ $_line =~ $_so_regex ]]; then
335 _file=${BASH_REMATCH[1]}
336 [[ -e ${initdir}/$_file ]] && continue
337 inst_library "$_file"
341 if [[ $_line =~ not\ found ]]; then
342 dfatal "Missing a shared library required by $_bin."
343 dfatal "Run \"ldd $_bin\" to find out what it is."
345 dfatal "dracut cannot create an initrd."
353 [[ -e $STATEFILE ]] && . $STATEFILE
354 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
355 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
356 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
362 initdir=$TESTDIR/root
366 ## @brief Converts numeric logging level to the first letter of level name.
368 # @param lvl Numeric logging level in range from 1 to 6.
369 # @retval 1 if @a lvl is out of range.
370 # @retval 0 if @a lvl is correct.
371 # @result Echoes first letter of level name.
384 ## @brief Internal helper function for _do_dlog()
386 # @param lvl Numeric logging level.
387 # @param msg Message.
388 # @retval 0 It's always returned, even if logging failed.
390 # @note This function is not supposed to be called manually. Please use
391 # dtrace(), ddebug(), or others instead which wrap this one.
393 # This function calls _do_dlog() either with parameter msg, or if
394 # none is given, it will read standard input and will use every line as
398 # dwarn "This is a warning"
399 # echo "This is a warning" | dwarn
403 [ -z "$LOG_LEVEL" ] && return 0
404 [ $1 -le $LOG_LEVEL ] || return 0
405 local lvl="$1"; shift
406 local lvlc=$(_lvl2char "$lvl") || return 0
408 if [ $# -ge 1 ]; then
412 echo "$lvlc: " "$line"
417 ## @brief Logs message at TRACE level (6)
419 # @param msg Message.
420 # @retval 0 It's always returned, even if logging failed.
424 [ -n "$debug" ] && set -x || :
427 ## @brief Logs message at DEBUG level (5)
429 # @param msg Message.
430 # @retval 0 It's always returned, even if logging failed.
434 # [ -n "$debug" ] && set -x || :
437 ## @brief Logs message at INFO level (4)
439 # @param msg Message.
440 # @retval 0 It's always returned, even if logging failed.
444 [ -n "$debug" ] && set -x || :
447 ## @brief Logs message at WARN level (3)
449 # @param msg Message.
450 # @retval 0 It's always returned, even if logging failed.
454 [ -n "$debug" ] && set -x || :
457 ## @brief Logs message at ERROR level (2)
459 # @param msg Message.
460 # @retval 0 It's always returned, even if logging failed.
464 # [ -n "$debug" ] && set -x || :
467 ## @brief Logs message at FATAL level (1)
469 # @param msg Message.
470 # @retval 0 It's always returned, even if logging failed.
474 [ -n "$debug" ] && set -x || :
478 # Generic substring function. If $2 is in $1, return 0.
479 strstr() { [ "${1#*$2*}" != "$1" ]; }
481 # normalize_path <path>
482 # Prints the normalized path, where it removes any duplicated
483 # and trailing slashes.
485 # $ normalize_path ///test/test//
489 set -- "${1//+(\/)//}"
494 # convert_abs_rel <from> <to>
495 # Prints the relative path, when creating a symlink to <to> from <from>.
497 # $ convert_abs_rel /usr/bin/test /bin/test-2
499 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
501 local __current __absolute __abssize __cursize __newpath
504 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
506 # corner case #1 - self looping link
507 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
509 # corner case #2 - own dir link
510 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
512 IFS="/" __current=($1)
513 IFS="/" __absolute=($2)
515 __abssize=${#__absolute[@]}
516 __cursize=${#__current[@]}
518 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
521 if (( __level > __abssize || __level > __cursize ))
527 for ((__i = __level; __i < __cursize-1; __i++))
531 __newpath=$__newpath"/"
533 __newpath=$__newpath".."
536 for ((__i = __level; __i < __abssize; __i++))
538 if [[ -n $__newpath ]]
540 __newpath=$__newpath"/"
542 __newpath=$__newpath${__absolute[__i]}
549 # Install a directory, keeping symlinks as on the original system.
550 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
551 # will create ${initdir}/lib64, ${initdir}/lib64/file,
552 # and a symlink ${initdir}/lib -> lib64.
554 [[ -e ${initdir}/"$1" ]] && return 0 # already there
556 local _dir="$1" _part="${1%/*}" _file
557 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
562 # iterate over parent directories
563 for _file in $_dir; do
564 [[ -e "${initdir}/$_file" ]] && continue
565 if [[ -L $_file ]]; then
566 inst_symlink "$_file"
569 mkdir -m 0755 -p "${initdir}/$_file" || return 1
570 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
571 chmod u+w "${initdir}/$_file"
576 # $1 = file to copy to ramdisk
577 # $2 (optional) Name for the file on the ramdisk
578 # Location of the image dir is assumed to be $initdir
579 # We never overwrite the target if it exists.
581 [[ -f "$1" ]] || return 1
582 strstr "$1" "/" || return 1
584 local _src=$1 target="${2:-$1}"
585 if ! [[ -d ${initdir}/$target ]]; then
586 [[ -e ${initdir}/$target ]] && return 0
587 [[ -L ${initdir}/$target ]] && return 0
588 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
590 # install checksum files also
591 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
592 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
594 ddebug "Installing $_src"
595 cp --sparse=always -pfL "$_src" "${initdir}/$target"
598 # find symlinks linked to given library file
600 # Function searches for symlinks by stripping version numbers appended to
601 # library filename, checks if it points to the same target and finally
602 # prints the list of symlinks to stdout.
605 # rev_lib_symlinks libfoo.so.8.1
606 # output: libfoo.so.8 libfoo.so
607 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
609 [[ ! $1 ]] && return 0
611 local fn="$1" orig="$(readlink -f "$1")" links=''
613 [[ ${fn} =~ .*\.so\..* ]] || return 1
615 until [[ ${fn##*.} == so ]]; do
617 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
623 # Same as above, but specialized to handle dynamic libraries.
624 # It handles making symlinks according to how the original library
627 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
628 strstr "$1" "/" || return 1
629 [[ -e $initdir/$_dest ]] && return 0
630 if [[ -L $_src ]]; then
631 # install checksum files also
632 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
633 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
635 _reallib=$(readlink -f "$_src")
636 inst_simple "$_reallib" "$_reallib"
637 inst_dir "${_dest%/*}"
638 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
639 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
641 inst_simple "$_src" "$_dest"
644 # Create additional symlinks. See rev_symlinks description.
645 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
646 [[ ! -e $initdir/$_symlink ]] && {
647 ddebug "Creating extra symlink: $_symlink"
648 inst_symlink $_symlink
653 # find a binary. If we were not passed the full path directly,
654 # search in the usual places to find the binary.
656 if [[ -z ${1##/*} ]]; then
657 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
666 # Same as above, but specialized to install binary executables.
667 # Install binary executable, and all shared library dependencies, if any.
670 _bin=$(find_binary "$1") || return 1
672 [[ -e $initdir/$_target ]] && return 0
673 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
675 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
677 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
678 [[ $_line = 'not a dynamic executable' ]] && break
680 if [[ $_line =~ $_so_regex ]]; then
681 _file=${BASH_REMATCH[1]}
682 [[ -e ${initdir}/$_file ]] && continue
683 inst_library "$_file"
687 if [[ $_line =~ not\ found ]]; then
688 dfatal "Missing a shared library required by $_bin."
689 dfatal "Run \"ldd $_bin\" to find out what it is."
691 dfatal "dracut cannot create an initrd."
695 inst_simple "$_bin" "$_target"
698 # same as above, except for shell scripts.
699 # If your shell script does not start with shebang, it is not a shell script.
702 _bin=$(find_binary "$1") || return 1
704 local _line _shebang_regex
705 read -r -n 80 _line <"$_bin"
706 # If debug is set, clean unprintable chars to prevent messing up the term
707 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
708 _shebang_regex='(#! *)(/[^ ]+).*'
709 [[ $_line =~ $_shebang_regex ]] || return 1
710 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
713 # same as above, but specialized for symlinks
715 local _src=$1 _target=${2:-$1} _realsrc
716 strstr "$1" "/" || return 1
717 [[ -L $1 ]] || return 1
718 [[ -L $initdir/$_target ]] && return 0
719 _realsrc=$(readlink -f "$_src")
720 if ! [[ -e $initdir/$_realsrc ]]; then
721 if [[ -d $_realsrc ]]; then
727 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
728 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
729 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
732 # attempt to install any programs specified in a udev rule
733 inst_rule_programs() {
736 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
737 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
738 if [ -x /lib/udev/$_prog ]; then
739 _bin=/lib/udev/$_prog
741 _bin=$(find_binary "$_prog") || {
742 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
747 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
748 dracut_install "$_bin"
753 # udev rules always get installed in the same place, so
754 # create a function to install them to make life simpler.
756 local _target=/etc/udev/rules.d _rule _found
758 inst_dir "/lib/udev/rules.d"
760 for _rule in "$@"; do
761 if [ "${rule#/}" = "$rule" ]; then
762 for r in /lib/udev/rules.d /etc/udev/rules.d; do
763 if [[ -f $r/$_rule ]]; then
765 inst_simple "$_found"
766 inst_rule_programs "$_found"
770 for r in '' ./ $dracutbasedir/rules.d/; do
771 if [[ -f ${r}$_rule ]]; then
773 inst_simple "$_found" "$_target/${_found##*/}"
774 inst_rule_programs "$_found"
777 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
781 # general purpose installation function
782 # Same args as above.
788 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
789 [[ $initdir = $2 ]] && set $1;;
790 3) [[ -z $initdir ]] && export initdir=$2
792 *) dfatal "inst only takes 1 or 2 or 3 arguments"
795 for _x in inst_symlink inst_script inst_binary inst_simple; do
801 # install any of listed files
803 # If first argument is '-d' and second some destination path, first accessible
804 # source is installed into this path, otherwise it will installed in the same
805 # path as source. If none of listed files was installed, function return 1.
806 # On first successful installation it returns with 0 status.
810 # inst_any -d /bin/foo /bin/bar /bin/baz
812 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
817 [[ $1 = '-d' ]] && to="$2" && shift 2
821 [[ $to ]] && inst "$f" "$to" && return 0
822 inst "$f" && return 0
829 # dracut_install [-o ] <file> [<file> ... ]
830 # Install <file> to the initramfs image
831 # -o optionally install the <file> and don't fail, if it is not there
834 if [[ $1 = '-o' ]]; then
839 if ! inst "$1" ; then
840 if [[ $_optional = yes ]]; then
841 dinfo "Skipping program $1 as it cannot be found and is" \
842 "flagged to be optional"
844 dfatal "Failed to install $1"
852 # Install a single kernel module along with any firmware it may require.
853 # $1 = full path to kernel module to install
854 install_kmod_with_fw() {
855 # no need to go further if the module is already installed
857 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
860 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
862 if [[ $omit_drivers ]]; then
866 if [[ "$_kmod" =~ $omit_drivers ]]; then
867 dinfo "Omitting driver $_kmod"
870 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
871 dinfo "Omitting driver $_kmod"
876 [ -d "$initdir/.kernelmodseen" ] && \
877 > "$initdir/.kernelmodseen/${1##*/}"
879 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
882 local _modname=${1##*/} _fwdir _found _fw
883 _modname=${_modname%.ko*}
884 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
886 for _fwdir in $fw_dir; do
887 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
888 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
892 if [[ $_found != yes ]]; then
893 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
894 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
897 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
905 # Do something with all the dependencies of a kernel module.
906 # Note that kernel modules depend on themselves using the technique we use
907 # $1 = function to call for each dependency we find
908 # It will be passed the full path to the found kernel module
909 # $2 = module to get dependencies for
910 # rest of args = arguments to modprobe
911 # _fderr specifies FD passed from surrounding scope
912 for_each_kmod_dep() {
913 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
915 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
916 while read _cmd _modpath _options; do
917 [[ $_cmd = insmod ]] || continue
918 $_func ${_modpath} || exit $?
921 [[ $_found -eq 0 ]] && exit 1
926 # filter kernel modules to install certain modules that meet specific
928 # $1 = search only in subdirectory of /kernel/$1
929 # $2 = function to call with module name to filter.
930 # This function will be passed the full path to the module to test.
931 # The behavior of this function can vary depending on whether $hostonly is set.
932 # If it is, we will only look at modules that are already in memory.
933 # If it is not, we will look at all kernel modules
934 # This function returns the full filenames of modules that match $1
935 filter_kernel_modules_by_path () (
936 local _modname _filtercmd
937 if ! [[ $hostonly ]]; then
938 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
939 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
940 _filtercmd+=' -o -name "*.ko.xz"'
941 _filtercmd+=' 2>/dev/null'
943 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
944 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
946 for _modname in $(eval $_filtercmd); do
948 *.ko) "$2" "$_modname" && echo "$_modname";;
949 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
950 $2 $initdir/$$.ko && echo "$_modname"
953 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
954 $2 $initdir/$$.ko && echo "$_modname"
960 find_kernel_modules_by_path () (
961 if ! [[ $hostonly ]]; then
962 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
963 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
965 cut -d " " -f 1 </proc/modules \
966 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
970 filter_kernel_modules () {
971 filter_kernel_modules_by_path drivers "$1"
974 find_kernel_modules () {
975 find_kernel_modules_by_path drivers
978 # instmods [-c] <kernel module> [<kernel module> ... ]
979 # instmods [-c] <kernel subsystem>
980 # install kernel modules along with all their dependencies.
981 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
983 [[ $no_kernel = yes ]] && return
984 # called [sub]functions inherit _fderr
987 if [[ $1 = '-c' ]]; then
992 function inst1mod() {
993 local _ret=0 _mod="$1"
996 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
997 ( [[ "$_mpargs" ]] && echo $_mpargs
998 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1001 ( [[ "$_mpargs" ]] && echo $_mpargs
1002 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1006 --*) _mpargs+=" $_mod" ;;
1007 i2o_scsi) return ;; # Do not load this diagnostic-only module
1010 # if we are already installed, skip this module and go on
1012 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1014 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1015 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1018 # If we are building a host-specific initramfs and this
1019 # module is not already loaded, move on to the next one.
1020 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1021 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1024 # We use '-d' option in modprobe only if modules prefix path
1025 # differs from default '/'. This allows us to use Dracut with
1026 # old version of modprobe which doesn't have '-d' option.
1027 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1028 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1030 # ok, load the module, all its dependencies, and any firmware
1032 for_each_kmod_dep install_kmod_with_fw $_mod \
1033 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1040 function instmods_1() {
1042 if (($# == 0)); then # filenames from stdin
1044 inst1mod "${_mod%.ko*}" || {
1045 if [ "$_check" = "yes" ]; then
1046 dfatal "Failed to install $_mod"
1052 while (($# > 0)); do # filenames as arguments
1053 inst1mod ${1%.ko*} || {
1054 if [ "$_check" = "yes" ]; then
1055 dfatal "Failed to install $1"
1064 local _ret _filter_not_found='FATAL: Module .* not found.'
1066 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1067 # redirections, but that would make dracut require bash4 at least.
1068 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1069 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1075 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1076 # Install a <file> located on a lib directory to the initramfs image
1077 # -n <pattern> install non-matching files
1078 inst_libdir_file() {
1079 if [[ "$1" == "-n" ]]; then
1082 for _dir in $libdirs; do
1084 for _f in "$_dir"/$_i; do
1085 [[ "$_i" =~ $_pattern ]] || continue
1086 [[ -e "$_i" ]] && dracut_install "$_i"
1091 for _dir in $libdirs; do
1093 for _f in "$_dir"/$_i; do
1094 [[ -e "$_f" ]] && dracut_install "$_f"
1102 [[ -d /sys/fs/cgroup/systemd ]]
1107 if [[ $UID != "0" ]]; then
1108 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1113 [[ $libdir ]] || for libdir in /lib64 /lib; do
1114 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1117 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1118 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1124 while (($# > 0)); do
1127 echo "TEST RUN: $TEST_DESCRIPTION"
1130 if [ $ret -eq 0 ]; then
1131 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1133 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1137 echo "TEST SETUP: $TEST_DESCRIPTION"
1141 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1147 echo -n "TEST: $TEST_DESCRIPTION ";
1149 test_setup && test_run
1155 ) </dev/null >test.log 2>&1
1157 if [ $ret -eq 0 ]; then
1162 echo "see $(pwd)/test.log"