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 # shellcheck disable=SC2233,SC2235
9 export SYSTEMD_LOG_LEVEL
=debug
11 # shellcheck disable=SC2317
15 if [ -z "${image_dir}" ]; then
18 umount
"${image_dir}/app0"
19 umount
"${image_dir}/app1"
20 umount
"${image_dir}/app-nodistro"
21 umount
"${image_dir}/service-scoped-test"
25 udevadm control
--log-level=debug
29 image_dir
="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
30 if [ -z "${image_dir}" ] ||
[ ! -d "${image_dir}" ]; then
31 echo "mktemp under /tmp failed"
37 cp /usr
/share
/minimal
* "${image_dir}/"
38 image
="${image_dir}/minimal_0"
39 roothash
="$(cat "${image}.roothash
")"
41 os_release
="$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)"
43 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"'
44 systemd-dissect
"${image}.raw" |
grep -q -F "MARKER=1"
45 systemd-dissect
"${image}.raw" |
grep -q -F -f <(sed 's/"//g' "$os_release")
47 systemd-dissect
--list "${image}.raw" |
grep -q '^etc/os-release$'
48 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]*$"
49 systemd-dissect
--mtree "${image}.raw" --mtree-hash no |
grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]*$"
51 read -r SHA256SUM1 _
< <(systemd-dissect
--copy-from "${image}.raw" etc
/os-release | sha256sum
)
52 test "$SHA256SUM1" != ""
53 read -r SHA256SUM2 _
< <(systemd-dissect
--read-only --with "${image}.raw" sha256sum etc
/os-release
)
54 test "$SHA256SUM2" != ""
55 test "$SHA256SUM1" = "$SHA256SUM2"
57 mv "${image}.verity" "${image}.fooverity"
58 mv "${image}.roothash" "${image}.foohash"
59 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"'
60 systemd-dissect
"${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" |
grep -q -F "MARKER=1"
61 systemd-dissect
"${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" |
grep -q -F -f <(sed 's/"//g' "$os_release")
62 mv "${image}.fooverity" "${image}.verity"
63 mv "${image}.foohash" "${image}.roothash"
65 mkdir
-p "${image_dir}/mount" "${image_dir}/mount2"
66 systemd-dissect
--mount "${image}.raw" "${image_dir}/mount"
67 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
68 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
69 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
70 # Verity volume should be shared (opened only once)
71 systemd-dissect
--mount "${image}.raw" "${image_dir}/mount2"
72 verity_count
=$
(find /dev
/mapper
/ -name "*verity*" |
wc -l)
73 # In theory we should check that count is exactly one. In practice, libdevmapper
74 # randomly and unpredictably fails with an unhelpful EINVAL when a device is open
75 # (and even mounted and in use), so best-effort is the most we can do for now
76 if [ "${verity_count}" -lt 1 ]; then
77 echo "Verity device ${image}.raw not found in /dev/mapper/"
80 systemd-dissect
--umount "${image_dir}/mount"
81 systemd-dissect
--umount "${image_dir}/mount2"
83 systemd-run
-P -p RootImage
="${image}.raw" cat /usr
/lib
/os-release |
grep -q -F "MARKER=1"
84 mv "${image}.verity" "${image}.fooverity"
85 mv "${image}.roothash" "${image}.foohash"
86 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"
87 # Let's use the long option name just here as a test
88 systemd-run
-P --property RootImage
="${image}.raw" --property RootHash="${roothash}" --property RootVerity="${image}.fooverity" cat /usr
/lib
/os-release |
grep -q -F "MARKER=1"
89 mv "${image}.fooverity" "${image}.verity"
90 mv "${image}.foohash" "${image}.roothash"
92 # Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
94 if [ "${machine}" = "x86_64" ]; then
95 root_guid
=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
96 verity_guid
=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
97 signature_guid
=41092b05-9fc8-4523-994f-2def0408b176
99 elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
100 root_guid
=44479540-f297-41b2-9af7-d131d5f0458a
101 verity_guid
=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
102 signature_guid
=5996fc05-109c-48de-808b-23fa0830b676
104 elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
105 root_guid
=b921b045-1df0-41c3-af44-4c6f280d3fae
106 verity_guid
=df3300ce-d69f-4c92-978c-9bfb0f38d820
107 signature_guid
=6db69de6-29f4-4758-a7a5-962190f00ce3
109 elif [ "${machine}" = "arm" ]; then
110 root_guid
=69dad710-2ce4-4e3c-b16c-21a1d49abed3
111 verity_guid
=7386cdf2-203c-47a9-a498-f2ecce45a2d6
112 signature_guid
=42b0455f-eb11-491d-98d3-56145ba9d037
114 elif [ "${machine}" = "loongarch64" ]; then
115 root_guid
=77055800-792c-4f94-b39a-98c91b762bb6
116 verity_guid
=f3393b22-e9af-4613-a948-9d3bfbd0c535
117 signature_guid
=5afb67eb-ecc8-4f85-ae8e-ac1e7c50e7d0
118 architecture
="loongarch64"
119 elif [ "${machine}" = "ia64" ]; then
120 root_guid
=993d8d3d-f80e-4225-855a-9daf8ed7ea97
121 verity_guid
=86ed10d5-b607-45bb-8957-d350f23d0571
122 signature_guid
=e98b36ee-32ba-4882-9b12-0ce14655f46a
124 elif [ "${machine}" = "s390x" ]; then
125 root_guid
=5eead9a9-fe09-4a1e-a1d7-520d00531306
126 verity_guid
=b325bfbe-c7be-4ab8-8357-139e652d2f6b
127 signature_guid
=c80187a5-73a3-491a-901a-017c3fa953e9
129 elif [ "${machine}" = "ppc64le" ]; then
130 root_guid
=c31c45e6-3f39-412e-80fb-4809c4980599
131 verity_guid
=906bd944-4589-4aae-a4e4-dd983917446a
132 signature_guid
=d4a236e7-e873-4c07-bf1d-bf6cf7f1c3c6
133 architecture
="ppc64-le"
135 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
138 # du rounds up to block size, which is more helpful for partitioning
139 root_size
="$(du -k "${image}.raw
" | cut -f1)"
140 verity_size
="$(du -k "${image}.verity
" | cut -f1)"
142 # 4MB seems to be the minimum size blkid will accept, below that probing fails
143 dd if=/dev
/zero of
="${image}.gpt" bs
=512 count
=$
((8192+root_size
*2+verity_size
*2+signature_size
*2))
144 # sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
145 # so do some basic rounding up if the minimal image is more than 1 MB
146 if [ "${root_size}" -ge 1024 ]; then
147 root_size
="$((root_size/1024 + 1))MiB"
149 root_size
="${root_size}KiB"
151 verity_size
="$((verity_size * 2))KiB"
152 signature_size
="$((signature_size * 2))KiB"
155 if systemctl
--version |
grep -q -- +OPENSSL
; then
156 # The openssl binary is installed conditionally.
157 # If we have OpenSSL support enabled and openssl is missing, fail early
158 # with a proper error message.
159 if ! command -v openssl
>/dev
/null
2>&1; then
160 echo "openssl missing" >/failed
165 OPENSSL_CONFIG
="$(mktemp)"
166 # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
167 cat >"${OPENSSL_CONFIG:?}" <<EOF
170 distinguished_name = req_distinguished_name
172 [ req_distinguished_name ]
179 emailAddress = test@email.com
183 openssl req
-config "$OPENSSL_CONFIG" -new -x509 -newkey rsa
:1024 -keyout "${image}.key" -out "${image}.crt" -days 365 -nodes
184 # Sign Verity root hash with it
185 openssl smime
-sign -nocerts -noattr -binary -in "${image}.roothash" -inkey "${image}.key" -signer "${image}.crt" -outform der -out "${image}.roothash.p7s"
186 # Generate signature partition JSON data
187 echo '{"rootHash":"'"${roothash}"'","signature":"'"$(base64 -w 0 <"${image}.roothash.p7s")"'"}' >"${image}.verity-sig
"
189 truncate -s "${signature_size}" "${image}.verity-sig
"
190 # Register certificate in the (userspace) verity key ring
191 mkdir -p /run/verity.d
192 ln -s "${image}.crt
" /run/verity.d/ok.crt
195 # Construct a UUID from hash
196 # input: 11111111222233334444555566667777
197 # output: 11111111-2222-3333-4444-555566667777
198 uuid="$
(head -c 32 "${image}.roothash" |
sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
199 echo -e "label
: gpt
\nsize
=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk "${image}.gpt
"
200 uuid="$
(tail -c 32 "${image}.roothash" |
sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
201 echo -e "size
=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk "${image}.gpt
" --append
202 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
203 echo -e "size
=${signature_size}, type=${signature_guid}" | sfdisk "${image}.gpt
" --append
205 sfdisk --part-label "${image}.gpt
" 1 "Root Partition
"
206 sfdisk --part-label "${image}.gpt
" 2 "Verity Partition
"
207 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
208 sfdisk --part-label "${image}.gpt
" 3 "Signature Partition
"
210 loop="$
(losetup
--show -P -f "${image}.gpt")"
215 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
216 partitions+=( "${loop:?}p3
" )
218 # The kernel sometimes(?) does not emit "add
" uevent for loop block partition devices.
219 # Let's not expect the devices to be initialized.
220 udevadm wait --timeout 60 --settle --initialized=no "${partitions[@]}"
221 udevadm lock --device="${loop}p1" dd if="${image}.raw" of="${loop}p1
"
222 udevadm lock --device="${loop}p2" dd if="${image}.verity" of="${loop}p2
"
223 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
224 udevadm lock --device="${loop}p3" dd if="${image}.verity-sig" of="${loop}p3
"
228 # Derive partition UUIDs from root hash, in UUID syntax
229 ROOT_UUID="$
(systemd-id128
-u show
"$(head -c 32 "${image}.roothash
")" -u |
tail -n 1 | cut
-b 6-)"
230 VERITY_UUID="$
(systemd-id128
-u show
"$(tail -c 32 "${image}.roothash
")" -u |
tail -n 1 | cut
-b 6-)"
232 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
",'
233 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,'
234 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
235 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,'
237 systemd-dissect --root-hash "${roothash}" "${image}.gpt
" | grep -q -F "MARKER
=1"
238 systemd-dissect --root-hash "${roothash}" "${image}.gpt
" | grep -q -F -f <(sed 's/"//g
' "$os_release")
240 # Test image policies
241 systemd-dissect --validate "${image}.gpt"
242 systemd-dissect --validate "${image}.gpt" --image-policy='*'
243 (! systemd-dissect --validate "${image}.gpt" --image-policy='~
')
244 (! systemd-dissect --validate "${image}.gpt" --image-policy='-')
245 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=absent)
246 (! systemd-dissect --validate "${image}.gpt" --image-policy=swap=unprotected+encrypted+verity)
247 systemd-dissect --validate "${image}.gpt" --image-policy=root=unprotected
248 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity
249 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity-sig=unused+absent
250 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent
251 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent+unprotected
252 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity=unused+absent)
253 systemd-dissect --validate "${image}.gpt" --image-policy=root=signed
254 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity-sig=unused+absent)
255 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity=unused+absent)
257 # Test RootImagePolicy= unit file setting
258 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -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='*' -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='~
' -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='-' -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
=absent
' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
263 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"
264 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"
265 (! 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")
267 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" "${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"
273 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" --in-memory "${image_dir}/mount"
274 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
275 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
276 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
277 systemd-dissect --umount "${image_dir}/mount"
279 # add explicit -p MountAPIVFS=yes once to test the parser
280 systemd-run -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
282 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"
283 systemd-run -P -p RootImage="${image}.gpt" -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
285 mkdir -p "${image_dir}/result"
286 cat >/run/systemd/system/testservice-50a.service <<EOF
289 ExecStart=bash -c "mount >/run/result/a"
290 BindPaths=${image_dir}/result:/run/result
291 TemporaryFileSystem=/run
292 RootImage=${image}.raw
293 RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
294 RootImageOptions=nosuid,dev
296 systemctl start testservice-50a.service
297 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F "noatime"
298 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F -v "nosuid"
300 cat >/run/systemd/system/testservice-50b.service <<EOF
303 ExecStart=bash -c "mount >/run/result/b"
304 BindPaths=${image_dir}/result:/run/result
305 TemporaryFileSystem=/run
306 RootImage=${image}.gpt
307 RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
308 RootImageOptions=home:ro,dev nosuid,dev,%%foo
309 # this is the default, but let's specify once to
test the parser
312 systemctl start testservice-50b.service
313 grep -F "squashfs" "${image_dir}/result/b" |
grep -q -F "noatime"
315 # Check that specifier escape is applied %%foo → %foo
316 busctl get-property org.freedesktop.systemd1
/org
/freedesktop
/systemd
1/unit
/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions |
grep -F "nosuid,dev,%foo"
318 # Now do some checks with MountImages, both by itself, with options and in combination with RootImage, and as single FS or GPT image
319 systemd-run
-P -p MountImages
="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run
/img
1/usr
/lib
/os-release |
grep -q -F "MARKER=1"
320 systemd-run
-P -p MountImages
="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run
/img
2/usr
/lib
/os-release |
grep -q -F "MARKER=1"
321 systemd-run
-P -p MountImages
="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount |
grep -F "squashfs" |
grep -q -F "nosuid"
322 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"
323 systemd-run
-P -p MountImages
="${image}.raw:/run/img2\:3" cat /run
/img2
:3/usr
/lib
/os-release |
grep -q -F "MARKER=1"
324 systemd-run
-P -p MountImages
="${image}.raw:/run/img2\:3:nosuid" mount |
grep -F "squashfs" |
grep -q -F "nosuid"
325 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"
326 systemd-run
-P -p TemporaryFileSystem
=/run
-p RootImage
="${image}.raw" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run
/img
1/usr
/lib
/os-release |
grep -q -F "MARKER=1"
327 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
/img
2/usr
/lib
/os-release |
grep -q -F "MARKER=1"
328 cat >/run
/systemd
/system
/testservice-50c.service
<<EOF
331 TemporaryFileSystem=/run
332 RootImage=${image}.raw
333 MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
334 MountImages=${image}.raw:/run/img2\:3:nosuid
335 ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
336 ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
337 ExecStart=bash -c "mount >>/run/result/c"
338 BindPaths=${image_dir}/result:/run/result
341 systemctl start testservice-50c.service
342 grep -q -F "MARKER=1" "${image_dir}/result/c"
343 grep -F "squashfs" "${image_dir}/result/c" |
grep -q -F "noatime"
344 grep -F "squashfs" "${image_dir}/result/c" |
grep -q -F -v "nosuid"
346 # Adding a new mounts at runtime works if the unit is in the active state,
347 # so use Type=notify to make sure there's no race condition in the test
348 cat >/run
/systemd
/system
/testservice-50d.service
<<EOF
356 systemd-notify --ready; \\
357 while [ ! -f /tmp/img/usr/lib/os-release ] || ! grep -q -F MARKER /tmp/img/usr/lib/os-release; do \\
361 mount | grep -F "on /tmp/img type squashfs" | grep -q -F "nosuid"; \\
364 systemctl start testservice-50d.service
366 # Mount twice to exercise mount-beneath (on kernel 6.5+, on older kernels it will just overmount)
367 mkdir
-p /tmp
/wrong
/foo
368 mksquashfs
/tmp
/wrong
/foo
/tmp
/wrong.raw
369 systemctl mount-image
--mkdir testservice-50d.service
/tmp
/wrong.raw
/tmp
/img
370 test "$(systemctl show -P SubState testservice-50d.service)" = "running"
371 systemctl mount-image
--mkdir testservice-50d.service
"${image}.raw" /tmp
/img root
:nosuid
373 while systemctl show
-P SubState testservice-50d.service |
grep -q running
378 systemctl is-active testservice-50d.service
380 # ExtensionImages will set up an overlay
381 systemd-run
-P --property ExtensionImages
=/usr
/share
/app0.raw
--property RootImage
="${image}.raw" cat /opt
/script0.sh |
grep -q -F "extension-release.app0"
382 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"
383 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"
384 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"
385 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"
386 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"
387 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"
388 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"
389 # Check that using a symlink to NAME-VERSION.raw works as long as the symlink has the correct name NAME.raw
390 mkdir
-p /usr
/share
/symlink-test
/
391 cp /usr
/share
/app-nodistro.raw
/usr
/share
/symlink-test
/app-nodistro-v1.raw
392 ln -fs /usr
/share
/symlink-test
/app-nodistro-v1.raw
/usr
/share
/symlink-test
/app-nodistro.raw
393 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"
395 # Symlink check again but for confext
396 mkdir
-p /etc
/symlink-test
/
397 cp /etc
/service-scoped-test.raw
/etc
/symlink-test
/service-scoped-test-v1.raw
398 ln -fs /etc
/symlink-test
/service-scoped-test-v1.raw
/etc
/symlink-test
/service-scoped-test.raw
399 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"
400 # And again mixing sysext and confext
402 --property ExtensionImages
=/usr
/share
/symlink-test
/app-nodistro.raw \
403 --property ExtensionImages
=/etc
/symlink-test
/service-scoped-test.raw \
404 --property RootImage
="${image}.raw" cat /etc
/systemd
/system
/some_file |
grep -q -F "MARKER_CONFEXT_123"
406 --property ExtensionImages
=/usr
/share
/symlink-test
/app-nodistro.raw \
407 --property ExtensionImages
=/etc
/symlink-test
/service-scoped-test.raw \
408 --property RootImage
="${image}.raw" cat /usr
/lib
/systemd
/system
/some_file |
grep -q -F "MARKER=1"
410 cat >/run
/systemd
/system
/testservice-50e.service
<<EOF
413 TemporaryFileSystem=/run /var/lib
415 RootImage=${image}.raw
416 ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
417 # Relevant only for sanitizer runs
418 UnsetEnvironment=LD_PRELOAD
419 ExecStart=bash -c '/opt/script0.sh | grep ID'
420 ExecStart=bash -c '/opt/script1.sh | grep ID'
424 systemctl start testservice-50e.service
425 systemctl is-active testservice-50e.service
427 # ExtensionDirectories will set up an overlay
428 mkdir
-p "${image_dir}/app0" "${image_dir}/app1" "${image_dir}/app-nodistro" "${image_dir}/service-scoped-test"
429 (! systemd-run
-P --property ExtensionDirectories
="${image_dir}/nonexistent" --property RootImage
="${image}.raw" cat /opt
/script0.sh
)
430 (! systemd-run
-P --property ExtensionDirectories
="${image_dir}/app0" --property RootImage
="${image}.raw" cat /opt
/script0.sh
)
431 systemd-dissect
--mount /usr
/share
/app0.raw
"${image_dir}/app0"
432 systemd-dissect
--mount /usr
/share
/app1.raw
"${image_dir}/app1"
433 systemd-dissect
--mount /usr
/share
/app-nodistro.raw
"${image_dir}/app-nodistro"
434 systemd-dissect
--mount /etc
/service-scoped-test.raw
"${image_dir}/service-scoped-test"
435 systemd-run
-P --property ExtensionDirectories
="${image_dir}/app0" --property RootImage
="${image}.raw" cat /opt
/script0.sh |
grep -q -F "extension-release.app0"
436 systemd-run
-P --property ExtensionDirectories
="${image_dir}/app0" --property RootImage
="${image}.raw" cat /usr
/lib
/systemd
/system
/some_file |
grep -q -F "MARKER=1"
437 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"
438 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"
439 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"
440 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"
441 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"
442 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"
443 cat >/run
/systemd
/system
/testservice-50f.service
<<EOF
446 TemporaryFileSystem=/run /var/lib
448 RootImage=${image}.raw
449 ExtensionDirectories=${image_dir}/app0 ${image_dir}/app1
450 # Relevant only for sanitizer runs
451 UnsetEnvironment=LD_PRELOAD
452 ExecStart=bash -c '/opt/script0.sh | grep ID'
453 ExecStart=bash -c '/opt/script1.sh | grep ID'
457 systemctl start testservice-50f.service
458 systemctl is-active testservice-50f.service
459 systemd-dissect
--umount "${image_dir}/app0"
460 systemd-dissect
--umount "${image_dir}/app1"
462 # Test that an extension consisting of an empty directory under /etc/extensions/ takes precedence
463 mkdir
-p /var
/lib
/extensions
/
464 ln -s /usr
/share
/app-nodistro.raw
/var
/lib
/extensions
/app-nodistro.raw
466 grep -q -F "MARKER=1" /usr
/lib
/systemd
/system
/some_file
467 systemd-sysext unmerge
468 mkdir
-p /etc
/extensions
/app-nodistro
470 test ! -e /usr
/lib
/systemd
/system
/some_file
471 systemd-sysext unmerge
472 rmdir /etc
/extensions
/app-nodistro
474 # Similar, but go via varlink
475 varlinkctl call
/run
/systemd
/io.systemd.sysext io.systemd.sysext.List
'{}'
476 (! grep -q -F "MARKER=1" /usr
/lib
/systemd
/system
/some_file
)
477 varlinkctl call
/run
/systemd
/io.systemd.sysext io.systemd.sysext.Merge
'{}'
478 grep -q -F "MARKER=1" /usr
/lib
/systemd
/system
/some_file
479 varlinkctl call
/run
/systemd
/io.systemd.sysext io.systemd.sysext.Refresh
'{}'
480 grep -q -F "MARKER=1" /usr
/lib
/systemd
/system
/some_file
481 varlinkctl call
/run
/systemd
/io.systemd.sysext io.systemd.sysext.Unmerge
'{}'
482 (! grep -q -F "MARKER=1" /usr
/lib
/systemd
/system
/some_file
)
484 # Check that extensions cannot contain os-release
485 mkdir
-p /run
/extensions
/app-reject
/usr
/lib
/{extension-release.d
/,systemd
/system
}
486 echo "ID=_any" >/run
/extensions
/app-reject
/usr
/lib
/extension-release.d
/extension-release.app-reject
487 echo "ID=_any" >/run
/extensions
/app-reject
/usr
/lib
/os-release
488 touch /run
/extensions
/app-reject
/usr
/lib
/systemd
/system
/other_file
489 (! systemd-sysext merge
)
490 test ! -e /usr
/lib
/systemd
/system
/some_file
491 test ! -e /usr
/lib
/systemd
/system
/other_file
492 systemd-sysext unmerge
493 rm -rf /run
/extensions
/app-reject
494 rm /var
/lib
/extensions
/app-nodistro.raw
496 # Some super basic test that RootImage= works with .v/ dirs
498 VDIR
="/tmp/${VBASE}.v"
501 ln -s "${image}.raw" "$VDIR/${VBASE}_33.raw"
502 ln -s "${image}.raw" "$VDIR/${VBASE}_34.raw"
503 ln -s "${image}.raw" "$VDIR/${VBASE}_35.raw"
505 systemd-run
-P -p RootImage
="$VDIR" cat /usr
/lib
/os-release |
grep -q -F "MARKER=1"
507 rm "$VDIR/${VBASE}_33.raw" "$VDIR/${VBASE}_34.raw" "$VDIR/${VBASE}_35.raw"
510 mkdir
-p /run
/machines
/run
/portables
/run
/extensions
511 touch /run
/machines
/a.raw
/run
/portables
/b.raw
/run
/extensions
/c.raw
513 systemd-dissect
--discover --json=short
>/tmp
/discover.json
514 grep -q -F '{"name":"a","type":"raw","class":"machine","ro":false,"path":"/run/machines/a.raw"' /tmp
/discover.json
515 grep -q -F '{"name":"b","type":"raw","class":"portable","ro":false,"path":"/run/portables/b.raw"' /tmp
/discover.json
516 grep -q -F '{"name":"c","type":"raw","class":"sysext","ro":false,"path":"/run/extensions/c.raw"' /tmp
/discover.json
517 rm /tmp
/discover.json
/run
/machines
/a.raw
/run
/portables
/b.raw
/run
/extensions
/c.raw
519 # Check that the /sbin/mount.ddi helper works
520 T
="/tmp/mounthelper.$RANDOM"
521 mount
-t ddi
"${image}.gpt" "$T" -o ro
,X-mount.mkdir
,discard
525 LOOP
="$(systemd-dissect --attach --loop-ref=waldo "${image}.raw
")"
527 # Wait until the symlinks we want to test are established
528 udevadm trigger
-w "$LOOP"
530 # Check if the /dev/loop/* symlinks really reference the right device
531 test /dev
/disk
/by-loop-ref
/waldo
-ef "$LOOP"
533 if [ "$(stat -c '%Hd:%Ld' "${image}.raw
")" != '?d:?d' ] ; then
534 # Old stat didn't know the %Hd and %Ld specifiers and turned them into ?d
535 # instead. Let's simply skip the test on such old systems.
536 test "$(stat -c '/dev/disk/by-loop-inode/%Hd:%Ld-%i' "${image}.raw
")" -ef "$LOOP"
539 # Detach by loopback device
540 systemd-dissect
--detach "$LOOP"
542 # Test long reference name.
543 # Note, sizeof_field(struct loop_info64, lo_file_name) == 64,
544 # and --loop-ref accepts upto 63 characters, and udev creates symlink
545 # based on the name when it has upto _62_ characters.
546 name
="$(for _ in {1..62}; do echo -n 'x'; done)"
547 LOOP
="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw
")"
548 udevadm trigger
-w "$LOOP"
550 # Check if the /dev/disk/by-loop-ref/$name symlink really references the right device
551 test "/dev/disk/by-loop-ref/$name" -ef "$LOOP"
553 # Detach by the /dev/disk/by-loop-ref symlink
554 systemd-dissect
--detach "/dev/disk/by-loop-ref/$name"
556 name
="$(for _ in {1..63}; do echo -n 'x'; done)"
557 LOOP
="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw
")"
558 udevadm trigger
-w "$LOOP"
560 # Check if the /dev/disk/by-loop-ref/$name symlink does not exist
561 test ! -e "/dev/disk/by-loop-ref/$name"
563 # Detach by backing inode
564 systemd-dissect
--detach "${image}.raw"
565 (! systemd-dissect
--detach "${image}.raw")
567 # check for confext functionality
568 mkdir
-p /run
/confexts
/test
/etc
/extension-release.d
569 echo "ID=_any" >/run
/confexts
/test
/etc
/extension-release.d
/extension-release.
test
570 echo "ARCHITECTURE=_any" >>/run
/confexts
/test
/etc
/extension-release.d
/extension-release.
test
571 echo "MARKER_CONFEXT_123" >/run
/confexts
/test
/etc
/testfile
572 cat <<EOF >/run/confexts/test/etc/testscript
574 echo "This should not happen"
576 chmod +x
/run
/confexts
/test
/etc
/testscript
577 systemd-confext merge
578 grep -q -F "MARKER_CONFEXT_123" /etc
/testfile
580 systemd-confext status
581 systemd-confext unmerge
582 rm -rf /run
/confexts
/
584 unsquashfs
-no-xattrs -d /tmp
/img
"${image}.raw"
585 systemd-run
--unit=test-root-ephemeral \
586 -p RootDirectory
=/tmp
/img \
587 -p RootEphemeral
=yes \
589 bash
-c "touch /abc && sleep infinity"
590 test -n "$(ls -A /var/lib/systemd/ephemeral-trees)"
591 systemctl stop test-root-ephemeral
592 # shellcheck disable=SC2016
593 timeout
10 bash
-c 'until test -z "$(ls -A /var/lib/systemd/ephemeral-trees)"; do sleep .5; done'
594 test ! -f /tmp
/img
/abc
596 systemd-dissect
--mtree /tmp
/img
597 systemd-dissect
--list /tmp
/img
599 read -r SHA256SUM1 _
< <(systemd-dissect
--copy-from /tmp
/img etc
/os-release | sha256sum
)
600 test "$SHA256SUM1" != ""
603 systemd-dissect
--copy-to /tmp
/img abc
/abc
606 # Test for dissect tool support with systemd-sysext
607 mkdir
-p /run
/extensions
/ testkit
/usr
/lib
/extension-release.d
/
608 echo "ID=_any" >testkit
/usr
/lib
/extension-release.d
/extension-release.testkit
609 echo "ARCHITECTURE=_any" >>testkit
/usr
/lib
/extension-release.d
/extension-release.testkit
610 echo "MARKER_SYSEXT_123" >testkit
/usr
/lib
/testfile
611 mksquashfs testkit
/ testkit.raw
612 cp testkit.raw
/run
/extensions
/
613 unsquashfs
-l /run
/extensions
/testkit.raw
614 systemd-dissect
--no-pager /run
/extensions
/testkit.raw |
grep -q '✓ sysext for portable service'
615 systemd-dissect
--no-pager /run
/extensions
/testkit.raw |
grep -q '✓ sysext for system'
617 systemd-sysext status
618 grep -q -F "MARKER_SYSEXT_123" /usr
/lib
/testfile
619 systemd-sysext unmerge
620 rm -rf /run
/extensions
/ testkit
/
622 # Test for dissect tool support with systemd-confext
623 mkdir
-p /run
/confexts
/ testjob
/etc
/extension-release.d
/
624 echo "ID=_any" >testjob
/etc
/extension-release.d
/extension-release.testjob
625 echo "ARCHITECTURE=_any" >>testjob
/etc
/extension-release.d
/extension-release.testjob
626 echo "MARKER_CONFEXT_123" >testjob
/etc
/testfile
627 mksquashfs testjob
/ testjob.raw
628 cp testjob.raw
/run
/confexts
/
629 unsquashfs
-l /run
/confexts
/testjob.raw
630 systemd-dissect
--no-pager /run
/confexts
/testjob.raw |
grep -q '✓ confext for system'
631 systemd-dissect
--no-pager /run
/confexts
/testjob.raw |
grep -q '✓ confext for portable service'
632 systemd-confext merge
633 systemd-confext status
634 grep -q -F "MARKER_CONFEXT_123" /etc
/testfile
635 systemd-confext unmerge
636 rm -rf /run
/confexts
/ testjob
/
638 systemd-run
-P -p RootImage
="${image}.raw" cat /run
/host
/os-release |
cmp "${os_release}"
640 # Test that systemd-sysext reloads the daemon.
641 mkdir
-p /var
/lib
/extensions
/
642 ln -s /usr
/share
/app-reload.raw
/var
/lib
/extensions
/app-reload.raw
643 systemd-sysext merge
--no-reload
644 # the service should not be running
645 if systemctl
--quiet is-active foo.service
; then
646 echo "foo.service should not be active"
649 systemd-sysext unmerge
--no-reload
651 for RETRY
in $
(seq 60) LAST
; do
652 if journalctl
--boot --unit foo.service |
grep -q -P 'echo\[[0-9]+\]: foo'; then
655 if [ "${RETRY}" = LAST
]; then
656 echo "Output of foo.service not found"
661 systemd-sysext unmerge
--no-reload
662 # Grep on the Warning to find the warning helper mentioning the daemon reload.
663 systemctl status foo.service
2>&1 |
grep -q -F "Warning"
665 systemd-sysext unmerge
666 systemctl status foo.service
2>&1 |
grep -v -q -F "Warning"
667 rm /var
/lib
/extensions
/app-reload.raw
669 # Test systemd-repart --make-ddi=:
670 if command -v mksquashfs
>/dev
/null
2>&1; then
672 openssl req
-config "$OPENSSL_CONFIG" -subj="/CN=waldo" -x509 -sha256 -nodes -days 365 -newkey rsa
:4096 -keyout /tmp
/test-50-privkey.key
-out /tmp
/test-50-cert.crt
674 mkdir
-p /tmp
/test-50-confext
/etc
/extension-release.d
/
676 echo "foobar50" > /tmp
/test-50-confext
/etc
/waldo
678 ( grep -e '^\(ID\|VERSION_ID\)=' /etc
/os-release
; echo IMAGE_ID
=waldo
; echo IMAGE_VERSION
=7 ) > /tmp
/test-50-confext
/etc
/extension-release.d
/extension-release.waldo
680 mkdir
-p /run
/confexts
682 SYSTEMD_REPART_OVERRIDE_FSTYPE
=squashfs systemd-repart
-C -s /tmp
/test-50-confext
--certificate=/tmp
/test-50-cert.crt
--private-key=/tmp
/test-50-privkey.key
/run
/confexts
/waldo.confext.raw
683 rm -rf /tmp
/test-50-confext
685 mkdir
-p /run
/verity.d
686 cp /tmp
/test-50-cert.crt
/run
/verity.d
/
687 systemd-dissect
--mtree /run
/confexts
/waldo.confext.raw
689 systemd-confext refresh
691 read -r X
< /etc
/waldo
694 rm /run
/confexts
/waldo.confext.raw
696 systemd-confext refresh
698 (! test -f /etc
/waldo
)
700 mkdir
-p /tmp
/test-50-sysext
/usr
/lib
/extension-release.d
/
702 # Make sure the sysext is big enough to not fit in the minimum partition size of repart so we know the
703 # Minimize= logic is working.
704 truncate
--size=50M
/tmp
/test-50-sysext
/usr
/waldo
706 ( grep -e '^\(ID\|VERSION_ID\)=' /etc
/os-release
; echo IMAGE_ID
=waldo
; echo IMAGE_VERSION
=7 ) > /tmp
/test-50-sysext
/usr
/lib
/extension-release.d
/extension-release.waldo
708 mkdir
-p /run
/extensions
710 SYSTEMD_REPART_OVERRIDE_FSTYPE
=squashfs systemd-repart
-S -s /tmp
/test-50-sysext
--certificate=/tmp
/test-50-cert.crt
--private-key=/tmp
/test-50-privkey.key
/run
/extensions
/waldo.sysext.raw
712 systemd-dissect
--mtree /run
/extensions
/waldo.sysext.raw
714 systemd-sysext refresh
718 rm /run
/verity.d
/test-50-cert.crt
/run
/extensions
/waldo.sysext.raw
/tmp
/test-50-cert.crt
/tmp
/test-50-privkey.key
720 systemd-sysext refresh
722 (! test -f /usr
/waldo
)
725 # Sneak in a couple of expected-to-fail invocations to cover
726 # https://github.com/systemd/systemd/issues/29610
727 (! systemd-run
-P -p MountImages
="/this/should/definitely/not/exist.img:/run/img2\:3:nosuid" false
)
728 (! systemd-run
-P -p ExtensionImages
="/this/should/definitely/not/exist.img" false
)
729 (! systemd-run
-P -p RootImage
="/this/should/definitely/not/exist.img" false
)
730 (! systemd-run
-P -p ExtensionDirectories
="/foo/bar /foo/baz" false
)