test-functions revision 1c36b4a73b876258fbe01fbe9bc9b750b7dcc9ce
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
export PATH
LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
fi
BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee"
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"
# SUSE and Red Hat call the binary qemu-kvm
# Debian and Gentoo call it kvm
# QEMU's own build system calls it qemu-system-x86_64
;;
i*86)
# new i386 version of QEMU
# i386 version of QEMU
;;
esac
echo "Could not find a suitable QEMU binary" >&2
return 1
fi
}
if [ -f /etc/machine-id ]; then
read MACHINE_ID < /etc/machine-id
&& INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
&& KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
fi
find_qemu_bin || return 1
KERNEL_APPEND="root=/dev/sda1 \
systemd.log_level=debug \
raid=noautodetect \
loglevel=2 \
init=$ROOTLIBDIR/systemd \
ro \
console=ttyS0 \
selinux=0 \
"
QEMU_OPTIONS="-machine accel=kvm:tcg \
-smp $QEMU_SMP \
-net none \
-m 512M \
-nographic \
-kernel $KERNEL_BIN \
"
QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
fi
( set -x
}
set -x
../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
}
# create the basic filesystem layout
# softlink mtab
}
type -P dmeventd >/dev/null && dracut_install dmeventd
}
# install compiled files
(cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
# remove unneeded documentation
# we strip binaries since debug symbols increase binaries size a lot
# and it could fill the available space
}
# install possible missing libraries
inst_libs $i
done
}
rm -f "$TESTDIR/rootdisk.img"
# Create the blank file to use as a root filesystem
LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
,290M
,
EOF
}
ret=1
[[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
[[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
return $ret
}
ddebug "Strip binaries"
find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
}
#!/bin/bash
exit 0
EOF
}
ddebug "install any Execs from the service files"
(
export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
| while read i; do
i=${i##Exec*=}; i=${i##-}
inst $i
done
)
}
if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
dfatal "\"depmod -a $KERNEL_VER\" failed."
exit 1
fi
}
}
# install plymouth, if found... else remove plymouth service files
# if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
# PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
# /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
# dracut_install plymouth plymouthd
# else
rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
# fi
}
cp -a /etc/ld.so.conf* $initdir/etc
}
# we want an empty environment
# set the hostname
# fstab
LABEL=systemd / ext3 rw 0 1
EOF
}
[[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
# in Debian ldconfig is just a shell script wrapper around ldconfig.real
}
[[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
}
# install libnss_files for login
NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
}
find \
| while read file; do
done
}
(
[[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
find \
) | while read file; do
done
}
for i in \
[[ -f $i ]] || continue
inst $i
done
}
for i in \
/usr/lib/kbd/consolefonts/eurlatgr* \
/usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
[[ -f $i ]] || continue
inst $i
done
}
[ -f ${_terminfodir}/l/linux ] && break
done
dracut_install -o ${_terminfodir}/l/linux
}
ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
# make the testsuite the default target
}
# we don't mount in the nspawn root
}
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
if [ -L "/$d" ]; then
inst_symlink "/$d"
else
inst_dir "/$d"
fi
done
}
local _bin=$1
local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
if [[ $_line =~ $_so_regex ]]; then
_file=${BASH_REMATCH[1]}
continue
fi
dfatal "dracut cannot create an initrd."
exit 1
fi
done
}
STATEFILE=".testdir"
[[ -e $STATEFILE ]] && . $STATEFILE
export TESTDIR
fi
}
export initdir
}
## @brief Converts numeric logging level to the first letter of level name.
#
# @param lvl Numeric logging level in range from 1 to 6.
# @retval 1 if @a lvl is out of range.
# @retval 0 if @a lvl is correct.
# @result Echoes first letter of level name.
1) echo F;;
2) echo E;;
3) echo W;;
4) echo I;;
5) echo D;;
6) echo T;;
*) return 1;;
esac
}
## @brief Internal helper function for _do_dlog()
#
# @param lvl Numeric logging level.
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
#
# @note This function is not supposed to be called manually. Please use
# dtrace(), ddebug(), or others instead which wrap this one.
#
# This function calls _do_dlog() either with parameter msg, or if
# none is given, it will read standard input and will use every line as
# a message.
#
# This enables:
# dwarn "This is a warning"
# echo "This is a warning" | dwarn
[ $1 -le $LOG_LEVEL ] || return 0
if [ $# -ge 1 ]; then
echo "$lvlc: $*"
else
while read line; do
done
fi
}
## @brief Logs message at TRACE level (6)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
set +x
dlog 6 "$@"
}
## @brief Logs message at DEBUG level (5)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
# set +x
dlog 5 "$@"
# [ -n "$debug" ] && set -x || :
}
## @brief Logs message at INFO level (4)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
set +x
dlog 4 "$@"
}
## @brief Logs message at WARN level (3)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
set +x
dlog 3 "$@"
}
## @brief Logs message at ERROR level (2)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
# set +x
dlog 2 "$@"
# [ -n "$debug" ] && set -x || :
}
## @brief Logs message at FATAL level (1)
#
# @param msg Message.
# @retval 0 It's always returned, even if logging failed.
set +x
dlog 1 "$@"
}
# Generic substring function. If $2 is in $1, return 0.
# normalize_path <path>
# Prints the normalized path, where it removes any duplicated
# and trailing slashes.
# Example:
set -- "${1//+(\/)//}"
echo "${1%/}"
}
# convert_abs_rel <from> <to>
# Prints the relative path, when creating a symlink to <to> from <from>.
# Example:
# corner case #1 - self looping link
[[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
# corner case #2 - own dir link
[[ "${1%/*}" == "$2" ]] && { echo "."; return; }
__abssize=${#__absolute[@]}
__cursize=${#__current[@]}
while [[ ${__absolute[__level]} == ${__current[__level]} ]]
do
then
break
fi
done
do
then
fi
__newpath=$__newpath".."
done
do
if [[ -n $__newpath ]]
then
fi
done
echo "$__newpath"
}
# Install a directory, keeping symlinks as on the original system.
# and a symlink ${initdir}/lib -> lib64.
done
# iterate over parent directories
if [[ -L $_file ]]; then
else
# create directory
fi
done
}
# $1 = file to copy to ramdisk
# $2 (optional) Name for the file on the ramdisk
# Location of the image dir is assumed to be $initdir
# We never overwrite the target if it exists.
[[ -f "$1" ]] || return 1
fi
# install checksum files also
if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
fi
}
# find symlinks linked to given library file
# $1 = library file
# Function searches for symlinks by stripping version numbers appended to
# library filename, checks if it points to the same target and finally
# prints the list of symlinks to stdout.
#
# Example:
# rev_lib_symlinks libfoo.so.8.1
[[ ! $1 ]] && return 0
until [[ ${fn##*.} == so ]]; do
fn="${fn%.*}"
done
echo "${links}"
}
# Same as above, but specialized to handle dynamic libraries.
# It handles making symlinks according to how the original library
# is referenced.
if [[ -L $_src ]]; then
# install checksum files also
if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
fi
inst_dir "${_dest%/*}"
else
inst_simple "$_src" "$_dest"
fi
# Create additional symlinks. See rev_symlinks description.
}
done
}
# find a binary. If we were not passed the full path directly,
# search in the usual places to find the binary.
if [[ -z ${1##/*} ]]; then
echo $1
return 0
fi
fi
type -P $1
}
# Same as above, but specialized to install binary executables.
# Install binary executable, and all shared library dependencies, if any.
[[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
# I love bash!
if [[ $_line =~ $_so_regex ]]; then
_file=${BASH_REMATCH[1]}
continue
fi
dfatal "dracut cannot create an initrd."
exit 1
fi
done
inst_simple "$_bin" "$_target"
}
# same as above, except for shell scripts.
# If your shell script does not start with shebang, it is not a shell script.
local _bin
shift
local _line _shebang_regex
# If debug is set, clean unprintable chars to prevent messing up the term
_shebang_regex='(#! *)(/[^ ]+).*'
[[ $_line =~ $_shebang_regex ]] || return 1
}
# same as above, but specialized for symlinks
[[ -L $1 ]] || return 1
if [[ -d $_realsrc ]]; then
else
fi
fi
}
# attempt to install any programs specified in a udev rule
else
continue;
}
fi
#dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
done
fi
}
# udev rules always get installed in the same place, so
# create a function to install them to make life simpler.
if [[ -f $r/$_rule ]]; then
fi
done
fi
if [[ -f ${r}$_rule ]]; then
inst_simple "$_found" "$_target/${_found##*/}"
fi
done
done
}
# general purpose installation function
# Same args as above.
local _x
case $# in
1) ;;
[[ $initdir = $2 ]] && set $1;;
set $1 $3;;
*) dfatal "inst only takes 1 or 2 or 3 arguments"
exit 1;;
esac
for _x in inst_symlink inst_script inst_binary inst_simple; do
done
return 1
}
# install any of listed files
#
# If first argument is '-d' and second some destination path, first accessible
# source is installed into this path, otherwise it will installed in the same
# path as source. If none of listed files was installed, function return 1.
# On first successful installation it returns with 0 status.
#
# Example:
#
#
# initramfs.
local to f
if [[ -e $f ]]; then
fi
done
return 1
}
# dracut_install [-o ] <file> [<file> ... ]
# Install <file> to the initramfs image
# -o optionally install the <file> and don't fail, if it is not there
if [[ $1 = '-o' ]]; then
shift
fi
while (($# > 0)); do
if [[ $_optional = yes ]]; then
"flagged to be optional"
else
dfatal "Failed to install $1"
exit 1
fi
fi
shift
done
}
# Install a single kernel module along with any firmware it may require.
# $1 = full path to kernel module to install
# no need to go further if the module is already installed
&& return 0
if [[ $omit_drivers ]]; then
local _kmod=${1##*/}
return 1
fi
return 1
fi
fi
> "$initdir/.kernelmodseen/${1##*/}"
|| return $?
local _modname=${1##*/} _fwdir _found _fw
_found=''
fi
done
"\"${_modname}.ko\""
else
"\"${_modname}.ko\""
fi
fi
done
return 0
}
# Do something with all the dependencies of a kernel module.
# Note that kernel modules depend on themselves using the technique we use
# $1 = function to call for each dependency we find
# It will be passed the full path to the found kernel module
# $2 = module to get dependencies for
# rest of args = arguments to modprobe
# _fderr specifies FD passed from surrounding scope
shift 2
_found=1
done
exit 0
)
}
# filter kernel modules to install certain modules that meet specific
# requirements.
# $1 = search only in subdirectory of /kernel/$1
# $2 = function to call with module name to filter.
# This function will be passed the full path to the module to test.
# The behavior of this function can vary depending on whether $hostonly is set.
# If it is, we will only look at modules that are already in memory.
# If it is not, we will look at all kernel modules
# This function returns the full filenames of modules that match $1
local _modname _filtercmd
if ! [[ $hostonly ]]; then
_filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
_filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
_filtercmd+=' -o -name "*.ko.xz"'
_filtercmd+=' 2>/dev/null'
else
_filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
_filtercmd+='-k $KERNEL_VER 2>/dev/null'
fi
;;
;;
esac
done
)
if ! [[ $hostonly ]]; then
else
fi
)
}
}
# instmods [-c] <kernel module> [<kernel module> ... ]
# instmods [-c] <kernel subsystem>
# install kernel modules along with all their dependencies.
[[ $no_kernel = yes ]] && return
# called [sub]functions inherit _fderr
local _fderr=9
if [[ $1 = '-c' ]]; then
shift
fi
=*)
if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
| instmods
else
| instmods
fi
;;
i2o_scsi) return ;; # Do not load this diagnostic-only module
*)
# if we are already installed, skip this module and go on
# to the next one.
dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
return
fi
# If we are building a host-specific initramfs and this
# module is not already loaded, move on to the next one.
&& return
# We use '-d' option in modprobe only if modules prefix path
# differs from default '/'. This allows us to use Dracut with
# old version of modprobe which doesn't have '-d' option.
local _moddirname=${KERNEL_MODS%%/lib/modules/*}
[[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
# ok, load the module, all its dependencies, and any firmware
# it may require
--set-version $KERNEL_VER ${_moddirname} $_mpargs
;;
esac
return $_ret
}
if (($# == 0)); then # filenames from stdin
while read _mod; do
return 1
fi
}
done
fi
while (($# > 0)); do # filenames as arguments
dfatal "Failed to install $1"
return 1
fi
}
shift
done
return 0
}
local _ret _filter_not_found='FATAL: Module .* not found.'
set -o pipefail
# Capture all stderr from modprobe to _fderr. We could use {var}>...
# redirections, but that would make dracut require bash4 at least.
eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
| while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
_ret=$?
set +o pipefail
return $_ret
}
# inst_libdir_file [-n <pattern>] <file> [<file>...]
# Install a <file> located on a lib directory to the initramfs image
# -n <pattern> install non-matching files
if [[ "$1" == "-n" ]]; then
local _pattern=$1
shift 2
done
done
done
else
done
done
done
fi
}
}
exit 0
fi
# Detect lib paths
done
done
while (($# > 0)); do
case $1 in
--run)
echo "TEST RUN: $TEST_DESCRIPTION"
ret=$?
echo "TEST RUN: $TEST_DESCRIPTION [OK]"
else
echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
fi
exit $ret;;
--setup)
echo "TEST SETUP: $TEST_DESCRIPTION"
exit $?;;
--clean)
echo "TEST CLEANUP: $TEST_DESCRIPTION"
exit $?;;
--all)
(
ret=$?
exit $ret
ret=$?
echo "[OK]"
else
echo "[FAILED]"
fi
exit $ret;;
*) break ;;
esac
shift
done
}