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 FSTYPE
="${FSTYPE:-ext3}"
13 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
14 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
15 ROOTLIBDIR
=/usr
/lib
/systemd
18 BASICTOOLS
="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm"
19 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"
21 function find_qemu_bin
() {
22 # SUSE and Red Hat call the binary qemu-kvm
23 # Debian and Gentoo call it kvm
24 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
26 [ "$ARCH" ] || ARCH
=$
(uname
-m)
29 # QEMU's own build system calls it qemu-system-x86_64
30 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
33 # new i386 version of QEMU
34 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
36 # i386 version of QEMU
37 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
41 if [ ! -e "$QEMU_BIN" ]; then
42 echo "Could not find a suitable QEMU binary" >&2
48 if [ -f /etc
/machine-id
]; then
49 read MACHINE_ID
< /etc
/machine-id
50 [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
51 && INITRD
="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
52 [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
53 && KERNEL_BIN
="/boot/$MACHINE_ID/$KERNEL_VER/linux"
56 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
57 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
58 [ "$KERNEL_BIN" ] || KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
59 [ "$INITRD" ] ||
{ [ -e "$default_fedora_initrd" ] && INITRD
=$default_fedora_initrd; }
60 [ "$INITRD" ] ||
{ [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD
=$default_debian_initrd; }
61 [ "$QEMU_SMP" ] || QEMU_SMP
=1
63 find_qemu_bin ||
return 1
65 KERNEL_APPEND
="root=/dev/sda1 \
66 systemd.log_level=debug \
69 init=$ROOTLIBDIR/systemd \
76 QEMU_OPTIONS
="-smp $QEMU_SMP \
83 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
84 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
87 if [ -c /dev
/kvm
]; then
88 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
91 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
92 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
95 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img
) ||
return 1
100 ..
/..
/systemd-nspawn
--register=no
--directory=$TESTDIR/nspawn-root
$ROOTLIBDIR/systemd
$KERNEL_APPEND
103 setup_basic_environment
() {
104 # create the basic filesystem layout
108 install_missing_libraries
126 generate_module_dependencies
130 # don't forget KERNEL_APPEND='... selinux=1 ...'
131 if [[ "$SETUP_SELINUX" != "yes" ]]; then
132 ddebug
"Don't setup SELinux"
135 ddebug
"Setup SELinux"
136 local _conf_dir
=/etc
/selinux
137 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
139 rm -rf $initdir/$_conf_dir
140 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
141 dfatal
"Failed to copy $_conf_dir"
145 cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
147 Description=Relabel all filesystems
148 DefaultDependencies=no
149 Requires=local-fs.target
150 Conflicts=shutdown.target
151 After=local-fs.target
152 Before=sysinit.target shutdown.target
153 ConditionSecurity=selinux
154 ConditionPathExists=|/.autorelabel
157 ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
163 touch $initdir/.autorelabel
164 mkdir
-p $initdir/etc
/systemd
/system
/basic.target.wants
165 ln -fs autorelabel.service
$initdir/etc
/systemd
/system
/basic.target.wants
/autorelabel.service
167 dracut_install
$_fixfiles_tools
168 dracut_install fixfiles
169 dracut_install sestatus
173 if ! type -p valgrind
; then
174 dfatal
"Failed to install valgrind"
178 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
179 dracut_install
$_valgrind_bins
181 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
182 dracut_install
$_valgrind_libs
184 local _valgrind_dbg_and_supp
=$
(
185 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
186 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
188 dracut_install
$_valgrind_dbg_and_supp
191 create_valgrind_wrapper
() {
192 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
193 ddebug
"Create $_valgrind_wrapper"
194 cat >$_valgrind_wrapper <<EOF
197 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
199 chmod 0755 $_valgrind_wrapper
203 dracut_install
/sbin
/fsck
*
204 dracut_install
-o /bin
/fsck
*
208 instmods dm_crypt
=crypto
209 type -P dmeventd
>/dev
/null
&& dracut_install dmeventd
210 inst_libdir_file
"libdevmapper-event.so*"
211 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
212 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
213 # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
214 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
216 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
221 # install compiled files
222 (cd $TEST_BASE_DIR/..
; set -x; make DESTDIR
=$initdir install)
223 # remove unneeded documentation
224 rm -fr $initdir/usr
/share
/{man
,doc
}
225 # we strip binaries since debug symbols increase binaries size a lot
226 # and it could fill the available space
230 install_missing_libraries
() {
231 # install possible missing libraries
232 for i
in $initdir/{sbin
,bin
}/* $initdir/lib
/systemd
/*; do
237 create_empty_image
() {
238 rm -f "$TESTDIR/rootdisk.img"
239 # Create the blank file to use as a root filesystem
240 dd if=/dev
/null of
="$TESTDIR/rootdisk.img" bs
=1M seek
=400
241 LOOPDEV
=$
(losetup
--show -P -f $TESTDIR/rootdisk.img
)
242 [ -b "$LOOPDEV" ] ||
return 1
243 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
244 sfdisk
"$LOOPDEV" <<EOF
249 mkfs
-t "$FSTYPE" -L systemd
"${LOOPDEV}p1"
252 check_result_nspawn
() {
254 [[ -e $TESTDIR/nspawn-root
/testok
]] && ret
=0
255 [[ -f $TESTDIR/nspawn-root
/failed
]] && cp -a $TESTDIR/nspawn-root
/failed
$TESTDIR
256 cp -a $TESTDIR/nspawn-root
/var
/log
/journal
$TESTDIR
257 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
258 ls -l $TESTDIR/journal
/*/*.journal
259 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
264 if [[ "$STRIP_BINARIES" = "no" ]]; then
265 ddebug
"Don't strip binaries"
268 ddebug
"Strip binaries"
269 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f |
xargs strip
--strip-unneeded | ddebug
273 mkdir
-p $initdir/etc
/rc.d
274 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
278 chmod 0755 $initdir/etc
/rc.d
/rc.
local
282 ddebug
"install any Execs from the service files"
284 export PKG_CONFIG_PATH
=$TEST_BASE_DIR/..
/src
/core
/
285 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
286 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
287 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
289 i
=${i##Exec*=}; i
=${i##-}
295 generate_module_dependencies
() {
296 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
297 ! depmod
-a -b "$initdir" $KERNEL_VER; then
298 dfatal
"\"depmod -a $KERNEL_VER\" failed."
303 install_depmod_files
() {
304 inst
/lib
/modules
/$KERNEL_VER/modules.order
305 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
309 # install plymouth, if found... else remove plymouth service files
310 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
311 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
312 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
313 # dracut_install plymouth plymouthd
315 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
319 install_ld_so_conf
() {
320 cp -a /etc
/ld.so.conf
* $initdir/etc
321 ldconfig
-r "$initdir"
324 install_config_files
() {
325 inst
/etc
/sysconfig
/init
331 inst
/etc
/nsswitch.conf
336 # we want an empty environment
337 > $initdir/etc
/environment
338 > $initdir/etc
/machine-id
340 echo systemd-testsuite
> $initdir/etc
/hostname
342 cat >$initdir/etc
/fstab
<<EOF
343 LABEL=systemd / ext3 rw 0 1
347 install_basic_tools
() {
348 [[ $BASICTOOLS ]] && dracut_install
$BASICTOOLS
349 dracut_install
-o sushell
350 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
351 dracut_install
-o ldconfig.real
354 install_debug_tools
() {
355 [[ $DEBUGTOOLS ]] && dracut_install
$DEBUGTOOLS
359 # install libnss_files for login
360 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
361 dracut_install
$NSS_LIBS
365 inst
$ROOTLIBDIR/system
/dbus.socket
366 inst
$ROOTLIBDIR/system
/dbus.service
369 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
370 |
while read file; do
377 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
&& find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
382 /lib
/security
-xtype f \
383 ) |
while read file; do
387 # pam_unix depends on unix_chkpwd.
388 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
389 dracut_install
-o unix_chkpwd
391 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
392 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
397 /usr
/lib
/kbd
/keymaps
/include
/* \
398 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
399 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
*; do
400 [[ -f $i ]] ||
continue
407 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
408 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
409 [[ -f $i ]] ||
continue
415 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
416 [ -f ${_terminfodir}/l
/linux
] && break
418 dracut_install
-o ${_terminfodir}/l
/linux
422 cp $TEST_BASE_DIR/testsuite.target
$initdir/etc
/systemd
/system
/
423 cp $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/
425 mkdir
-p $initdir/etc
/systemd
/system
/testsuite.target.wants
426 ln -fs $TEST_BASE_DIR/testsuite.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/testsuite.service
427 ln -fs $TEST_BASE_DIR/end.service
$initdir/etc
/systemd
/system
/testsuite.target.wants
/end.service
429 # make the testsuite the default target
430 ln -fs testsuite.target
$initdir/etc
/systemd
/system
/default.target
433 setup_nspawn_root
() {
434 rm -fr $TESTDIR/nspawn-root
435 ddebug
"cp -ar $initdir $TESTDIR/nspawn-root"
436 cp -ar $initdir $TESTDIR/nspawn-root
437 # we don't mount in the nspawn root
438 rm -f $TESTDIR/nspawn-root
/etc
/fstab
442 mkdir
-p $initdir/run
443 mkdir
-p $initdir/etc
/systemd
/system
444 mkdir
-p $initdir/var
/log
/journal
446 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
447 if [ -L "/$d" ]; then
454 ln -sfn /run
"$initdir/var/run"
455 ln -sfn /run
/lock
"$initdir/var/lock"
460 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
463 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
464 [[ $_line = 'not a dynamic executable' ]] && break
466 if [[ $_line =~
$_so_regex ]]; then
467 _file
=${BASH_REMATCH[1]}
468 [[ -e ${initdir}/$_file ]] && continue
469 inst_library
"$_file"
473 if [[ $_line =~ not\ found
]]; then
474 dfatal
"Missing a shared library required by $_bin."
475 dfatal
"Run \"ldd $_bin\" to find out what it is."
477 dfatal
"dracut cannot create an initrd."
485 [[ -e $STATEFILE ]] && .
$STATEFILE
486 if [[ -z "$TESTDIR" ]] ||
[[ ! -d "$TESTDIR" ]]; then
487 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
488 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
494 initdir
=$TESTDIR/root
498 ## @brief Converts numeric logging level to the first letter of level name.
500 # @param lvl Numeric logging level in range from 1 to 6.
501 # @retval 1 if @a lvl is out of range.
502 # @retval 0 if @a lvl is correct.
503 # @result Echoes first letter of level name.
516 ## @brief Internal helper function for _do_dlog()
518 # @param lvl Numeric logging level.
519 # @param msg Message.
520 # @retval 0 It's always returned, even if logging failed.
522 # @note This function is not supposed to be called manually. Please use
523 # dtrace(), ddebug(), or others instead which wrap this one.
525 # This function calls _do_dlog() either with parameter msg, or if
526 # none is given, it will read standard input and will use every line as
530 # dwarn "This is a warning"
531 # echo "This is a warning" | dwarn
535 [ -z "$LOG_LEVEL" ] && return 0
536 [ $1 -le $LOG_LEVEL ] ||
return 0
537 local lvl
="$1"; shift
538 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
540 if [ $# -ge 1 ]; then
544 echo "$lvlc: " "$line"
549 ## @brief Logs message at TRACE level (6)
551 # @param msg Message.
552 # @retval 0 It's always returned, even if logging failed.
556 [ -n "$debug" ] && set -x ||
:
559 ## @brief Logs message at DEBUG level (5)
561 # @param msg Message.
562 # @retval 0 It's always returned, even if logging failed.
566 # [ -n "$debug" ] && set -x || :
569 ## @brief Logs message at INFO level (4)
571 # @param msg Message.
572 # @retval 0 It's always returned, even if logging failed.
576 [ -n "$debug" ] && set -x ||
:
579 ## @brief Logs message at WARN level (3)
581 # @param msg Message.
582 # @retval 0 It's always returned, even if logging failed.
586 [ -n "$debug" ] && set -x ||
:
589 ## @brief Logs message at ERROR level (2)
591 # @param msg Message.
592 # @retval 0 It's always returned, even if logging failed.
596 # [ -n "$debug" ] && set -x || :
599 ## @brief Logs message at FATAL level (1)
601 # @param msg Message.
602 # @retval 0 It's always returned, even if logging failed.
606 [ -n "$debug" ] && set -x ||
:
610 # Generic substring function. If $2 is in $1, return 0.
611 strstr
() { [ "${1#*$2*}" != "$1" ]; }
613 # normalize_path <path>
614 # Prints the normalized path, where it removes any duplicated
615 # and trailing slashes.
617 # $ normalize_path ///test/test//
621 set -- "${1//+(\/)//}"
626 # convert_abs_rel <from> <to>
627 # Prints the relative path, when creating a symlink to <to> from <from>.
629 # $ convert_abs_rel /usr/bin/test /bin/test-2
631 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
633 local __current __absolute __abssize __cursize __newpath
636 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
638 # corner case #1 - self looping link
639 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
641 # corner case #2 - own dir link
642 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
644 IFS
="/" __current
=($1)
645 IFS
="/" __absolute
=($2)
647 __abssize
=${#__absolute[@]}
648 __cursize
=${#__current[@]}
650 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
653 if (( __level
> __abssize || __level
> __cursize
))
659 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
663 __newpath
=$__newpath"/"
665 __newpath
=$__newpath".."
668 for ((__i
= __level
; __i
< __abssize
; __i
++))
670 if [[ -n $__newpath ]]
672 __newpath
=$__newpath"/"
674 __newpath
=$__newpath${__absolute[__i]}
681 # Install a directory, keeping symlinks as on the original system.
682 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
683 # will create ${initdir}/lib64, ${initdir}/lib64/file,
684 # and a symlink ${initdir}/lib -> lib64.
686 [[ -e ${initdir}/"$1" ]] && return 0 # already there
688 local _dir
="$1" _part
="${1%/*}" _file
689 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
694 # iterate over parent directories
695 for _file
in $_dir; do
696 [[ -e "${initdir}/$_file" ]] && continue
697 if [[ -L $_file ]]; then
698 inst_symlink
"$_file"
701 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
702 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
703 chmod u
+w
"${initdir}/$_file"
708 # $1 = file to copy to ramdisk
709 # $2 (optional) Name for the file on the ramdisk
710 # Location of the image dir is assumed to be $initdir
711 # We never overwrite the target if it exists.
713 [[ -f "$1" ]] ||
return 1
714 strstr
"$1" "/" ||
return 1
716 local _src
=$1 target
="${2:-$1}"
717 if ! [[ -d ${initdir}/$target ]]; then
718 [[ -e ${initdir}/$target ]] && return 0
719 [[ -L ${initdir}/$target ]] && return 0
720 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
722 # install checksum files also
723 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
724 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
726 ddebug
"Installing $_src"
727 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
730 # find symlinks linked to given library file
732 # Function searches for symlinks by stripping version numbers appended to
733 # library filename, checks if it points to the same target and finally
734 # prints the list of symlinks to stdout.
737 # rev_lib_symlinks libfoo.so.8.1
738 # output: libfoo.so.8 libfoo.so
739 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
741 [[ ! $1 ]] && return 0
743 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
745 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
747 until [[ ${fn##*.} == so
]]; do
749 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
755 # Same as above, but specialized to handle dynamic libraries.
756 # It handles making symlinks according to how the original library
759 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
760 strstr "$1" "/" || return 1
761 [[ -e $initdir/$_dest ]] && return 0
762 if [[ -L $_src ]]; then
763 # install checksum files also
764 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
765 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
767 _reallib=$(readlink -f "$_src")
768 inst_simple "$_reallib" "$_reallib"
769 inst_dir "${_dest%/*}"
770 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
771 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
773 inst_simple
"$_src" "$_dest"
776 # Create additional symlinks. See rev_symlinks description.
777 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
778 [[ ! -e $initdir/$_symlink ]] && {
779 ddebug
"Creating extra symlink: $_symlink"
780 inst_symlink
$_symlink
785 # find a binary. If we were not passed the full path directly,
786 # search in the usual places to find the binary.
788 if [[ -z ${1##/*} ]]; then
789 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
798 # Same as above, but specialized to install binary executables.
799 # Install binary executable, and all shared library dependencies, if any.
802 _bin
=$
(find_binary
"$1") ||
return 1
804 [[ -e $initdir/$_target ]] && return 0
805 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
807 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
809 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
810 [[ $_line = 'not a dynamic executable' ]] && break
812 if [[ $_line =~
$_so_regex ]]; then
813 _file
=${BASH_REMATCH[1]}
814 [[ -e ${initdir}/$_file ]] && continue
815 inst_library
"$_file"
819 if [[ $_line =~ not\ found
]]; then
820 dfatal
"Missing a shared library required by $_bin."
821 dfatal
"Run \"ldd $_bin\" to find out what it is."
823 dfatal
"dracut cannot create an initrd."
827 inst_simple
"$_bin" "$_target"
830 # same as above, except for shell scripts.
831 # If your shell script does not start with shebang, it is not a shell script.
834 _bin
=$
(find_binary
"$1") ||
return 1
836 local _line _shebang_regex
837 read -r -n 80 _line
<"$_bin"
838 # If debug is set, clean unprintable chars to prevent messing up the term
839 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
840 _shebang_regex
='(#! *)(/[^ ]+).*'
841 [[ $_line =~
$_shebang_regex ]] ||
return 1
842 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
845 # same as above, but specialized for symlinks
847 local _src
=$1 _target
=${2:-$1} _realsrc
848 strstr
"$1" "/" ||
return 1
849 [[ -L $1 ]] ||
return 1
850 [[ -L $initdir/$_target ]] && return 0
851 _realsrc
=$
(readlink
-f "$_src")
852 if ! [[ -e $initdir/$_realsrc ]]; then
853 if [[ -d $_realsrc ]]; then
859 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
860 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
861 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
864 # attempt to install any programs specified in a udev rule
865 inst_rule_programs
() {
868 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
869 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
870 if [ -x /lib
/udev
/$_prog ]; then
871 _bin
=/lib
/udev
/$_prog
873 _bin
=$
(find_binary
"$_prog") ||
{
874 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
879 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
880 dracut_install
"$_bin"
885 # udev rules always get installed in the same place, so
886 # create a function to install them to make life simpler.
888 local _target
=/etc
/udev
/rules.d _rule _found
890 inst_dir
"/lib/udev/rules.d"
892 for _rule
in "$@"; do
893 if [ "${rule#/}" = "$rule" ]; then
894 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
895 if [[ -f $r/$_rule ]]; then
897 inst_simple
"$_found"
898 inst_rule_programs
"$_found"
902 for r
in '' .
/ $dracutbasedir/rules.d
/; do
903 if [[ -f ${r}$_rule ]]; then
905 inst_simple
"$_found" "$_target/${_found##*/}"
906 inst_rule_programs
"$_found"
909 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
913 # general purpose installation function
914 # Same args as above.
920 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
921 [[ $initdir = $2 ]] && set $1;;
922 3) [[ -z $initdir ]] && export initdir
=$2
924 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
927 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
933 # install any of listed files
935 # If first argument is '-d' and second some destination path, first accessible
936 # source is installed into this path, otherwise it will installed in the same
937 # path as source. If none of listed files was installed, function return 1.
938 # On first successful installation it returns with 0 status.
942 # inst_any -d /bin/foo /bin/bar /bin/baz
944 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
949 [[ $1 = '-d' ]] && to
="$2" && shift 2
953 [[ $to ]] && inst
"$f" "$to" && return 0
954 inst
"$f" && return 0
961 # dracut_install [-o ] <file> [<file> ... ]
962 # Install <file> to the initramfs image
963 # -o optionally install the <file> and don't fail, if it is not there
966 if [[ $1 = '-o' ]]; then
971 if ! inst
"$1" ; then
972 if [[ $_optional = yes ]]; then
973 dinfo
"Skipping program $1 as it cannot be found and is" \
974 "flagged to be optional"
976 dfatal
"Failed to install $1"
984 # Install a single kernel module along with any firmware it may require.
985 # $1 = full path to kernel module to install
986 install_kmod_with_fw
() {
987 # no need to go further if the module is already installed
989 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
992 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
994 if [[ $omit_drivers ]]; then
998 if [[ "$_kmod" =~
$omit_drivers ]]; then
999 dinfo
"Omitting driver $_kmod"
1002 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1003 dinfo
"Omitting driver $_kmod"
1008 [ -d "$initdir/.kernelmodseen" ] && \
1009 > "$initdir/.kernelmodseen/${1##*/}"
1011 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1014 local _modname
=${1##*/} _fwdir _found _fw
1015 _modname
=${_modname%.ko*}
1016 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1018 for _fwdir
in $fw_dir; do
1019 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1020 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1024 if [[ $_found != yes ]]; then
1025 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1026 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1027 "\"${_modname}.ko\""
1029 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1030 "\"${_modname}.ko\""
1037 # Do something with all the dependencies of a kernel module.
1038 # Note that kernel modules depend on themselves using the technique we use
1039 # $1 = function to call for each dependency we find
1040 # It will be passed the full path to the found kernel module
1041 # $2 = module to get dependencies for
1042 # rest of args = arguments to modprobe
1043 # _fderr specifies FD passed from surrounding scope
1044 for_each_kmod_dep
() {
1045 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1047 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1048 while read _cmd _modpath _options
; do
1049 [[ $_cmd = insmod
]] ||
continue
1050 $_func ${_modpath} ||
exit $?
1053 [[ $_found -eq 0 ]] && exit 1
1058 # filter kernel modules to install certain modules that meet specific
1060 # $1 = search only in subdirectory of /kernel/$1
1061 # $2 = function to call with module name to filter.
1062 # This function will be passed the full path to the module to test.
1063 # The behavior of this function can vary depending on whether $hostonly is set.
1064 # If it is, we will only look at modules that are already in memory.
1065 # If it is not, we will look at all kernel modules
1066 # This function returns the full filenames of modules that match $1
1067 filter_kernel_modules_by_path
() (
1068 local _modname _filtercmd
1069 if ! [[ $hostonly ]]; then
1070 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1071 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1072 _filtercmd
+=' -o -name "*.ko.xz"'
1073 _filtercmd
+=' 2>/dev/null'
1075 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1076 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1078 for _modname
in $
(eval $_filtercmd); do
1080 *.ko
) "$2" "$_modname" && echo "$_modname";;
1081 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1082 $2 $initdir/$$.ko
&& echo "$_modname"
1083 rm -f $initdir/$$.ko
1085 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1086 $2 $initdir/$$.ko
&& echo "$_modname"
1087 rm -f $initdir/$$.ko
1092 find_kernel_modules_by_path
() (
1093 if ! [[ $hostonly ]]; then
1094 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1095 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1097 cut
-d " " -f 1 </proc
/modules \
1098 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1102 filter_kernel_modules
() {
1103 filter_kernel_modules_by_path drivers
"$1"
1106 find_kernel_modules
() {
1107 find_kernel_modules_by_path drivers
1110 # instmods [-c] <kernel module> [<kernel module> ... ]
1111 # instmods [-c] <kernel subsystem>
1112 # install kernel modules along with all their dependencies.
1113 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1115 [[ $no_kernel = yes ]] && return
1116 # called [sub]functions inherit _fderr
1119 if [[ $1 = '-c' ]]; then
1124 function inst1mod
() {
1125 local _ret
=0 _mod
="$1"
1128 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1129 ( [[ "$_mpargs" ]] && echo $_mpargs
1130 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1133 ( [[ "$_mpargs" ]] && echo $_mpargs
1134 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1138 --*) _mpargs
+=" $_mod" ;;
1139 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1142 # if we are already installed, skip this module and go on
1144 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1146 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1147 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1150 # If we are building a host-specific initramfs and this
1151 # module is not already loaded, move on to the next one.
1152 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1153 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1156 # We use '-d' option in modprobe only if modules prefix path
1157 # differs from default '/'. This allows us to use Dracut with
1158 # old version of modprobe which doesn't have '-d' option.
1159 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1160 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1162 # ok, load the module, all its dependencies, and any firmware
1164 for_each_kmod_dep install_kmod_with_fw
$_mod \
1165 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1172 function instmods_1
() {
1174 if (($# == 0)); then # filenames from stdin
1176 inst1mod
"${_mod%.ko*}" ||
{
1177 if [ "$_check" = "yes" ]; then
1178 dfatal
"Failed to install $_mod"
1184 while (($# > 0)); do # filenames as arguments
1185 inst1mod
${1%.ko*} ||
{
1186 if [ "$_check" = "yes" ]; then
1187 dfatal
"Failed to install $1"
1196 local _ret _filter_not_found
='FATAL: Module .* not found.'
1198 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1199 # redirections, but that would make dracut require bash4 at least.
1200 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1201 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1207 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1208 # Install a <file> located on a lib directory to the initramfs image
1209 # -n <pattern> install non-matching files
1210 inst_libdir_file
() {
1211 if [[ "$1" == "-n" ]]; then
1214 for _dir
in $libdirs; do
1216 for _f
in "$_dir"/$_i; do
1217 [[ "$_i" =~
$_pattern ]] ||
continue
1218 [[ -e "$_i" ]] && dracut_install
"$_i"
1223 for _dir
in $libdirs; do
1225 for _f
in "$_dir"/$_i; do
1226 [[ -e "$_f" ]] && dracut_install
"$_f"
1234 [[ -d /run
/systemd
/system
]]
1239 if [[ $UID != "0" ]]; then
1240 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1245 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
1246 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
1249 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
1250 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
1256 while (($# > 0)); do
1259 echo "TEST RUN: $TEST_DESCRIPTION"
1262 if [ $ret -eq 0 ]; then
1263 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1265 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1269 echo "TEST SETUP: $TEST_DESCRIPTION"
1273 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1279 echo -n "TEST: $TEST_DESCRIPTION ";
1281 test_setup
&& test_run
1287 ) </dev
/null
>test.log
2>&1
1289 if [ $ret -eq 0 ]; then
1294 echo "see $(pwd)/test.log"