]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-50.sh
service: add new RootImageOptions feature
[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
0f5d24a8
LB
27cp /usr/share/minimal.* "${image_dir}/"
28image="${image_dir}/minimal"
69398734 29roothash="$(cat ${image}.roothash)"
e7cbe5cb
LB
30
31/usr/lib/systemd/systemd-dissect ${image}.raw | grep -q -F "Found read-only 'root' partition of type squashfs with verity"
0f5d24a8 32/usr/lib/systemd/systemd-dissect ${image}.raw | grep -q -F "MARKER=1"
e7cbe5cb
LB
33/usr/lib/systemd/systemd-dissect ${image}.raw | grep -q -F -f /usr/lib/os-release
34
35mv ${image}.verity ${image}.fooverity
36mv ${image}.roothash ${image}.foohash
69398734 37/usr/lib/systemd/systemd-dissect ${image}.raw --root-hash=${roothash} --verity-data=${image}.fooverity | grep -q -F "Found read-only 'root' partition of type squashfs with verity"
0f5d24a8 38/usr/lib/systemd/systemd-dissect ${image}.raw --root-hash=${roothash} --verity-data=${image}.fooverity | grep -q -F "MARKER=1"
69398734
LB
39/usr/lib/systemd/systemd-dissect ${image}.raw --root-hash=${roothash} --verity-data=${image}.fooverity | grep -q -F -f /usr/lib/os-release
40mv ${image}.fooverity ${image}.verity
41mv ${image}.foohash ${image}.roothash
42
ac1f3ad0 43mkdir -p ${image_dir}/mount ${image_dir}/mount2
69398734 44/usr/lib/systemd/systemd-dissect --mount ${image}.raw ${image_dir}/mount
0f5d24a8
LB
45cat ${image_dir}/mount/usr/lib/os-release | grep -q -F -f /usr/lib/os-release
46cat ${image_dir}/mount/etc/os-release | grep -q -F -f /usr/lib/os-release
47cat ${image_dir}/mount/usr/lib/os-release | grep -q -F "MARKER=1"
ac1f3ad0
LB
48# Verity volume should be shared (opened only once)
49/usr/lib/systemd/systemd-dissect --mount ${image}.raw ${image_dir}/mount2
50verity_count=$(ls -1 /dev/mapper/ | grep -c verity)
51# In theory we should check that count is exactly one. In practice, libdevmapper
52# randomly and unpredictably fails with an unhelpful EINVAL when a device is open
53# (and even mounted and in use), so best-effort is the most we can do for now
54if [ ${verity_count} -lt 1 ]; then
55 echo "Verity device ${image}.raw not found in /dev/mapper/"
56 exit 1
57fi
69398734 58umount ${image_dir}/mount
ac1f3ad0 59umount ${image_dir}/mount2
e7cbe5cb 60
2bc148ad
LB
61systemd-run -t --property RootImage=${image}.raw /usr/bin/cat /usr/lib/os-release | grep -q -F "MARKER=1"
62mv ${image}.verity ${image}.fooverity
63mv ${image}.roothash ${image}.foohash
64systemd-run -t --property RootImage=${image}.raw --property RootHash=${image}.foohash --property RootVerity=${image}.fooverity /usr/bin/cat /usr/lib/os-release | grep -q -F "MARKER=1"
65systemd-run -t --property RootImage=${image}.raw --property RootHash=${roothash} --property RootVerity=${image}.fooverity /usr/bin/cat /usr/lib/os-release | grep -q -F "MARKER=1"
66mv ${image}.fooverity ${image}.verity
67mv ${image}.foohash ${image}.roothash
68
a5f1d665
LB
69# Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
70machine="$(uname -m)"
71if [ "${machine}" = "x86_64" ]; then
72 root_guid=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
73 verity_guid=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
74elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
75 root_guid=44479540-f297-41b2-9af7-d131d5f0458a
76 verity_guid=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
77elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
78 root_guid=b921b045-1df0-41c3-af44-4c6f280d3fae
79 verity_guid=df3300ce-d69f-4c92-978c-9bfb0f38d820
80elif [ "${machine}" = "arm" ]; then
81 root_guid=69dad710-2ce4-4e3c-b16c-21a1d49abed3
82 verity_guid=7386cdf2-203c-47a9-a498-f2ecce45a2d6
83elif [ "${machine}" = "ia64" ]; then
84 root_guid=993d8d3d-f80e-4225-855a-9daf8ed7ea97
85 verity_guid=86ed10d5-b607-45bb-8957-d350f23d0571
08de6f94
LB
86else
87 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
88 exit 1
a5f1d665
LB
89fi
90# du rounds up to block size, which is more helpful for partitioning
91root_size="$(du -k ${image}.raw | cut -f1)"
92verity_size="$(du -k ${image}.verity | cut -f1)"
93# 4MB seems to be the minimum size blkid will accept, below that probing fails
94dd if=/dev/zero of=${image}.gpt bs=512 count=$((8192+${root_size}*2+${verity_size}*2))
95# sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
96# so do some basic rounding up if the minimal image is more than 1 MB
97if [ ${root_size} -ge 1024 ]; then
98 root_size="$((${root_size}/1024 + 1))MiB"
99else
100 root_size="${root_size}KiB"
101fi
102verity_size="${verity_size}KiB"
103uuid="$(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-)"
104echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk ${image}.gpt
105uuid="$(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-)"
106echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk ${image}.gpt --append
107sfdisk --part-label ${image}.gpt 1 "Root Partition"
108sfdisk --part-label ${image}.gpt 2 "Verity Partition"
109loop="$(losetup --show -P -f ${image}.gpt)"
110dd if=${image}.raw of=${loop}p1
111dd if=${image}.verity of=${loop}p2
112losetup -d ${loop}
113
114/usr/lib/systemd/systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q "Found read-only 'root' partition (UUID $(head -c 32 ${image}.roothash)) of type squashfs for .* with verity on partition #1"
115/usr/lib/systemd/systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q "Found read-only 'root-verity' partition (UUID $(tail -c 32 ${image}.roothash)) of type DM_verity_hash for .* on partition #2"
0f5d24a8 116/usr/lib/systemd/systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q -F "MARKER=1"
a5f1d665
LB
117/usr/lib/systemd/systemd-dissect --root-hash ${roothash} ${image}.gpt | grep -q -F -f /usr/lib/os-release
118
119/usr/lib/systemd/systemd-dissect --root-hash ${roothash} --mount ${image}.gpt ${image_dir}/mount
0f5d24a8
LB
120cat ${image_dir}/mount/usr/lib/os-release | grep -q -F -f /usr/lib/os-release
121cat ${image_dir}/mount/etc/os-release | grep -q -F -f /usr/lib/os-release
122cat ${image_dir}/mount/usr/lib/os-release | grep -q -F "MARKER=1"
a5f1d665
LB
123umount ${image_dir}/mount
124
2bc148ad
LB
125systemd-run -t --property RootImage=${image}.gpt --property RootHash=${roothash} /usr/bin/cat /usr/lib/os-release | grep -q -F "MARKER=1"
126
18d73705
LB
127systemd-run -t --property RootImage=${image}.raw --property RootImageOptions="1:ro,noatime 2:ro,dev nosuid,dev" --property MountAPIVFS=yes mount | grep -F "squashfs" | grep -q -F "nosuid"
128systemd-run -t --property RootImage=${image}.gpt --property RootImageOptions="1:ro,noatime 1:ro,dev" --property MountAPIVFS=yes mount | grep -F "squashfs" | grep -q -F "noatime"
129
130cat > /run/systemd/system/testservice-50a.service <<EOF
131[Service]
132Type=oneshot
133ExecStart=mount
134MountAPIVFS=yes
135RootImage=${image}.raw
136RootImageOptions=1:ro,noatime,nosuid 2:ro,dev noatime,dev
137RootImageOptions=nosuid,dev
138EOF
139systemctl start testservice-50a.service
140journalctl -b -u testservice-50a.service | grep -F "squashfs" | grep -q -F "noatime"
141journalctl -b -u testservice-50a.service | grep -F "squashfs" | grep -q -F -v "nosuid"
142
143cat > /run/systemd/system/testservice-50b.service <<EOF
144[Service]
145Type=oneshot
146ExecStart=mount
147MountAPIVFS=yes
148RootImage=${image}.gpt
149RootImageOptions=1:ro,noatime,nosuid 2:ro,dev nosuid,dev
150RootImageOptions=2:ro,dev nosuid,dev,%%foo
151EOF
152systemctl start testservice-50b.service
153journalctl -b -u testservice-50b.service | grep -F "squashfs" | grep -q -F "noatime"
154
155# Check that specifier escape is applied %%foo -> %foo
156busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions | grep -F "nosuid,dev,%foo"
157
e7cbe5cb
LB
158echo OK > /testok
159
160exit 0