]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-13.machinectl.sh
Merge pull request #27670 from poettering/switch-root-umount-all
[thirdparty/systemd.git] / test / units / testsuite-13.machinectl.sh
CommitLineData
378db9e2
FS
1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3# shellcheck disable=SC2016
4set -eux
5set -o pipefail
6
5656759d
FS
7# shellcheck source=test/units/util.sh
8. "$(dirname "$0")"/util.sh
378db9e2 9
5656759d 10export PAGER=
378db9e2
FS
11
12at_exit() {
13 set +e
14
15 machinectl status long-running >/dev/null && machinectl kill --signal=KILL long-running
16 mountpoint -q /var/lib/machines && timeout 10 sh -c "while ! umount /var/lib/machines; do sleep .5; done"
a1ff1ac2 17 [[ -n "${NSPAWN_FRAGMENT:-}" ]] && rm -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" "/var/lib/machines/$NSPAWN_FRAGMENT"
28ed2326 18 rm -f /run/systemd/nspawn/*.nspawn
378db9e2
FS
19}
20
21trap at_exit EXIT
22
23# Mount tmpfs over /var/lib/machines to not pollute the image
24mkdir -p /var/lib/machines
25mount -t tmpfs tmpfs /var/lib/machines
26
27# Create a couple of containers we can refer to in tests
28for i in {0..4}; do
5656759d 29 create_dummy_container "/var/lib/machines/container$i"
378db9e2
FS
30 machinectl start "container$i"
31done
32# Create one "long running" container with some basic signal handling
5656759d 33create_dummy_container /var/lib/machines/long-running
378db9e2 34cat >/var/lib/machines/long-running/sbin/init <<\EOF
5656759d
FS
35#!/usr/bin/bash -x
36
378db9e2
FS
37PID=0
38
5656759d 39trap "touch /poweroff" RTMIN+4
378db9e2
FS
40trap "touch /reboot" INT
41trap "touch /trap" TRAP
42trap 'kill $PID' EXIT
43
44# We need to wait for the sleep process asynchronously in order to allow
45# bash to process signals
46sleep infinity &
47PID=$!
48while :; do
49 wait || :
50done
51EOF
52machinectl start long-running
53
54machinectl
55machinectl --no-pager --help
56machinectl --version
57machinectl list
58machinectl list --no-legend --no-ask-password
59
60machinectl status long-running long-running long-running
61machinectl status --full long-running
62machinectl status --quiet --lines=1 long-running
63machinectl status --lines=0 --max-addresses=0 long-running
64machinectl status --machine=testuser@.host long-running
65machinectl status --output=help long-running
66while read -r output; do
67 machinectl status --output="$output" long-running
68done < <(machinectl --output=help)
69
70machinectl show
71machinectl show --all
72machinectl show --all --machine=root@
73machinectl show --all --machine=testuser@
74[[ "$(machinectl show --property=PoolPath --value)" == "/var/lib/machines" ]]
75machinectl show long-running
76machinectl show long-running long-running long-running --all
77[[ "$(machinectl show --property=RootDirectory --value long-running)" == "/var/lib/machines/long-running" ]]
78
79machinectl enable long-running
80test -L /etc/systemd/system/machines.target.wants/systemd-nspawn@long-running.service
81machinectl enable long-running long-running long-running container1
82machinectl disable long-running
83test ! -L /etc/systemd/system/machines.target.wants/systemd-nspawn@long-running.service
84machinectl disable long-running long-running long-running container1
85
5656759d
FS
86[[ "$(machinectl shell testuser@ /usr/bin/bash -c 'echo -ne $FOO')" == "" ]]
87[[ "$(machinectl shell --setenv=FOO=bar testuser@ /usr/bin/bash -c 'echo -ne $FOO')" == "bar" ]]
378db9e2
FS
88
89[[ "$(machinectl show --property=State --value long-running)" == "running" ]]
90# Equivalent to machinectl kill --signal=SIGRTMIN+4 --kill-whom=leader
91rm -f /var/lib/machines/long-running/poweroff
92machinectl poweroff long-running
5656759d 93timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/poweroff; do sleep .5; done"
378db9e2
FS
94machinectl poweroff long-running long-running long-running
95# Equivalent to machinectl kill --signal=SIGINT --kill-whom=leader
96rm -f /var/lib/machines/long-running/reboot
97machinectl reboot long-running
5656759d 98timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/reboot; do sleep .5; done"
378db9e2
FS
99machinectl reboot long-running long-running long-running
100# Skip machinectl terminate for now, as it doesn't play well with our "init"
101rm -f /var/lib/machines/long-running/trap
102machinectl kill --signal=SIGTRAP --kill-whom=leader long-running
5656759d 103timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/trap; do sleep .5; done"
378db9e2
FS
104machinectl kill --signal=SIGTRAP --kill-whom=leader long-running long-running long-running
105# All used signals should've been caught by a handler
106[[ "$(machinectl show --property=State --value long-running)" == "running" ]]
107
108cp /etc/machine-id /tmp/foo
109machinectl copy-to long-running /tmp/foo /root/foo
110test -f /var/lib/machines/long-running/root/foo
111machinectl copy-from long-running /root/foo /tmp/bar
112diff /tmp/foo /tmp/bar
113rm -f /tmp/{foo,bar}
114
115# machinectl bind is covered by testcase_check_machinectl_bind() in nspawn tests
116
117machinectl list-images
118machinectl list-images --no-legend
119machinectl image-status
120machinectl image-status container1
121machinectl image-status container1 container1 container{0..4}
122machinectl show-image
123machinectl show-image container1
124machinectl show-image container1 container1 container{0..4}
125
126machinectl clone container1 clone1
127machinectl show-image clone1
128machinectl rename clone1 clone2
129(! machinectl show-image clone1)
130machinectl show-image clone2
131if lsattr -d /var/lib/machines >/dev/null; then
132 [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == no ]]
133 machinectl read-only clone2 yes
134 [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == yes ]]
135 machinectl read-only clone2 no
136 [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == no ]]
137fi
138machinectl remove clone2
139for i in {0..4}; do
140 machinectl clone container1 "clone$i"
141done
142machinectl remove clone{0..4}
143for i in {0..4}; do
144 machinectl clone container1 ".hidden$i"
145done
146machinectl list-images --all
147test -d /var/lib/machines/.hidden1
148machinectl clean
149test ! -d /var/lib/machines/.hidden1
150
151# Prepare a simple raw container
152mkdir -p /tmp/mnt
153dd if=/dev/zero of=/tmp/container.raw bs=1M count=32
154mkfs.ext4 /tmp/container.raw
155mount -o loop /tmp/container.raw /tmp/mnt
156cp -r /var/lib/machines/container1/* /tmp/mnt
157umount /tmp/mnt
158# Try to import it, run it, export it, and re-import it
159machinectl import-raw /tmp/container.raw container-raw
160[[ "$(machinectl show-image --property=Type --value container-raw)" == "raw" ]]
161machinectl start container-raw
162machinectl export-raw container-raw /tmp/container-export.raw
163machinectl import-raw /tmp/container-export.raw container-raw-reimport
164[[ "$(machinectl show-image --property=Type --value container-raw-reimport)" == "raw" ]]
165rm -f /tmp/container{,-export}.raw
166
167# Prepare a simple tar.gz container
168tar -pczf /tmp/container.tar.gz -C /var/lib/machines/container1 .
169# Try to import it, run it, export it, and re-import it
170machinectl import-tar /tmp/container.tar.gz container-tar
171[[ "$(machinectl show-image --property=Type --value container-tar)" == "directory" ]]
172machinectl start container-tar
173machinectl export-tar container-tar /tmp/container-export.tar.gz
174machinectl import-tar /tmp/container-export.tar.gz container-tar-reimport
175[[ "$(machinectl show-image --property=Type --value container-tar-reimport)" == "directory" ]]
176rm -f /tmp/container{,-export}.tar.gz
177
178# Try to import a container directory & run it
179cp -r /var/lib/machines/container1 /tmp/container.dir
180machinectl import-fs /tmp/container.dir container-dir
181[[ "$(machinectl show-image --property=Type --value container-dir)" == "directory" ]]
182machinectl start container-dir
183rm -fr /tmp/container.dir
184
5656759d 185timeout 10 bash -c "while ! machinectl clean --all; do sleep .5; done"
378db9e2 186
a1ff1ac2
FS
187NSPAWN_FRAGMENT="machinectl-test-$RANDOM.nspawn"
188cat >"/var/lib/machines/$NSPAWN_FRAGMENT" <<EOF
189[Exec]
190Boot=true
191EOF
192machinectl cat "$NSPAWN_FRAGMENT"
193EDITOR=true script -qec "machinectl edit $NSPAWN_FRAGMENT" /dev/null
194test -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT"
195diff "/var/lib/machines/$NSPAWN_FRAGMENT" "/etc/systemd/nspawn/$NSPAWN_FRAGMENT"
196
197cat >/tmp/fragment.nspawn <<EOF
198[Exec]
199Boot=false
200EOF
201machinectl cat /tmp/fragment.nspawn
202EDITOR="cp /tmp/fragment.nspawn" script -qec "machinectl edit $NSPAWN_FRAGMENT" /dev/null
203diff /tmp/fragment.nspawn "/etc/systemd/nspawn/$NSPAWN_FRAGMENT"
204
378db9e2
FS
205for opt in format lines machine max-addresses output setenv verify; do
206 (! machinectl status "--$opt=" long-running)
207 (! machinectl status "--$opt=-1" long-running)
208 (! machinectl status "--$opt=''" long-running)
209done
210(! machinectl show "")
211(! machinectl enable)
212(! machinectl enable "")
213(! machinectl disable)
214(! machinectl disable "")
215(! machinectl read-only container1 "")
216(! machinectl read-only container1 foo)
217(! machinectl read-only container1 -- -1)