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 LOOKS_LIKE_DEBIAN
=$
(source /etc
/os-release
&& [[ "$ID" = "debian" ||
"$ID_LIKE" = "debian" ]] && echo yes)
8 KERNEL_VER
=${KERNEL_VER-$(uname -r)}
9 KERNEL_MODS
="/lib/modules/$KERNEL_VER/"
11 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
12 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
13 ROOTLIBDIR
=/usr
/lib
/systemd
16 BASICTOOLS
="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
17 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"
19 function find_qemu_bin
() {
20 # SUSE and Red Hat call the binary qemu-kvm
21 # Debian and Gentoo call it kvm
22 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
24 [ "$ARCH" ] || ARCH
=$
(uname
-m)
27 # QEMU's own build system calls it qemu-system-x86_64
28 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
31 # new i386 version of QEMU
32 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
34 # i386 version of QEMU
35 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
39 if [ ! -e "$QEMU_BIN" ]; then
40 echo "Could not find a suitable QEMU binary" >&2
46 if [ -f /etc
/machine-id
]; then
47 read MACHINE_ID
< /etc
/machine-id
48 [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
49 && INITRD
="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
50 [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
51 && KERNEL_BIN
="/boot/$MACHINE_ID/$KERNEL_VER/linux"
54 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
55 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
56 [ "$KERNEL_BIN" ] || KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
57 [ "$INITRD" ] ||
{ [ -e "$default_fedora_initrd" ] && INITRD
=$default_fedora_initrd; }
58 [ "$INITRD" ] ||
{ [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD
=$default_debian_initrd; }
59 [ "$QEMU_SMP" ] || QEMU_SMP
=1
61 find_qemu_bin ||
return 1
63 KERNEL_APPEND
="root=/dev/sda1 \
64 systemd.log_level=debug \
67 init=$ROOTLIBDIR/systemd \
74 QEMU_OPTIONS
="-smp $QEMU_SMP \
81 if [ "$INITRD" ]; then
82 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
85 if [ -c /dev
/kvm
]; then
86 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
90 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img
) ||
return 1
95 ..
/..
/systemd-nspawn
--register=no
--boot --directory=$TESTDIR/nspawn-root
$ROOTLIBDIR/systemd
$KERNEL_APPEND
98 setup_basic_environment
() {
99 # create the basic filesystem layout
103 install_missing_libraries
119 generate_module_dependencies
121 ln -fs /proc
/self
/mounts
$initdir/etc
/mtab
125 instmods dm_crypt
=crypto
126 type -P dmeventd
>/dev
/null
&& dracut_install dmeventd
127 inst_libdir_file
"libdevmapper-event.so*"
128 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
132 # install compiled files
133 (cd $TEST_BASE_DIR/..
; set -x; make DESTDIR
=$initdir install)
134 # remove unneeded documentation
135 rm -fr $initdir/usr
/share
/{man
,doc
}
136 # we strip binaries since debug symbols increase binaries size a lot
137 # and it could fill the available space
141 install_missing_libraries
() {
142 # install possible missing libraries
143 for i
in $initdir/{sbin
,bin
}/* $initdir/lib
/systemd
/*; do
148 create_empty_image
() {
149 rm -f "$TESTDIR/rootdisk.img"
150 # Create the blank file to use as a root filesystem
151 dd if=/dev
/null of
="$TESTDIR/rootdisk.img" bs
=1M seek
=400
152 LOOPDEV
=$
(losetup
--show -P -f $TESTDIR/rootdisk.img
)
153 [ -b "$LOOPDEV" ] ||
return 1
154 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
155 sfdisk
"$LOOPDEV" <<EOF
160 mkfs.ext3
-L systemd
"${LOOPDEV}p1"
163 check_result_nspawn
() {
165 [[ -e $TESTDIR/nspawn-root
/testok
]] && ret
=0
166 [[ -f $TESTDIR/nspawn-root
/failed
]] && cp -a $TESTDIR/nspawn-root
/failed
$TESTDIR
167 cp -a $TESTDIR/nspawn-root
/var
/log
/journal
$TESTDIR
168 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
169 ls -l $TESTDIR/journal
/*/*.journal
170 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
175 ddebug
"Strip binaries"
176 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f |
xargs strip
--strip-unneeded | ddebug
180 mkdir
-p $initdir/etc
/rc.d
181 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
185 chmod 0755 $initdir/etc
/rc.d
/rc.
local
189 ddebug
"install any Execs from the service files"
191 export PKG_CONFIG_PATH
=$TEST_BASE_DIR/..
/src
/core
/
192 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
193 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
194 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
196 i
=${i##Exec*=}; i
=${i##-}
202 generate_module_dependencies
() {
203 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
204 ! depmod
-a -b "$initdir" $KERNEL_VER; then
205 dfatal
"\"depmod -a $KERNEL_VER\" failed."
210 install_depmod_files
() {
211 inst
/lib
/modules
/$KERNEL_VER/modules.order
212 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
216 # install plymouth, if found... else remove plymouth service files
217 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
218 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
219 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
220 # dracut_install plymouth plymouthd
222 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
226 install_ld_so_conf
() {
227 cp -a /etc
/ld.so.conf
* $initdir/etc
228 ldconfig
-r "$initdir"
231 install_config_files
() {
232 inst
/etc
/sysconfig
/init
238 inst
/etc
/nsswitch.conf
243 # we want an empty environment
244 > $initdir/etc
/environment
245 > $initdir/etc
/machine-id
247 echo systemd-testsuite
> $initdir/etc
/hostname
249 cat >$initdir/etc
/fstab
<<EOF
250 LABEL=systemd / ext3 rw 0 1
254 install_basic_tools
() {
255 [[ $BASICTOOLS ]] && dracut_install
$BASICTOOLS
256 dracut_install
-o sushell
257 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
258 dracut_install
-o ldconfig.real
261 install_debug_tools
() {
262 [[ $DEBUGTOOLS ]] && dracut_install
$DEBUGTOOLS
266 # install libnss_files for login
267 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
268 dracut_install
$NSS_LIBS
272 inst
$ROOTLIBDIR/system
/dbus.socket
273 inst
$ROOTLIBDIR/system
/dbus.service
276 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
277 |
while read file; do
284 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
&& find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
289 /lib
/security
-xtype f \
290 ) |
while read file; do
294 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
295 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
300 /usr
/lib
/kbd
/keymaps
/include
/* \
301 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
302 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
*; do
303 [[ -f $i ]] ||
continue
310 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
311 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
312 [[ -f $i ]] ||
continue
318 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
319 [ -f ${_terminfodir}/l
/linux
] && break
321 dracut_install
-o ${_terminfodir}/l
/linux
325 cp $TEST_BASE_DIR/testsuite.target
$initdir/etc
/systemd
/system
/
326 cp $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/
328 mkdir
-p $initdir/etc
/systemd
/system
/testsuite.target.wants
329 ln -fs $TEST_BASE_DIR/testsuite.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/testsuite.service
330 ln -fs $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/end.service
332 # make the testsuite the default target
333 ln -fs testsuite.target
$initdir/etc
/systemd
/system
/default.target
336 setup_nspawn_root
() {
337 rm -fr $TESTDIR/nspawn-root
338 ddebug
"cp -ar $initdir $TESTDIR/nspawn-root"
339 cp -ar $initdir $TESTDIR/nspawn-root
340 # we don't mount in the nspawn root
341 rm -f $TESTDIR/nspawn-root
/etc
/fstab
345 mkdir
-p $initdir/run
346 mkdir
-p $initdir/etc
/systemd
/system
347 mkdir
-p $initdir/var
/log
/journal
349 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
350 if [ -L "/$d" ]; then
357 ln -sfn /run
"$initdir/var/run"
358 ln -sfn /run
/lock
"$initdir/var/lock"
363 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
366 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
367 [[ $_line = 'not a dynamic executable' ]] && break
369 if [[ $_line =~
$_so_regex ]]; then
370 _file
=${BASH_REMATCH[1]}
371 [[ -e ${initdir}/$_file ]] && continue
372 inst_library
"$_file"
376 if [[ $_line =~ not\ found
]]; then
377 dfatal
"Missing a shared library required by $_bin."
378 dfatal
"Run \"ldd $_bin\" to find out what it is."
380 dfatal
"dracut cannot create an initrd."
388 [[ -e $STATEFILE ]] && .
$STATEFILE
389 if [[ -z "$TESTDIR" ]] ||
[[ ! -d "$TESTDIR" ]]; then
390 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
391 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
397 initdir
=$TESTDIR/root
401 ## @brief Converts numeric logging level to the first letter of level name.
403 # @param lvl Numeric logging level in range from 1 to 6.
404 # @retval 1 if @a lvl is out of range.
405 # @retval 0 if @a lvl is correct.
406 # @result Echoes first letter of level name.
419 ## @brief Internal helper function for _do_dlog()
421 # @param lvl Numeric logging level.
422 # @param msg Message.
423 # @retval 0 It's always returned, even if logging failed.
425 # @note This function is not supposed to be called manually. Please use
426 # dtrace(), ddebug(), or others instead which wrap this one.
428 # This function calls _do_dlog() either with parameter msg, or if
429 # none is given, it will read standard input and will use every line as
433 # dwarn "This is a warning"
434 # echo "This is a warning" | dwarn
438 [ -z "$LOG_LEVEL" ] && return 0
439 [ $1 -le $LOG_LEVEL ] ||
return 0
440 local lvl
="$1"; shift
441 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
443 if [ $# -ge 1 ]; then
447 echo "$lvlc: " "$line"
452 ## @brief Logs message at TRACE level (6)
454 # @param msg Message.
455 # @retval 0 It's always returned, even if logging failed.
459 [ -n "$debug" ] && set -x ||
:
462 ## @brief Logs message at DEBUG level (5)
464 # @param msg Message.
465 # @retval 0 It's always returned, even if logging failed.
469 # [ -n "$debug" ] && set -x || :
472 ## @brief Logs message at INFO level (4)
474 # @param msg Message.
475 # @retval 0 It's always returned, even if logging failed.
479 [ -n "$debug" ] && set -x ||
:
482 ## @brief Logs message at WARN level (3)
484 # @param msg Message.
485 # @retval 0 It's always returned, even if logging failed.
489 [ -n "$debug" ] && set -x ||
:
492 ## @brief Logs message at ERROR level (2)
494 # @param msg Message.
495 # @retval 0 It's always returned, even if logging failed.
499 # [ -n "$debug" ] && set -x || :
502 ## @brief Logs message at FATAL level (1)
504 # @param msg Message.
505 # @retval 0 It's always returned, even if logging failed.
509 [ -n "$debug" ] && set -x ||
:
513 # Generic substring function. If $2 is in $1, return 0.
514 strstr
() { [ "${1#*$2*}" != "$1" ]; }
516 # normalize_path <path>
517 # Prints the normalized path, where it removes any duplicated
518 # and trailing slashes.
520 # $ normalize_path ///test/test//
524 set -- "${1//+(\/)//}"
529 # convert_abs_rel <from> <to>
530 # Prints the relative path, when creating a symlink to <to> from <from>.
532 # $ convert_abs_rel /usr/bin/test /bin/test-2
534 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
536 local __current __absolute __abssize __cursize __newpath
539 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
541 # corner case #1 - self looping link
542 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
544 # corner case #2 - own dir link
545 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
547 IFS
="/" __current
=($1)
548 IFS
="/" __absolute
=($2)
550 __abssize
=${#__absolute[@]}
551 __cursize
=${#__current[@]}
553 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
556 if (( __level
> __abssize || __level
> __cursize
))
562 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
566 __newpath
=$__newpath"/"
568 __newpath
=$__newpath".."
571 for ((__i
= __level
; __i
< __abssize
; __i
++))
573 if [[ -n $__newpath ]]
575 __newpath
=$__newpath"/"
577 __newpath
=$__newpath${__absolute[__i]}
584 # Install a directory, keeping symlinks as on the original system.
585 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
586 # will create ${initdir}/lib64, ${initdir}/lib64/file,
587 # and a symlink ${initdir}/lib -> lib64.
589 [[ -e ${initdir}/"$1" ]] && return 0 # already there
591 local _dir
="$1" _part
="${1%/*}" _file
592 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
597 # iterate over parent directories
598 for _file
in $_dir; do
599 [[ -e "${initdir}/$_file" ]] && continue
600 if [[ -L $_file ]]; then
601 inst_symlink
"$_file"
604 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
605 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
606 chmod u
+w
"${initdir}/$_file"
611 # $1 = file to copy to ramdisk
612 # $2 (optional) Name for the file on the ramdisk
613 # Location of the image dir is assumed to be $initdir
614 # We never overwrite the target if it exists.
616 [[ -f "$1" ]] ||
return 1
617 strstr
"$1" "/" ||
return 1
619 local _src
=$1 target
="${2:-$1}"
620 if ! [[ -d ${initdir}/$target ]]; then
621 [[ -e ${initdir}/$target ]] && return 0
622 [[ -L ${initdir}/$target ]] && return 0
623 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
625 # install checksum files also
626 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
627 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
629 ddebug
"Installing $_src"
630 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
633 # find symlinks linked to given library file
635 # Function searches for symlinks by stripping version numbers appended to
636 # library filename, checks if it points to the same target and finally
637 # prints the list of symlinks to stdout.
640 # rev_lib_symlinks libfoo.so.8.1
641 # output: libfoo.so.8 libfoo.so
642 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
644 [[ ! $1 ]] && return 0
646 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
648 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
650 until [[ ${fn##*.} == so
]]; do
652 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
658 # Same as above, but specialized to handle dynamic libraries.
659 # It handles making symlinks according to how the original library
662 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
663 strstr "$1" "/" || return 1
664 [[ -e $initdir/$_dest ]] && return 0
665 if [[ -L $_src ]]; then
666 # install checksum files also
667 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
668 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
670 _reallib=$(readlink -f "$_src")
671 inst_simple "$_reallib" "$_reallib"
672 inst_dir "${_dest%/*}"
673 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
674 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
676 inst_simple
"$_src" "$_dest"
679 # Create additional symlinks. See rev_symlinks description.
680 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
681 [[ ! -e $initdir/$_symlink ]] && {
682 ddebug
"Creating extra symlink: $_symlink"
683 inst_symlink
$_symlink
688 # find a binary. If we were not passed the full path directly,
689 # search in the usual places to find the binary.
691 if [[ -z ${1##/*} ]]; then
692 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
701 # Same as above, but specialized to install binary executables.
702 # Install binary executable, and all shared library dependencies, if any.
705 _bin
=$
(find_binary
"$1") ||
return 1
707 [[ -e $initdir/$_target ]] && return 0
708 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
710 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
712 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
713 [[ $_line = 'not a dynamic executable' ]] && break
715 if [[ $_line =~
$_so_regex ]]; then
716 _file
=${BASH_REMATCH[1]}
717 [[ -e ${initdir}/$_file ]] && continue
718 inst_library
"$_file"
722 if [[ $_line =~ not\ found
]]; then
723 dfatal
"Missing a shared library required by $_bin."
724 dfatal
"Run \"ldd $_bin\" to find out what it is."
726 dfatal
"dracut cannot create an initrd."
730 inst_simple
"$_bin" "$_target"
733 # same as above, except for shell scripts.
734 # If your shell script does not start with shebang, it is not a shell script.
737 _bin
=$
(find_binary
"$1") ||
return 1
739 local _line _shebang_regex
740 read -r -n 80 _line
<"$_bin"
741 # If debug is set, clean unprintable chars to prevent messing up the term
742 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
743 _shebang_regex
='(#! *)(/[^ ]+).*'
744 [[ $_line =~
$_shebang_regex ]] ||
return 1
745 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
748 # same as above, but specialized for symlinks
750 local _src
=$1 _target
=${2:-$1} _realsrc
751 strstr
"$1" "/" ||
return 1
752 [[ -L $1 ]] ||
return 1
753 [[ -L $initdir/$_target ]] && return 0
754 _realsrc
=$
(readlink
-f "$_src")
755 if ! [[ -e $initdir/$_realsrc ]]; then
756 if [[ -d $_realsrc ]]; then
762 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
763 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
764 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
767 # attempt to install any programs specified in a udev rule
768 inst_rule_programs
() {
771 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
772 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
773 if [ -x /lib
/udev
/$_prog ]; then
774 _bin
=/lib
/udev
/$_prog
776 _bin
=$
(find_binary
"$_prog") ||
{
777 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
782 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
783 dracut_install
"$_bin"
788 # udev rules always get installed in the same place, so
789 # create a function to install them to make life simpler.
791 local _target
=/etc
/udev
/rules.d _rule _found
793 inst_dir
"/lib/udev/rules.d"
795 for _rule
in "$@"; do
796 if [ "${rule#/}" = "$rule" ]; then
797 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
798 if [[ -f $r/$_rule ]]; then
800 inst_simple
"$_found"
801 inst_rule_programs
"$_found"
805 for r
in '' .
/ $dracutbasedir/rules.d
/; do
806 if [[ -f ${r}$_rule ]]; then
808 inst_simple
"$_found" "$_target/${_found##*/}"
809 inst_rule_programs
"$_found"
812 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
816 # general purpose installation function
817 # Same args as above.
823 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
824 [[ $initdir = $2 ]] && set $1;;
825 3) [[ -z $initdir ]] && export initdir
=$2
827 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
830 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
836 # install any of listed files
838 # If first argument is '-d' and second some destination path, first accessible
839 # source is installed into this path, otherwise it will installed in the same
840 # path as source. If none of listed files was installed, function return 1.
841 # On first successful installation it returns with 0 status.
845 # inst_any -d /bin/foo /bin/bar /bin/baz
847 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
852 [[ $1 = '-d' ]] && to
="$2" && shift 2
856 [[ $to ]] && inst
"$f" "$to" && return 0
857 inst
"$f" && return 0
864 # dracut_install [-o ] <file> [<file> ... ]
865 # Install <file> to the initramfs image
866 # -o optionally install the <file> and don't fail, if it is not there
869 if [[ $1 = '-o' ]]; then
874 if ! inst
"$1" ; then
875 if [[ $_optional = yes ]]; then
876 dinfo
"Skipping program $1 as it cannot be found and is" \
877 "flagged to be optional"
879 dfatal
"Failed to install $1"
887 # Install a single kernel module along with any firmware it may require.
888 # $1 = full path to kernel module to install
889 install_kmod_with_fw
() {
890 # no need to go further if the module is already installed
892 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
895 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
897 if [[ $omit_drivers ]]; then
901 if [[ "$_kmod" =~
$omit_drivers ]]; then
902 dinfo
"Omitting driver $_kmod"
905 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
906 dinfo
"Omitting driver $_kmod"
911 [ -d "$initdir/.kernelmodseen" ] && \
912 > "$initdir/.kernelmodseen/${1##*/}"
914 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
917 local _modname
=${1##*/} _fwdir _found _fw
918 _modname
=${_modname%.ko*}
919 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
921 for _fwdir
in $fw_dir; do
922 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
923 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
927 if [[ $_found != yes ]]; then
928 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
929 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
932 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
940 # Do something with all the dependencies of a kernel module.
941 # Note that kernel modules depend on themselves using the technique we use
942 # $1 = function to call for each dependency we find
943 # It will be passed the full path to the found kernel module
944 # $2 = module to get dependencies for
945 # rest of args = arguments to modprobe
946 # _fderr specifies FD passed from surrounding scope
947 for_each_kmod_dep
() {
948 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
950 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
951 while read _cmd _modpath _options
; do
952 [[ $_cmd = insmod
]] ||
continue
953 $_func ${_modpath} ||
exit $?
956 [[ $_found -eq 0 ]] && exit 1
961 # filter kernel modules to install certain modules that meet specific
963 # $1 = search only in subdirectory of /kernel/$1
964 # $2 = function to call with module name to filter.
965 # This function will be passed the full path to the module to test.
966 # The behavior of this function can vary depending on whether $hostonly is set.
967 # If it is, we will only look at modules that are already in memory.
968 # If it is not, we will look at all kernel modules
969 # This function returns the full filenames of modules that match $1
970 filter_kernel_modules_by_path
() (
971 local _modname _filtercmd
972 if ! [[ $hostonly ]]; then
973 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
974 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
975 _filtercmd
+=' -o -name "*.ko.xz"'
976 _filtercmd
+=' 2>/dev/null'
978 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
979 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
981 for _modname
in $
(eval $_filtercmd); do
983 *.ko
) "$2" "$_modname" && echo "$_modname";;
984 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
985 $2 $initdir/$$.ko
&& echo "$_modname"
988 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
989 $2 $initdir/$$.ko
&& echo "$_modname"
995 find_kernel_modules_by_path
() (
996 if ! [[ $hostonly ]]; then
997 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
998 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1000 cut
-d " " -f 1 </proc
/modules \
1001 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1005 filter_kernel_modules
() {
1006 filter_kernel_modules_by_path drivers
"$1"
1009 find_kernel_modules
() {
1010 find_kernel_modules_by_path drivers
1013 # instmods [-c] <kernel module> [<kernel module> ... ]
1014 # instmods [-c] <kernel subsystem>
1015 # install kernel modules along with all their dependencies.
1016 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1018 [[ $no_kernel = yes ]] && return
1019 # called [sub]functions inherit _fderr
1022 if [[ $1 = '-c' ]]; then
1027 function inst1mod
() {
1028 local _ret
=0 _mod
="$1"
1031 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1032 ( [[ "$_mpargs" ]] && echo $_mpargs
1033 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1036 ( [[ "$_mpargs" ]] && echo $_mpargs
1037 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1041 --*) _mpargs
+=" $_mod" ;;
1042 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1045 # if we are already installed, skip this module and go on
1047 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1049 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1050 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1053 # If we are building a host-specific initramfs and this
1054 # module is not already loaded, move on to the next one.
1055 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1056 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1059 # We use '-d' option in modprobe only if modules prefix path
1060 # differs from default '/'. This allows us to use Dracut with
1061 # old version of modprobe which doesn't have '-d' option.
1062 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1063 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1065 # ok, load the module, all its dependencies, and any firmware
1067 for_each_kmod_dep install_kmod_with_fw
$_mod \
1068 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1075 function instmods_1
() {
1077 if (($# == 0)); then # filenames from stdin
1079 inst1mod
"${_mod%.ko*}" ||
{
1080 if [ "$_check" = "yes" ]; then
1081 dfatal
"Failed to install $_mod"
1087 while (($# > 0)); do # filenames as arguments
1088 inst1mod
${1%.ko*} ||
{
1089 if [ "$_check" = "yes" ]; then
1090 dfatal
"Failed to install $1"
1099 local _ret _filter_not_found
='FATAL: Module .* not found.'
1101 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1102 # redirections, but that would make dracut require bash4 at least.
1103 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1104 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1110 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1111 # Install a <file> located on a lib directory to the initramfs image
1112 # -n <pattern> install non-matching files
1113 inst_libdir_file
() {
1114 if [[ "$1" == "-n" ]]; then
1117 for _dir
in $libdirs; do
1119 for _f
in "$_dir"/$_i; do
1120 [[ "$_i" =~
$_pattern ]] ||
continue
1121 [[ -e "$_i" ]] && dracut_install
"$_i"
1126 for _dir
in $libdirs; do
1128 for _f
in "$_dir"/$_i; do
1129 [[ -e "$_f" ]] && dracut_install
"$_f"
1137 [[ -d /run
/systemd
/system
]]
1142 if [[ $UID != "0" ]]; then
1143 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1148 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
1149 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
1152 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
1153 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
1159 while (($# > 0)); do
1162 echo "TEST RUN: $TEST_DESCRIPTION"
1165 if [ $ret -eq 0 ]; then
1166 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1168 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1172 echo "TEST SETUP: $TEST_DESCRIPTION"
1176 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1182 echo -n "TEST: $TEST_DESCRIPTION ";
1184 test_setup
&& test_run
1190 ) </dev
/null
>test.log
2>&1
1192 if [ $ret -eq 0 ]; then
1197 echo "see $(pwd)/test.log"