]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - test/test-functions
tests: introduce UNIFIED_CGROUP_HIERARCHY (#3419)
[thirdparty/systemd.git] / test / test-functions
index 961a6254d8920629fd7e94940078fa6acaa92f29..5f95a8129efb048f79bfb6edac7e4b42e1b91364 100644 (file)
@@ -7,14 +7,18 @@ export PATH
 LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
 KERNEL_VER=${KERNEL_VER-$(uname -r)}
 KERNEL_MODS="/lib/modules/$KERNEL_VER/"
+QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}"
+NSPAWN_TIMEOUT="${NSPAWN_TIMEOUT:-infinity}"
+FSTYPE="${FSTYPE:-ext3}"
+UNIFIED_CGROUP_HIERARCHY="${UNIFIED_CGROUP_HIERARCHY:-no}"
 
 if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
     echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
     ROOTLIBDIR=/usr/lib/systemd
 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"
+BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm"
+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"
 
 function find_qemu_bin() {
     # SUSE and Red Hat call the binary qemu-kvm
@@ -61,13 +65,13 @@ run_qemu() {
     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 \
+systemd.unified_cgroup_hierarchy=$UNIFIED_CGROUP_HIERARCHY \
 $KERNEL_APPEND \
 "
 
@@ -76,9 +80,10 @@ $KERNEL_APPEND \
 -m 512M \
 -nographic \
 -kernel $KERNEL_BIN \
+-drive format=raw,cache=unsafe,file=${TESTDIR}/rootdisk.img \
 "
 
-    if [ "$INITRD" ]; then
+    if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
         QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
     fi
 
@@ -86,13 +91,23 @@ $KERNEL_APPEND \
         QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
     fi
 
+    if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
+        QEMU_BIN="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
+    fi
     ( set -x
-      $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
+      $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" ) || return 1
 }
 
 run_nspawn() {
+    local _nspawn_cmd="../../systemd-nspawn --register=no --kill-signal=SIGKILL --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND"
+    if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
+        _nspawn_cmd="timeout --foreground $NSPAWN_TIMEOUT $_nspawn_cmd"
+    fi
+
+    _nspawn_cmd="env UNIFIED_CGROUP_HIERARCHY=$UNIFIED_CGROUP_HIERARCHY $_nspawn_cmd"
+
     set -x
-    ../../systemd-nspawn --register=no --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
+    $_nspawn_cmd
 }
 
 setup_basic_environment() {
@@ -115,11 +130,55 @@ setup_basic_environment() {
     install_plymouth
     install_debug_tools
     install_ld_so_conf
+    setup_selinux
     strip_binaries
     install_depmod_files
     generate_module_dependencies
 }
 
+setup_selinux() {
+    # don't forget KERNEL_APPEND='... selinux=1 ...'
+    if [[ "$SETUP_SELINUX" != "yes" ]]; then
+        ddebug "Don't setup SELinux"
+        return 0
+    fi
+    ddebug "Setup SELinux"
+    local _conf_dir=/etc/selinux
+    local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
+
+    rm -rf $initdir/$_conf_dir
+    if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
+        dfatal "Failed to copy $_conf_dir"
+        exit 1
+    fi
+
+    cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
+[Unit]
+Description=Relabel all filesystems
+DefaultDependencies=no
+Requires=local-fs.target
+Conflicts=shutdown.target
+After=local-fs.target
+Before=sysinit.target shutdown.target
+ConditionSecurity=selinux
+ConditionPathExists=|/.autorelabel
+
+[Service]
+ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
+Type=oneshot
+TimeoutSec=0
+RemainAfterExit=yes
+EOF
+
+    touch $initdir/.autorelabel
+    mkdir -p $initdir/etc/systemd/system/basic.target.wants
+    ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service
+
+    dracut_install $_fixfiles_tools
+    dracut_install fixfiles
+    dracut_install sestatus
+}
+
 install_valgrind() {
     if ! type -p valgrind; then
         dfatal "Failed to install valgrind"
@@ -150,9 +209,23 @@ EOF
     chmod 0755 $_valgrind_wrapper
 }
 
+create_strace_wrapper() {
+    local _strace_wrapper=$initdir/$ROOTLIBDIR/systemd-under-strace
+    ddebug "Create $_strace_wrapper"
+    cat >$_strace_wrapper <<EOF
+#!/bin/bash
+
+exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
+EOF
+    chmod 0755 $_strace_wrapper
+}
+
 install_fsck() {
     dracut_install /sbin/fsck*
     dracut_install -o /bin/fsck*
+
+    # fskc.reiserfs calls reiserfsck. so, install it
+    dracut_install -o reiserfsck
 }
 
 install_dmevent() {
@@ -176,6 +249,9 @@ install_systemd() {
     # we strip binaries since debug symbols increase binaries size a lot
     # and it could fill the available space
     strip_binaries
+
+    # enable debug logging in PID1
+    echo LogLevel=debug >> $initdir/etc/systemd/system.conf
 }
 
 install_missing_libraries() {
@@ -197,7 +273,13 @@ create_empty_image() {
 ,
 EOF
 
-    mkfs.ext3 -L systemd "${LOOPDEV}p1"
+    local _label="-L systemd"
+    # mkfs.reiserfs doesn't know -L. so, use --label instead
+    [[ "$FSTYPE" == "reiserfs" ]] && _label="--label systemd"
+    if ! mkfs -t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; then
+        dfatal "Failed to mkfs -t ${FSTYPE}"
+        exit 1
+    fi
 }
 
 check_result_nspawn() {
@@ -291,7 +373,7 @@ install_config_files() {
     echo systemd-testsuite > $initdir/etc/hostname
     # fstab
     cat >$initdir/etc/fstab <<EOF
-LABEL=systemd           /       ext3    rw 0 1
+LABEL=systemd           /       ${FSTYPE}    rw 0 1
 EOF
 }
 
@@ -335,8 +417,15 @@ install_pam() {
         inst $file
     done
 
+    # pam_unix depends on unix_chkpwd.
+    # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
+    dracut_install -o unix_chkpwd
+
     [[ "$LOOKS_LIKE_DEBIAN" ]] &&
         cp /etc/pam.d/systemd-user $initdir/etc/pam.d/
+
+    # set empty root password for easy debugging
+    sed -i 's/^root:x:/root::/' $initdir/etc/passwd
 }
 
 install_keymaps() {