]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-50.sh
Merge pull request #18990 from yuwata/network-dhcpv6-use-domains
[thirdparty/systemd.git] / test / units / testsuite-50.sh
CommitLineData
e7cbe5cb
LB
1#!/usr/bin/env bash
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
4set -ex
5set -o pipefail
6
08de6f94
LB
7export SYSTEMD_LOG_LEVEL=debug
8
69398734
LB
9cleanup()
10{
11 if [ -z "${image_dir}" ]; then
12 return
13 fi
14 rm -rf "${image_dir}"
15}
16
e7cbe5cb
LB
17cd /tmp
18
69398734
LB
19image_dir="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
20if [ -z "${image_dir}" ] || [ ! -d "${image_dir}" ]; then
21 echo "mktemp under /tmp failed"
22 exit 1
e7cbe5cb
LB
23fi
24
69398734
LB
25trap cleanup EXIT
26
9785c44d
LB
27cp /usr/share/minimal* "${image_dir}/"
28image="${image_dir}/minimal_0"
69398734 29roothash="$(cat ${image}.roothash)"
e7cbe5cb 30
ae6c5987
ZJS
31os_release=$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)
32
1474d7ac 33systemd-dissect --json=short ${image}.raw | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
35afe47a 34systemd-dissect ${image}.raw | grep -q -F "MARKER=1"
ae6c5987 35systemd-dissect ${image}.raw | grep -q -F -f $os_release
e7cbe5cb
LB
36
37mv ${image}.verity ${image}.fooverity
38mv ${image}.roothash ${image}.foohash
1474d7ac 39systemd-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"'
35afe47a 40systemd-dissect ${image}.raw --root-hash=${roothash} --verity-data=${image}.fooverity | grep -q -F "MARKER=1"
ae6c5987 41systemd-dissect ${image}.raw --root-hash=${roothash} --verity-data=${image}.fooverity | grep -q -F -f $os_release
69398734
LB
42mv ${image}.fooverity ${image}.verity
43mv ${image}.foohash ${image}.roothash
44
ac1f3ad0 45mkdir -p ${image_dir}/mount ${image_dir}/mount2
35afe47a 46systemd-dissect --mount ${image}.raw ${image_dir}/mount
ae6c5987
ZJS
47cat ${image_dir}/mount/usr/lib/os-release | grep -q -F -f $os_release
48cat ${image_dir}/mount/etc/os-release | grep -q -F -f $os_release
0f5d24a8 49cat ${image_dir}/mount/usr/lib/os-release | grep -q -F "MARKER=1"
ac1f3ad0 50# Verity volume should be shared (opened only once)
35afe47a 51systemd-dissect --mount ${image}.raw ${image_dir}/mount2
ac1f3ad0
LB
52verity_count=$(ls -1 /dev/mapper/ | grep -c verity)
53# In theory we should check that count is exactly one. In practice, libdevmapper
54# randomly and unpredictably fails with an unhelpful EINVAL when a device is open
55# (and even mounted and in use), so best-effort is the most we can do for now
56if [ ${verity_count} -lt 1 ]; then
57 echo "Verity device ${image}.raw not found in /dev/mapper/"
58 exit 1
59fi
69398734 60umount ${image_dir}/mount
ac1f3ad0 61umount ${image_dir}/mount2
e7cbe5cb 62
d583cf45 63systemd-run -t -p RootImage=${image}.raw cat /usr/lib/os-release | grep -q -F "MARKER=1"
2bc148ad
LB
64mv ${image}.verity ${image}.fooverity
65mv ${image}.roothash ${image}.foohash
d583cf45
ZJS
66systemd-run -t -p RootImage=${image}.raw -p RootHash=${image}.foohash -p RootVerity=${image}.fooverity cat /usr/lib/os-release | grep -q -F "MARKER=1"
67# Let's use the long option name just here as a test
68systemd-run -t --property RootImage=${image}.raw --property RootHash=${roothash} --property RootVerity=${image}.fooverity cat /usr/lib/os-release | grep -q -F "MARKER=1"
2bc148ad
LB
69mv ${image}.fooverity ${image}.verity
70mv ${image}.foohash ${image}.roothash
71
a5f1d665
LB
72# Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
73machine="$(uname -m)"
74if [ "${machine}" = "x86_64" ]; then
75 root_guid=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
76 verity_guid=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
91987527 77 architecture="x86-64"
a5f1d665
LB
78elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
79 root_guid=44479540-f297-41b2-9af7-d131d5f0458a
80 verity_guid=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
91987527 81 architecture="x86"
a5f1d665
LB
82elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
83 root_guid=b921b045-1df0-41c3-af44-4c6f280d3fae
84 verity_guid=df3300ce-d69f-4c92-978c-9bfb0f38d820
91987527 85 architecture="arm64"
a5f1d665
LB
86elif [ "${machine}" = "arm" ]; then
87 root_guid=69dad710-2ce4-4e3c-b16c-21a1d49abed3
88 verity_guid=7386cdf2-203c-47a9-a498-f2ecce45a2d6
91987527 89 architecture="arm"
a5f1d665
LB
90elif [ "${machine}" = "ia64" ]; then
91 root_guid=993d8d3d-f80e-4225-855a-9daf8ed7ea97
92 verity_guid=86ed10d5-b607-45bb-8957-d350f23d0571
91987527
LB
93 architecture="ia64"
94elif [ "${machine}" = "ppc64le" ]; then
95 # There's no support of PPC in the discoverable partitions specification yet, so skip the rest for now
96 echo OK >/testok
97 exit 0
08de6f94
LB
98else
99 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
100 exit 1
a5f1d665
LB
101fi
102# du rounds up to block size, which is more helpful for partitioning
103root_size="$(du -k ${image}.raw | cut -f1)"
104verity_size="$(du -k ${image}.verity | cut -f1)"
105# 4MB seems to be the minimum size blkid will accept, below that probing fails
106dd if=/dev/zero of=${image}.gpt bs=512 count=$((8192+${root_size}*2+${verity_size}*2))
107# sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
108# so do some basic rounding up if the minimal image is more than 1 MB
109if [ ${root_size} -ge 1024 ]; then
110 root_size="$((${root_size}/1024 + 1))MiB"
111else
112 root_size="${root_size}KiB"
113fi
7580a647 114verity_size="$((${verity_size} * 2))KiB"
a5f1d665
LB
115uuid="$(head -c 32 ${image}.roothash | cut -c -8)-$(head -c 32 ${image}.roothash | cut -c 9-12)-$(head -c 32 ${image}.roothash | cut -c 13-16)-$(head -c 32 ${image}.roothash | cut -c 17-20)-$(head -c 32 ${image}.roothash | cut -c 21-)"
116echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk ${image}.gpt
117uuid="$(tail -c 32 ${image}.roothash | cut -c -8)-$(tail -c 32 ${image}.roothash | cut -c 9-12)-$(tail -c 32 ${image}.roothash | cut -c 13-16)-$(tail -c 32 ${image}.roothash | cut -c 17-20)-$(tail -c 32 ${image}.roothash | cut -c 21-)"
118echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk ${image}.gpt --append
119sfdisk --part-label ${image}.gpt 1 "Root Partition"
120sfdisk --part-label ${image}.gpt 2 "Verity Partition"
121loop="$(losetup --show -P -f ${image}.gpt)"
122dd if=${image}.raw of=${loop}p1
123dd if=${image}.verity of=${loop}p2
124losetup -d ${loop}
125
35afe47a
LP
126# Derive partition UUIDs from root hash, in UUID syntax
127ROOT_UUID=$(systemd-id128 -u show $(head -c 32 ${image}.roothash) -u | tail -n 1 | cut -b 6-)
128VERITY_UUID=$(systemd-id128 -u show $(tail -c 32 ${image}.roothash) -u | tail -n 1 | cut -b 6-)
a5f1d665 129
1474d7ac
LP
130systemd-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":"yes","node":'
131systemd-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,"node":'
35afe47a 132systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q -F "MARKER=1"
ae6c5987 133systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q -F -f $os_release
35afe47a
LP
134
135systemd-dissect --root-hash ${roothash} --mount ${image}.gpt ${image_dir}/mount
ae6c5987
ZJS
136cat ${image_dir}/mount/usr/lib/os-release | grep -q -F -f $os_release
137cat ${image_dir}/mount/etc/os-release | grep -q -F -f $os_release
0f5d24a8 138cat ${image_dir}/mount/usr/lib/os-release | grep -q -F "MARKER=1"
a5f1d665
LB
139umount ${image_dir}/mount
140
d583cf45
ZJS
141# add explicit -p MountAPIVFS=yes once to test the parser
142systemd-run -t -p RootImage=${image}.gpt -p RootHash=${roothash} -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
2bc148ad 143
d583cf45
ZJS
144systemd-run -t -p RootImage=${image}.raw -p RootImageOptions="root:nosuid,dev home:ro,dev ro,noatime" mount | grep -F "squashfs" | grep -q -F "nosuid"
145systemd-run -t -p RootImage=${image}.gpt -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
18d73705 146
9ece6444 147mkdir -p mkdir -p ${image_dir}/result
d583cf45 148cat >/run/systemd/system/testservice-50a.service <<EOF
18d73705
LB
149[Service]
150Type=oneshot
d583cf45 151ExecStart=bash -c "mount >/run/result/a"
9ece6444
LB
152BindPaths=${image_dir}/result:/run/result
153TemporaryFileSystem=/run
18d73705 154RootImage=${image}.raw
9ece6444 155RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
18d73705
LB
156RootImageOptions=nosuid,dev
157EOF
158systemctl start testservice-50a.service
9ece6444
LB
159grep -F "squashfs" ${image_dir}/result/a | grep -q -F "noatime"
160grep -F "squashfs" ${image_dir}/result/a | grep -q -F -v "nosuid"
18d73705 161
d583cf45 162cat >/run/systemd/system/testservice-50b.service <<EOF
18d73705
LB
163[Service]
164Type=oneshot
d583cf45 165ExecStart=bash -c "mount >/run/result/b"
9ece6444
LB
166BindPaths=${image_dir}/result:/run/result
167TemporaryFileSystem=/run
18d73705 168RootImage=${image}.gpt
9ece6444
LB
169RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
170RootImageOptions=home:ro,dev nosuid,dev,%%foo
d583cf45
ZJS
171# this is the default, but let's specify once to test the parser
172MountAPIVFS=yes
18d73705
LB
173EOF
174systemctl start testservice-50b.service
9ece6444 175grep -F "squashfs" ${image_dir}/result/b | grep -q -F "noatime"
18d73705 176
d583cf45 177# Check that specifier escape is applied %%foo → %foo
18d73705
LB
178busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions | grep -F "nosuid,dev,%foo"
179
427353f6 180# Now do some checks with MountImages, both by itself, with options and in combination with RootImage, and as single FS or GPT image
d583cf45
ZJS
181systemd-run -t -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
182systemd-run -t -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
183systemd-run -t -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount | grep -F "squashfs" | grep -q -F "nosuid"
184systemd-run -t -p MountImages="${image}.gpt:/run/img1:root:nosuid ${image}.raw:/run/img2:home:suid" mount | grep -F "squashfs" | grep -q -F "nosuid"
185systemd-run -t -p MountImages="${image}.raw:/run/img2\:3" cat /run/img2:3/usr/lib/os-release | grep -q -F "MARKER=1"
186systemd-run -t -p MountImages="${image}.raw:/run/img2\:3:nosuid" mount | grep -F "squashfs" | grep -q -F "nosuid"
187systemd-run -t -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"
188systemd-run -t -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"
189systemd-run -t -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"
427353f6 190cat >/run/systemd/system/testservice-50c.service <<EOF
b3d13314 191[Service]
427353f6 192MountAPIVFS=yes
b3d13314
LB
193TemporaryFileSystem=/run
194RootImage=${image}.raw
427353f6
LB
195MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
196MountImages=${image}.raw:/run/img2\:3:nosuid
d583cf45
ZJS
197ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
198ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
199ExecStart=bash -c "mount >>/run/result/c"
427353f6 200BindPaths=${image_dir}/result:/run/result
b3d13314
LB
201Type=oneshot
202EOF
427353f6
LB
203systemctl start testservice-50c.service
204grep -q -F "MARKER=1" ${image_dir}/result/c
205grep -F "squashfs" ${image_dir}/result/c | grep -q -F "noatime"
206grep -F "squashfs" ${image_dir}/result/c | grep -q -F -v "nosuid"
b3d13314 207
6faecbd3
LB
208# Adding a new mounts at runtime works if the unit is in the active state,
209# so use Type=notify to make sure there's no race condition in the test
210cat > /run/systemd/system/testservice-50d.service <<EOF
211[Service]
212RuntimeMaxSec=300
213Type=notify
214RemainAfterExit=yes
215MountAPIVFS=yes
216PrivateTmp=yes
217ExecStart=/bin/sh -c 'systemd-notify --ready; while ! grep -q -F MARKER /tmp/img/usr/lib/os-release; do sleep 0.1; done; mount | grep -F "/tmp/img" | grep -q -F "nosuid"'
218EOF
219systemctl start testservice-50d.service
220
221systemctl mount-image --mkdir testservice-50d.service ${image}.raw /tmp/img root:nosuid
222
223while systemctl show -P SubState testservice-50d.service | grep -q running
224do
225 sleep 0.1
226done
227
228systemctl is-active testservice-50d.service
229
93f59701
LB
230# ExtensionImages will set up an overlay
231systemd-run -t --property ExtensionImages=/usr/share/app0.raw --property RootImage=${image}.raw cat /opt/script0.sh | grep -q -F "extension-release.app0"
232systemd-run -t --property ExtensionImages=/usr/share/app0.raw --property RootImage=${image}.raw cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
233systemd-run -t --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage=${image}.raw cat /opt/script0.sh | grep -q -F "extension-release.app0"
234systemd-run -t --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"
235systemd-run -t --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage=${image}.raw cat /opt/script1.sh | grep -q -F "extension-release.app1"
236systemd-run -t --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"
237cat >/run/systemd/system/testservice-50e.service <<EOF
238[Service]
239MountAPIVFS=yes
240TemporaryFileSystem=/run
241RootImage=${image}.raw
242ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
c335b7c3
FS
243# Relevant only for sanitizer runs
244UnsetEnvironment=LD_PRELOAD
93f59701
LB
245ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
246ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
247Type=oneshot
248RemainAfterExit=yes
249EOF
250systemctl start testservice-50e.service
251systemctl is-active testservice-50e.service
252
d583cf45 253echo OK >/testok
e7cbe5cb
LB
254
255exit 0