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