test-functions revision d5172c79136fceaac6324fd1896efe1e576fd71d
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen# ex: ts=8 sw=4 sts=4 et filetype=sh
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom GundersenLOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersenif ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom GundersenBASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom GundersenDEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen # QEMU's own build system calls it qemu-system-x86_64
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen echo "Could not find a suitable QEMU binary" >&2
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen if [ -f /etc/machine-id ]; then
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen default_fedora_initrd=/boot/initramfs-${KERNEL_VER}.img
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen default_debian_initrd=/boot/initrd.img-${KERNEL_VER}
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich [ "$INITRD" ] || { [ -e "$default_fedora_initrd" ] && INITRD=$default_fedora_initrd; }
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich [ "$INITRD" ] || { [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD=$default_debian_initrd; }
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichraid=noautodetect \
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichconsole=ttyS0 \
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich if [ -c /dev/kvm ]; then
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich ../../systemd-nspawn --register=no --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen # don't forget KERNEL_APPEND='... selinux=1 ...'
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenDescription=Relabel all filesystems
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenDefaultDependencies=no
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenRequires=local-fs.target
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenConflicts=shutdown.target
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenAfter=local-fs.target
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenBefore=sysinit.target shutdown.target
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenConditionSecurity=selinux
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenConditionPathExists=|/.autorelabel
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom GundersenRemainAfterExit=yes
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen mkdir -p $initdir/etc/systemd/system/basic.target.wants
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen if ! type -p valgrind; then
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/')
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen strace -e open valgrind /bin/true 2>&1 >/dev/null |
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersenexec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
505f8da7325591defe5f751f328bd26915267602Tom Gundersen type -P dmeventd >/dev/null && dracut_install dmeventd
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
inst_libs $i
ret=1
[[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
[[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
return $ret
find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
inst $i
if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
cp -a /etc/ld.so.conf* $initdir/etc
[[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
[[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
find \
| while read file; do
[[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
find \
for i in \
inst $i
for i in \
/usr/lib/kbd/consolefonts/eurlatgr* \
/usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
inst $i
[ -f ${_terminfodir}/l/linux ] && break
dracut_install -o ${_terminfodir}/l/linux
ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
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
local _bin=$1
if [[ $_line =~ $_so_regex ]]; then
_file=${BASH_REMATCH[1]}
[[ -e $STATEFILE ]] && . $STATEFILE
export TESTDIR
export initdir
[ $1 -le $LOG_LEVEL ] || return 0
if [ $# -ge 1 ]; then
while read line; do
while [[ ${__absolute[__level]} == ${__current[__level]} ]]
if [[ -n $__newpath ]]
if [[ -L $_file ]]; then
# rev_lib_symlinks libfoo.so.8.1
if [[ -L $_src ]]; then
[[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
if [[ $_line =~ $_so_regex ]]; then
_file=${BASH_REMATCH[1]}
local _bin
local _line _shebang_regex
[[ $_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;;