]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/test-repart.sh
tests: reflect that we can now handle devices with very long sysfs paths
[thirdparty/systemd.git] / src / partition / test-repart.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -eux
4 set -o pipefail
5
6 [[ -e /dev/loop-control ]] || exit 77
7
8 repart="${1:?}"
9 udevadm="${2:?}"
10 test -x "$repart"
11 test -x "$udevadm"
12
13 PATH=$PATH:/sbin:/usr/sbin
14
15 D="$(mktemp --tmpdir --directory "test-repart.XXXXXXXXXX")"
16
17 # shellcheck disable=SC2064
18 trap "rm -rf '$D'" EXIT INT QUIT PIPE
19 mkdir -p "$D/definitions"
20
21 SEED=e2a40bf9-73f1-4278-9160-49c031e7aef8
22
23 echo "### Testing systemd-repart --empty=create ###"
24
25 "$repart" "$D/zzz" --empty=create --size=1G --seed="$SEED"
26
27 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/empty"
28
29 cmp "$D/empty" - <<EOF
30 label: gpt
31 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
32 device: $D/zzz
33 unit: sectors
34 first-lba: 2048
35 last-lba: 2097118
36 EOF
37
38 echo "### Testing with root, root2, home, & swap ###"
39
40 cat >"$D/definitions/root.conf" <<EOF
41 [Partition]
42 Type=root-x86-64
43 EOF
44
45 ln -s root.conf "$D/definitions/root2.conf"
46
47 cat >"$D/definitions/home.conf" <<EOF
48 [Partition]
49 Type=home
50 Label=home-first
51 Label=home-always-too-long-xxxxxxxxxxxxxx-%v
52 EOF
53
54 cat >"$D/definitions/swap.conf" <<EOF
55 [Partition]
56 Type=swap
57 SizeMaxBytes=64M
58 PaddingMinBytes=92M
59 EOF
60
61 "$repart" "$D/zzz" --dry-run=no --seed="$SEED" --definitions="$D/definitions"
62
63 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated"
64
65 cmp "$D/populated" - <<EOF
66 label: gpt
67 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
68 device: $D/zzz
69 unit: sectors
70 first-lba: 2048
71 last-lba: 2097118
72 $D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
73 $D/zzz2 : start= 593904, size= 591856, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=CE9C76EB-A8F1-40FF-813C-11DCA6C0A55B, name="root-x86-64", attrs="GUID:59"
74 $D/zzz3 : start= 1185760, size= 591864, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=AC60A837-550C-43BD-B5C4-9CB73B884E79, name="root-x86-64-2", attrs="GUID:59"
75 $D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
76 EOF
77
78 echo "### Testing with root, root2, home, swap, & another partition ###"
79
80 cat >"$D/definitions/swap.conf" <<EOF
81 [Partition]
82 Type=swap
83 SizeMaxBytes=64M
84 EOF
85
86 cat >"$D/definitions/extra.conf" <<EOF
87 [Partition]
88 Type=linux-generic
89 Label=custom_label
90 UUID=a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
91 EOF
92
93 echo "Label=ignored_label" >>"$D/definitions/home.conf"
94 echo "UUID=b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" >>"$D/definitions/home.conf"
95
96 "$repart" "$D/zzz" --dry-run=no --seed="$SEED" --definitions="$D/definitions"
97
98 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated2"
99
100 cmp "$D/populated2" - <<EOF
101 label: gpt
102 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
103 device: $D/zzz
104 unit: sectors
105 first-lba: 2048
106 last-lba: 2097118
107 $D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
108 $D/zzz2 : start= 593904, size= 591856, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=CE9C76EB-A8F1-40FF-813C-11DCA6C0A55B, name="root-x86-64", attrs="GUID:59"
109 $D/zzz3 : start= 1185760, size= 591864, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=AC60A837-550C-43BD-B5C4-9CB73B884E79, name="root-x86-64-2", attrs="GUID:59"
110 $D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
111 $D/zzz5 : start= 1908696, size= 188416, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
112 EOF
113
114 echo "### Resizing to 2G ###"
115
116 "$repart" "$D/zzz" --size=2G --dry-run=no --seed="$SEED" --definitions="$D/definitions"
117
118 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated3"
119
120 cmp "$D/populated3" - <<EOF
121 label: gpt
122 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
123 device: $D/zzz
124 unit: sectors
125 first-lba: 2048
126 last-lba: 4194270
127 $D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
128 $D/zzz2 : start= 593904, size= 591856, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=CE9C76EB-A8F1-40FF-813C-11DCA6C0A55B, name="root-x86-64", attrs="GUID:59"
129 $D/zzz3 : start= 1185760, size= 591864, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=AC60A837-550C-43BD-B5C4-9CB73B884E79, name="root-x86-64-2", attrs="GUID:59"
130 $D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
131 $D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
132 EOF
133
134 dd if=/dev/urandom of="$D/block-copy" bs=4096 count=10240
135
136 echo "### Testing with root, root2, home, swap, another partition, & partition copy ###"
137
138 cat >"$D/definitions/extra2.conf" <<EOF
139 [Partition]
140 Type=linux-generic
141 Label=block-copy
142 UUID=2a1d97e1d0a346cca26eadc643926617
143 CopyBlocks=$D/block-copy
144 EOF
145
146 "$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions"
147
148 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated4"
149
150 cmp "$D/populated4" - <<EOF
151 label: gpt
152 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
153 device: $D/zzz
154 unit: sectors
155 first-lba: 2048
156 last-lba: 6291422
157 $D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
158 $D/zzz2 : start= 593904, size= 591856, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=CE9C76EB-A8F1-40FF-813C-11DCA6C0A55B, name="root-x86-64", attrs="GUID:59"
159 $D/zzz3 : start= 1185760, size= 591864, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=AC60A837-550C-43BD-B5C4-9CB73B884E79, name="root-x86-64-2", attrs="GUID:59"
160 $D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
161 $D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
162 $D/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name="block-copy"
163 EOF
164
165 cmp --bytes=41943040 --ignore-initial=0:$((512*4194264)) "$D/block-copy" "$D/zzz"
166
167 if [ "$(id -u)" -eq 0 ] && type -P cryptsetup diff losetup >/dev/null ; then
168 echo "### Testing Format=/Encrypt=/CopyFiles="
169
170 # These tests require privileges unfortunately
171
172 cat >"$D/definitions/extra3.conf" <<EOF
173 [Partition]
174 Type=linux-generic
175 Label=luks-format-copy
176 UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
177 Format=ext4
178 Encrypt=yes
179 CopyFiles=$D/definitions:/def
180 SizeMinBytes=48M
181 EOF
182
183 "$repart" "$D/zzz" --size=auto --dry-run=no --seed="$SEED" --definitions="$D/definitions"
184
185 sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated5"
186
187 cmp "$D/populated5" - <<EOF
188 label: gpt
189 label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
190 device: $D/zzz
191 unit: sectors
192 first-lba: 2048
193 last-lba: 6389726
194 $D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
195 $D/zzz2 : start= 593904, size= 591856, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=CE9C76EB-A8F1-40FF-813C-11DCA6C0A55B, name="root-x86-64", attrs="GUID:59"
196 $D/zzz3 : start= 1185760, size= 591864, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=AC60A837-550C-43BD-B5C4-9CB73B884E79, name="root-x86-64-2", attrs="GUID:59"
197 $D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
198 $D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
199 $D/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name="block-copy"
200 $D/zzz7 : start= 6291416, size= 98304, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name="luks-format-copy"
201 EOF
202
203 LOOP="$(losetup -P --show --find "$D/zzz")"
204 "${udevadm:?}" wait --timeout 60 --settle "${LOOP:?}"
205
206 VOLUME="test-repart-$RANDOM"
207
208 touch "$D/empty-password"
209 cryptsetup open --type=luks2 --key-file="$D/empty-password" "${LOOP}p7" "$VOLUME"
210 mkdir "$D/mount"
211 mount -t ext4 "/dev/mapper/$VOLUME" "$D/mount"
212 # Use deferred closing on the mapper and autoclear on the loop, so they are cleaned up on umount
213 cryptsetup close --deferred "$VOLUME"
214 losetup -d "$LOOP"
215 diff -r "$D/mount/def" "$D/definitions" >/dev/null
216 umount "$D/mount"
217 else
218 echo "### Skipping Format=/Encrypt=/CopyFiles= test, lacking privileges or missing cryptsetup/diff/losetup"
219 fi
220
221 echo "### Testing json output ###"
222 "$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=help
223 "$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=pretty
224 "$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=short