]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-50.sh
test: use 'until' instead of 'while !'
[thirdparty/systemd.git] / test / units / testsuite-50.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
4 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 set -eux
6 set -o pipefail
7
8 export SYSTEMD_LOG_LEVEL=debug
9
10 # shellcheck disable=SC2317
11 cleanup() {(
12 set +ex
13
14 if [ -z "${image_dir}" ]; then
15 return
16 fi
17 umount "${image_dir}/app0"
18 umount "${image_dir}/app1"
19 umount "${image_dir}/app-nodistro"
20 umount "${image_dir}/service-scoped-test"
21 rm -rf "${image_dir}"
22 )}
23
24 udevadm control --log-level=debug
25
26 cd /tmp
27
28 image_dir="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
29 if [ -z "${image_dir}" ] || [ ! -d "${image_dir}" ]; then
30 echo "mktemp under /tmp failed"
31 exit 1
32 fi
33
34 trap cleanup EXIT
35
36 cp /usr/share/minimal* "${image_dir}/"
37 image="${image_dir}/minimal_0"
38 roothash="$(cat "${image}.roothash")"
39
40 os_release="$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)"
41
42 systemd-dissect --json=short "${image}.raw" | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
43 systemd-dissect "${image}.raw" | grep -q -F "MARKER=1"
44 systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release")
45
46 systemd-dissect --list "${image}.raw" | grep -q '^etc/os-release$'
47 systemd-dissect --mtree "${image}.raw" --mtree-hash yes | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]* sha256sum=[a-z0-9]*$"
48 systemd-dissect --mtree "${image}.raw" --mtree-hash no | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]*$"
49
50 read -r SHA256SUM1 _ < <(systemd-dissect --copy-from "${image}.raw" etc/os-release | sha256sum)
51 test "$SHA256SUM1" != ""
52 read -r SHA256SUM2 _ < <(systemd-dissect --read-only --with "${image}.raw" sha256sum etc/os-release)
53 test "$SHA256SUM2" != ""
54 test "$SHA256SUM1" = "$SHA256SUM2"
55
56 mv "${image}.verity" "${image}.fooverity"
57 mv "${image}.roothash" "${image}.foohash"
58 systemd-dissect --json=short "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
59 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F "MARKER=1"
60 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F -f <(sed 's/"//g' "$os_release")
61 mv "${image}.fooverity" "${image}.verity"
62 mv "${image}.foohash" "${image}.roothash"
63
64 mkdir -p "${image_dir}/mount" "${image_dir}/mount2"
65 systemd-dissect --mount "${image}.raw" "${image_dir}/mount"
66 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
67 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
68 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
69 # Verity volume should be shared (opened only once)
70 systemd-dissect --mount "${image}.raw" "${image_dir}/mount2"
71 verity_count=$(find /dev/mapper/ -name "*verity*" | wc -l)
72 # In theory we should check that count is exactly one. In practice, libdevmapper
73 # randomly and unpredictably fails with an unhelpful EINVAL when a device is open
74 # (and even mounted and in use), so best-effort is the most we can do for now
75 if [ "${verity_count}" -lt 1 ]; then
76 echo "Verity device ${image}.raw not found in /dev/mapper/"
77 exit 1
78 fi
79 systemd-dissect --umount "${image_dir}/mount"
80 systemd-dissect --umount "${image_dir}/mount2"
81
82 systemd-run -P -p RootImage="${image}.raw" cat /usr/lib/os-release | grep -q -F "MARKER=1"
83 mv "${image}.verity" "${image}.fooverity"
84 mv "${image}.roothash" "${image}.foohash"
85 systemd-run -P -p RootImage="${image}.raw" -p RootHash="${image}.foohash" -p RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
86 # Let's use the long option name just here as a test
87 systemd-run -P --property RootImage="${image}.raw" --property RootHash="${roothash}" --property RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
88 mv "${image}.fooverity" "${image}.verity"
89 mv "${image}.foohash" "${image}.roothash"
90
91 # Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
92 machine="$(uname -m)"
93 if [ "${machine}" = "x86_64" ]; then
94 root_guid=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
95 verity_guid=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
96 signature_guid=41092b05-9fc8-4523-994f-2def0408b176
97 architecture="x86-64"
98 elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
99 root_guid=44479540-f297-41b2-9af7-d131d5f0458a
100 verity_guid=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
101 signature_guid=5996fc05-109c-48de-808b-23fa0830b676
102 architecture="x86"
103 elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
104 root_guid=b921b045-1df0-41c3-af44-4c6f280d3fae
105 verity_guid=df3300ce-d69f-4c92-978c-9bfb0f38d820
106 signature_guid=6db69de6-29f4-4758-a7a5-962190f00ce3
107 architecture="arm64"
108 elif [ "${machine}" = "arm" ]; then
109 root_guid=69dad710-2ce4-4e3c-b16c-21a1d49abed3
110 verity_guid=7386cdf2-203c-47a9-a498-f2ecce45a2d6
111 signature_guid=42b0455f-eb11-491d-98d3-56145ba9d037
112 architecture="arm"
113 elif [ "${machine}" = "loongarch64" ]; then
114 root_guid=77055800-792c-4f94-b39a-98c91b762bb6
115 verity_guid=f3393b22-e9af-4613-a948-9d3bfbd0c535
116 signature_guid=5afb67eb-ecc8-4f85-ae8e-ac1e7c50e7d0
117 architecture="loongarch64"
118 elif [ "${machine}" = "ia64" ]; then
119 root_guid=993d8d3d-f80e-4225-855a-9daf8ed7ea97
120 verity_guid=86ed10d5-b607-45bb-8957-d350f23d0571
121 signature_guid=e98b36ee-32ba-4882-9b12-0ce14655f46a
122 architecture="ia64"
123 elif [ "${machine}" = "s390x" ]; then
124 root_guid=5eead9a9-fe09-4a1e-a1d7-520d00531306
125 verity_guid=b325bfbe-c7be-4ab8-8357-139e652d2f6b
126 signature_guid=c80187a5-73a3-491a-901a-017c3fa953e9
127 architecture="s390x"
128 elif [ "${machine}" = "ppc64le" ]; then
129 root_guid=c31c45e6-3f39-412e-80fb-4809c4980599
130 verity_guid=906bd944-4589-4aae-a4e4-dd983917446a
131 signature_guid=d4a236e7-e873-4c07-bf1d-bf6cf7f1c3c6
132 architecture="ppc64-le"
133 else
134 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
135 exit 1
136 fi
137 # du rounds up to block size, which is more helpful for partitioning
138 root_size="$(du -k "${image}.raw" | cut -f1)"
139 verity_size="$(du -k "${image}.verity" | cut -f1)"
140 signature_size=4
141 # 4MB seems to be the minimum size blkid will accept, below that probing fails
142 dd if=/dev/zero of="${image}.gpt" bs=512 count=$((8192+root_size*2+verity_size*2+signature_size*2))
143 # sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
144 # so do some basic rounding up if the minimal image is more than 1 MB
145 if [ "${root_size}" -ge 1024 ]; then
146 root_size="$((root_size/1024 + 1))MiB"
147 else
148 root_size="${root_size}KiB"
149 fi
150 verity_size="$((verity_size * 2))KiB"
151 signature_size="$((signature_size * 2))KiB"
152
153 HAVE_OPENSSL=0
154 if systemctl --version | grep -q -- +OPENSSL ; then
155 # The openssl binary is installed conditionally.
156 # If we have OpenSSL support enabled and openssl is missing, fail early
157 # with a proper error message.
158 if ! command -v openssl >/dev/null 2>&1; then
159 echo "openssl missing" >/failed
160 exit 1
161 fi
162 HAVE_OPENSSL=1
163 # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
164 cat >>"${image}.openssl.cnf" <<EOF
165 [ req ]
166 prompt = no
167 distinguished_name = req_distinguished_name
168
169 [ req_distinguished_name ]
170 C = DE
171 ST = Test State
172 L = Test Locality
173 O = Org Name
174 OU = Org Unit Name
175 CN = Common Name
176 emailAddress = test@email.com
177 EOF
178
179 # Create key pair
180 openssl req -config "${image}.openssl.cnf" -new -x509 -newkey rsa:1024 -keyout "${image}.key" -out "${image}.crt" -days 365 -nodes
181 # Sign Verity root hash with it
182 openssl smime -sign -nocerts -noattr -binary -in "${image}.roothash" -inkey "${image}.key" -signer "${image}.crt" -outform der -out "${image}.roothash.p7s"
183 # Generate signature partition JSON data
184 echo '{"rootHash":"'"${roothash}"'","signature":"'"$(base64 -w 0 <"${image}.roothash.p7s")"'"}' >"${image}.verity-sig"
185 # Pad it
186 truncate -s "${signature_size}" "${image}.verity-sig"
187 # Register certificate in the (userspace) verity key ring
188 mkdir -p /run/verity.d
189 ln -s "${image}.crt" /run/verity.d/ok.crt
190 fi
191
192 # Construct a UUID from hash
193 # input: 11111111222233334444555566667777
194 # output: 11111111-2222-3333-4444-555566667777
195 uuid="$(head -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
196 echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk "${image}.gpt"
197 uuid="$(tail -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
198 echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk "${image}.gpt" --append
199 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
200 echo -e "size=${signature_size}, type=${signature_guid}" | sfdisk "${image}.gpt" --append
201 fi
202 sfdisk --part-label "${image}.gpt" 1 "Root Partition"
203 sfdisk --part-label "${image}.gpt" 2 "Verity Partition"
204 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
205 sfdisk --part-label "${image}.gpt" 3 "Signature Partition"
206 fi
207 loop="$(losetup --show -P -f "${image}.gpt")"
208 partitions=(
209 "${loop:?}p1"
210 "${loop:?}p2"
211 )
212 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
213 partitions+=( "${loop:?}p3" )
214 fi
215 # The kernel sometimes(?) does not emit "add" uevent for loop block partition devices.
216 # Let's not expect the devices to be initialized.
217 udevadm wait --timeout 60 --settle --initialized=no "${partitions[@]}"
218 udevadm lock --device="${loop}p1" dd if="${image}.raw" of="${loop}p1"
219 udevadm lock --device="${loop}p2" dd if="${image}.verity" of="${loop}p2"
220 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
221 udevadm lock --device="${loop}p3" dd if="${image}.verity-sig" of="${loop}p3"
222 fi
223 losetup -d "${loop}"
224
225 # Derive partition UUIDs from root hash, in UUID syntax
226 ROOT_UUID="$(systemd-id128 -u show "$(head -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
227 VERITY_UUID="$(systemd-id128 -u show "$(tail -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
228
229 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q '{"rw":"ro","designator":"root","partition_uuid":"'"$ROOT_UUID"'","partition_label":"Root Partition","fstype":"squashfs","architecture":"'"$architecture"'","verity":"signed",'
230 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q '{"rw":"ro","designator":"root-verity","partition_uuid":"'"$VERITY_UUID"'","partition_label":"Verity Partition","fstype":"DM_verity_hash","architecture":"'"$architecture"'","verity":null,'
231 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
232 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q -E '{"rw":"ro","designator":"root-verity-sig","partition_uuid":"'".*"'","partition_label":"Signature Partition","fstype":"verity_hash_signature","architecture":"'"$architecture"'","verity":null,'
233 fi
234 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F "MARKER=1"
235 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F -f <(sed 's/"//g' "$os_release")
236
237 # Test image policies
238 systemd-dissect --validate "${image}.gpt"
239 systemd-dissect --validate "${image}.gpt" --image-policy='*'
240 (! systemd-dissect --validate "${image}.gpt" --image-policy='~')
241 (! systemd-dissect --validate "${image}.gpt" --image-policy='-')
242 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=absent)
243 (! systemd-dissect --validate "${image}.gpt" --image-policy=swap=unprotected+encrypted+verity)
244 systemd-dissect --validate "${image}.gpt" --image-policy=root=unprotected
245 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity
246 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity-sig=unused+absent
247 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent
248 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent+unprotected
249 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity=unused+absent)
250 systemd-dissect --validate "${image}.gpt" --image-policy=root=signed
251 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity-sig=unused+absent)
252 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity=unused+absent)
253
254 # Test RootImagePolicy= unit file setting
255 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
256 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='*' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
257 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='~' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
258 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='-' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
259 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=absent' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
260 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=verity' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
261 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=signed' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
262 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=encrypted' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
263
264 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" "${image_dir}/mount"
265 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
266 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
267 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
268 systemd-dissect --umount "${image_dir}/mount"
269
270 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" --in-memory "${image_dir}/mount"
271 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
272 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
273 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
274 systemd-dissect --umount "${image_dir}/mount"
275
276 # add explicit -p MountAPIVFS=yes once to test the parser
277 systemd-run -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
278
279 systemd-run -P -p RootImage="${image}.raw" -p RootImageOptions="root:nosuid,dev home:ro,dev ro,noatime" mount | grep -F "squashfs" | grep -q -F "nosuid"
280 systemd-run -P -p RootImage="${image}.gpt" -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
281
282 mkdir -p "${image_dir}/result"
283 cat >/run/systemd/system/testservice-50a.service <<EOF
284 [Service]
285 Type=oneshot
286 ExecStart=bash -c "mount >/run/result/a"
287 BindPaths=${image_dir}/result:/run/result
288 TemporaryFileSystem=/run
289 RootImage=${image}.raw
290 RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
291 RootImageOptions=nosuid,dev
292 EOF
293 systemctl start testservice-50a.service
294 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F "noatime"
295 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F -v "nosuid"
296
297 cat >/run/systemd/system/testservice-50b.service <<EOF
298 [Service]
299 Type=oneshot
300 ExecStart=bash -c "mount >/run/result/b"
301 BindPaths=${image_dir}/result:/run/result
302 TemporaryFileSystem=/run
303 RootImage=${image}.gpt
304 RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
305 RootImageOptions=home:ro,dev nosuid,dev,%%foo
306 # this is the default, but let's specify once to test the parser
307 MountAPIVFS=yes
308 EOF
309 systemctl start testservice-50b.service
310 grep -F "squashfs" "${image_dir}/result/b" | grep -q -F "noatime"
311
312 # Check that specifier escape is applied %%foo → %foo
313 busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions | grep -F "nosuid,dev,%foo"
314
315 # Now do some checks with MountImages, both by itself, with options and in combination with RootImage, and as single FS or GPT image
316 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
317 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
318 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount | grep -F "squashfs" | grep -q -F "nosuid"
319 systemd-run -P -p MountImages="${image}.gpt:/run/img1:root:nosuid ${image}.raw:/run/img2:home:suid" mount | grep -F "squashfs" | grep -q -F "nosuid"
320 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3" cat /run/img2:3/usr/lib/os-release | grep -q -F "MARKER=1"
321 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3:nosuid" mount | grep -F "squashfs" | grep -q -F "nosuid"
322 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.raw" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /usr/lib/os-release | grep -q -F "MARKER=1"
323 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.raw" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
324 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
325 cat >/run/systemd/system/testservice-50c.service <<EOF
326 [Service]
327 MountAPIVFS=yes
328 TemporaryFileSystem=/run
329 RootImage=${image}.raw
330 MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
331 MountImages=${image}.raw:/run/img2\:3:nosuid
332 ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
333 ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
334 ExecStart=bash -c "mount >>/run/result/c"
335 BindPaths=${image_dir}/result:/run/result
336 Type=oneshot
337 EOF
338 systemctl start testservice-50c.service
339 grep -q -F "MARKER=1" "${image_dir}/result/c"
340 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F "noatime"
341 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F -v "nosuid"
342
343 # Adding a new mounts at runtime works if the unit is in the active state,
344 # so use Type=notify to make sure there's no race condition in the test
345 cat >/run/systemd/system/testservice-50d.service <<EOF
346 [Service]
347 RuntimeMaxSec=300
348 Type=notify
349 RemainAfterExit=yes
350 MountAPIVFS=yes
351 PrivateTmp=yes
352 ExecStart=/bin/sh -c ' \\
353 systemd-notify --ready; \\
354 while [[ ! -f /tmp/img/usr/lib/os-release ]] || ! grep -q -F MARKER /tmp/img/usr/lib/os-release; do \\
355 sleep 0.1; \\
356 done; \\
357 mount; \\
358 mount | grep -F "on /tmp/img type squashfs" | grep -q -F "nosuid"; \\
359 '
360 EOF
361 systemctl start testservice-50d.service
362
363 systemctl mount-image --mkdir testservice-50d.service "${image}.raw" /tmp/img root:nosuid
364
365 while systemctl show -P SubState testservice-50d.service | grep -q running
366 do
367 sleep 0.1
368 done
369
370 systemctl is-active testservice-50d.service
371
372 # ExtensionImages will set up an overlay
373 systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
374 systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
375 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
376 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
377 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /opt/script1.sh | grep -q -F "extension-release.app2"
378 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/other_file | grep -q -F "MARKER=1"
379 systemd-run -P --property ExtensionImages=/usr/share/app-nodistro.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
380 systemd-run -P --property ExtensionImages=/etc/service-scoped-test.raw --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
381 # Check that using a symlink to NAME-VERSION.raw works as long as the symlink has the correct name NAME.raw
382 mkdir -p /usr/share/symlink-test/
383 cp /usr/share/app-nodistro.raw /usr/share/symlink-test/app-nodistro-v1.raw
384 ln -fs /usr/share/symlink-test/app-nodistro-v1.raw /usr/share/symlink-test/app-nodistro.raw
385 systemd-run -P --property ExtensionImages=/usr/share/symlink-test/app-nodistro.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
386
387 # Symlink check again but for confext
388 mkdir -p /etc/symlink-test/
389 cp /etc/service-scoped-test.raw /etc/symlink-test/service-scoped-test-v1.raw
390 ln -fs /etc/symlink-test/service-scoped-test-v1.raw /etc/symlink-test/service-scoped-test.raw
391 systemd-run -P --property ExtensionImages=/etc/symlink-test/service-scoped-test.raw --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
392
393 cat >/run/systemd/system/testservice-50e.service <<EOF
394 [Service]
395 MountAPIVFS=yes
396 TemporaryFileSystem=/run /var/lib
397 StateDirectory=app0
398 RootImage=${image}.raw
399 ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
400 # Relevant only for sanitizer runs
401 UnsetEnvironment=LD_PRELOAD
402 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
403 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
404 Type=oneshot
405 RemainAfterExit=yes
406 EOF
407 systemctl start testservice-50e.service
408 systemctl is-active testservice-50e.service
409
410 # ExtensionDirectories will set up an overlay
411 mkdir -p "${image_dir}/app0" "${image_dir}/app1" "${image_dir}/app-nodistro" "${image_dir}/service-scoped-test"
412 (! systemd-run -P --property ExtensionDirectories="${image_dir}/nonexistent" --property RootImage="${image}.raw" cat /opt/script0.sh)
413 (! systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh)
414 systemd-dissect --mount /usr/share/app0.raw "${image_dir}/app0"
415 systemd-dissect --mount /usr/share/app1.raw "${image_dir}/app1"
416 systemd-dissect --mount /usr/share/app-nodistro.raw "${image_dir}/app-nodistro"
417 systemd-dissect --mount /etc/service-scoped-test.raw "${image_dir}/service-scoped-test"
418 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
419 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
420 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
421 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
422 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script1.sh | grep -q -F "extension-release.app2"
423 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/other_file | grep -q -F "MARKER=1"
424 systemd-run -P --property ExtensionDirectories="${image_dir}/app-nodistro" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
425 systemd-run -P --property ExtensionDirectories="${image_dir}/service-scoped-test" --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
426 cat >/run/systemd/system/testservice-50f.service <<EOF
427 [Service]
428 MountAPIVFS=yes
429 TemporaryFileSystem=/run /var/lib
430 StateDirectory=app0
431 RootImage=${image}.raw
432 ExtensionDirectories=${image_dir}/app0 ${image_dir}/app1
433 # Relevant only for sanitizer runs
434 UnsetEnvironment=LD_PRELOAD
435 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
436 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
437 Type=oneshot
438 RemainAfterExit=yes
439 EOF
440 systemctl start testservice-50f.service
441 systemctl is-active testservice-50f.service
442 systemd-dissect --umount "${image_dir}/app0"
443 systemd-dissect --umount "${image_dir}/app1"
444
445 # Test that an extension consisting of an empty directory under /etc/extensions/ takes precedence
446 mkdir -p /var/lib/extensions/
447 ln -s /usr/share/app-nodistro.raw /var/lib/extensions/app-nodistro.raw
448 systemd-sysext merge
449 grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file
450 systemd-sysext unmerge
451 mkdir -p /etc/extensions/app-nodistro
452 systemd-sysext merge
453 test ! -e /usr/lib/systemd/system/some_file
454 systemd-sysext unmerge
455 rmdir /etc/extensions/app-nodistro
456
457 # Check that extensions cannot contain os-release
458 mkdir -p /run/extensions/app-reject/usr/lib/{extension-release.d/,systemd/system}
459 echo "ID=_any" >/run/extensions/app-reject/usr/lib/extension-release.d/extension-release.app-reject
460 echo "ID=_any" >/run/extensions/app-reject/usr/lib/os-release
461 touch /run/extensions/app-reject/usr/lib/systemd/system/other_file
462 (! systemd-sysext merge)
463 test ! -e /usr/lib/systemd/system/some_file
464 test ! -e /usr/lib/systemd/system/other_file
465 systemd-sysext unmerge
466 rm -rf /run/extensions/app-reject
467 rm /var/lib/extensions/app-nodistro.raw
468
469 mkdir -p /run/machines /run/portables /run/extensions
470 touch /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
471
472 systemd-dissect --discover --json=short >/tmp/discover.json
473 grep -q -F '{"name":"a","type":"raw","class":"machine","ro":false,"path":"/run/machines/a.raw"' /tmp/discover.json
474 grep -q -F '{"name":"b","type":"raw","class":"portable","ro":false,"path":"/run/portables/b.raw"' /tmp/discover.json
475 grep -q -F '{"name":"c","type":"raw","class":"sysext","ro":false,"path":"/run/extensions/c.raw"' /tmp/discover.json
476 rm /tmp/discover.json /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
477
478 # Check that the /sbin/mount.ddi helper works
479 T="/tmp/mounthelper.$RANDOM"
480 mount -t ddi "${image}.gpt" "$T" -o ro,X-mount.mkdir,discard
481 umount -R "$T"
482 rmdir "$T"
483
484 LOOP="$(systemd-dissect --attach --loop-ref=waldo "${image}.raw")"
485
486 # Wait until the symlinks we want to test are established
487 udevadm trigger -w "$LOOP"
488
489 # Check if the /dev/loop/* symlinks really reference the right device
490 test /dev/disk/by-loop-ref/waldo -ef "$LOOP"
491
492 if [ "$(stat -c '%Hd:%Ld' "${image}.raw")" != '?d:?d' ] ; then
493 # Old stat didn't know the %Hd and %Ld specifiers and turned them into ?d
494 # instead. Let's simply skip the test on such old systems.
495 test "$(stat -c '/dev/disk/by-loop-inode/%Hd:%Ld-%i' "${image}.raw")" -ef "$LOOP"
496 fi
497
498 # Detach by loopback device
499 systemd-dissect --detach "$LOOP"
500
501 # Test long reference name.
502 # Note, sizeof_field(struct loop_info64, lo_file_name) == 64,
503 # and --loop-ref accepts upto 63 characters, and udev creates symlink
504 # based on the name when it has upto _62_ characters.
505 name="$(for _ in {1..62}; do echo -n 'x'; done)"
506 LOOP="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw")"
507 udevadm trigger -w "$LOOP"
508
509 # Check if the /dev/disk/by-loop-ref/$name symlink really references the right device
510 test "/dev/disk/by-loop-ref/$name" -ef "$LOOP"
511
512 # Detach by the /dev/disk/by-loop-ref symlink
513 systemd-dissect --detach "/dev/disk/by-loop-ref/$name"
514
515 name="$(for _ in {1..63}; do echo -n 'x'; done)"
516 LOOP="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw")"
517 udevadm trigger -w "$LOOP"
518
519 # Check if the /dev/disk/by-loop-ref/$name symlink does not exist
520 test ! -e "/dev/disk/by-loop-ref/$name"
521
522 # Detach by backing inode
523 systemd-dissect --detach "${image}.raw"
524 (! systemd-dissect --detach "${image}.raw")
525
526 # check for confext functionality
527 mkdir -p /run/confexts/test/etc/extension-release.d
528 echo "ID=_any" >/run/confexts/test/etc/extension-release.d/extension-release.test
529 echo "ARCHITECTURE=_any" >>/run/confexts/test/etc/extension-release.d/extension-release.test
530 echo "MARKER_CONFEXT_123" >/run/confexts/test/etc/testfile
531 cat <<EOF >/run/confexts/test/etc/testscript
532 #!/bin/bash
533 echo "This should not happen"
534 EOF
535 chmod +x /run/confexts/test/etc/testscript
536 systemd-confext merge
537 grep -q -F "MARKER_CONFEXT_123" /etc/testfile
538 (! /etc/testscript)
539 systemd-confext status
540 systemd-confext unmerge
541 rm -rf /run/confexts/
542
543 unsquashfs -no-xattrs -d /tmp/img "${image}.raw"
544 systemd-run --unit=test-root-ephemeral \
545 -p RootDirectory=/tmp/img \
546 -p RootEphemeral=yes \
547 -p Type=exec \
548 bash -c "touch /abc && sleep infinity"
549 test -n "$(ls -A /var/lib/systemd/ephemeral-trees)"
550 systemctl stop test-root-ephemeral
551 # shellcheck disable=SC2016
552 timeout 10 bash -c 'until test -z "$(ls -A /var/lib/systemd/ephemeral-trees)"; do sleep .5; done'
553 test ! -f /tmp/img/abc
554
555 systemd-dissect --mtree /tmp/img
556 systemd-dissect --list /tmp/img
557
558 read -r SHA256SUM1 _ < <(systemd-dissect --copy-from /tmp/img etc/os-release | sha256sum)
559 test "$SHA256SUM1" != ""
560
561 echo abc > abc
562 systemd-dissect --copy-to /tmp/img abc /abc
563 test -f /tmp/img/abc
564
565 # Test for dissect tool support with systemd-sysext
566 mkdir -p /run/extensions/ testkit/usr/lib/extension-release.d/
567 echo "ID=_any" >testkit/usr/lib/extension-release.d/extension-release.testkit
568 echo "ARCHITECTURE=_any" >>testkit/usr/lib/extension-release.d/extension-release.testkit
569 echo "MARKER_SYSEXT_123" >testkit/usr/lib/testfile
570 mksquashfs testkit/ testkit.raw
571 cp testkit.raw /run/extensions/
572 unsquashfs -l /run/extensions/testkit.raw
573 systemd-dissect --no-pager /run/extensions/testkit.raw | grep -q '✓ sysext extension for portable service'
574 systemd-dissect --no-pager /run/extensions/testkit.raw | grep -q '✓ sysext extension for system'
575 systemd-sysext merge
576 systemd-sysext status
577 grep -q -F "MARKER_SYSEXT_123" /usr/lib/testfile
578 systemd-sysext unmerge
579 rm -rf /run/extensions/ testkit/
580
581 # Test for dissect tool support with systemd-confext
582 mkdir -p /run/confexts/ testjob/etc/extension-release.d/
583 echo "ID=_any" >testjob/etc/extension-release.d/extension-release.testjob
584 echo "ARCHITECTURE=_any" >>testjob/etc/extension-release.d/extension-release.testjob
585 echo "MARKER_CONFEXT_123" >testjob/etc/testfile
586 mksquashfs testjob/ testjob.raw
587 cp testjob.raw /run/confexts/
588 unsquashfs -l /run/confexts/testjob.raw
589 systemd-dissect --no-pager /run/confexts/testjob.raw | grep -q '✓ confext extension for system'
590 systemd-dissect --no-pager /run/confexts/testjob.raw | grep -q '✓ confext extension for portable service'
591 systemd-confext merge
592 systemd-confext status
593 grep -q -F "MARKER_CONFEXT_123" /etc/testfile
594 systemd-confext unmerge
595 rm -rf /run/confexts/ testjob/
596
597 systemd-run -P -p RootImage="${image}.raw" cat /run/host/os-release | cmp "${os_release}"
598
599 # Test that systemd-sysext reloads the daemon.
600 mkdir -p /var/lib/extensions/
601 ln -s /usr/share/app-reload.raw /var/lib/extensions/app-reload.raw
602 systemd-sysext merge --no-reload
603 # the service should not be running
604 if systemctl --quiet is-active foo.service; then
605 echo "foo.service should not be active"
606 exit 1
607 fi
608 systemd-sysext unmerge --no-reload
609 systemd-sysext merge
610 for RETRY in $(seq 60) LAST; do
611 if journalctl --boot --unit foo.service | grep -q -P 'echo\[[0-9]+\]: foo'; then
612 break
613 fi
614 if [ "${RETRY}" = LAST ]; then
615 echo "Output of foo.service not found"
616 exit 1
617 fi
618 sleep 0.5
619 done
620 systemd-sysext unmerge --no-reload
621 # Grep on the Warning to find the warning helper mentioning the daemon reload.
622 systemctl status foo.service 2>&1 | grep -q -F "Warning"
623 systemd-sysext merge
624 systemd-sysext unmerge
625 systemctl status foo.service 2>&1 | grep -v -q -F "Warning"
626
627 touch /testok