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/"
10 QEMU_TIMEOUT
="${QEMU_TIMEOUT:-infinity}"
11 NSPAWN_TIMEOUT
="${NSPAWN_TIMEOUT:-infinity}"
12 TIMED_OUT
= # will be 1 after run_* if *_TIMEOUT is set and test timed out
13 FSTYPE
="${FSTYPE:-ext3}"
14 UNIFIED_CGROUP_HIERARCHY
="${UNIFIED_CGROUP_HIERARCHY:-no}"
16 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
17 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
18 ROOTLIBDIR
=/usr
/lib
/systemd
21 BASICTOOLS
="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm"
22 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 find"
24 function find_qemu_bin
() {
25 # SUSE and Red Hat call the binary qemu-kvm
26 # Debian and Gentoo call it kvm
27 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
29 [ "$ARCH" ] || ARCH
=$
(uname
-m)
32 # QEMU's own build system calls it qemu-system-x86_64
33 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
36 # new i386 version of QEMU
37 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
39 # i386 version of QEMU
40 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
44 if [ ! -e "$QEMU_BIN" ]; then
45 echo "Could not find a suitable QEMU binary" >&2
50 # Return 0 if QEMU did run (then you must check the result state/logs for actual
51 # success), or 1 if QEMU is not available.
53 if [ -f /etc
/machine-id
]; then
54 read MACHINE_ID
< /etc
/machine-id
55 [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
56 && INITRD
="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
57 [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
58 && KERNEL_BIN
="/boot/$MACHINE_ID/$KERNEL_VER/linux"
61 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
62 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
63 [ "$KERNEL_BIN" ] || KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
64 [ "$INITRD" ] ||
{ [ -e "$default_fedora_initrd" ] && INITRD
=$default_fedora_initrd; }
65 [ "$INITRD" ] ||
{ [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD
=$default_debian_initrd; }
66 [ "$QEMU_SMP" ] || QEMU_SMP
=1
68 find_qemu_bin ||
return 1
70 KERNEL_APPEND
="root=/dev/sda1 \
73 init=$ROOTLIBDIR/systemd \
77 systemd.unified_cgroup_hierarchy=$UNIFIED_CGROUP_HIERARCHY \
81 QEMU_OPTIONS
="-smp $QEMU_SMP \
86 -drive format=raw,cache=unsafe,file=${TESTDIR}/rootdisk.img \
89 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
90 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
93 if [ -c /dev
/kvm
]; then
94 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
97 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
98 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
100 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
102 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
103 derror
"test timed out after $QEMU_TIMEOUT s"
106 [ "$rc" != 0 ] && derror
"QEMU failed with exit code $rc"
111 # Return 0 if nspawn did run (then you must check the result state/logs for actual
112 # success), or 1 if nspawn is not available.
114 [[ -d /run
/systemd
/system
]] ||
return 1
116 local _nspawn_cmd
="../../systemd-nspawn --register=no --kill-signal=SIGKILL --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND"
117 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
118 _nspawn_cmd
="timeout --foreground $NSPAWN_TIMEOUT $_nspawn_cmd"
121 _nspawn_cmd
="env UNIFIED_CGROUP_HIERARCHY=$UNIFIED_CGROUP_HIERARCHY $_nspawn_cmd"
123 (set -x; $_nspawn_cmd)
125 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
126 derror
"test timed out after $NSPAWN_TIMEOUT s"
129 [ "$rc" != 0 ] && derror
"nspawn failed with exit code $rc"
134 setup_basic_environment
() {
135 # create the basic filesystem layout
139 install_missing_libraries
157 generate_module_dependencies
161 # don't forget KERNEL_APPEND='... selinux=1 ...'
162 if [[ "$SETUP_SELINUX" != "yes" ]]; then
163 ddebug
"Don't setup SELinux"
166 ddebug
"Setup SELinux"
167 local _conf_dir
=/etc
/selinux
168 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
170 rm -rf $initdir/$_conf_dir
171 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
172 dfatal
"Failed to copy $_conf_dir"
176 cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
178 Description=Relabel all filesystems
179 DefaultDependencies=no
180 Requires=local-fs.target
181 Conflicts=shutdown.target
182 After=local-fs.target
183 Before=sysinit.target shutdown.target
184 ConditionSecurity=selinux
185 ConditionPathExists=|/.autorelabel
188 ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
194 touch $initdir/.autorelabel
195 mkdir
-p $initdir/etc
/systemd
/system
/basic.target.wants
196 ln -fs autorelabel.service
$initdir/etc
/systemd
/system
/basic.target.wants
/autorelabel.service
198 dracut_install
$_fixfiles_tools
199 dracut_install fixfiles
200 dracut_install sestatus
204 if ! type -p valgrind
; then
205 dfatal
"Failed to install valgrind"
209 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
210 dracut_install
$_valgrind_bins
212 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
213 dracut_install
$_valgrind_libs
215 local _valgrind_dbg_and_supp
=$
(
216 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
217 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
219 dracut_install
$_valgrind_dbg_and_supp
222 create_valgrind_wrapper
() {
223 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
224 ddebug
"Create $_valgrind_wrapper"
225 cat >$_valgrind_wrapper <<EOF
228 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
230 chmod 0755 $_valgrind_wrapper
233 create_strace_wrapper
() {
234 local _strace_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-strace
235 ddebug
"Create $_strace_wrapper"
236 cat >$_strace_wrapper <<EOF
239 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
241 chmod 0755 $_strace_wrapper
245 dracut_install
/sbin
/fsck
*
246 dracut_install
-o /bin
/fsck
*
248 # fskc.reiserfs calls reiserfsck. so, install it
249 dracut_install
-o reiserfsck
253 instmods dm_crypt
=crypto
254 type -P dmeventd
>/dev
/null
&& dracut_install dmeventd
255 inst_libdir_file
"libdevmapper-event.so*"
256 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
257 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
258 # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
259 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
261 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
266 # install compiled files
267 (cd $TEST_BASE_DIR/..
; set -x; make DESTDIR
=$initdir install)
268 # remove unneeded documentation
269 rm -fr $initdir/usr
/share
/{man
,doc
}
270 # we strip binaries since debug symbols increase binaries size a lot
271 # and it could fill the available space
274 # enable debug logging in PID1
275 echo LogLevel
=debug
>> $initdir/etc
/systemd
/system.conf
280 objdump
-p "$_bin" 2>/dev
/null |
awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" |
paste -sd :
283 install_missing_libraries
() {
284 # install possible missing libraries
285 for i
in $initdir{,/usr
}/{sbin
,bin
}/* $initdir{,/usr
}/lib
/systemd
/*; do
286 LD_LIBRARY_PATH
=$
(get_ldpath
$i) inst_libs
$i
290 create_empty_image
() {
291 rm -f "$TESTDIR/rootdisk.img"
292 # Create the blank file to use as a root filesystem
293 dd if=/dev
/null of
="$TESTDIR/rootdisk.img" bs
=1M seek
=400
294 LOOPDEV
=$
(losetup
--show -P -f $TESTDIR/rootdisk.img
)
295 [ -b "$LOOPDEV" ] ||
return 1
296 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
297 sfdisk
"$LOOPDEV" <<EOF
302 local _label
="-L systemd"
303 # mkfs.reiserfs doesn't know -L. so, use --label instead
304 [[ "$FSTYPE" == "reiserfs" ]] && _label
="--label systemd"
305 if ! mkfs
-t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; then
306 dfatal
"Failed to mkfs -t ${FSTYPE}"
311 check_result_nspawn
() {
313 [[ -e $TESTDIR/nspawn-root
/testok
]] && ret
=0
314 [[ -f $TESTDIR/nspawn-root
/failed
]] && cp -a $TESTDIR/nspawn-root
/failed
$TESTDIR
315 cp -a $TESTDIR/nspawn-root
/var
/log
/journal
$TESTDIR
316 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
317 ls -l $TESTDIR/journal
/*/*.journal
318 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
319 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
324 if [[ "$STRIP_BINARIES" = "no" ]]; then
325 ddebug
"Don't strip binaries"
328 ddebug
"Strip binaries"
329 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f |
xargs strip
--strip-unneeded | ddebug
333 mkdir
-p $initdir/etc
/rc.d
334 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
338 chmod 0755 $initdir/etc
/rc.d
/rc.
local
342 ddebug
"install any Execs from the service files"
344 export PKG_CONFIG_PATH
=$TEST_BASE_DIR/..
/src
/core
/
345 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
346 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
347 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
349 i
=${i##Exec*=}; i
=${i##-}
355 generate_module_dependencies
() {
356 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
357 ! depmod
-a -b "$initdir" $KERNEL_VER; then
358 dfatal
"\"depmod -a $KERNEL_VER\" failed."
363 install_depmod_files
() {
364 inst
/lib
/modules
/$KERNEL_VER/modules.order
365 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
369 # install plymouth, if found... else remove plymouth service files
370 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
371 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
372 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
373 # dracut_install plymouth plymouthd
375 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
379 install_ld_so_conf
() {
380 cp -a /etc
/ld.so.conf
* $initdir/etc
381 ldconfig
-r "$initdir"
384 install_config_files
() {
385 inst
/etc
/sysconfig
/init
391 inst
/etc
/nsswitch.conf
396 # we want an empty environment
397 > $initdir/etc
/environment
398 > $initdir/etc
/machine-id
400 echo systemd-testsuite
> $initdir/etc
/hostname
402 cat >$initdir/etc
/fstab
<<EOF
403 LABEL=systemd / ${FSTYPE} rw 0 1
407 install_basic_tools
() {
408 [[ $BASICTOOLS ]] && dracut_install
$BASICTOOLS
409 dracut_install
-o sushell
410 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
411 dracut_install
-o ldconfig.real
414 install_debug_tools
() {
415 [[ $DEBUGTOOLS ]] && dracut_install
$DEBUGTOOLS
419 # install libnss_files for login
420 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
421 dracut_install
$NSS_LIBS
425 inst
$ROOTLIBDIR/system
/dbus.socket
426 inst
$ROOTLIBDIR/system
/dbus.service
429 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
430 |
while read file; do
437 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
&& find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
442 /lib
/security
-xtype f \
443 ) |
while read file; do
447 # pam_unix depends on unix_chkpwd.
448 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
449 dracut_install
-o unix_chkpwd
451 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
452 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
454 # set empty root password for easy debugging
455 sed -i 's/^root:x:/root::/' $initdir/etc
/passwd
460 /usr
/lib
/kbd
/keymaps
/include
/* \
461 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
462 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
*; do
463 [[ -f $i ]] ||
continue
470 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
471 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
472 [[ -f $i ]] ||
continue
478 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
479 [ -f ${_terminfodir}/l
/linux
] && break
481 dracut_install
-o ${_terminfodir}/l
/linux
485 cp $TEST_BASE_DIR/testsuite.target
$initdir/etc
/systemd
/system
/
486 cp $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/
488 mkdir
-p $initdir/etc
/systemd
/system
/testsuite.target.wants
489 ln -fs $TEST_BASE_DIR/testsuite.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/testsuite.service
490 ln -fs $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/end.service
492 # make the testsuite the default target
493 ln -fs testsuite.target
$initdir/etc
/systemd
/system
/default.target
496 setup_nspawn_root
() {
497 rm -fr $TESTDIR/nspawn-root
498 ddebug
"cp -ar $initdir $TESTDIR/nspawn-root"
499 cp -ar $initdir $TESTDIR/nspawn-root
500 # we don't mount in the nspawn root
501 rm -f $TESTDIR/nspawn-root
/etc
/fstab
505 mkdir
-p $initdir/run
506 mkdir
-p $initdir/etc
/systemd
/system
507 mkdir
-p $initdir/var
/log
/journal
509 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
510 if [ -L "/$d" ]; then
517 ln -sfn /run
"$initdir/var/run"
518 ln -sfn /run
/lock
"$initdir/var/lock"
523 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
526 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
527 [[ $_line = 'not a dynamic executable' ]] && break
529 if [[ $_line =~
$_so_regex ]]; then
530 _file
=${BASH_REMATCH[1]}
531 [[ -e ${initdir}/$_file ]] && continue
532 inst_library
"$_file"
536 if [[ $_line =~ not\ found
]]; then
537 dfatal
"Missing a shared library required by $_bin."
538 dfatal
"Run \"ldd $_bin\" to find out what it is."
540 dfatal
"dracut cannot create an initrd."
548 [[ -e $STATEFILE ]] && .
$STATEFILE
549 if [[ -z "$TESTDIR" ]] ||
[[ ! -d "$TESTDIR" ]]; then
550 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
551 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
557 initdir
=$TESTDIR/root
561 ## @brief Converts numeric logging level to the first letter of level name.
563 # @param lvl Numeric logging level in range from 1 to 6.
564 # @retval 1 if @a lvl is out of range.
565 # @retval 0 if @a lvl is correct.
566 # @result Echoes first letter of level name.
579 ## @brief Internal helper function for _do_dlog()
581 # @param lvl Numeric logging level.
582 # @param msg Message.
583 # @retval 0 It's always returned, even if logging failed.
585 # @note This function is not supposed to be called manually. Please use
586 # dtrace(), ddebug(), or others instead which wrap this one.
588 # This function calls _do_dlog() either with parameter msg, or if
589 # none is given, it will read standard input and will use every line as
593 # dwarn "This is a warning"
594 # echo "This is a warning" | dwarn
598 [ -z "$LOG_LEVEL" ] && return 0
599 [ $1 -le $LOG_LEVEL ] ||
return 0
600 local lvl
="$1"; shift
601 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
603 if [ $# -ge 1 ]; then
607 echo "$lvlc: " "$line"
612 ## @brief Logs message at TRACE level (6)
614 # @param msg Message.
615 # @retval 0 It's always returned, even if logging failed.
619 [ -n "$debug" ] && set -x ||
:
622 ## @brief Logs message at DEBUG level (5)
624 # @param msg Message.
625 # @retval 0 It's always returned, even if logging failed.
629 # [ -n "$debug" ] && set -x || :
632 ## @brief Logs message at INFO level (4)
634 # @param msg Message.
635 # @retval 0 It's always returned, even if logging failed.
639 [ -n "$debug" ] && set -x ||
:
642 ## @brief Logs message at WARN level (3)
644 # @param msg Message.
645 # @retval 0 It's always returned, even if logging failed.
649 [ -n "$debug" ] && set -x ||
:
652 ## @brief Logs message at ERROR level (2)
654 # @param msg Message.
655 # @retval 0 It's always returned, even if logging failed.
659 # [ -n "$debug" ] && set -x || :
662 ## @brief Logs message at FATAL level (1)
664 # @param msg Message.
665 # @retval 0 It's always returned, even if logging failed.
669 [ -n "$debug" ] && set -x ||
:
673 # Generic substring function. If $2 is in $1, return 0.
674 strstr
() { [ "${1#*$2*}" != "$1" ]; }
676 # normalize_path <path>
677 # Prints the normalized path, where it removes any duplicated
678 # and trailing slashes.
680 # $ normalize_path ///test/test//
684 set -- "${1//+(\/)//}"
689 # convert_abs_rel <from> <to>
690 # Prints the relative path, when creating a symlink to <to> from <from>.
692 # $ convert_abs_rel /usr/bin/test /bin/test-2
694 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
696 local __current __absolute __abssize __cursize __newpath
699 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
701 # corner case #1 - self looping link
702 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
704 # corner case #2 - own dir link
705 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
707 IFS
="/" __current
=($1)
708 IFS
="/" __absolute
=($2)
710 __abssize
=${#__absolute[@]}
711 __cursize
=${#__current[@]}
713 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
716 if (( __level
> __abssize || __level
> __cursize
))
722 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
726 __newpath
=$__newpath"/"
728 __newpath
=$__newpath".."
731 for ((__i
= __level
; __i
< __abssize
; __i
++))
733 if [[ -n $__newpath ]]
735 __newpath
=$__newpath"/"
737 __newpath
=$__newpath${__absolute[__i]}
744 # Install a directory, keeping symlinks as on the original system.
745 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
746 # will create ${initdir}/lib64, ${initdir}/lib64/file,
747 # and a symlink ${initdir}/lib -> lib64.
749 [[ -e ${initdir}/"$1" ]] && return 0 # already there
751 local _dir
="$1" _part
="${1%/*}" _file
752 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
757 # iterate over parent directories
758 for _file
in $_dir; do
759 [[ -e "${initdir}/$_file" ]] && continue
760 if [[ -L $_file ]]; then
761 inst_symlink
"$_file"
764 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
765 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
766 chmod u
+w
"${initdir}/$_file"
771 # $1 = file to copy to ramdisk
772 # $2 (optional) Name for the file on the ramdisk
773 # Location of the image dir is assumed to be $initdir
774 # We never overwrite the target if it exists.
776 [[ -f "$1" ]] ||
return 1
777 strstr
"$1" "/" ||
return 1
779 local _src
=$1 target
="${2:-$1}"
780 if ! [[ -d ${initdir}/$target ]]; then
781 [[ -e ${initdir}/$target ]] && return 0
782 [[ -L ${initdir}/$target ]] && return 0
783 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
785 # install checksum files also
786 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
787 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
789 ddebug
"Installing $_src"
790 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
793 # find symlinks linked to given library file
795 # Function searches for symlinks by stripping version numbers appended to
796 # library filename, checks if it points to the same target and finally
797 # prints the list of symlinks to stdout.
800 # rev_lib_symlinks libfoo.so.8.1
801 # output: libfoo.so.8 libfoo.so
802 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
804 [[ ! $1 ]] && return 0
806 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
808 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
810 until [[ ${fn##*.} == so
]]; do
812 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
818 # Same as above, but specialized to handle dynamic libraries.
819 # It handles making symlinks according to how the original library
822 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
823 strstr "$1" "/" || return 1
824 [[ -e $initdir/$_dest ]] && return 0
825 if [[ -L $_src ]]; then
826 # install checksum files also
827 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
828 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
830 _reallib=$(readlink -f "$_src")
831 inst_simple "$_reallib" "$_reallib"
832 inst_dir "${_dest%/*}"
833 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
834 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
836 inst_simple
"$_src" "$_dest"
839 # Create additional symlinks. See rev_symlinks description.
840 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
841 [[ ! -e $initdir/$_symlink ]] && {
842 ddebug
"Creating extra symlink: $_symlink"
843 inst_symlink
$_symlink
848 # find a binary. If we were not passed the full path directly,
849 # search in the usual places to find the binary.
851 if [[ -z ${1##/*} ]]; then
852 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
861 # Same as above, but specialized to install binary executables.
862 # Install binary executable, and all shared library dependencies, if any.
865 _bin
=$
(find_binary
"$1") ||
return 1
867 [[ -e $initdir/$_target ]] && return 0
868 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
870 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
872 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
873 [[ $_line = 'not a dynamic executable' ]] && break
875 if [[ $_line =~
$_so_regex ]]; then
876 _file
=${BASH_REMATCH[1]}
877 [[ -e ${initdir}/$_file ]] && continue
878 inst_library
"$_file"
882 if [[ $_line =~ not\ found
]]; then
883 dfatal
"Missing a shared library required by $_bin."
884 dfatal
"Run \"ldd $_bin\" to find out what it is."
886 dfatal
"dracut cannot create an initrd."
890 inst_simple
"$_bin" "$_target"
893 # same as above, except for shell scripts.
894 # If your shell script does not start with shebang, it is not a shell script.
897 _bin
=$
(find_binary
"$1") ||
return 1
899 local _line _shebang_regex
900 read -r -n 80 _line
<"$_bin"
901 # If debug is set, clean unprintable chars to prevent messing up the term
902 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
903 _shebang_regex
='(#! *)(/[^ ]+).*'
904 [[ $_line =~
$_shebang_regex ]] ||
return 1
905 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
908 # same as above, but specialized for symlinks
910 local _src
=$1 _target
=${2:-$1} _realsrc
911 strstr
"$1" "/" ||
return 1
912 [[ -L $1 ]] ||
return 1
913 [[ -L $initdir/$_target ]] && return 0
914 _realsrc
=$
(readlink
-f "$_src")
915 if ! [[ -e $initdir/$_realsrc ]]; then
916 if [[ -d $_realsrc ]]; then
922 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
923 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
924 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
927 # attempt to install any programs specified in a udev rule
928 inst_rule_programs
() {
931 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
932 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
933 if [ -x /lib
/udev
/$_prog ]; then
934 _bin
=/lib
/udev
/$_prog
936 _bin
=$
(find_binary
"$_prog") ||
{
937 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
942 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
943 dracut_install
"$_bin"
948 # udev rules always get installed in the same place, so
949 # create a function to install them to make life simpler.
951 local _target
=/etc
/udev
/rules.d _rule _found
953 inst_dir
"/lib/udev/rules.d"
955 for _rule
in "$@"; do
956 if [ "${rule#/}" = "$rule" ]; then
957 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
958 if [[ -f $r/$_rule ]]; then
960 inst_simple
"$_found"
961 inst_rule_programs
"$_found"
965 for r
in '' .
/ $dracutbasedir/rules.d
/; do
966 if [[ -f ${r}$_rule ]]; then
968 inst_simple
"$_found" "$_target/${_found##*/}"
969 inst_rule_programs
"$_found"
972 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
976 # general purpose installation function
977 # Same args as above.
983 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
984 [[ $initdir = $2 ]] && set $1;;
985 3) [[ -z $initdir ]] && export initdir
=$2
987 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
990 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
996 # install any of listed files
998 # If first argument is '-d' and second some destination path, first accessible
999 # source is installed into this path, otherwise it will installed in the same
1000 # path as source. If none of listed files was installed, function return 1.
1001 # On first successful installation it returns with 0 status.
1005 # inst_any -d /bin/foo /bin/bar /bin/baz
1007 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1012 [[ $1 = '-d' ]] && to
="$2" && shift 2
1015 if [[ -e $f ]]; then
1016 [[ $to ]] && inst
"$f" "$to" && return 0
1017 inst
"$f" && return 0
1024 # dracut_install [-o ] <file> [<file> ... ]
1025 # Install <file> to the initramfs image
1026 # -o optionally install the <file> and don't fail, if it is not there
1029 if [[ $1 = '-o' ]]; then
1033 while (($# > 0)); do
1034 if ! inst
"$1" ; then
1035 if [[ $_optional = yes ]]; then
1036 dinfo
"Skipping program $1 as it cannot be found and is" \
1037 "flagged to be optional"
1039 dfatal
"Failed to install $1"
1047 # Install a single kernel module along with any firmware it may require.
1048 # $1 = full path to kernel module to install
1049 install_kmod_with_fw
() {
1050 # no need to go further if the module is already installed
1052 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1055 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1057 if [[ $omit_drivers ]]; then
1058 local _kmod
=${1##*/}
1061 if [[ "$_kmod" =~
$omit_drivers ]]; then
1062 dinfo
"Omitting driver $_kmod"
1065 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1066 dinfo
"Omitting driver $_kmod"
1071 [ -d "$initdir/.kernelmodseen" ] && \
1072 > "$initdir/.kernelmodseen/${1##*/}"
1074 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1077 local _modname
=${1##*/} _fwdir _found _fw
1078 _modname
=${_modname%.ko*}
1079 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1081 for _fwdir
in $fw_dir; do
1082 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1083 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1087 if [[ $_found != yes ]]; then
1088 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1089 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1090 "\"${_modname}.ko\""
1092 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1093 "\"${_modname}.ko\""
1100 # Do something with all the dependencies of a kernel module.
1101 # Note that kernel modules depend on themselves using the technique we use
1102 # $1 = function to call for each dependency we find
1103 # It will be passed the full path to the found kernel module
1104 # $2 = module to get dependencies for
1105 # rest of args = arguments to modprobe
1106 # _fderr specifies FD passed from surrounding scope
1107 for_each_kmod_dep
() {
1108 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1110 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1111 while read _cmd _modpath _options
; do
1112 [[ $_cmd = insmod
]] ||
continue
1113 $_func ${_modpath} ||
exit $?
1116 [[ $_found -eq 0 ]] && exit 1
1121 # filter kernel modules to install certain modules that meet specific
1123 # $1 = search only in subdirectory of /kernel/$1
1124 # $2 = function to call with module name to filter.
1125 # This function will be passed the full path to the module to test.
1126 # The behavior of this function can vary depending on whether $hostonly is set.
1127 # If it is, we will only look at modules that are already in memory.
1128 # If it is not, we will look at all kernel modules
1129 # This function returns the full filenames of modules that match $1
1130 filter_kernel_modules_by_path
() (
1131 local _modname _filtercmd
1132 if ! [[ $hostonly ]]; then
1133 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1134 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1135 _filtercmd
+=' -o -name "*.ko.xz"'
1136 _filtercmd
+=' 2>/dev/null'
1138 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1139 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1141 for _modname
in $
(eval $_filtercmd); do
1143 *.ko
) "$2" "$_modname" && echo "$_modname";;
1144 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1145 $2 $initdir/$$.ko
&& echo "$_modname"
1146 rm -f $initdir/$$.ko
1148 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1149 $2 $initdir/$$.ko
&& echo "$_modname"
1150 rm -f $initdir/$$.ko
1155 find_kernel_modules_by_path
() (
1156 if ! [[ $hostonly ]]; then
1157 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1158 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1160 cut
-d " " -f 1 </proc
/modules \
1161 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1165 filter_kernel_modules
() {
1166 filter_kernel_modules_by_path drivers
"$1"
1169 find_kernel_modules
() {
1170 find_kernel_modules_by_path drivers
1173 # instmods [-c] <kernel module> [<kernel module> ... ]
1174 # instmods [-c] <kernel subsystem>
1175 # install kernel modules along with all their dependencies.
1176 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1178 [[ $no_kernel = yes ]] && return
1179 # called [sub]functions inherit _fderr
1182 if [[ $1 = '-c' ]]; then
1187 function inst1mod
() {
1188 local _ret
=0 _mod
="$1"
1191 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1192 ( [[ "$_mpargs" ]] && echo $_mpargs
1193 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1196 ( [[ "$_mpargs" ]] && echo $_mpargs
1197 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1201 --*) _mpargs
+=" $_mod" ;;
1202 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1205 # if we are already installed, skip this module and go on
1207 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1209 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1210 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1213 # If we are building a host-specific initramfs and this
1214 # module is not already loaded, move on to the next one.
1215 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1216 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1219 # We use '-d' option in modprobe only if modules prefix path
1220 # differs from default '/'. This allows us to use Dracut with
1221 # old version of modprobe which doesn't have '-d' option.
1222 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1223 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1225 # ok, load the module, all its dependencies, and any firmware
1227 for_each_kmod_dep install_kmod_with_fw
$_mod \
1228 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1235 function instmods_1
() {
1237 if (($# == 0)); then # filenames from stdin
1239 inst1mod
"${_mod%.ko*}" ||
{
1240 if [ "$_check" = "yes" ]; then
1241 dfatal
"Failed to install $_mod"
1247 while (($# > 0)); do # filenames as arguments
1248 inst1mod
${1%.ko*} ||
{
1249 if [ "$_check" = "yes" ]; then
1250 dfatal
"Failed to install $1"
1259 local _ret _filter_not_found
='FATAL: Module .* not found.'
1261 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1262 # redirections, but that would make dracut require bash4 at least.
1263 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1264 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1270 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1271 # Install a <file> located on a lib directory to the initramfs image
1272 # -n <pattern> install non-matching files
1273 inst_libdir_file
() {
1274 if [[ "$1" == "-n" ]]; then
1277 for _dir
in $libdirs; do
1279 for _f
in "$_dir"/$_i; do
1280 [[ "$_i" =~
$_pattern ]] ||
continue
1281 [[ -e "$_i" ]] && dracut_install
"$_i"
1286 for _dir
in $libdirs; do
1288 for _f
in "$_dir"/$_i; do
1289 [[ -e "$_f" ]] && dracut_install
"$_f"
1297 if [[ $UID != "0" ]]; then
1298 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1303 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
1304 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
1307 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
1308 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
1314 while (($# > 0)); do
1317 echo "TEST RUN: $TEST_DESCRIPTION"
1320 if [ $ret -eq 0 ]; then
1321 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1323 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1327 echo "TEST SETUP: $TEST_DESCRIPTION"
1331 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1337 echo -n "TEST: $TEST_DESCRIPTION ";
1339 test_setup
&& test_run
1345 ) </dev
/null
>test.log
2>&1
1347 if [ $ret -eq 0 ]; then
1352 echo "see $(pwd)/test.log"