test-functions revision e3ce42e70504922f0ea7149f90fc80b549844e93
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# ex: ts=8 sw=4 sts=4 et filetype=sh
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringLOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringif ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringBASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # SUSE and Red Hat call the binary qemu-kvm
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # QEMU's own build system calls it qemu-system-x86_64
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering echo "Could not find a suitable QEMU binary" >&2
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [ -f /etc/machine-id ]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering default_fedora_initrd=/boot/initramfs-${KERNEL_VER}.img
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering default_debian_initrd=/boot/initrd.img-${KERNEL_VER}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$INITRD" ] || { [ -e "$default_fedora_initrd" ] && INITRD=$default_fedora_initrd; }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ "$INITRD" ] || { [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD=$default_debian_initrd; }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringraid=noautodetect \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringconsole=ttyS0 \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [ -c /dev/kvm ]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ../../systemd-nspawn --register=no --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # don't forget KERNEL_APPEND='... selinux=1 ...'
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDescription=Relabel all filesystems
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDefaultDependencies=no
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringRequires=local-fs.target
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringConflicts=shutdown.target
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringAfter=local-fs.target
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringBefore=sysinit.target shutdown.target
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringConditionSecurity=selinux
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringConditionPathExists=|/.autorelabel
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringRemainAfterExit=yes
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering mkdir -p $initdir/etc/systemd/system/basic.target.wants
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ! type -p valgrind; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering strace -e open valgrind /bin/true 2>&1 >/dev/null |
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringexec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering type -P dmeventd >/dev/null && dracut_install dmeventd
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst_libdir_file "libdevmapper-event.so*"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst_rules 55-dm.rules 60-persistent-storage-dm.rules
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # we strip binaries since debug symbols increase binaries size a lot
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # Create the blank file to use as a root filesystem
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=400
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test -s $TESTDIR/failed && ret=$(($ret+1))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ddebug "install any Execs from the service files"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering | while read i; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ! depmod -a -b "$initdir" $KERNEL_VER; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dfatal "\"depmod -a $KERNEL_VER\" failed."
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst /lib/modules/$KERNEL_VER/modules.order
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst /lib/modules/$KERNEL_VER/modules.builtin
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # install plymouth, if found... else remove plymouth service files
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering echo systemd-testsuite > $initdir/etc/hostname
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringLABEL=systemd / ext3 rw 0 1
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # in Debian ldconfig is just a shell script wrapper around ldconfig.real
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering | while read file; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna cp /etc/pam.d/systemd-user $initdir/etc/pam.d/
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna [[ -f $i ]] || continue
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna [[ -f $i ]] || continue
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna [ -f ${_terminfodir}/l/linux ] && break
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek dracut_install -o ${_terminfodir}/l/linux
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna ln -fs testsuite.target $initdir/etc/systemd/system/default.target
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek rm -f $TESTDIR/nspawn-root/etc/fstab
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna 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
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ $_line = 'not a dynamic executable' ]] && break
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen if [[ $_line =~ $_so_regex ]]; then
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek [[ -e ${initdir}/$_file ]] && continue
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ $_line =~ not\ found ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dfatal "Missing a shared library required by $_bin."
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dfatal "Run \"ldd $_bin\" to find out what it is."
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Converts numeric logging level to the first letter of level name.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param lvl Numeric logging level in range from 1 to 6.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 1 if @a lvl is out of range.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 if @a lvl is correct.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @result Echoes first letter of level name.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Internal helper function for _do_dlog()
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param lvl Numeric logging level.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @note This function is not supposed to be called manually. Please use
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# dtrace(), ddebug(), or others instead which wrap this one.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# This function calls _do_dlog() either with parameter msg, or if
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# none is given, it will read standard input and will use every line as
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# This enables:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# dwarn "This is a warning"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# echo "This is a warning" | dwarn
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [ $1 -le $LOG_LEVEL ] || return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local lvlc=$(_lvl2char "$lvl") || return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [ $# -ge 1 ]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while read line; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at TRACE level (6)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at DEBUG level (5)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# [ -n "$debug" ] && set -x || :
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at INFO level (4)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at WARN level (3)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at ERROR level (2)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# [ -n "$debug" ] && set -x || :
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering## @brief Logs message at FATAL level (1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @param msg Message.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# @retval 0 It's always returned, even if logging failed.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Generic substring function. If $2 is in $1, return 0.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# normalize_path <path>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Prints the normalized path, where it removes any duplicated
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# and trailing slashes.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $ normalize_path ///test/test//
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# convert_abs_rel <from> <to>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Prints the relative path, when creating a symlink to <to> from <from>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $ convert_abs_rel /usr/bin/test /bin/test-2
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local __current __absolute __abssize __cursize __newpath
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while [[ ${__absolute[__level]} == ${__current[__level]} ]]
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (( __level > __abssize || __level > __cursize ))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for ((__i = __level; __i < __cursize-1; __i++))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for ((__i = __level; __i < __abssize; __i++))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Install a directory, keeping symlinks as on the original system.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# will create ${initdir}/lib64, ${initdir}/lib64/file,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# and a symlink ${initdir}/lib -> lib64.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e ${initdir}/"$1" ]] && return 0 # already there
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -L $_file ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering mkdir -m 0755 -p "${initdir}/$_file" || return 1
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $1 = file to copy to ramdisk
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $2 (optional) Name for the file on the ramdisk
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Location of the image dir is assumed to be $initdir
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# We never overwrite the target if it exists.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if ! [[ -d ${initdir}/$target ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e ${initdir}/$target ]] && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -L ${initdir}/$target ]] && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering cp --sparse=always -pfL "$_src" "${initdir}/$target"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# find symlinks linked to given library file
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# $1 = library file
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Function searches for symlinks by stripping version numbers appended to
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# library filename, checks if it points to the same target and finally
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# prints the list of symlinks to stdout.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# rev_lib_symlinks libfoo.so.8.1
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# (Only if libfoo.so.8 and libfoo.so exists on host system.)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ ! $1 ]] && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local fn="$1" orig="$(readlink -f "$1")" links=''
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ ${fn} =~ .*\.so\..* ]] || return 1
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Same as above, but specialized to handle dynamic libraries.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# It handles making symlinks according to how the original library
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# is referenced.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e $initdir/$_dest ]] && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -L $_src ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # Create additional symlinks. See rev_symlinks description.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ddebug "Creating extra symlink: $_symlink"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# find a binary. If we were not passed the full path directly,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# search in the usual places to find the binary.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Same as above, but specialized to install binary executables.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# Install binary executable, and all shared library dependencies, if any.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e $initdir/$_target ]] && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ $_line = 'not a dynamic executable' ]] && break
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ $_line =~ $_so_regex ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering [[ -e ${initdir}/$_file ]] && continue
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if [[ $_line =~ not\ found ]]; then
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dfatal "Missing a shared library required by $_bin."
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dfatal "Run \"ldd $_bin\" to find out what it is."
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# same as above, except for shell scripts.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering# If your shell script does not start with shebang, it is not a shell script.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering # If debug is set, clean unprintable chars to prevent messing up the term
[[ $_line =~ $_shebang_regex ]] || return 1
if [[ -d $_realsrc ]]; then
if [[ -f $r/$_rule ]]; then
if [[ -f ${r}$_rule ]]; then
local _x
case $# in
[[ $initdir = $2 ]] && set $1;;
for _x in inst_symlink inst_script inst_binary inst_simple; do
local to f
if [[ $_optional = yes ]]; then
if [[ $omit_drivers ]]; then
_found=1
local _modname _filtercmd
if ! [[ $hostonly ]]; then
if ! [[ $hostonly ]]; then
[[ $no_kernel = yes ]] && return
local _fderr=9
| instmods
| instmods
local _moddirname=${KERNEL_MODS%%/lib/modules/*}
--set-version $KERNEL_VER ${_moddirname} $_mpargs
return $_ret
while read _mod; do
set -o pipefail
| while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
_ret=$?
set +o pipefail
return $_ret
local _pattern=$1
case $1 in
--run)
ret=$?
exit $ret;;
--setup)
--clean)
--all)
ret=$?
exit $ret
ret=$?
exit $ret;;