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 hostname"
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 if [ -f /etc/machine-id ]; then
46 read MACHINE_ID < /etc/machine-id
47 [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
48 && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
49 [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
50 && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
53 [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
54 [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
55 [ "$QEMU_SMP" ] || QEMU_SMP=1
57 find_qemu_bin || return 1
59 KERNEL_APPEND="root=/dev/sda1 \
60 systemd.log_level=debug \
63 init=$ROOTLIBDIR/systemd \
70 QEMU_OPTIONS="-machine accel=kvm:tcg \
78 if [ "$INITRD" ]; then
79 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
83 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
88 ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
91 setup_basic_environment() {
92 # create the basic filesystem layout
96 install_missing_libraries
112 generate_module_dependencies
114 ln -fs /proc/self/mounts $initdir/etc/mtab
118 instmods dm_crypt =crypto
119 type -P dmeventd >/dev/null && dracut_install dmeventd
120 inst_libdir_file "libdevmapper-event.so*"
121 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
125 # install compiled files
126 (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
127 # remove unneeded documentation
128 rm -fr $initdir/usr/share/{man,doc,gtk-doc}
129 # we strip binaries since debug symbols increase binaries size a lot
130 # and it could fill the available space
134 install_missing_libraries() {
135 # install possible missing libraries
136 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
141 create_empty_image() {
142 rm -f "$TESTDIR/rootdisk.img"
143 # Create the blank file to use as a root filesystem
144 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
145 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
146 [ -b "$LOOPDEV" ] || return 1
147 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
148 sfdisk "$LOOPDEV" <<EOF
153 mkfs.ext3 -L systemd "${LOOPDEV}p1"
156 check_result_nspawn() {
158 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
159 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
160 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
161 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
162 ls -l $TESTDIR/journal/*/*.journal
163 test -s $TESTDIR/failed && ret=$(($ret+1))
168 ddebug "Strip binaries"
169 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
173 mkdir -p $initdir/etc/rc.d
174 cat >$initdir/etc/rc.d/rc.local <<EOF
178 chmod 0755 $initdir/etc/rc.d/rc.local
182 # install any Execs from the service files
183 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
185 i=${i##Exec*=}; i=${i##-}
190 generate_module_dependencies() {
191 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
192 ! depmod -a -b "$initdir" $KERNEL_VER; then
193 dfatal "\"depmod -a $KERNEL_VER\" failed."
198 install_depmod_files() {
199 inst /lib/modules/$KERNEL_VER/modules.order
200 inst /lib/modules/$KERNEL_VER/modules.builtin
204 # install plymouth, if found... else remove plymouth service files
205 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
206 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
207 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
208 # dracut_install plymouth plymouthd
210 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
214 install_ld_so_conf() {
215 cp -a /etc/ld.so.conf* $initdir/etc
216 ldconfig -r "$initdir"
219 install_config_files() {
220 inst /etc/sysconfig/init
225 inst /etc/nsswitch.conf
230 # we want an empty environment
231 > $initdir/etc/environment
232 > $initdir/etc/machine-id
234 echo systemd-testsuite > $initdir/etc/hostname
236 cat >$initdir/etc/fstab <<EOF
237 LABEL=systemd / ext3 rw 0 1
241 install_basic_tools() {
242 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
243 dracut_install -o sushell
244 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
245 dracut_install -o ldconfig.real
248 install_debug_tools() {
249 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
253 # install libnss_files for login
254 NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
255 dracut_install $NSS_LIBS
259 inst $ROOTLIBDIR/system/dbus.socket
260 inst $ROOTLIBDIR/system/dbus.service
263 /etc/dbus-1 -xtype f \
264 | while read file; do
274 /lib/security -xtype f \
275 | while read file; do
282 /usr/lib/kbd/keymaps/include/* \
283 /usr/lib/kbd/keymaps/i386/include/* \
284 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
285 [[ -f $i ]] || continue
292 /usr/lib/kbd/consolefonts/eurlatgr* \
293 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
294 [[ -f $i ]] || continue
300 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
301 [ -f ${_terminfodir}/l/linux ] && break
303 dracut_install -o ${_terminfodir}/l/linux
307 cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
308 sed "s#@SYSTEMCTL@#$(type -P systemctl)#g" $TEST_BASE_DIR/end.service.in > $initdir/etc/systemd/system/end.service
310 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
311 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
312 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
314 # make the testsuite the default target
315 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
318 setup_nspawn_root() {
319 rm -fr $TESTDIR/nspawn-root
320 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
321 cp -ar $initdir $TESTDIR/nspawn-root
322 # we don't mount in the nspawn root
323 rm -f $TESTDIR/nspawn-root/etc/fstab
327 mkdir -p $initdir/run
328 mkdir -p $initdir/etc/systemd/system
329 mkdir -p $initdir/var/log/journal
331 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
332 if [ -L "/$d" ]; then
339 ln -sfn /run "$initdir/var/run"
340 ln -sfn /run/lock "$initdir/var/lock"
345 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
348 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
349 [[ $_line = 'not a dynamic executable' ]] && break
351 if [[ $_line =~ $_so_regex ]]; then
352 _file=${BASH_REMATCH[1]}
353 [[ -e ${initdir}/$_file ]] && continue
354 inst_library "$_file"
358 if [[ $_line =~ not\ found ]]; then
359 dfatal "Missing a shared library required by $_bin."
360 dfatal "Run \"ldd $_bin\" to find out what it is."
362 dfatal "dracut cannot create an initrd."
370 [[ -e $STATEFILE ]] && . $STATEFILE
371 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
372 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
373 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
379 initdir=$TESTDIR/root
383 ## @brief Converts numeric logging level to the first letter of level name.
385 # @param lvl Numeric logging level in range from 1 to 6.
386 # @retval 1 if @a lvl is out of range.
387 # @retval 0 if @a lvl is correct.
388 # @result Echoes first letter of level name.
401 ## @brief Internal helper function for _do_dlog()
403 # @param lvl Numeric logging level.
404 # @param msg Message.
405 # @retval 0 It's always returned, even if logging failed.
407 # @note This function is not supposed to be called manually. Please use
408 # dtrace(), ddebug(), or others instead which wrap this one.
410 # This function calls _do_dlog() either with parameter msg, or if
411 # none is given, it will read standard input and will use every line as
415 # dwarn "This is a warning"
416 # echo "This is a warning" | dwarn
420 [ -z "$LOG_LEVEL" ] && return 0
421 [ $1 -le $LOG_LEVEL ] || return 0
422 local lvl="$1"; shift
423 local lvlc=$(_lvl2char "$lvl") || return 0
425 if [ $# -ge 1 ]; then
429 echo "$lvlc: " "$line"
434 ## @brief Logs message at TRACE level (6)
436 # @param msg Message.
437 # @retval 0 It's always returned, even if logging failed.
441 [ -n "$debug" ] && set -x || :
444 ## @brief Logs message at DEBUG level (5)
446 # @param msg Message.
447 # @retval 0 It's always returned, even if logging failed.
451 # [ -n "$debug" ] && set -x || :
454 ## @brief Logs message at INFO level (4)
456 # @param msg Message.
457 # @retval 0 It's always returned, even if logging failed.
461 [ -n "$debug" ] && set -x || :
464 ## @brief Logs message at WARN level (3)
466 # @param msg Message.
467 # @retval 0 It's always returned, even if logging failed.
471 [ -n "$debug" ] && set -x || :
474 ## @brief Logs message at ERROR level (2)
476 # @param msg Message.
477 # @retval 0 It's always returned, even if logging failed.
481 # [ -n "$debug" ] && set -x || :
484 ## @brief Logs message at FATAL level (1)
486 # @param msg Message.
487 # @retval 0 It's always returned, even if logging failed.
491 [ -n "$debug" ] && set -x || :
495 # Generic substring function. If $2 is in $1, return 0.
496 strstr() { [ "${1#*$2*}" != "$1" ]; }
498 # normalize_path <path>
499 # Prints the normalized path, where it removes any duplicated
500 # and trailing slashes.
502 # $ normalize_path ///test/test//
506 set -- "${1//+(\/)//}"
511 # convert_abs_rel <from> <to>
512 # Prints the relative path, when creating a symlink to <to> from <from>.
514 # $ convert_abs_rel /usr/bin/test /bin/test-2
516 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
518 local __current __absolute __abssize __cursize __newpath
521 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
523 # corner case #1 - self looping link
524 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
526 # corner case #2 - own dir link
527 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
529 IFS="/" __current=($1)
530 IFS="/" __absolute=($2)
532 __abssize=${#__absolute[@]}
533 __cursize=${#__current[@]}
535 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
538 if (( __level > __abssize || __level > __cursize ))
544 for ((__i = __level; __i < __cursize-1; __i++))
548 __newpath=$__newpath"/"
550 __newpath=$__newpath".."
553 for ((__i = __level; __i < __abssize; __i++))
555 if [[ -n $__newpath ]]
557 __newpath=$__newpath"/"
559 __newpath=$__newpath${__absolute[__i]}
566 # Install a directory, keeping symlinks as on the original system.
567 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
568 # will create ${initdir}/lib64, ${initdir}/lib64/file,
569 # and a symlink ${initdir}/lib -> lib64.
571 [[ -e ${initdir}/"$1" ]] && return 0 # already there
573 local _dir="$1" _part="${1%/*}" _file
574 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
579 # iterate over parent directories
580 for _file in $_dir; do
581 [[ -e "${initdir}/$_file" ]] && continue
582 if [[ -L $_file ]]; then
583 inst_symlink "$_file"
586 mkdir -m 0755 -p "${initdir}/$_file" || return 1
587 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
588 chmod u+w "${initdir}/$_file"
593 # $1 = file to copy to ramdisk
594 # $2 (optional) Name for the file on the ramdisk
595 # Location of the image dir is assumed to be $initdir
596 # We never overwrite the target if it exists.
598 [[ -f "$1" ]] || return 1
599 strstr "$1" "/" || return 1
601 local _src=$1 target="${2:-$1}"
602 if ! [[ -d ${initdir}/$target ]]; then
603 [[ -e ${initdir}/$target ]] && return 0
604 [[ -L ${initdir}/$target ]] && return 0
605 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
607 # install checksum files also
608 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
609 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
611 ddebug "Installing $_src"
612 cp --sparse=always -pfL "$_src" "${initdir}/$target"
615 # find symlinks linked to given library file
617 # Function searches for symlinks by stripping version numbers appended to
618 # library filename, checks if it points to the same target and finally
619 # prints the list of symlinks to stdout.
622 # rev_lib_symlinks libfoo.so.8.1
623 # output: libfoo.so.8 libfoo.so
624 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
626 [[ ! $1 ]] && return 0
628 local fn="$1" orig="$(readlink -f "$1")" links=''
630 [[ ${fn} =~ .*\.so\..* ]] || return 1
632 until [[ ${fn##*.} == so ]]; do
634 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
640 # Same as above, but specialized to handle dynamic libraries.
641 # It handles making symlinks according to how the original library
644 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
645 strstr "$1" "/" || return 1
646 [[ -e $initdir/$_dest ]] && return 0
647 if [[ -L $_src ]]; then
648 # install checksum files also
649 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
650 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
652 _reallib=$(readlink -f "$_src")
653 inst_simple "$_reallib" "$_reallib"
654 inst_dir "${_dest%/*}"
655 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
656 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
658 inst_simple "$_src" "$_dest"
661 # Create additional symlinks. See rev_symlinks description.
662 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
663 [[ ! -e $initdir/$_symlink ]] && {
664 ddebug "Creating extra symlink: $_symlink"
665 inst_symlink $_symlink
670 # find a binary. If we were not passed the full path directly,
671 # search in the usual places to find the binary.
673 if [[ -z ${1##/*} ]]; then
674 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
683 # Same as above, but specialized to install binary executables.
684 # Install binary executable, and all shared library dependencies, if any.
687 _bin=$(find_binary "$1") || return 1
689 [[ -e $initdir/$_target ]] && return 0
690 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
692 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
694 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
695 [[ $_line = 'not a dynamic executable' ]] && break
697 if [[ $_line =~ $_so_regex ]]; then
698 _file=${BASH_REMATCH[1]}
699 [[ -e ${initdir}/$_file ]] && continue
700 inst_library "$_file"
704 if [[ $_line =~ not\ found ]]; then
705 dfatal "Missing a shared library required by $_bin."
706 dfatal "Run \"ldd $_bin\" to find out what it is."
708 dfatal "dracut cannot create an initrd."
712 inst_simple "$_bin" "$_target"
715 # same as above, except for shell scripts.
716 # If your shell script does not start with shebang, it is not a shell script.
719 _bin=$(find_binary "$1") || return 1
721 local _line _shebang_regex
722 read -r -n 80 _line <"$_bin"
723 # If debug is set, clean unprintable chars to prevent messing up the term
724 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
725 _shebang_regex='(#! *)(/[^ ]+).*'
726 [[ $_line =~ $_shebang_regex ]] || return 1
727 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
730 # same as above, but specialized for symlinks
732 local _src=$1 _target=${2:-$1} _realsrc
733 strstr "$1" "/" || return 1
734 [[ -L $1 ]] || return 1
735 [[ -L $initdir/$_target ]] && return 0
736 _realsrc=$(readlink -f "$_src")
737 if ! [[ -e $initdir/$_realsrc ]]; then
738 if [[ -d $_realsrc ]]; then
744 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
745 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
746 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
749 # attempt to install any programs specified in a udev rule
750 inst_rule_programs() {
753 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
754 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
755 if [ -x /lib/udev/$_prog ]; then
756 _bin=/lib/udev/$_prog
758 _bin=$(find_binary "$_prog") || {
759 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
764 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
765 dracut_install "$_bin"
770 # udev rules always get installed in the same place, so
771 # create a function to install them to make life simpler.
773 local _target=/etc/udev/rules.d _rule _found
775 inst_dir "/lib/udev/rules.d"
777 for _rule in "$@"; do
778 if [ "${rule#/}" = "$rule" ]; then
779 for r in /lib/udev/rules.d /etc/udev/rules.d; do
780 if [[ -f $r/$_rule ]]; then
782 inst_simple "$_found"
783 inst_rule_programs "$_found"
787 for r in '' ./ $dracutbasedir/rules.d/; do
788 if [[ -f ${r}$_rule ]]; then
790 inst_simple "$_found" "$_target/${_found##*/}"
791 inst_rule_programs "$_found"
794 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
798 # general purpose installation function
799 # Same args as above.
805 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
806 [[ $initdir = $2 ]] && set $1;;
807 3) [[ -z $initdir ]] && export initdir=$2
809 *) dfatal "inst only takes 1 or 2 or 3 arguments"
812 for _x in inst_symlink inst_script inst_binary inst_simple; do
818 # install any of listed files
820 # If first argument is '-d' and second some destination path, first accessible
821 # source is installed into this path, otherwise it will installed in the same
822 # path as source. If none of listed files was installed, function return 1.
823 # On first successful installation it returns with 0 status.
827 # inst_any -d /bin/foo /bin/bar /bin/baz
829 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
834 [[ $1 = '-d' ]] && to="$2" && shift 2
838 [[ $to ]] && inst "$f" "$to" && return 0
839 inst "$f" && return 0
846 # dracut_install [-o ] <file> [<file> ... ]
847 # Install <file> to the initramfs image
848 # -o optionally install the <file> and don't fail, if it is not there
851 if [[ $1 = '-o' ]]; then
856 if ! inst "$1" ; then
857 if [[ $_optional = yes ]]; then
858 dinfo "Skipping program $1 as it cannot be found and is" \
859 "flagged to be optional"
861 dfatal "Failed to install $1"
869 # Install a single kernel module along with any firmware it may require.
870 # $1 = full path to kernel module to install
871 install_kmod_with_fw() {
872 # no need to go further if the module is already installed
874 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
877 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
879 if [[ $omit_drivers ]]; then
883 if [[ "$_kmod" =~ $omit_drivers ]]; then
884 dinfo "Omitting driver $_kmod"
887 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
888 dinfo "Omitting driver $_kmod"
893 [ -d "$initdir/.kernelmodseen" ] && \
894 > "$initdir/.kernelmodseen/${1##*/}"
896 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
899 local _modname=${1##*/} _fwdir _found _fw
900 _modname=${_modname%.ko*}
901 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
903 for _fwdir in $fw_dir; do
904 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
905 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
909 if [[ $_found != yes ]]; then
910 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
911 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
914 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
922 # Do something with all the dependencies of a kernel module.
923 # Note that kernel modules depend on themselves using the technique we use
924 # $1 = function to call for each dependency we find
925 # It will be passed the full path to the found kernel module
926 # $2 = module to get dependencies for
927 # rest of args = arguments to modprobe
928 # _fderr specifies FD passed from surrounding scope
929 for_each_kmod_dep() {
930 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
932 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
933 while read _cmd _modpath _options; do
934 [[ $_cmd = insmod ]] || continue
935 $_func ${_modpath} || exit $?
938 [[ $_found -eq 0 ]] && exit 1
943 # filter kernel modules to install certain modules that meet specific
945 # $1 = search only in subdirectory of /kernel/$1
946 # $2 = function to call with module name to filter.
947 # This function will be passed the full path to the module to test.
948 # The behavior of this function can vary depending on whether $hostonly is set.
949 # If it is, we will only look at modules that are already in memory.
950 # If it is not, we will look at all kernel modules
951 # This function returns the full filenames of modules that match $1
952 filter_kernel_modules_by_path () (
953 local _modname _filtercmd
954 if ! [[ $hostonly ]]; then
955 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
956 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
957 _filtercmd+=' -o -name "*.ko.xz"'
958 _filtercmd+=' 2>/dev/null'
960 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
961 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
963 for _modname in $(eval $_filtercmd); do
965 *.ko) "$2" "$_modname" && echo "$_modname";;
966 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
967 $2 $initdir/$$.ko && echo "$_modname"
970 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
971 $2 $initdir/$$.ko && echo "$_modname"
977 find_kernel_modules_by_path () (
978 if ! [[ $hostonly ]]; then
979 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
980 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
982 cut -d " " -f 1 </proc/modules \
983 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
987 filter_kernel_modules () {
988 filter_kernel_modules_by_path drivers "$1"
991 find_kernel_modules () {
992 find_kernel_modules_by_path drivers
995 # instmods [-c] <kernel module> [<kernel module> ... ]
996 # instmods [-c] <kernel subsystem>
997 # install kernel modules along with all their dependencies.
998 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1000 [[ $no_kernel = yes ]] && return
1001 # called [sub]functions inherit _fderr
1004 if [[ $1 = '-c' ]]; then
1009 function inst1mod() {
1010 local _ret=0 _mod="$1"
1013 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1014 ( [[ "$_mpargs" ]] && echo $_mpargs
1015 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1018 ( [[ "$_mpargs" ]] && echo $_mpargs
1019 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1023 --*) _mpargs+=" $_mod" ;;
1024 i2o_scsi) return ;; # Do not load this diagnostic-only module
1027 # if we are already installed, skip this module and go on
1029 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1031 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1032 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1035 # If we are building a host-specific initramfs and this
1036 # module is not already loaded, move on to the next one.
1037 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1038 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1041 # We use '-d' option in modprobe only if modules prefix path
1042 # differs from default '/'. This allows us to use Dracut with
1043 # old version of modprobe which doesn't have '-d' option.
1044 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1045 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1047 # ok, load the module, all its dependencies, and any firmware
1049 for_each_kmod_dep install_kmod_with_fw $_mod \
1050 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1057 function instmods_1() {
1059 if (($# == 0)); then # filenames from stdin
1061 inst1mod "${_mod%.ko*}" || {
1062 if [ "$_check" = "yes" ]; then
1063 dfatal "Failed to install $_mod"
1069 while (($# > 0)); do # filenames as arguments
1070 inst1mod ${1%.ko*} || {
1071 if [ "$_check" = "yes" ]; then
1072 dfatal "Failed to install $1"
1081 local _ret _filter_not_found='FATAL: Module .* not found.'
1083 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1084 # redirections, but that would make dracut require bash4 at least.
1085 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1086 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1092 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1093 # Install a <file> located on a lib directory to the initramfs image
1094 # -n <pattern> install non-matching files
1095 inst_libdir_file() {
1096 if [[ "$1" == "-n" ]]; then
1099 for _dir in $libdirs; do
1101 for _f in "$_dir"/$_i; do
1102 [[ "$_i" =~ $_pattern ]] || continue
1103 [[ -e "$_i" ]] && dracut_install "$_i"
1108 for _dir in $libdirs; do
1110 for _f in "$_dir"/$_i; do
1111 [[ -e "$_f" ]] && dracut_install "$_f"
1119 [[ -d /sys/fs/cgroup/systemd ]]
1124 if [[ $UID != "0" ]]; then
1125 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1130 [[ $libdir ]] || for libdir in /lib64 /lib; do
1131 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1134 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1135 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1141 while (($# > 0)); do
1144 echo "TEST RUN: $TEST_DESCRIPTION"
1147 if [ $ret -eq 0 ]; then
1148 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1150 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1154 echo "TEST SETUP: $TEST_DESCRIPTION"
1158 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1164 echo -n "TEST: $TEST_DESCRIPTION ";
1166 test_setup && test_run
1172 ) </dev/null >test.log 2>&1
1174 if [ $ret -eq 0 ]; then
1179 echo "see $(pwd)/test.log"