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