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 sed cmp tee"
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
--directory=$TESTDIR/nspawn-root
$ROOTLIBDIR/systemd
$KERNEL_APPEND
98 setup_basic_environment
() {
99 # create the basic filesystem layout
103 install_missing_libraries
120 generate_module_dependencies
124 if ! type -p valgrind
; then
125 dfatal
"Failed to install valgrind"
129 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
130 dracut_install
$_valgrind_bins
132 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
133 dracut_install
$_valgrind_libs
135 local _valgrind_dbg_and_supp
=$
(
136 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
137 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
139 dracut_install
$_valgrind_dbg_and_supp
142 create_valgrind_wrapper
() {
143 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
144 ddebug
"Create $_valgrind_wrapper"
145 cat >$_valgrind_wrapper <<EOF
148 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
150 chmod 0755 $_valgrind_wrapper
154 dracut_install
/sbin
/fsck
*
155 dracut_install
-o /bin
/fsck
*
159 instmods dm_crypt
=crypto
160 type -P dmeventd
>/dev
/null
&& dracut_install dmeventd
161 inst_libdir_file
"libdevmapper-event.so*"
162 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
163 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
164 # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
165 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
167 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
172 # install compiled files
173 (cd $TEST_BASE_DIR/..
; set -x; make DESTDIR
=$initdir install)
174 # remove unneeded documentation
175 rm -fr $initdir/usr
/share
/{man
,doc
}
176 # we strip binaries since debug symbols increase binaries size a lot
177 # and it could fill the available space
181 install_missing_libraries
() {
182 # install possible missing libraries
183 for i
in $initdir/{sbin
,bin
}/* $initdir/lib
/systemd
/*; do
188 create_empty_image
() {
189 rm -f "$TESTDIR/rootdisk.img"
190 # Create the blank file to use as a root filesystem
191 dd if=/dev
/null of
="$TESTDIR/rootdisk.img" bs
=1M seek
=400
192 LOOPDEV
=$
(losetup
--show -P -f $TESTDIR/rootdisk.img
)
193 [ -b "$LOOPDEV" ] ||
return 1
194 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
195 sfdisk
"$LOOPDEV" <<EOF
200 mkfs.ext3
-L systemd
"${LOOPDEV}p1"
203 check_result_nspawn
() {
205 [[ -e $TESTDIR/nspawn-root
/testok
]] && ret
=0
206 [[ -f $TESTDIR/nspawn-root
/failed
]] && cp -a $TESTDIR/nspawn-root
/failed
$TESTDIR
207 cp -a $TESTDIR/nspawn-root
/var
/log
/journal
$TESTDIR
208 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
209 ls -l $TESTDIR/journal
/*/*.journal
210 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
215 if [[ "$STRIP_BINARIES" = "no" ]]; then
216 ddebug
"Don't strip binaries"
219 ddebug
"Strip binaries"
220 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f |
xargs strip
--strip-unneeded | ddebug
224 mkdir
-p $initdir/etc
/rc.d
225 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
229 chmod 0755 $initdir/etc
/rc.d
/rc.
local
233 ddebug
"install any Execs from the service files"
235 export PKG_CONFIG_PATH
=$TEST_BASE_DIR/..
/src
/core
/
236 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
237 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
238 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
240 i
=${i##Exec*=}; i
=${i##-}
246 generate_module_dependencies
() {
247 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
248 ! depmod
-a -b "$initdir" $KERNEL_VER; then
249 dfatal
"\"depmod -a $KERNEL_VER\" failed."
254 install_depmod_files
() {
255 inst
/lib
/modules
/$KERNEL_VER/modules.order
256 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
260 # install plymouth, if found... else remove plymouth service files
261 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
262 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
263 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
264 # dracut_install plymouth plymouthd
266 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
270 install_ld_so_conf
() {
271 cp -a /etc
/ld.so.conf
* $initdir/etc
272 ldconfig
-r "$initdir"
275 install_config_files
() {
276 inst
/etc
/sysconfig
/init
282 inst
/etc
/nsswitch.conf
287 # we want an empty environment
288 > $initdir/etc
/environment
289 > $initdir/etc
/machine-id
291 echo systemd-testsuite
> $initdir/etc
/hostname
293 cat >$initdir/etc
/fstab
<<EOF
294 LABEL=systemd / ext3 rw 0 1
298 install_basic_tools
() {
299 [[ $BASICTOOLS ]] && dracut_install
$BASICTOOLS
300 dracut_install
-o sushell
301 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
302 dracut_install
-o ldconfig.real
305 install_debug_tools
() {
306 [[ $DEBUGTOOLS ]] && dracut_install
$DEBUGTOOLS
310 # install libnss_files for login
311 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
312 dracut_install
$NSS_LIBS
316 inst
$ROOTLIBDIR/system
/dbus.socket
317 inst
$ROOTLIBDIR/system
/dbus.service
320 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
321 |
while read file; do
328 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
&& find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
333 /lib
/security
-xtype f \
334 ) |
while read file; do
338 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
339 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
344 /usr
/lib
/kbd
/keymaps
/include
/* \
345 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
346 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
*; do
347 [[ -f $i ]] ||
continue
354 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
355 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
356 [[ -f $i ]] ||
continue
362 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
363 [ -f ${_terminfodir}/l
/linux
] && break
365 dracut_install
-o ${_terminfodir}/l
/linux
369 cp $TEST_BASE_DIR/testsuite.target
$initdir/etc
/systemd
/system
/
370 cp $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/
372 mkdir
-p $initdir/etc
/systemd
/system
/testsuite.target.wants
373 ln -fs $TEST_BASE_DIR/testsuite.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/testsuite.service
374 ln -fs $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/end.service
376 # make the testsuite the default target
377 ln -fs testsuite.target
$initdir/etc
/systemd
/system
/default.target
380 setup_nspawn_root
() {
381 rm -fr $TESTDIR/nspawn-root
382 ddebug
"cp -ar $initdir $TESTDIR/nspawn-root"
383 cp -ar $initdir $TESTDIR/nspawn-root
384 # we don't mount in the nspawn root
385 rm -f $TESTDIR/nspawn-root
/etc
/fstab
389 mkdir
-p $initdir/run
390 mkdir
-p $initdir/etc
/systemd
/system
391 mkdir
-p $initdir/var
/log
/journal
393 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
394 if [ -L "/$d" ]; then
401 ln -sfn /run
"$initdir/var/run"
402 ln -sfn /run
/lock
"$initdir/var/lock"
407 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
410 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
411 [[ $_line = 'not a dynamic executable' ]] && break
413 if [[ $_line =~
$_so_regex ]]; then
414 _file
=${BASH_REMATCH[1]}
415 [[ -e ${initdir}/$_file ]] && continue
416 inst_library
"$_file"
420 if [[ $_line =~ not\ found
]]; then
421 dfatal
"Missing a shared library required by $_bin."
422 dfatal
"Run \"ldd $_bin\" to find out what it is."
424 dfatal
"dracut cannot create an initrd."
432 [[ -e $STATEFILE ]] && .
$STATEFILE
433 if [[ -z "$TESTDIR" ]] ||
[[ ! -d "$TESTDIR" ]]; then
434 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
435 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
441 initdir
=$TESTDIR/root
445 ## @brief Converts numeric logging level to the first letter of level name.
447 # @param lvl Numeric logging level in range from 1 to 6.
448 # @retval 1 if @a lvl is out of range.
449 # @retval 0 if @a lvl is correct.
450 # @result Echoes first letter of level name.
463 ## @brief Internal helper function for _do_dlog()
465 # @param lvl Numeric logging level.
466 # @param msg Message.
467 # @retval 0 It's always returned, even if logging failed.
469 # @note This function is not supposed to be called manually. Please use
470 # dtrace(), ddebug(), or others instead which wrap this one.
472 # This function calls _do_dlog() either with parameter msg, or if
473 # none is given, it will read standard input and will use every line as
477 # dwarn "This is a warning"
478 # echo "This is a warning" | dwarn
482 [ -z "$LOG_LEVEL" ] && return 0
483 [ $1 -le $LOG_LEVEL ] ||
return 0
484 local lvl
="$1"; shift
485 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
487 if [ $# -ge 1 ]; then
491 echo "$lvlc: " "$line"
496 ## @brief Logs message at TRACE level (6)
498 # @param msg Message.
499 # @retval 0 It's always returned, even if logging failed.
503 [ -n "$debug" ] && set -x ||
:
506 ## @brief Logs message at DEBUG level (5)
508 # @param msg Message.
509 # @retval 0 It's always returned, even if logging failed.
513 # [ -n "$debug" ] && set -x || :
516 ## @brief Logs message at INFO level (4)
518 # @param msg Message.
519 # @retval 0 It's always returned, even if logging failed.
523 [ -n "$debug" ] && set -x ||
:
526 ## @brief Logs message at WARN level (3)
528 # @param msg Message.
529 # @retval 0 It's always returned, even if logging failed.
533 [ -n "$debug" ] && set -x ||
:
536 ## @brief Logs message at ERROR level (2)
538 # @param msg Message.
539 # @retval 0 It's always returned, even if logging failed.
543 # [ -n "$debug" ] && set -x || :
546 ## @brief Logs message at FATAL level (1)
548 # @param msg Message.
549 # @retval 0 It's always returned, even if logging failed.
553 [ -n "$debug" ] && set -x ||
:
557 # Generic substring function. If $2 is in $1, return 0.
558 strstr
() { [ "${1#*$2*}" != "$1" ]; }
560 # normalize_path <path>
561 # Prints the normalized path, where it removes any duplicated
562 # and trailing slashes.
564 # $ normalize_path ///test/test//
568 set -- "${1//+(\/)//}"
573 # convert_abs_rel <from> <to>
574 # Prints the relative path, when creating a symlink to <to> from <from>.
576 # $ convert_abs_rel /usr/bin/test /bin/test-2
578 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
580 local __current __absolute __abssize __cursize __newpath
583 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
585 # corner case #1 - self looping link
586 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
588 # corner case #2 - own dir link
589 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
591 IFS
="/" __current
=($1)
592 IFS
="/" __absolute
=($2)
594 __abssize
=${#__absolute[@]}
595 __cursize
=${#__current[@]}
597 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
600 if (( __level
> __abssize || __level
> __cursize
))
606 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
610 __newpath
=$__newpath"/"
612 __newpath
=$__newpath".."
615 for ((__i
= __level
; __i
< __abssize
; __i
++))
617 if [[ -n $__newpath ]]
619 __newpath
=$__newpath"/"
621 __newpath
=$__newpath${__absolute[__i]}
628 # Install a directory, keeping symlinks as on the original system.
629 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
630 # will create ${initdir}/lib64, ${initdir}/lib64/file,
631 # and a symlink ${initdir}/lib -> lib64.
633 [[ -e ${initdir}/"$1" ]] && return 0 # already there
635 local _dir
="$1" _part
="${1%/*}" _file
636 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
641 # iterate over parent directories
642 for _file
in $_dir; do
643 [[ -e "${initdir}/$_file" ]] && continue
644 if [[ -L $_file ]]; then
645 inst_symlink
"$_file"
648 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
649 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
650 chmod u
+w
"${initdir}/$_file"
655 # $1 = file to copy to ramdisk
656 # $2 (optional) Name for the file on the ramdisk
657 # Location of the image dir is assumed to be $initdir
658 # We never overwrite the target if it exists.
660 [[ -f "$1" ]] ||
return 1
661 strstr
"$1" "/" ||
return 1
663 local _src
=$1 target
="${2:-$1}"
664 if ! [[ -d ${initdir}/$target ]]; then
665 [[ -e ${initdir}/$target ]] && return 0
666 [[ -L ${initdir}/$target ]] && return 0
667 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
669 # install checksum files also
670 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
671 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
673 ddebug
"Installing $_src"
674 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
677 # find symlinks linked to given library file
679 # Function searches for symlinks by stripping version numbers appended to
680 # library filename, checks if it points to the same target and finally
681 # prints the list of symlinks to stdout.
684 # rev_lib_symlinks libfoo.so.8.1
685 # output: libfoo.so.8 libfoo.so
686 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
688 [[ ! $1 ]] && return 0
690 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
692 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
694 until [[ ${fn##*.} == so
]]; do
696 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
702 # Same as above, but specialized to handle dynamic libraries.
703 # It handles making symlinks according to how the original library
706 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
707 strstr "$1" "/" || return 1
708 [[ -e $initdir/$_dest ]] && return 0
709 if [[ -L $_src ]]; then
710 # install checksum files also
711 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
712 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
714 _reallib=$(readlink -f "$_src")
715 inst_simple "$_reallib" "$_reallib"
716 inst_dir "${_dest%/*}"
717 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
718 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
720 inst_simple
"$_src" "$_dest"
723 # Create additional symlinks. See rev_symlinks description.
724 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
725 [[ ! -e $initdir/$_symlink ]] && {
726 ddebug
"Creating extra symlink: $_symlink"
727 inst_symlink
$_symlink
732 # find a binary. If we were not passed the full path directly,
733 # search in the usual places to find the binary.
735 if [[ -z ${1##/*} ]]; then
736 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
745 # Same as above, but specialized to install binary executables.
746 # Install binary executable, and all shared library dependencies, if any.
749 _bin
=$
(find_binary
"$1") ||
return 1
751 [[ -e $initdir/$_target ]] && return 0
752 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
754 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
756 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
757 [[ $_line = 'not a dynamic executable' ]] && break
759 if [[ $_line =~
$_so_regex ]]; then
760 _file
=${BASH_REMATCH[1]}
761 [[ -e ${initdir}/$_file ]] && continue
762 inst_library
"$_file"
766 if [[ $_line =~ not\ found
]]; then
767 dfatal
"Missing a shared library required by $_bin."
768 dfatal
"Run \"ldd $_bin\" to find out what it is."
770 dfatal
"dracut cannot create an initrd."
774 inst_simple
"$_bin" "$_target"
777 # same as above, except for shell scripts.
778 # If your shell script does not start with shebang, it is not a shell script.
781 _bin
=$
(find_binary
"$1") ||
return 1
783 local _line _shebang_regex
784 read -r -n 80 _line
<"$_bin"
785 # If debug is set, clean unprintable chars to prevent messing up the term
786 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
787 _shebang_regex
='(#! *)(/[^ ]+).*'
788 [[ $_line =~
$_shebang_regex ]] ||
return 1
789 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
792 # same as above, but specialized for symlinks
794 local _src
=$1 _target
=${2:-$1} _realsrc
795 strstr
"$1" "/" ||
return 1
796 [[ -L $1 ]] ||
return 1
797 [[ -L $initdir/$_target ]] && return 0
798 _realsrc
=$
(readlink
-f "$_src")
799 if ! [[ -e $initdir/$_realsrc ]]; then
800 if [[ -d $_realsrc ]]; then
806 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
807 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
808 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
811 # attempt to install any programs specified in a udev rule
812 inst_rule_programs
() {
815 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
816 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
817 if [ -x /lib
/udev
/$_prog ]; then
818 _bin
=/lib
/udev
/$_prog
820 _bin
=$
(find_binary
"$_prog") ||
{
821 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
826 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
827 dracut_install
"$_bin"
832 # udev rules always get installed in the same place, so
833 # create a function to install them to make life simpler.
835 local _target
=/etc
/udev
/rules.d _rule _found
837 inst_dir
"/lib/udev/rules.d"
839 for _rule
in "$@"; do
840 if [ "${rule#/}" = "$rule" ]; then
841 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
842 if [[ -f $r/$_rule ]]; then
844 inst_simple
"$_found"
845 inst_rule_programs
"$_found"
849 for r
in '' .
/ $dracutbasedir/rules.d
/; do
850 if [[ -f ${r}$_rule ]]; then
852 inst_simple
"$_found" "$_target/${_found##*/}"
853 inst_rule_programs
"$_found"
856 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
860 # general purpose installation function
861 # Same args as above.
867 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
868 [[ $initdir = $2 ]] && set $1;;
869 3) [[ -z $initdir ]] && export initdir
=$2
871 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
874 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
880 # install any of listed files
882 # If first argument is '-d' and second some destination path, first accessible
883 # source is installed into this path, otherwise it will installed in the same
884 # path as source. If none of listed files was installed, function return 1.
885 # On first successful installation it returns with 0 status.
889 # inst_any -d /bin/foo /bin/bar /bin/baz
891 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
896 [[ $1 = '-d' ]] && to
="$2" && shift 2
900 [[ $to ]] && inst
"$f" "$to" && return 0
901 inst
"$f" && return 0
908 # dracut_install [-o ] <file> [<file> ... ]
909 # Install <file> to the initramfs image
910 # -o optionally install the <file> and don't fail, if it is not there
913 if [[ $1 = '-o' ]]; then
918 if ! inst
"$1" ; then
919 if [[ $_optional = yes ]]; then
920 dinfo
"Skipping program $1 as it cannot be found and is" \
921 "flagged to be optional"
923 dfatal
"Failed to install $1"
931 # Install a single kernel module along with any firmware it may require.
932 # $1 = full path to kernel module to install
933 install_kmod_with_fw
() {
934 # no need to go further if the module is already installed
936 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
939 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
941 if [[ $omit_drivers ]]; then
945 if [[ "$_kmod" =~
$omit_drivers ]]; then
946 dinfo
"Omitting driver $_kmod"
949 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
950 dinfo
"Omitting driver $_kmod"
955 [ -d "$initdir/.kernelmodseen" ] && \
956 > "$initdir/.kernelmodseen/${1##*/}"
958 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
961 local _modname
=${1##*/} _fwdir _found _fw
962 _modname
=${_modname%.ko*}
963 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
965 for _fwdir
in $fw_dir; do
966 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
967 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
971 if [[ $_found != yes ]]; then
972 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
973 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
976 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
984 # Do something with all the dependencies of a kernel module.
985 # Note that kernel modules depend on themselves using the technique we use
986 # $1 = function to call for each dependency we find
987 # It will be passed the full path to the found kernel module
988 # $2 = module to get dependencies for
989 # rest of args = arguments to modprobe
990 # _fderr specifies FD passed from surrounding scope
991 for_each_kmod_dep
() {
992 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
994 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
995 while read _cmd _modpath _options
; do
996 [[ $_cmd = insmod
]] ||
continue
997 $_func ${_modpath} ||
exit $?
1000 [[ $_found -eq 0 ]] && exit 1
1005 # filter kernel modules to install certain modules that meet specific
1007 # $1 = search only in subdirectory of /kernel/$1
1008 # $2 = function to call with module name to filter.
1009 # This function will be passed the full path to the module to test.
1010 # The behavior of this function can vary depending on whether $hostonly is set.
1011 # If it is, we will only look at modules that are already in memory.
1012 # If it is not, we will look at all kernel modules
1013 # This function returns the full filenames of modules that match $1
1014 filter_kernel_modules_by_path
() (
1015 local _modname _filtercmd
1016 if ! [[ $hostonly ]]; then
1017 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1018 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1019 _filtercmd
+=' -o -name "*.ko.xz"'
1020 _filtercmd
+=' 2>/dev/null'
1022 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1023 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1025 for _modname
in $
(eval $_filtercmd); do
1027 *.ko
) "$2" "$_modname" && echo "$_modname";;
1028 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1029 $2 $initdir/$$.ko
&& echo "$_modname"
1030 rm -f $initdir/$$.ko
1032 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1033 $2 $initdir/$$.ko
&& echo "$_modname"
1034 rm -f $initdir/$$.ko
1039 find_kernel_modules_by_path
() (
1040 if ! [[ $hostonly ]]; then
1041 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1042 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1044 cut
-d " " -f 1 </proc
/modules \
1045 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1049 filter_kernel_modules
() {
1050 filter_kernel_modules_by_path drivers
"$1"
1053 find_kernel_modules
() {
1054 find_kernel_modules_by_path drivers
1057 # instmods [-c] <kernel module> [<kernel module> ... ]
1058 # instmods [-c] <kernel subsystem>
1059 # install kernel modules along with all their dependencies.
1060 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1062 [[ $no_kernel = yes ]] && return
1063 # called [sub]functions inherit _fderr
1066 if [[ $1 = '-c' ]]; then
1071 function inst1mod
() {
1072 local _ret
=0 _mod
="$1"
1075 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1076 ( [[ "$_mpargs" ]] && echo $_mpargs
1077 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1080 ( [[ "$_mpargs" ]] && echo $_mpargs
1081 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1085 --*) _mpargs
+=" $_mod" ;;
1086 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1089 # if we are already installed, skip this module and go on
1091 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1093 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1094 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1097 # If we are building a host-specific initramfs and this
1098 # module is not already loaded, move on to the next one.
1099 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1100 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1103 # We use '-d' option in modprobe only if modules prefix path
1104 # differs from default '/'. This allows us to use Dracut with
1105 # old version of modprobe which doesn't have '-d' option.
1106 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1107 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1109 # ok, load the module, all its dependencies, and any firmware
1111 for_each_kmod_dep install_kmod_with_fw
$_mod \
1112 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1119 function instmods_1
() {
1121 if (($# == 0)); then # filenames from stdin
1123 inst1mod
"${_mod%.ko*}" ||
{
1124 if [ "$_check" = "yes" ]; then
1125 dfatal
"Failed to install $_mod"
1131 while (($# > 0)); do # filenames as arguments
1132 inst1mod
${1%.ko*} ||
{
1133 if [ "$_check" = "yes" ]; then
1134 dfatal
"Failed to install $1"
1143 local _ret _filter_not_found
='FATAL: Module .* not found.'
1145 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1146 # redirections, but that would make dracut require bash4 at least.
1147 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1148 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1154 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1155 # Install a <file> located on a lib directory to the initramfs image
1156 # -n <pattern> install non-matching files
1157 inst_libdir_file
() {
1158 if [[ "$1" == "-n" ]]; then
1161 for _dir
in $libdirs; do
1163 for _f
in "$_dir"/$_i; do
1164 [[ "$_i" =~
$_pattern ]] ||
continue
1165 [[ -e "$_i" ]] && dracut_install
"$_i"
1170 for _dir
in $libdirs; do
1172 for _f
in "$_dir"/$_i; do
1173 [[ -e "$_f" ]] && dracut_install
"$_f"
1181 [[ -d /run
/systemd
/system
]]
1186 if [[ $UID != "0" ]]; then
1187 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1192 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
1193 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
1196 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
1197 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
1203 while (($# > 0)); do
1206 echo "TEST RUN: $TEST_DESCRIPTION"
1209 if [ $ret -eq 0 ]; then
1210 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1212 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1216 echo "TEST SETUP: $TEST_DESCRIPTION"
1220 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1226 echo -n "TEST: $TEST_DESCRIPTION ";
1228 test_setup
&& test_run
1234 ) </dev
/null
>test.log
2>&1
1236 if [ $ret -eq 0 ]; then
1241 echo "see $(pwd)/test.log"