]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-58.sh
TEST-58: add echo calls to print what is happening
[thirdparty/systemd.git] / test / units / testsuite-58.sh
CommitLineData
5a3b8640 1#!/usr/bin/env bash
7b3cec95 2# SPDX-License-Identifier: LGPL-2.1-or-later
7234a213 3# shellcheck disable=SC2317
5a3b8640 4set -eux
30f56248 5set -o pipefail
5a3b8640 6
7c6fa5bf
FS
7# shellcheck source=test/units/util.sh
8. "$(dirname "$0")"/util.sh
e2d05745 9
f89a20f1
ZJS
10if ! command -v systemd-repart &>/dev/null; then
11 echo "no systemd-repart" >/skipped
12 exit 0
13fi
14
7234a213
FS
15# shellcheck source=test/units/test-control.sh
16. "$(dirname "$0")"/test-control.sh
cb153b4f
FS
17# shellcheck source=test/units/util.sh
18. "$(dirname "$0")"/util.sh
5996f116 19
5a3b8640
LP
20export SYSTEMD_LOG_LEVEL=debug
21export PAGER=cat
22
71ea8436
LP
23# Disable use of special glyphs such as →
24export SYSTEMD_UTF8=0
25
5996f116
YW
26seed=750b6cd5c4ae4012a15e7be3c29e6a47
27
76097112 28if ! systemd-detect-virt --quiet --container; then
e867b016 29 udevadm control --log-level debug
76097112 30fi
2b21522e 31
8e65d93e
LB
32machine="$(uname -m)"
33if [ "${machine}" = "x86_64" ]; then
5996f116 34 root_guid=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
8e65d93e 35 root_uuid=60F33797-1D71-4DCB-AA6F-20564F036CD0
2b21522e 36 root_uuid2=73A4CCD2-EAF5-44DA-A366-F99188210FDC
5996f116 37 usr_guid=8484680C-9521-48C6-9C11-B0720656F69E
8e65d93e
LB
38 usr_uuid=7E3369DD-D653-4513-ADF5-B993A9F20C16
39 architecture="x86-64"
40elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
5996f116
YW
41 root_guid=44479540-F297-41B2-9AF7-D131D5F0458A
42 root_uuid=02B4253F-29A4-404E-8972-1669D3B03C87
2b21522e 43 root_uuid2=268E0FD3-B468-4806-A823-E533FE9BB9CC
5996f116
YW
44 usr_guid=75250D76-8CC6-458E-BD66-BD47CC81A812
45 usr_uuid=7B42FFB0-B0E1-4395-B20B-C78F4A571648
8e65d93e
LB
46 architecture="x86"
47elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
5996f116
YW
48 root_guid=B921B045-1DF0-41C3-AF44-4C6F280D3FAE
49 root_uuid=055D0227-53A6-4033-85C3-9A5973EFF483
2b21522e 50 root_uuid2=F7DBBE48-8FD0-4833-8411-AA34E7C8E60A
5996f116
YW
51 usr_guid=B0E01050-EE5F-4390-949A-9101B17104E9
52 usr_uuid=FCE3C75E-D6A4-44C0-87F0-4C105183FB1F
8e65d93e
LB
53 architecture="arm64"
54elif [ "${machine}" = "arm" ]; then
5996f116
YW
55 root_guid=69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
56 root_uuid=567DA89E-8DE2-4499-8D10-18F212DFF034
2b21522e 57 root_uuid2=813ECFE5-4C89-4193-8A52-437493F2F96E
5996f116
YW
58 usr_guid=7D0359A3-02B3-4F0A-865C-654403E70625
59 usr_uuid=71E93DC2-5073-42CB-8A84-A354E64D8966
8e65d93e
LB
60 architecture="arm"
61elif [ "${machine}" = "loongarch64" ]; then
5996f116
YW
62 root_guid=77055800-792C-4F94-B39A-98C91B762BB6
63 root_uuid=D8EFC2D2-0133-41E4-BDCB-3B9F4CFDDDE8
2b21522e 64 root_uuid2=36499F9E-0688-40C1-A746-EA8FD9543C56
5996f116
YW
65 usr_guid=E611C702-575C-4CBE-9A46-434FA0BF7E3F
66 usr_uuid=031FFA75-00BB-49B6-A70D-911D2D82A5B7
8e65d93e
LB
67 architecture="loongarch64"
68elif [ "${machine}" = "ia64" ]; then
5996f116
YW
69 root_guid=993D8D3D-F80E-4225-855A-9DAF8ED7EA97
70 root_uuid=DCF33449-0896-4EA9-BC24-7D58AEEF522D
2b21522e 71 root_uuid2=C2A6CAB7-ABEA-4FBA-8C48-CB4C52E6CA38
5996f116
YW
72 usr_guid=4301D2A6-4E3B-4B2A-BB94-9E0B2C4225EA
73 usr_uuid=BC2BCCE7-80D6-449A-85CC-637424CE5241
8e65d93e
LB
74 architecture="ia64"
75elif [ "${machine}" = "s390x" ]; then
5996f116
YW
76 root_guid=5EEAD9A9-FE09-4A1E-A1D7-520D00531306
77 root_uuid=7EBE0C85-E27E-48EC-B164-F4807606232E
2b21522e 78 root_uuid2=2A074E1C-2A19-4094-A0C2-24B1A5D52FCB
5996f116
YW
79 usr_guid=8A4F5770-50AA-4ED3-874A-99B710DB6FEA
80 usr_uuid=51171D30-35CF-4A49-B8B5-9478B9B796A5
8e65d93e
LB
81 architecture="s390x"
82elif [ "${machine}" = "ppc64le" ]; then
5996f116
YW
83 root_guid=C31C45E6-3F39-412E-80FB-4809C4980599
84 root_uuid=061E67A1-092F-482F-8150-B525D50D6654
2b21522e 85 root_uuid2=A6687CEF-4E4F-44E7-90B3-CDA52EA81739
5996f116
YW
86 usr_guid=15BB03AF-77E7-4D4A-B12B-C0D084F7491C
87 usr_uuid=C0D0823B-8040-4C7C-A629-026248E297FB
8e65d93e
LB
88 architecture="ppc64-le"
89else
90 echo "Unexpected uname -m: ${machine} in testsuite-58.sh, please fix me"
91 exit 1
92fi
93
7234a213 94testcase_basic() {
2b21522e
YW
95 local defs imgs output
96 local loop volume
97
dfb3ebfd
ZJS
98 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
99 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
2b21522e
YW
100 # shellcheck disable=SC2064
101 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 102 chmod a+rx "$defs"
2b21522e 103
1b6f8915 104 echo "*** 1. create an empty image ***"
2b21522e 105
e2d05745
DDM
106 runas testuser systemd-repart --empty=create \
107 --size=1G \
108 --seed="$seed" \
109 "$imgs/zzz"
2b21522e
YW
110
111 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
112
113 assert_eq "$output" "label: gpt
114label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
115device: $imgs/zzz
116unit: sectors
117first-lba: 2048
118last-lba: 2097118"
119
1b6f8915 120 echo "*** 2. Testing with root, root2, home, and swap ***"
2b21522e 121
dfb3ebfd 122 tee "$defs/root.conf" <<EOF
2b21522e
YW
123[Partition]
124Type=root
125EOF
126
127 ln -s root.conf "$defs/root2.conf"
128
dfb3ebfd 129 tee "$defs/home.conf" <<EOF
2b21522e
YW
130[Partition]
131Type=home
132Label=home-first
133Label=home-always-too-long-xxxxxxxxxxxxxx-%v
134EOF
135
dfb3ebfd 136 tee "$defs/swap.conf" <<EOF
2b21522e
YW
137[Partition]
138Type=swap
139SizeMaxBytes=64M
140PaddingMinBytes=92M
141EOF
142
e2d05745
DDM
143 runas testuser systemd-repart --definitions="$defs" \
144 --dry-run=no \
145 --seed="$seed" \
146 --include-partitions=home,swap \
147 "$imgs/zzz"
56bb434e
DDM
148
149 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
150
151 assert_eq "$output" "label: gpt
152label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
153device: $imgs/zzz
154unit: sectors
155first-lba: 2048
156last-lba: 2097118
7d505753
DDM
157$imgs/zzz1 : start= 2048, size= 1775576, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
158$imgs/zzz2 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
56bb434e 159
e2d05745
DDM
160 runas testuser systemd-repart --definitions="$defs" \
161 --dry-run=no \
162 --seed="$seed" \
7d505753 163 --empty=force \
8275334b 164 --defer-partitions=home,root \
e2d05745 165 "$imgs/zzz"
56bb434e
DDM
166
167 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
168
169 assert_eq "$output" "label: gpt
170label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
171device: $imgs/zzz
172unit: sectors
173first-lba: 2048
174last-lba: 2097118
56bb434e
DDM
175$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
176
e2d05745
DDM
177 runas testuser systemd-repart --definitions="$defs" \
178 --dry-run=no \
179 --seed="$seed" \
180 "$imgs/zzz"
2b21522e
YW
181
182 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
183
184 assert_eq "$output" "label: gpt
185label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
186device: $imgs/zzz
187unit: sectors
188first-lba: 2048
189last-lba: 2097118
190$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
191$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
192$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
193$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
194
1b6f8915 195 echo "*** 3. Testing with root, root2, home, swap, and another partition ***"
2b21522e 196
dfb3ebfd 197 tee "$defs/swap.conf" <<EOF
2b21522e
YW
198[Partition]
199Type=swap
200SizeMaxBytes=64M
201EOF
202
dfb3ebfd 203 tee "$defs/extra.conf" <<EOF
2b21522e
YW
204[Partition]
205Type=linux-generic
206Label=custom_label
207UUID=a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
208EOF
209
210 echo "Label=ignored_label" >>"$defs/home.conf"
211 echo "UUID=b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" >>"$defs/home.conf"
212
e2d05745
DDM
213 runas testuser systemd-repart --definitions="$defs" \
214 --dry-run=no \
215 --seed="$seed" \
216 "$imgs/zzz"
2b21522e
YW
217
218 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
219
220 assert_eq "$output" "label: gpt
221label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
222device: $imgs/zzz
223unit: sectors
224first-lba: 2048
225last-lba: 2097118
226$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
227$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
228$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
229$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
230$imgs/zzz5 : start= 1908696, size= 188416, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\""
231
1b6f8915 232 echo "*** 4. Resizing to 2G ***"
2b21522e 233
e2d05745
DDM
234 runas testuser systemd-repart --definitions="$defs" \
235 --size=2G \
236 --dry-run=no \
237 --seed="$seed" \
238 "$imgs/zzz"
2b21522e
YW
239
240 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
241
242 assert_eq "$output" "label: gpt
243label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
244device: $imgs/zzz
245unit: sectors
246first-lba: 2048
247last-lba: 4194270
248$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
249$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
250$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
251$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
252$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\""
253
1b6f8915 254 echo "*** 5. Testing with root, root2, home, swap, another partition, and partition copy ***"
2b21522e
YW
255
256 dd if=/dev/urandom of="$imgs/block-copy" bs=4096 count=10240
257
dfb3ebfd 258 tee "$defs/extra2.conf" <<EOF
2b21522e
YW
259[Partition]
260Type=linux-generic
261Label=block-copy
262UUID=2a1d97e1d0a346cca26eadc643926617
263CopyBlocks=$imgs/block-copy
264EOF
265
e2d05745
DDM
266 runas testuser systemd-repart --definitions="$defs" \
267 --size=3G \
268 --dry-run=no \
269 --seed="$seed" \
270 "$imgs/zzz"
2b21522e
YW
271
272 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
273
274 assert_eq "$output" "label: gpt
275label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
276device: $imgs/zzz
277unit: sectors
278first-lba: 2048
279last-lba: 6291422
280$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
281$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
282$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
283$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
284$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\"
285$imgs/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name=\"block-copy\""
286
287 cmp --bytes=$((4096*10240)) --ignore-initial=0:$((512*4194264)) "$imgs/block-copy" "$imgs/zzz"
288
1b6f8915 289 echo "*** 6. Testing Format=/Encrypt=/CopyFiles= ***"
2b21522e 290
dfb3ebfd 291 tee "$defs/extra3.conf" <<EOF
2b21522e
YW
292[Partition]
293Type=linux-generic
294Label=luks-format-copy
295UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
296Format=ext4
297Encrypt=yes
298CopyFiles=$defs:/def
299SizeMinBytes=48M
300EOF
301
dfb3ebfd
ZJS
302 # CopyFiles will fail if we try to chown the target file to root.
303 # Make the files owned by the user so that the invocation below works.
304 chown testuser -R "$defs"
305
e2d05745
DDM
306 runas testuser systemd-repart --definitions="$defs" \
307 --size=auto \
308 --dry-run=no \
309 --seed="$seed" \
310 "$imgs/zzz"
2b21522e
YW
311
312 output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
313
314 assert_eq "$output" "label: gpt
315label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
316device: $imgs/zzz
317unit: sectors
318first-lba: 2048
319last-lba: 6389726
320$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
321$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
322$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
323$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
324$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\"
325$imgs/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name=\"block-copy\"
326$imgs/zzz7 : start= 6291416, size= 98304, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name=\"luks-format-copy\""
327
e2d05745
DDM
328 if systemd-detect-virt --quiet --container; then
329 echo "Skipping encrypt mount tests in container."
330 return
331 fi
332
2b21522e
YW
333 loop="$(losetup -P --show --find "$imgs/zzz")"
334 udevadm wait --timeout 60 --settle "${loop:?}"
335
336 volume="test-repart-$RANDOM"
337
338 touch "$imgs/empty-password"
339 cryptsetup open --type=luks2 --key-file="$imgs/empty-password" "${loop}p7" "$volume"
340 mkdir -p "$imgs/mount"
341 mount -t ext4 "/dev/mapper/$volume" "$imgs/mount"
342 # Use deferred closing on the mapper and autoclear on the loop, so they are cleaned up on umount
343 cryptsetup close --deferred "$volume"
344 losetup -d "$loop"
345 diff -r "$imgs/mount/def" "$defs" >/dev/null
346 umount "$imgs/mount"
347}
348
7234a213 349testcase_dropin() {
2b21522e
YW
350 local defs imgs output
351
dfb3ebfd
ZJS
352 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
353 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
2b21522e
YW
354 # shellcheck disable=SC2064
355 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 356 chmod a+rx "$defs"
2b21522e 357
dfb3ebfd 358 tee "$defs/root.conf" <<EOF
2b21522e
YW
359[Partition]
360Type=swap
361SizeMaxBytes=64M
362UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
363EOF
364
dfb3ebfd
ZJS
365 mkdir -p "$defs/root.conf.d"
366 tee "$defs/root.conf.d/override1.conf" <<EOF
2b21522e
YW
367[Partition]
368Label=label1
369SizeMaxBytes=32M
370EOF
371
dfb3ebfd 372 tee "$defs/root.conf.d/override2.conf" <<EOF
2b21522e
YW
373[Partition]
374Label=label2
375EOF
376
e2d05745
DDM
377 output=$(runas testuser systemd-repart --definitions="$defs" \
378 --empty=create \
379 --size=100M \
380 --json=pretty \
381 "$imgs/zzz")
2b21522e 382
71ea8436 383 diff -u <(echo "$output") - <<EOF
2b21522e
YW
384[
385 {
386 "type" : "swap",
387 "label" : "label2",
388 "uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
d0242ac9 389 "file" : "$defs/root.conf",
2b21522e
YW
390 "node" : "$imgs/zzz1",
391 "offset" : 1048576,
392 "old_size" : 0,
393 "raw_size" : 33554432,
71ea8436 394 "size" : "-> 32.0M",
2b21522e
YW
395 "old_padding" : 0,
396 "raw_padding" : 0,
71ea8436 397 "padding" : "-> 0B",
2b21522e
YW
398 "activity" : "create",
399 "drop-in_files" : [
400 "$defs/root.conf.d/override1.conf",
401 "$defs/root.conf.d/override2.conf"
402 ]
403 }
404]
405EOF
406}
407
7234a213 408testcase_multiple_definitions() {
2b21522e
YW
409 local defs imgs output
410
dfb3ebfd
ZJS
411 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
412 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
2b21522e
YW
413 # shellcheck disable=SC2064
414 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 415 chmod a+rx "$defs"
2b21522e 416
dfb3ebfd
ZJS
417 mkdir -p "$defs/1"
418 tee "$defs/1/root1.conf" <<EOF
2b21522e
YW
419[Partition]
420Type=swap
421SizeMaxBytes=32M
422UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
423Label=label1
424EOF
425
dfb3ebfd
ZJS
426 mkdir -p "$defs/2"
427 tee "$defs/2/root2.conf" <<EOF
2b21522e
YW
428[Partition]
429Type=swap
430SizeMaxBytes=32M
431UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
432Label=label2
433EOF
434
e2d05745
DDM
435 output=$(runas testuser systemd-repart --definitions="$defs/1" \
436 --definitions="$defs/2" \
437 --empty=create \
438 --size=100M \
439 --json=pretty \
440 "$imgs/zzz")
2b21522e 441
71ea8436 442 diff -u <(echo "$output") - <<EOF
2b21522e
YW
443[
444 {
445 "type" : "swap",
446 "label" : "label1",
447 "uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
d0242ac9 448 "file" : "$defs/1/root1.conf",
2b21522e
YW
449 "node" : "$imgs/zzz1",
450 "offset" : 1048576,
451 "old_size" : 0,
452 "raw_size" : 33554432,
71ea8436 453 "size" : "-> 32.0M",
2b21522e
YW
454 "old_padding" : 0,
455 "raw_padding" : 0,
71ea8436 456 "padding" : "-> 0B",
2b21522e
YW
457 "activity" : "create"
458 },
459 {
460 "type" : "swap",
461 "label" : "label2",
462 "uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
d0242ac9 463 "file" : "$defs/2/root2.conf",
2b21522e
YW
464 "node" : "$imgs/zzz2",
465 "offset" : 34603008,
466 "old_size" : 0,
467 "raw_size" : 33554432,
71ea8436 468 "size" : "-> 32.0M",
2b21522e
YW
469 "old_padding" : 0,
470 "raw_padding" : 0,
71ea8436 471 "padding" : "-> 0B",
2b21522e
YW
472 "activity" : "create"
473 }
474]
475EOF
476}
477
7234a213 478testcase_copy_blocks() {
5996f116
YW
479 local defs imgs output
480
dfb3ebfd
ZJS
481 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
482 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
5996f116
YW
483 # shellcheck disable=SC2064
484 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 485 chmod a+rx "$defs"
5a3b8640 486
1b6f8915 487 echo "*** First, create a disk image and verify its in order ***"
5a3b8640 488
dfb3ebfd 489 tee "$defs/esp.conf" <<EOF
5a3b8640
LP
490[Partition]
491Type=esp
492SizeMinBytes=10M
493Format=vfat
494EOF
495
dfb3ebfd 496 tee "$defs/usr.conf" <<EOF
5a3b8640 497[Partition]
8e65d93e 498Type=usr-${architecture}
5a3b8640
LP
499SizeMinBytes=10M
500Format=ext4
501ReadOnly=yes
502EOF
503
dfb3ebfd 504 tee "$defs/root.conf" <<EOF
5a3b8640 505[Partition]
8e65d93e 506Type=root-${architecture}
5a3b8640
LP
507SizeMinBytes=10M
508Format=ext4
509MakeDirectories=/usr /efi
510EOF
511
e2d05745
DDM
512 runas testuser systemd-repart --definitions="$defs" \
513 --empty=create \
514 --size=auto \
515 --seed="$seed" \
516 "$imgs/zzz"
5a3b8640 517
5996f116 518 output=$(sfdisk --dump "$imgs/zzz")
5a3b8640 519
5996f116
YW
520 assert_in "$imgs/zzz1 : start= 2048, size= 20480, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=39107B09-615D-48FB-BA37-C663885FCE67, name=\"esp\"" "$output"
521 assert_in "$imgs/zzz2 : start= 22528, size= 20480, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"" "$output"
522 assert_in "$imgs/zzz3 : start= 43008, size= 20480, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:60\"" "$output"
5a3b8640 523
e2d05745
DDM
524 if systemd-detect-virt --quiet --container; then
525 echo "Skipping second part of copy blocks tests in container."
526 return
527 fi
528
1b6f8915 529 echo "*** Second, create another image with CopyBlocks=auto ***"
5a3b8640 530
dfb3ebfd 531 tee "$defs/esp.conf" <<EOF
5a3b8640
LP
532[Partition]
533Type=esp
534CopyBlocks=auto
535EOF
536
dfb3ebfd 537 tee "$defs/usr.conf" <<EOF
5a3b8640 538[Partition]
8e65d93e 539Type=usr-${architecture}
5a3b8640
LP
540ReadOnly=yes
541CopyBlocks=auto
542EOF
543
dfb3ebfd 544 tee "$defs/root.conf" <<EOF
5a3b8640 545[Partition]
8e65d93e 546Type=root-${architecture}
5a3b8640
LP
547CopyBlocks=auto
548EOF
549
e2d05745 550 # --image needs root privileges so skip runas testuser here.
5996f116
YW
551 systemd-repart --definitions="$defs" \
552 --empty=create \
553 --size=auto \
554 --seed="$seed" \
555 --image="$imgs/zzz" \
556 "$imgs/yyy"
5a3b8640 557
5996f116
YW
558 cmp "$imgs/zzz" "$imgs/yyy"
559}
5a3b8640 560
7234a213 561testcase_unaligned_partition() {
5996f116 562 local defs imgs output
5a3b8640 563
dfb3ebfd
ZJS
564 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
565 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
5996f116
YW
566 # shellcheck disable=SC2064
567 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 568 chmod a+rx "$defs"
15c59776 569
1b6f8915 570 echo "*** Operate on an image with unaligned partition ***"
15c59776 571
dfb3ebfd 572 tee "$defs/root.conf" <<EOF
15c59776 573[Partition]
8e65d93e 574Type=root-${architecture}
15c59776
LP
575EOF
576
e2d05745 577 runas testuser truncate -s 10g "$imgs/unaligned"
5996f116 578 sfdisk "$imgs/unaligned" <<EOF
15c59776
LP
579label: gpt
580
581start=2048, size=69044
582start=71092, size=3591848
583EOF
584
e2d05745
DDM
585 runas testuser systemd-repart --definitions="$defs" \
586 --seed="$seed" \
587 --dry-run=no \
588 "$imgs/unaligned"
5996f116
YW
589
590 output=$(sfdisk --dump "$imgs/unaligned")
15c59776 591
5996f116
YW
592 assert_in "$imgs/unaligned1 : start= 2048, size= 69044," "$output"
593 assert_in "$imgs/unaligned2 : start= 71092, size= 3591848," "$output"
594 assert_in "$imgs/unaligned3 : start= 3662944, size= 17308536, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"" "$output"
595}
596
7234a213 597testcase_issue_21817() {
5996f116 598 local defs imgs output
15c59776 599
dfb3ebfd
ZJS
600 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
601 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
5996f116
YW
602 # shellcheck disable=SC2064
603 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 604 chmod a+rx "$defs"
15c59776 605
1b6f8915
ZJS
606 echo "*** testcase for #21817 ***"
607
dfb3ebfd 608 tee "$defs/test.conf" <<EOF
5996f116
YW
609[Partition]
610Type=root
611EOF
612
e2d05745 613 runas testuser truncate -s 100m "$imgs/21817.img"
5996f116 614 sfdisk "$imgs/21817.img" <<EOF
70a3ce51
YW
615label: gpt
616
617size=50M, type=${root_guid}
618,
619EOF
71bb6048 620
e2d05745
DDM
621 runas testuser systemd-repart --pretty=yes \
622 --definitions "$imgs" \
623 --seed="$seed" \
624 --dry-run=no \
625 "$imgs/21817.img"
71bb6048 626
5996f116 627 output=$(sfdisk --dump "$imgs/21817.img")
71bb6048 628
5996f116
YW
629 assert_in "$imgs/21817.img1 : start= 2048, size= 102400, type=${root_guid}," "$output"
630 # Accept both unpadded (pre-v2.38 util-linux) and padded (v2.38+ util-linux) sizes
631 assert_in "$imgs/21817.img2 : start= 104448, size= (100319| 98304)," "$output"
632}
2cd341af 633
7234a213 634testcase_issue_24553() {
3b19e160
YW
635 local defs imgs output
636
dfb3ebfd
ZJS
637 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
638 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
3b19e160
YW
639 # shellcheck disable=SC2064
640 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 641 chmod a+rx "$defs"
3b19e160 642
1b6f8915
ZJS
643 echo "*** testcase for #24553 ***"
644
dfb3ebfd 645 tee "$defs/root.conf" <<EOF
3b19e160
YW
646[Partition]
647Type=root
648SizeMinBytes=10G
649SizeMaxBytes=120G
650EOF
651
dfb3ebfd 652 tee "$imgs/partscript" <<EOF
3b19e160
YW
653label: gpt
654label-id: C9FFE979-A415-C449-B729-78C7AA664B10
655unit: sectors
656first-lba: 40
657
658start=40, size=524288, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=F2E89C8A-DC5D-4C4C-A29C-6CFB643B74FD, name="ESP System Partition"
659start=524328, size=14848000, type=${root_guid}, uuid=${root_uuid}, name="root-${architecture}"
660EOF
661
1b6f8915 662 echo "*** 1. Operate on a small image compared with SizeMinBytes= ***"
e2d05745 663 runas testuser truncate -s 8g "$imgs/zzz"
3b19e160
YW
664 sfdisk "$imgs/zzz" <"$imgs/partscript"
665
666 # This should fail, but not trigger assertions.
e2d05745
DDM
667 assert_rc 1 runas testuser systemd-repart --definitions="$defs" \
668 --seed="$seed" \
669 --dry-run=no \
670 "$imgs/zzz"
3b19e160
YW
671
672 output=$(sfdisk --dump "$imgs/zzz")
673 assert_in "$imgs/zzz2 : start= 524328, size= 14848000, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
674
1b6f8915 675 echo "*** 2. Operate on an larger image compared with SizeMinBytes= ***"
3b19e160 676 rm -f "$imgs/zzz"
e2d05745 677 runas testuser truncate -s 12g "$imgs/zzz"
3b19e160
YW
678 sfdisk "$imgs/zzz" <"$imgs/partscript"
679
680 # This should succeed.
e2d05745
DDM
681 runas testuser systemd-repart --definitions="$defs" \
682 --seed="$seed" \
683 --dry-run=no \
684 "$imgs/zzz"
3b19e160
YW
685
686 output=$(sfdisk --dump "$imgs/zzz")
687 assert_in "$imgs/zzz2 : start= 524328, size= 24641456, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
688
1b6f8915 689 echo "*** 3. Multiple partitions with Priority= (small disk) ***"
dfb3ebfd 690 tee "$defs/root.conf" <<EOF
3b19e160
YW
691[Partition]
692Type=root
693SizeMinBytes=10G
694SizeMaxBytes=120G
695Priority=100
696EOF
697
dfb3ebfd 698 tee "$defs/usr.conf" <<EOF
3b19e160
YW
699[Partition]
700Type=usr
701SizeMinBytes=10M
702Priority=10
703EOF
704
705 rm -f "$imgs/zzz"
e2d05745 706 runas testuser truncate -s 8g "$imgs/zzz"
3b19e160
YW
707 sfdisk "$imgs/zzz" <"$imgs/partscript"
708
709 # This should also succeed, but root is not extended.
e2d05745
DDM
710 runas testuser systemd-repart --definitions="$defs" \
711 --seed="$seed" \
712 --dry-run=no \
713 "$imgs/zzz"
3b19e160
YW
714
715 output=$(sfdisk --dump "$imgs/zzz")
716 assert_in "$imgs/zzz2 : start= 524328, size= 14848000, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
717 assert_in "$imgs/zzz3 : start= 15372328, size= 1404848, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:59\"" "$output"
718
1b6f8915 719 echo "*** 4. Multiple partitions with Priority= (large disk) ***"
3b19e160 720 rm -f "$imgs/zzz"
e2d05745 721 runas testuser truncate -s 12g "$imgs/zzz"
3b19e160
YW
722 sfdisk "$imgs/zzz" <"$imgs/partscript"
723
724 # This should also succeed, and root is extended.
e2d05745
DDM
725 runas testuser systemd-repart --definitions="$defs" \
726 --seed="$seed" \
727 --dry-run=no \
728 "$imgs/zzz"
3b19e160
YW
729
730 output=$(sfdisk --dump "$imgs/zzz")
731 assert_in "$imgs/zzz2 : start= 524328, size= 20971520, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
732 assert_in "$imgs/zzz3 : start= 21495848, size= 3669936, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:59\"" "$output"
733}
734
7234a213 735testcase_zero_uuid() {
11749b61
DDM
736 local defs imgs output
737
dfb3ebfd
ZJS
738 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
739 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
11749b61
DDM
740 # shellcheck disable=SC2064
741 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 742 chmod a+rx "$defs"
11749b61 743
1b6f8915 744 echo "*** Test image with zero UUID ***"
11749b61 745
dfb3ebfd 746 tee "$defs/root.conf" <<EOF
11749b61
DDM
747[Partition]
748Type=root-${architecture}
749UUID=null
750EOF
751
e2d05745
DDM
752 runas testuser systemd-repart --definitions="$defs" \
753 --seed="$seed" \
754 --dry-run=no \
755 --empty=create \
756 --size=auto \
757 "$imgs/zero"
11749b61
DDM
758
759 output=$(sfdisk --dump "$imgs/zero")
760
761 assert_in "$imgs/zero1 : start= 2048, size= 20480, type=${root_guid}, uuid=00000000-0000-0000-0000-000000000000" "$output"
762}
763
7234a213 764testcase_verity() {
b5b7879a
DDM
765 local defs imgs output
766
dfb3ebfd
ZJS
767 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
768 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
b5b7879a
DDM
769 # shellcheck disable=SC2064
770 trap "rm -rf '$defs' '$imgs'" RETURN
dfb3ebfd 771 chmod a+rx "$defs"
b5b7879a 772
1b6f8915
ZJS
773 echo "*** dm-verity ***"
774
dfb3ebfd 775 tee "$defs/verity-data.conf" <<EOF
b5b7879a
DDM
776[Partition]
777Type=root-${architecture}
778CopyFiles=${defs}
779Verity=data
780VerityMatchKey=root
5eef7047 781Minimize=guess
b5b7879a
DDM
782EOF
783
dfb3ebfd 784 tee "$defs/verity-hash.conf" <<EOF
b5b7879a
DDM
785[Partition]
786Type=root-${architecture}-verity
787Verity=hash
788VerityMatchKey=root
5eef7047 789Minimize=yes
b5b7879a
DDM
790EOF
791
dfb3ebfd 792 tee "$defs/verity-sig.conf" <<EOF
b456191d
DDM
793[Partition]
794Type=root-${architecture}-verity-sig
795Verity=signature
796VerityMatchKey=root
797EOF
798
799 # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
dfb3ebfd 800 tee >"$defs/verity.openssl.cnf" <<EOF
b456191d
DDM
801[ req ]
802prompt = no
803distinguished_name = req_distinguished_name
804
805[ req_distinguished_name ]
806C = DE
807ST = Test State
808L = Test Locality
809O = Org Name
810OU = Org Unit Name
811CN = Common Name
812emailAddress = test@email.com
813EOF
814
dfb3ebfd
ZJS
815 openssl req \
816 -config "$defs/verity.openssl.cnf" \
817 -new -x509 \
818 -newkey rsa:1024 \
819 -keyout "$defs/verity.key" \
820 -out "$defs/verity.crt" \
821 -days 365 \
822 -nodes
b456191d
DDM
823
824 mkdir -p /run/verity.d
825 ln -s "$defs/verity.crt" /run/verity.d/ok.crt
826
dfb3ebfd
ZJS
827 # CopyFiles will fail if we try to chown the target file to root.
828 # Make the files owned by the user so that the invocation below works.
829 chown testuser -R "$defs"
830
e2d05745
DDM
831 output=$(runas testuser systemd-repart --definitions="$defs" \
832 --seed="$seed" \
833 --dry-run=no \
834 --empty=create \
835 --size=auto \
836 --json=pretty \
837 --private-key="$defs/verity.key" \
838 --certificate="$defs/verity.crt" \
839 "$imgs/verity")
b5b7879a 840
2ecc7a5b
DDM
841 drh=$(jq -r ".[] | select(.type == \"root-${architecture}\") | .roothash" <<< "$output")
842 hrh=$(jq -r ".[] | select(.type == \"root-${architecture}-verity\") | .roothash" <<< "$output")
843 srh=$(jq -r ".[] | select(.type == \"root-${architecture}-verity-sig\") | .roothash" <<< "$output")
844
845 assert_eq "$drh" "$hrh"
846 assert_eq "$hrh" "$srh"
b5b7879a 847
63b130a6 848 # Check that we can dissect, mount and unmount a repart verity image. (and that the image UUID is deterministic)
b5b7879a 849
e2d05745
DDM
850 if systemd-detect-virt --quiet --container; then
851 echo "Skipping verity test dissect part in container."
852 return
853 fi
854
2ecc7a5b
DDM
855 systemd-dissect "$imgs/verity" --root-hash "$drh"
856 systemd-dissect "$imgs/verity" --root-hash "$drh" --json=short | grep -q '"imageUuid":"1d2ce291-7cce-4f7d-bc83-fdb49ad74ebd"'
857 systemd-dissect "$imgs/verity" --root-hash "$drh" -M "$imgs/mnt"
b5b7879a
DDM
858 systemd-dissect -U "$imgs/mnt"
859}
860
7234a213 861testcase_exclude_files() {
bd69c2a8
DDM
862 local defs imgs root output
863
dfb3ebfd
ZJS
864 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
865 imgs="$(runas testuser mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
866 root="$(runas testuser mktemp --directory "/var/tmp/test-repart.root.XXXXXXXXXX")"
bd69c2a8
DDM
867 # shellcheck disable=SC2064
868 trap "rm -rf '$defs' '$imgs' '$root'" RETURN
dfb3ebfd
ZJS
869 chmod a+rx "$defs"
870
1b6f8915
ZJS
871 echo "*** file exclusion ***"
872
dfb3ebfd
ZJS
873 touch "$root/abc"
874 mkdir "$root/usr"
875 touch "$root/usr/def"
876 touch "$root/usr/qed"
877 mkdir "$root/tmp"
878 touch "$root/tmp/prs"
879 mkdir "$root/proc"
880 touch "$root/proc/prs"
881 mkdir "$root/zzz"
882 mkdir "$root/zzz/usr"
883 touch "$root/zzz/usr/prs"
884 mkdir "$root/zzz/proc"
885 touch "$root/zzz/proc/prs"
886
887 tee "$defs/00-root.conf" <<EOF
bd69c2a8
DDM
888[Partition]
889Type=root-${architecture}
890CopyFiles=/
e57b7020 891CopyFiles=/zzz:/
bd69c2a8
DDM
892EOF
893
dfb3ebfd 894 tee "$defs/10-usr.conf" <<EOF
bd69c2a8
DDM
895[Partition]
896Type=usr-${architecture}
897CopyFiles=/usr:/
a9af8276 898ExcludeFiles=/usr/qed
bd69c2a8
DDM
899EOF
900
dfb3ebfd
ZJS
901 # CopyFiles will fail if we try to chown the target file to root.
902 # Make the files owned by the user so that the invocation below works.
903 chown testuser -R "$root"
904
e2d05745
DDM
905 output=$(runas testuser systemd-repart --definitions="$defs" \
906 --seed="$seed" \
907 --dry-run=no \
908 --empty=create \
909 --size=auto \
910 --json=pretty \
911 --root="$root" \
912 "$imgs/zzz")
913
914 if systemd-detect-virt --quiet --container; then
915 echo "Skipping issue 24786 test loop/mount parts in container."
916 return
917 fi
bd69c2a8
DDM
918
919 loop=$(losetup -P --show -f "$imgs/zzz")
920 udevadm wait --timeout 60 --settle "${loop:?}"
921
b63bd125 922 # Test that /usr/def did not end up in the root partition but other files did.
bd69c2a8
DDM
923 mkdir "$imgs/mnt"
924 mount -t ext4 "${loop}p1" "$imgs/mnt"
925 assert_rc 0 ls "$imgs/mnt/abc"
b63bd125
DDM
926 assert_rc 0 ls "$imgs/mnt/usr"
927 assert_rc 2 ls "$imgs/mnt/usr/def"
a9af8276 928
e57b7020
DDM
929 # Test that /zzz/usr/prs did not end up in the root partition under /usr but did end up in /zzz/usr/prs
930 assert_rc 2 ls "$imgs/mnt/usr/prs"
931 assert_rc 0 ls "$imgs/mnt/zzz/usr/prs"
932
4d8ff4e8
DDM
933 # Test that /tmp/prs did not end up in the root partition but /tmp did.
934 assert_rc 0 ls "$imgs/mnt/tmp"
935 assert_rc 2 ls "$imgs/mnt/tmp/prs"
936
b63bd125 937 # Test that /usr/qed did not end up in the usr partition but /usr/def did.
bd69c2a8
DDM
938 mount -t ext4 "${loop}p2" "$imgs/mnt/usr"
939 assert_rc 0 ls "$imgs/mnt/usr/def"
a9af8276 940 assert_rc 2 ls "$imgs/mnt/usr/qed"
bd69c2a8 941
e57b7020
DDM
942 # Test that /zzz/proc/prs did not end up in the root partition but /proc did.
943 assert_rc 0 ls "$imgs/mnt/proc"
944 assert_rc 2 ls "$imgs/mnt/proc/prs"
945
946 # Test that /zzz/usr/prs did not end up in the usr partition.
947 assert_rc 2 ls "$imgs/mnt/usr/prs"
948
bd69c2a8
DDM
949 umount -R "$imgs/mnt"
950 losetup -d "$loop"
951}
952
7234a213 953testcase_minimize() {
14697c41
DDM
954 local defs imgs output
955
956 if systemd-detect-virt --quiet --container; then
957 echo "Skipping minimize test in container."
958 return
959 fi
960
1b6f8915
ZJS
961 echo "*** minimization ***"
962
dfb3ebfd
ZJS
963 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
964 imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
14697c41
DDM
965 # shellcheck disable=SC2064
966 trap "rm -rf '$defs' '$imgs'" RETURN
967
09e917ea 968 for format in ext4 vfat erofs; do
14697c41
DDM
969 if ! command -v "mkfs.$format" >/dev/null; then
970 continue
971 fi
972
ff1b55ff 973 tee "$defs/root-$format.conf" <<EOF
14697c41
DDM
974[Partition]
975Type=root-${architecture}
976Format=${format}
977CopyFiles=${defs}
640c02d5 978Minimize=guess
14697c41
DDM
979EOF
980 done
981
8fd25ba3 982 if command -v mksquashfs >/dev/null; then
ff1b55ff 983 tee "$defs/root-squashfs.conf" <<EOF
14697c41
DDM
984[Partition]
985Type=root-${architecture}
986Format=squashfs
987CopyFiles=${defs}
640c02d5 988Minimize=best
14697c41
DDM
989EOF
990 fi
991
992 output=$(systemd-repart --definitions="$defs" \
993 --seed="$seed" \
994 --dry-run=no \
995 --empty=create \
996 --size=auto \
997 --json=pretty \
998 "$imgs/zzz")
999
1000 # Check that we can dissect, mount and unmount a minimized image.
1001
1002 systemd-dissect "$imgs/zzz"
1003 systemd-dissect "$imgs/zzz" -M "$imgs/mnt"
1004 systemd-dissect -U "$imgs/mnt"
1005}
1006
5996f116
YW
1007test_sector() {
1008 local defs imgs output loop
1009 local start size ratio
1010 local sector="${1?}"
1011
76097112
YW
1012 if systemd-detect-virt --quiet --container; then
1013 echo "Skipping sector size tests in container."
1014 return
1015 fi
1016
1b6f8915
ZJS
1017 echo "*** sector sizes ***"
1018
dfb3ebfd
ZJS
1019 defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
1020 imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
5996f116
YW
1021 # shellcheck disable=SC2064
1022 trap "rm -rf '$defs' '$imgs'" RETURN
1023
ff1b55ff 1024 tee "$defs/a.conf" <<EOF
2cd341af
LP
1025[Partition]
1026Type=root
1027SizeMaxBytes=15M
1028SizeMinBytes=15M
1029EOF
ff1b55ff 1030 tee "$defs/b.conf" <<EOF
2cd341af
LP
1031[Partition]
1032Type=linux-generic
1033Weight=250
1034EOF
1035
ff1b55ff 1036 tee "$defs/c.conf" <<EOF
2cd341af
LP
1037[Partition]
1038Type=linux-generic
1039Weight=750
1040EOF
1041
5996f116
YW
1042 truncate -s 100m "$imgs/$sector.img"
1043 loop=$(losetup -b "$sector" -P --show -f "$imgs/$sector.img" )
1044 udevadm wait --timeout 60 --settle "${loop:?}"
e2d05745
DDM
1045 # This operates on a loop device which we don't support doing without root privileges so we skip runas
1046 # here.
5996f116
YW
1047 systemd-repart --pretty=yes \
1048 --definitions="$defs" \
1049 --seed="$seed" \
1050 --empty=require \
1051 --dry-run=no \
1052 "$loop"
1053
1054 sfdisk --verify "$loop"
1055 output=$(sfdisk --dump "$loop")
1056 losetup -d "$loop"
1057
1058 ratio=$(( sector / 512 ))
1059 start=$(( 2048 / ratio ))
1060 size=$(( 30720 / ratio ))
1061 assert_in "${loop}p1 : start= *${start}, size= *${size}, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"" "$output"
1062 start=$(( start + size ))
1063 size=$(( 42992 / ratio ))
1064 assert_in "${loop}p2 : start= *${start}, size= *${size}, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=DF71F5E3-080A-4D16-824B-18591B881380, name=\"linux-generic\"" "$output"
1065 start=$(( start + size ))
1066 size=$(( 129000 / ratio ))
1067 assert_in "${loop}p3 : start= *${start}, size= *${size}, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=DB081670-07AE-48CA-9F5E-813D5E40B976, name=\"linux-generic-2\"" "$output"
2cd341af
LP
1068}
1069
7234a213 1070run_testcases
5996f116 1071
2cd341af
LP
1072# Valid block sizes on the Linux block layer are >= 512 and <= PAGE_SIZE, and
1073# must be powers of 2. Which leaves exactly four different ones to test on
1074# typical hardware
5996f116
YW
1075test_sector 512
1076test_sector 1024
1077test_sector 2048
1078test_sector 4096
2cd341af 1079
5a3b8640
LP
1080echo OK >/testok
1081
1082exit 0