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
="-machine accel=kvm:tcg \
82 if [ "$INITRD" ]; then
83 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
87 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img
) ||
return 1
92 ..
/..
/systemd-nspawn
--boot --directory=$TESTDIR/nspawn-root
$ROOTLIBDIR/systemd
$KERNEL_APPEND
95 setup_basic_environment
() {
96 # create the basic filesystem layout
100 install_missing_libraries
116 generate_module_dependencies
118 ln -fs /proc
/self
/mounts
$initdir/etc
/mtab
122 instmods dm_crypt
=crypto
123 type -P dmeventd
>/dev
/null
&& dracut_install dmeventd
124 inst_libdir_file
"libdevmapper-event.so*"
125 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
129 # install compiled files
130 (cd $TEST_BASE_DIR/..
; set -x; make DESTDIR
=$initdir install)
131 # remove unneeded documentation
132 rm -fr $initdir/usr
/share
/{man
,doc
}
133 # we strip binaries since debug symbols increase binaries size a lot
134 # and it could fill the available space
138 install_missing_libraries
() {
139 # install possible missing libraries
140 for i
in $initdir/{sbin
,bin
}/* $initdir/lib
/systemd
/*; do
145 create_empty_image
() {
146 rm -f "$TESTDIR/rootdisk.img"
147 # Create the blank file to use as a root filesystem
148 dd if=/dev
/null of
="$TESTDIR/rootdisk.img" bs
=1M seek
=300
149 LOOPDEV
=$
(losetup
--show -P -f $TESTDIR/rootdisk.img
)
150 [ -b "$LOOPDEV" ] ||
return 1
151 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
152 sfdisk
"$LOOPDEV" <<EOF
157 mkfs.ext3
-L systemd
"${LOOPDEV}p1"
160 check_result_nspawn
() {
162 [[ -e $TESTDIR/nspawn-root
/testok
]] && ret
=0
163 [[ -f $TESTDIR/nspawn-root
/failed
]] && cp -a $TESTDIR/nspawn-root
/failed
$TESTDIR
164 cp -a $TESTDIR/nspawn-root
/var
/log
/journal
$TESTDIR
165 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
166 ls -l $TESTDIR/journal
/*/*.journal
167 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
172 ddebug
"Strip binaries"
173 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f |
xargs strip
--strip-unneeded | ddebug
177 mkdir
-p $initdir/etc
/rc.d
178 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
182 chmod 0755 $initdir/etc
/rc.d
/rc.
local
186 ddebug
"install any Execs from the service files"
188 export PKG_CONFIG_PATH
=$TEST_BASE_DIR/..
/src
/core
/
189 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
190 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
191 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
193 i
=${i##Exec*=}; i
=${i##-}
199 generate_module_dependencies
() {
200 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
201 ! depmod
-a -b "$initdir" $KERNEL_VER; then
202 dfatal
"\"depmod -a $KERNEL_VER\" failed."
207 install_depmod_files
() {
208 inst
/lib
/modules
/$KERNEL_VER/modules.order
209 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
213 # install plymouth, if found... else remove plymouth service files
214 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
215 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
216 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
217 # dracut_install plymouth plymouthd
219 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
223 install_ld_so_conf
() {
224 cp -a /etc
/ld.so.conf
* $initdir/etc
225 ldconfig
-r "$initdir"
228 install_config_files
() {
229 inst
/etc
/sysconfig
/init
235 inst
/etc
/nsswitch.conf
240 # we want an empty environment
241 > $initdir/etc
/environment
242 > $initdir/etc
/machine-id
244 echo systemd-testsuite
> $initdir/etc
/hostname
246 cat >$initdir/etc
/fstab
<<EOF
247 LABEL=systemd / ext3 rw 0 1
251 install_basic_tools
() {
252 [[ $BASICTOOLS ]] && dracut_install
$BASICTOOLS
253 dracut_install
-o sushell
254 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
255 dracut_install
-o ldconfig.real
258 install_debug_tools
() {
259 [[ $DEBUGTOOLS ]] && dracut_install
$DEBUGTOOLS
263 # install libnss_files for login
264 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
265 dracut_install
$NSS_LIBS
269 inst
$ROOTLIBDIR/system
/dbus.socket
270 inst
$ROOTLIBDIR/system
/dbus.service
273 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
274 |
while read file; do
281 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
&& find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
286 /lib
/security
-xtype f \
287 ) |
while read file; do
291 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
292 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
297 /usr
/lib
/kbd
/keymaps
/include
/* \
298 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
299 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
*; do
300 [[ -f $i ]] ||
continue
307 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
308 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
309 [[ -f $i ]] ||
continue
315 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
316 [ -f ${_terminfodir}/l
/linux
] && break
318 dracut_install
-o ${_terminfodir}/l
/linux
322 cp $TEST_BASE_DIR/testsuite.target
$initdir/etc
/systemd
/system
/
323 cp $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/
325 mkdir
-p $initdir/etc
/systemd
/system
/testsuite.target.wants
326 ln -fs $TEST_BASE_DIR/testsuite.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/testsuite.service
327 ln -fs $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/end.service
329 # make the testsuite the default target
330 ln -fs testsuite.target
$initdir/etc
/systemd
/system
/default.target
333 setup_nspawn_root
() {
334 rm -fr $TESTDIR/nspawn-root
335 ddebug
"cp -ar $initdir $TESTDIR/nspawn-root"
336 cp -ar $initdir $TESTDIR/nspawn-root
337 # we don't mount in the nspawn root
338 rm -f $TESTDIR/nspawn-root
/etc
/fstab
342 mkdir
-p $initdir/run
343 mkdir
-p $initdir/etc
/systemd
/system
344 mkdir
-p $initdir/var
/log
/journal
346 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
347 if [ -L "/$d" ]; then
354 ln -sfn /run
"$initdir/var/run"
355 ln -sfn /run
/lock
"$initdir/var/lock"
360 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
363 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
364 [[ $_line = 'not a dynamic executable' ]] && break
366 if [[ $_line =~
$_so_regex ]]; then
367 _file
=${BASH_REMATCH[1]}
368 [[ -e ${initdir}/$_file ]] && continue
369 inst_library
"$_file"
373 if [[ $_line =~ not\ found
]]; then
374 dfatal
"Missing a shared library required by $_bin."
375 dfatal
"Run \"ldd $_bin\" to find out what it is."
377 dfatal
"dracut cannot create an initrd."
385 [[ -e $STATEFILE ]] && .
$STATEFILE
386 if [[ -z "$TESTDIR" ]] ||
[[ ! -d "$TESTDIR" ]]; then
387 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
388 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
394 initdir
=$TESTDIR/root
398 ## @brief Converts numeric logging level to the first letter of level name.
400 # @param lvl Numeric logging level in range from 1 to 6.
401 # @retval 1 if @a lvl is out of range.
402 # @retval 0 if @a lvl is correct.
403 # @result Echoes first letter of level name.
416 ## @brief Internal helper function for _do_dlog()
418 # @param lvl Numeric logging level.
419 # @param msg Message.
420 # @retval 0 It's always returned, even if logging failed.
422 # @note This function is not supposed to be called manually. Please use
423 # dtrace(), ddebug(), or others instead which wrap this one.
425 # This function calls _do_dlog() either with parameter msg, or if
426 # none is given, it will read standard input and will use every line as
430 # dwarn "This is a warning"
431 # echo "This is a warning" | dwarn
435 [ -z "$LOG_LEVEL" ] && return 0
436 [ $1 -le $LOG_LEVEL ] ||
return 0
437 local lvl
="$1"; shift
438 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
440 if [ $# -ge 1 ]; then
444 echo "$lvlc: " "$line"
449 ## @brief Logs message at TRACE level (6)
451 # @param msg Message.
452 # @retval 0 It's always returned, even if logging failed.
456 [ -n "$debug" ] && set -x ||
:
459 ## @brief Logs message at DEBUG level (5)
461 # @param msg Message.
462 # @retval 0 It's always returned, even if logging failed.
466 # [ -n "$debug" ] && set -x || :
469 ## @brief Logs message at INFO level (4)
471 # @param msg Message.
472 # @retval 0 It's always returned, even if logging failed.
476 [ -n "$debug" ] && set -x ||
:
479 ## @brief Logs message at WARN level (3)
481 # @param msg Message.
482 # @retval 0 It's always returned, even if logging failed.
486 [ -n "$debug" ] && set -x ||
:
489 ## @brief Logs message at ERROR level (2)
491 # @param msg Message.
492 # @retval 0 It's always returned, even if logging failed.
496 # [ -n "$debug" ] && set -x || :
499 ## @brief Logs message at FATAL level (1)
501 # @param msg Message.
502 # @retval 0 It's always returned, even if logging failed.
506 [ -n "$debug" ] && set -x ||
:
510 # Generic substring function. If $2 is in $1, return 0.
511 strstr
() { [ "${1#*$2*}" != "$1" ]; }
513 # normalize_path <path>
514 # Prints the normalized path, where it removes any duplicated
515 # and trailing slashes.
517 # $ normalize_path ///test/test//
521 set -- "${1//+(\/)//}"
526 # convert_abs_rel <from> <to>
527 # Prints the relative path, when creating a symlink to <to> from <from>.
529 # $ convert_abs_rel /usr/bin/test /bin/test-2
531 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
533 local __current __absolute __abssize __cursize __newpath
536 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
538 # corner case #1 - self looping link
539 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
541 # corner case #2 - own dir link
542 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
544 IFS
="/" __current
=($1)
545 IFS
="/" __absolute
=($2)
547 __abssize
=${#__absolute[@]}
548 __cursize
=${#__current[@]}
550 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
553 if (( __level
> __abssize || __level
> __cursize
))
559 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
563 __newpath
=$__newpath"/"
565 __newpath
=$__newpath".."
568 for ((__i
= __level
; __i
< __abssize
; __i
++))
570 if [[ -n $__newpath ]]
572 __newpath
=$__newpath"/"
574 __newpath
=$__newpath${__absolute[__i]}
581 # Install a directory, keeping symlinks as on the original system.
582 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
583 # will create ${initdir}/lib64, ${initdir}/lib64/file,
584 # and a symlink ${initdir}/lib -> lib64.
586 [[ -e ${initdir}/"$1" ]] && return 0 # already there
588 local _dir
="$1" _part
="${1%/*}" _file
589 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
594 # iterate over parent directories
595 for _file
in $_dir; do
596 [[ -e "${initdir}/$_file" ]] && continue
597 if [[ -L $_file ]]; then
598 inst_symlink
"$_file"
601 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
602 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
603 chmod u
+w
"${initdir}/$_file"
608 # $1 = file to copy to ramdisk
609 # $2 (optional) Name for the file on the ramdisk
610 # Location of the image dir is assumed to be $initdir
611 # We never overwrite the target if it exists.
613 [[ -f "$1" ]] ||
return 1
614 strstr
"$1" "/" ||
return 1
616 local _src
=$1 target
="${2:-$1}"
617 if ! [[ -d ${initdir}/$target ]]; then
618 [[ -e ${initdir}/$target ]] && return 0
619 [[ -L ${initdir}/$target ]] && return 0
620 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
622 # install checksum files also
623 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
624 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
626 ddebug
"Installing $_src"
627 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
630 # find symlinks linked to given library file
632 # Function searches for symlinks by stripping version numbers appended to
633 # library filename, checks if it points to the same target and finally
634 # prints the list of symlinks to stdout.
637 # rev_lib_symlinks libfoo.so.8.1
638 # output: libfoo.so.8 libfoo.so
639 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
641 [[ ! $1 ]] && return 0
643 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
645 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
647 until [[ ${fn##*.} == so
]]; do
649 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
655 # Same as above, but specialized to handle dynamic libraries.
656 # It handles making symlinks according to how the original library
659 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
660 strstr "$1" "/" || return 1
661 [[ -e $initdir/$_dest ]] && return 0
662 if [[ -L $_src ]]; then
663 # install checksum files also
664 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
665 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
667 _reallib=$(readlink -f "$_src")
668 inst_simple "$_reallib" "$_reallib"
669 inst_dir "${_dest%/*}"
670 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
671 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
673 inst_simple
"$_src" "$_dest"
676 # Create additional symlinks. See rev_symlinks description.
677 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
678 [[ ! -e $initdir/$_symlink ]] && {
679 ddebug
"Creating extra symlink: $_symlink"
680 inst_symlink
$_symlink
685 # find a binary. If we were not passed the full path directly,
686 # search in the usual places to find the binary.
688 if [[ -z ${1##/*} ]]; then
689 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
698 # Same as above, but specialized to install binary executables.
699 # Install binary executable, and all shared library dependencies, if any.
702 _bin
=$
(find_binary
"$1") ||
return 1
704 [[ -e $initdir/$_target ]] && return 0
705 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
707 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
709 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
710 [[ $_line = 'not a dynamic executable' ]] && break
712 if [[ $_line =~
$_so_regex ]]; then
713 _file
=${BASH_REMATCH[1]}
714 [[ -e ${initdir}/$_file ]] && continue
715 inst_library
"$_file"
719 if [[ $_line =~ not\ found
]]; then
720 dfatal
"Missing a shared library required by $_bin."
721 dfatal
"Run \"ldd $_bin\" to find out what it is."
723 dfatal
"dracut cannot create an initrd."
727 inst_simple
"$_bin" "$_target"
730 # same as above, except for shell scripts.
731 # If your shell script does not start with shebang, it is not a shell script.
734 _bin
=$
(find_binary
"$1") ||
return 1
736 local _line _shebang_regex
737 read -r -n 80 _line
<"$_bin"
738 # If debug is set, clean unprintable chars to prevent messing up the term
739 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
740 _shebang_regex
='(#! *)(/[^ ]+).*'
741 [[ $_line =~
$_shebang_regex ]] ||
return 1
742 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
745 # same as above, but specialized for symlinks
747 local _src
=$1 _target
=${2:-$1} _realsrc
748 strstr
"$1" "/" ||
return 1
749 [[ -L $1 ]] ||
return 1
750 [[ -L $initdir/$_target ]] && return 0
751 _realsrc
=$
(readlink
-f "$_src")
752 if ! [[ -e $initdir/$_realsrc ]]; then
753 if [[ -d $_realsrc ]]; then
759 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
760 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
761 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
764 # attempt to install any programs specified in a udev rule
765 inst_rule_programs
() {
768 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
769 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
770 if [ -x /lib
/udev
/$_prog ]; then
771 _bin
=/lib
/udev
/$_prog
773 _bin
=$
(find_binary
"$_prog") ||
{
774 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
779 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
780 dracut_install
"$_bin"
785 # udev rules always get installed in the same place, so
786 # create a function to install them to make life simpler.
788 local _target
=/etc
/udev
/rules.d _rule _found
790 inst_dir
"/lib/udev/rules.d"
792 for _rule
in "$@"; do
793 if [ "${rule#/}" = "$rule" ]; then
794 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
795 if [[ -f $r/$_rule ]]; then
797 inst_simple
"$_found"
798 inst_rule_programs
"$_found"
802 for r
in '' .
/ $dracutbasedir/rules.d
/; do
803 if [[ -f ${r}$_rule ]]; then
805 inst_simple
"$_found" "$_target/${_found##*/}"
806 inst_rule_programs
"$_found"
809 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
813 # general purpose installation function
814 # Same args as above.
820 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
821 [[ $initdir = $2 ]] && set $1;;
822 3) [[ -z $initdir ]] && export initdir
=$2
824 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
827 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
833 # install any of listed files
835 # If first argument is '-d' and second some destination path, first accessible
836 # source is installed into this path, otherwise it will installed in the same
837 # path as source. If none of listed files was installed, function return 1.
838 # On first successful installation it returns with 0 status.
842 # inst_any -d /bin/foo /bin/bar /bin/baz
844 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
849 [[ $1 = '-d' ]] && to
="$2" && shift 2
853 [[ $to ]] && inst
"$f" "$to" && return 0
854 inst
"$f" && return 0
861 # dracut_install [-o ] <file> [<file> ... ]
862 # Install <file> to the initramfs image
863 # -o optionally install the <file> and don't fail, if it is not there
866 if [[ $1 = '-o' ]]; then
871 if ! inst
"$1" ; then
872 if [[ $_optional = yes ]]; then
873 dinfo
"Skipping program $1 as it cannot be found and is" \
874 "flagged to be optional"
876 dfatal
"Failed to install $1"
884 # Install a single kernel module along with any firmware it may require.
885 # $1 = full path to kernel module to install
886 install_kmod_with_fw
() {
887 # no need to go further if the module is already installed
889 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
892 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
894 if [[ $omit_drivers ]]; then
898 if [[ "$_kmod" =~
$omit_drivers ]]; then
899 dinfo
"Omitting driver $_kmod"
902 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
903 dinfo
"Omitting driver $_kmod"
908 [ -d "$initdir/.kernelmodseen" ] && \
909 > "$initdir/.kernelmodseen/${1##*/}"
911 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
914 local _modname
=${1##*/} _fwdir _found _fw
915 _modname
=${_modname%.ko*}
916 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
918 for _fwdir
in $fw_dir; do
919 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
920 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
924 if [[ $_found != yes ]]; then
925 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
926 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
929 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
937 # Do something with all the dependencies of a kernel module.
938 # Note that kernel modules depend on themselves using the technique we use
939 # $1 = function to call for each dependency we find
940 # It will be passed the full path to the found kernel module
941 # $2 = module to get dependencies for
942 # rest of args = arguments to modprobe
943 # _fderr specifies FD passed from surrounding scope
944 for_each_kmod_dep
() {
945 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
947 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
948 while read _cmd _modpath _options
; do
949 [[ $_cmd = insmod
]] ||
continue
950 $_func ${_modpath} ||
exit $?
953 [[ $_found -eq 0 ]] && exit 1
958 # filter kernel modules to install certain modules that meet specific
960 # $1 = search only in subdirectory of /kernel/$1
961 # $2 = function to call with module name to filter.
962 # This function will be passed the full path to the module to test.
963 # The behavior of this function can vary depending on whether $hostonly is set.
964 # If it is, we will only look at modules that are already in memory.
965 # If it is not, we will look at all kernel modules
966 # This function returns the full filenames of modules that match $1
967 filter_kernel_modules_by_path
() (
968 local _modname _filtercmd
969 if ! [[ $hostonly ]]; then
970 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
971 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
972 _filtercmd
+=' -o -name "*.ko.xz"'
973 _filtercmd
+=' 2>/dev/null'
975 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
976 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
978 for _modname
in $
(eval $_filtercmd); do
980 *.ko
) "$2" "$_modname" && echo "$_modname";;
981 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
982 $2 $initdir/$$.ko
&& echo "$_modname"
985 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
986 $2 $initdir/$$.ko
&& echo "$_modname"
992 find_kernel_modules_by_path
() (
993 if ! [[ $hostonly ]]; then
994 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
995 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
997 cut
-d " " -f 1 </proc
/modules \
998 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1002 filter_kernel_modules
() {
1003 filter_kernel_modules_by_path drivers
"$1"
1006 find_kernel_modules
() {
1007 find_kernel_modules_by_path drivers
1010 # instmods [-c] <kernel module> [<kernel module> ... ]
1011 # instmods [-c] <kernel subsystem>
1012 # install kernel modules along with all their dependencies.
1013 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1015 [[ $no_kernel = yes ]] && return
1016 # called [sub]functions inherit _fderr
1019 if [[ $1 = '-c' ]]; then
1024 function inst1mod
() {
1025 local _ret
=0 _mod
="$1"
1028 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1029 ( [[ "$_mpargs" ]] && echo $_mpargs
1030 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1033 ( [[ "$_mpargs" ]] && echo $_mpargs
1034 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1038 --*) _mpargs
+=" $_mod" ;;
1039 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1042 # if we are already installed, skip this module and go on
1044 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1046 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1047 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1050 # If we are building a host-specific initramfs and this
1051 # module is not already loaded, move on to the next one.
1052 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1053 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1056 # We use '-d' option in modprobe only if modules prefix path
1057 # differs from default '/'. This allows us to use Dracut with
1058 # old version of modprobe which doesn't have '-d' option.
1059 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1060 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1062 # ok, load the module, all its dependencies, and any firmware
1064 for_each_kmod_dep install_kmod_with_fw
$_mod \
1065 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1072 function instmods_1
() {
1074 if (($# == 0)); then # filenames from stdin
1076 inst1mod
"${_mod%.ko*}" ||
{
1077 if [ "$_check" = "yes" ]; then
1078 dfatal
"Failed to install $_mod"
1084 while (($# > 0)); do # filenames as arguments
1085 inst1mod
${1%.ko*} ||
{
1086 if [ "$_check" = "yes" ]; then
1087 dfatal
"Failed to install $1"
1096 local _ret _filter_not_found
='FATAL: Module .* not found.'
1098 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1099 # redirections, but that would make dracut require bash4 at least.
1100 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1101 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1107 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1108 # Install a <file> located on a lib directory to the initramfs image
1109 # -n <pattern> install non-matching files
1110 inst_libdir_file
() {
1111 if [[ "$1" == "-n" ]]; then
1114 for _dir
in $libdirs; do
1116 for _f
in "$_dir"/$_i; do
1117 [[ "$_i" =~
$_pattern ]] ||
continue
1118 [[ -e "$_i" ]] && dracut_install
"$_i"
1123 for _dir
in $libdirs; do
1125 for _f
in "$_dir"/$_i; do
1126 [[ -e "$_f" ]] && dracut_install
"$_f"
1134 [[ -d /sys
/fs
/cgroup
/systemd
]]
1139 if [[ $UID != "0" ]]; then
1140 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1145 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
1146 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
1149 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
1150 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
1156 while (($# > 0)); do
1159 echo "TEST RUN: $TEST_DESCRIPTION"
1162 if [ $ret -eq 0 ]; then
1163 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1165 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1169 echo "TEST SETUP: $TEST_DESCRIPTION"
1173 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1179 echo -n "TEST: $TEST_DESCRIPTION ";
1181 test_setup
&& test_run
1187 ) </dev
/null
>test.log
2>&1
1189 if [ $ret -eq 0 ]; then
1194 echo "see $(pwd)/test.log"