]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-13-NSPAWN-SMOKE/test.sh
7f7380fd6267eb77db360ac78c57ad189727fcba
[thirdparty/systemd.git] / test / TEST-13-NSPAWN-SMOKE / test.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 set -e
5 TEST_DESCRIPTION="systemd-nspawn smoke test"
6 TEST_NO_NSPAWN=1
7 SKIP_INITRD=yes
8 . $TEST_BASE_DIR/test-functions
9
10 test_setup() {
11 create_empty_image
12 mkdir -p $TESTDIR/root
13 mount ${LOOPDEV}p1 $TESTDIR/root
14
15 # Create what will eventually be our root filesystem onto an overlay
16 (
17 LOG_LEVEL=5
18 eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
19
20 setup_basic_environment
21 dracut_install busybox chmod rmdir unshare
22
23 cp create-busybox-container $initdir/
24
25 ./create-busybox-container $initdir/nc-container
26 initdir="$initdir/nc-container" dracut_install nc
27
28 # setup the testsuite service
29 cat >$initdir/etc/systemd/system/testsuite.service <<EOF
30 [Unit]
31 Description=Testsuite service
32 After=multi-user.target
33
34 [Service]
35 ExecStart=/test-nspawn.sh
36 Type=oneshot
37 EOF
38
39 cat >$initdir/test-nspawn.sh <<'EOF'
40 #!/bin/bash
41 set -x
42 set -e
43 set -u
44 set -o pipefail
45
46 export SYSTEMD_LOG_LEVEL=debug
47
48 # check cgroup-v2
49 is_v2_supported=no
50 mkdir -p /tmp/cgroup2
51 if mount -t cgroup2 cgroup2 /tmp/cgroup2; then
52 is_v2_supported=yes
53 umount /tmp/cgroup2
54 fi
55 rmdir /tmp/cgroup2
56
57 # check cgroup namespaces
58 is_cgns_supported=no
59 if [[ -f /proc/1/ns/cgroup ]]; then
60 is_cgns_supported=yes
61 fi
62
63 is_user_ns_supported=no
64 if unshare -U sh -c :; then
65 is_user_ns_supported=yes
66 fi
67
68 function check_bind_tmp_path {
69 # https://github.com/systemd/systemd/issues/4789
70 local _root="/var/lib/machines/bind-tmp-path"
71 /create-busybox-container "$_root"
72 >/tmp/bind
73 systemd-nspawn --register=no -D "$_root" --bind=/tmp/bind /bin/sh -c 'test -e /tmp/bind'
74 }
75
76 function check_notification_socket {
77 # https://github.com/systemd/systemd/issues/4944
78 local _cmd='echo a | $(busybox which nc) -U -u -w 1 /run/systemd/nspawn/notify'
79 systemd-nspawn --register=no -D /nc-container /bin/sh -x -c "$_cmd"
80 systemd-nspawn --register=no -D /nc-container -U /bin/sh -x -c "$_cmd"
81 }
82
83 function run {
84 if [[ "$1" = "yes" && "$is_v2_supported" = "no" ]]; then
85 printf "Unified cgroup hierarchy is not supported. Skipping.\n" >&2
86 return 0
87 fi
88 if [[ "$2" = "yes" && "$is_cgns_supported" = "no" ]]; then
89 printf "Cgroup namespaces are not supported. Skipping.\n" >&2
90 return 0
91 fi
92
93 local _root="/var/lib/machines/unified-$1-cgns-$2-api-vfs-writable-$3"
94 /create-busybox-container "$_root"
95 UNIFIED_CGROUP_HIERARCHY="$1" SYSTEMD_NSPAWN_USE_CGNS="$2" SYSTEMD_NSPAWN_API_VFS_WRITABLE="$3" systemd-nspawn --register=no -D "$_root" -b
96 UNIFIED_CGROUP_HIERARCHY="$1" SYSTEMD_NSPAWN_USE_CGNS="$2" SYSTEMD_NSPAWN_API_VFS_WRITABLE="$3" systemd-nspawn --register=no -D "$_root" --private-network -b
97
98 if UNIFIED_CGROUP_HIERARCHY="$1" SYSTEMD_NSPAWN_USE_CGNS="$2" SYSTEMD_NSPAWN_API_VFS_WRITABLE="$3" systemd-nspawn --register=no -D "$_root" -U -b; then
99 [[ "$is_user_ns_supported" = "yes" && "$3" = "network" ]] && return 1
100 else
101 [[ "$is_user_ns_supported" = "no" && "$3" = "network" ]] && return 1
102 fi
103
104 if UNIFIED_CGROUP_HIERARCHY="$1" SYSTEMD_NSPAWN_USE_CGNS="$2" SYSTEMD_NSPAWN_API_VFS_WRITABLE="$3" systemd-nspawn --register=no -D "$_root" --private-network -U -b; then
105 [[ "$is_user_ns_supported" = "yes" && "$3" = "yes" ]] && return 1
106 else
107 [[ "$is_user_ns_supported" = "no" && "$3" = "yes" ]] && return 1
108 fi
109
110 return 0
111 }
112
113 check_bind_tmp_path
114
115 check_notification_socket
116
117 for api_vfs_writable in yes no network; do
118 run no no $api_vfs_writable
119 run yes no $api_vfs_writable
120 run no yes $api_vfs_writable
121 run yes yes $api_vfs_writable
122 done
123
124 touch /testok
125 EOF
126
127 chmod 0755 $initdir/test-nspawn.sh
128 setup_testsuite
129 ) || return 1
130
131 ddebug "umount $TESTDIR/root"
132 umount $TESTDIR/root
133 }
134
135 do_test "$@"