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