test: merge test-repart.sh and TEST-58-REPART

This commit is contained in:
Yu Watanabe 2022-08-20 04:37:00 +09:00 committed by Daan De Meyer
parent 5996f11679
commit 2b21522e82
5 changed files with 358 additions and 339 deletions

View file

@ -3668,12 +3668,6 @@ if conf.get('ENABLE_REPART') == 1
install : true,
install_dir : rootbindir)
public_programs += exe
if want_tests != 'false'
test('test-repart',
test_repart_sh,
args : [exe.full_path(), udevadm.full_path()])
endif
endif
executable(

View file

@ -1,5 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
systemd_repart_sources = files('repart.c')
test_repart_sh = find_program('test-repart.sh')

View file

@ -1,331 +0,0 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail
[[ -e /dev/loop-control ]] || exit 77
repart="${1:?}"
udevadm="${2:?}"
test -x "$repart"
test -x "$udevadm"
PATH=$PATH:/sbin:/usr/sbin
D="$(mktemp --tmpdir --directory "test-repart.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$D'" EXIT INT QUIT PIPE
mkdir -p "$D/definitions"
SEED=e2a40bf9-73f1-4278-9160-49c031e7aef8
echo "### Testing systemd-repart --empty=create ###"
"$repart" "$D/zzz" --empty=create --size=1G --seed="$SEED" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/empty"
cmp "$D/empty" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118
EOF
echo "### Testing with root, root2, home, & swap ###"
cat >"$D/definitions/root.conf" <<EOF
[Partition]
Type=root-x86-64
EOF
ln -s root.conf "$D/definitions/root2.conf"
cat >"$D/definitions/home.conf" <<EOF
[Partition]
Type=home
Label=home-first
Label=home-always-too-long-xxxxxxxxxxxxxx-%v
EOF
cat >"$D/definitions/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
PaddingMinBytes=92M
EOF
"$repart" "$D/zzz" --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated"
cmp "$D/populated" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118
$D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
$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"
$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"
$D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
EOF
echo "### Testing with root, root2, home, swap, & another partition ###"
cat >"$D/definitions/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
EOF
cat >"$D/definitions/extra.conf" <<EOF
[Partition]
Type=linux-generic
Label=custom_label
UUID=a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
EOF
echo "Label=ignored_label" >>"$D/definitions/home.conf"
echo "UUID=b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" >>"$D/definitions/home.conf"
"$repart" "$D/zzz" --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated2"
cmp "$D/populated2" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118
$D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
$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"
$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"
$D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
$D/zzz5 : start= 1908696, size= 188416, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
EOF
echo "### Resizing to 2G ###"
"$repart" "$D/zzz" --size=2G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated3"
cmp "$D/populated3" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 4194270
$D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
$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"
$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"
$D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
$D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
EOF
dd if=/dev/urandom of="$D/block-copy" bs=4096 count=10240
echo "### Testing with root, root2, home, swap, another partition, & partition copy ###"
cat >"$D/definitions/extra2.conf" <<EOF
[Partition]
Type=linux-generic
Label=block-copy
UUID=2a1d97e1d0a346cca26eadc643926617
CopyBlocks=$D/block-copy
EOF
"$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated4"
cmp "$D/populated4" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 6291422
$D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
$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"
$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"
$D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
$D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
$D/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name="block-copy"
EOF
cmp --bytes=41943040 --ignore-initial=0:$((512*4194264)) "$D/block-copy" "$D/zzz"
if [ "$(id -u)" -eq 0 ] && type -P cryptsetup diff losetup >/dev/null ; then
echo "### Testing Format=/Encrypt=/CopyFiles="
# These tests require privileges unfortunately
cat >"$D/definitions/extra3.conf" <<EOF
[Partition]
Type=linux-generic
Label=luks-format-copy
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Format=ext4
Encrypt=yes
CopyFiles=$D/definitions:/def
SizeMinBytes=48M
EOF
"$repart" "$D/zzz" --size=auto --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager
sfdisk -d "$D/zzz" | grep -v -e 'sector-size' -e '^$' >"$D/populated5"
cmp "$D/populated5" - <<EOF
label: gpt
label-id: EF7F7EE2-47B3-4251-B1A1-09EA8BF12D5D
device: $D/zzz
unit: sectors
first-lba: 2048
last-lba: 6389726
$D/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=A6005774-F558-4330-A8E5-D6D2C01C01D6, name="home-first", attrs="GUID:59"
$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"
$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"
$D/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=2AA78CDB-59C7-4173-AF11-C7453737A5D1, name="swap"
$D/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name="custom_label"
$D/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name="block-copy"
$D/zzz7 : start= 6291416, size= 98304, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name="luks-format-copy"
EOF
LOOP="$(losetup -P --show --find "$D/zzz")"
"${udevadm:?}" wait --timeout 60 --settle "${LOOP:?}"
VOLUME="test-repart-$RANDOM"
touch "$D/empty-password"
cryptsetup open --type=luks2 --key-file="$D/empty-password" "${LOOP}p7" "$VOLUME"
mkdir "$D/mount"
mount -t ext4 "/dev/mapper/$VOLUME" "$D/mount"
# Use deferred closing on the mapper and autoclear on the loop, so they are cleaned up on umount
cryptsetup close --deferred "$VOLUME"
losetup -d "$LOOP"
diff -r "$D/mount/def" "$D/definitions" >/dev/null
umount "$D/mount"
else
echo "### Skipping Format=/Encrypt=/CopyFiles= test, lacking privileges or missing cryptsetup/diff/losetup"
fi
echo "### Testing json output ###"
"$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=help
"$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=pretty
"$repart" "$D/zzz" --size=3G --dry-run=no --seed="$SEED" --definitions="$D/definitions" --no-pager --json=short
echo "### Testing drop-in overrides ###"
mkdir -p "$D/definitions-overrides"
cat >"$D/definitions-overrides/root.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
EOF
mkdir -p "$D/definitions-overrides/root.conf.d"
cat >"$D/definitions-overrides/root.conf.d/override1.conf" <<EOF
[Partition]
Label=label1
SizeMaxBytes=32M
EOF
cat >"$D/definitions-overrides/root.conf.d/override2.conf" <<EOF
[Partition]
Label=label2
EOF
rm -f test-drop-in-image
JSON_OUTPUT=$("$repart" --definitions="$D/definitions-overrides" --dry-run=yes --empty=create --size=100M --json=pretty test-drop-in-image)
diff <(echo "$JSON_OUTPUT") - <<EOF
[
{
"type" : "swap",
"label" : "label2",
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
"file" : "root.conf",
"node" : "test-drop-in-image1",
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"old_padding" : 0,
"raw_padding" : 0,
"activity" : "create",
"drop-in_files" : [
"$D/definitions-overrides/root.conf.d/override1.conf",
"$D/definitions-overrides/root.conf.d/override2.conf"
]
}
]
EOF
echo "### Testing list of definitions directories ###"
mkdir -p "$D/definitions1"
cat >"$D/definitions1/root1.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Label=label1
EOF
mkdir -p "$D/definitions2"
cat >"$D/definitions2/root2.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
Label=label2
EOF
rm -f test-definitions
JSON_OUTPUT=$("$repart" --definitions="$D/definitions1" --definitions="$D/definitions2" --dry-run=yes --empty=create --size=100M --json=pretty test-definitions)
diff <(echo "$JSON_OUTPUT") - <<EOF
[
{
"type" : "swap",
"label" : "label1",
"uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
"file" : "root1.conf",
"node" : "test-definitions1",
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"old_padding" : 0,
"raw_padding" : 0,
"activity" : "create"
},
{
"type" : "swap",
"label" : "label2",
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
"file" : "root2.conf",
"node" : "test-definitions2",
"offset" : 34603008,
"old_size" : 0,
"raw_size" : 33554432,
"old_padding" : 0,
"raw_padding" : 0,
"activity" : "create"
}
]
EOF

View file

@ -8,4 +8,9 @@ TEST_NO_NSPAWN=1
# shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"
test_append_files() {
install_dmevent
generate_module_dependencies
}
do_test "$@"

View file

@ -16,52 +16,71 @@ export PAGER=cat
seed=750b6cd5c4ae4012a15e7be3c29e6a47
mkdir -p /run/systemd/system/systemd-udevd.service.d
cat >/run/systemd/system/systemd-udevd.service.d/debug.conf <<EOF
[Service]
Environment=SYSTEMD_LOG_LEVEL=debug
EOF
systemctl daemon-reload
udevadm settle
systemctl restart systemd-udevd.service
udevadm control --ping
machine="$(uname -m)"
if [ "${machine}" = "x86_64" ]; then
root_guid=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
root_uuid=60F33797-1D71-4DCB-AA6F-20564F036CD0
root_uuid2=73A4CCD2-EAF5-44DA-A366-F99188210FDC
usr_guid=8484680C-9521-48C6-9C11-B0720656F69E
usr_uuid=7E3369DD-D653-4513-ADF5-B993A9F20C16
architecture="x86-64"
elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
root_guid=44479540-F297-41B2-9AF7-D131D5F0458A
root_uuid=02B4253F-29A4-404E-8972-1669D3B03C87
root_uuid2=268E0FD3-B468-4806-A823-E533FE9BB9CC
usr_guid=75250D76-8CC6-458E-BD66-BD47CC81A812
usr_uuid=7B42FFB0-B0E1-4395-B20B-C78F4A571648
architecture="x86"
elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
root_guid=B921B045-1DF0-41C3-AF44-4C6F280D3FAE
root_uuid=055D0227-53A6-4033-85C3-9A5973EFF483
root_uuid2=F7DBBE48-8FD0-4833-8411-AA34E7C8E60A
usr_guid=B0E01050-EE5F-4390-949A-9101B17104E9
usr_uuid=FCE3C75E-D6A4-44C0-87F0-4C105183FB1F
architecture="arm64"
elif [ "${machine}" = "arm" ]; then
root_guid=69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
root_uuid=567DA89E-8DE2-4499-8D10-18F212DFF034
root_uuid2=813ECFE5-4C89-4193-8A52-437493F2F96E
usr_guid=7D0359A3-02B3-4F0A-865C-654403E70625
usr_uuid=71E93DC2-5073-42CB-8A84-A354E64D8966
architecture="arm"
elif [ "${machine}" = "loongarch64" ]; then
root_guid=77055800-792C-4F94-B39A-98C91B762BB6
root_uuid=D8EFC2D2-0133-41E4-BDCB-3B9F4CFDDDE8
root_uuid2=36499F9E-0688-40C1-A746-EA8FD9543C56
usr_guid=E611C702-575C-4CBE-9A46-434FA0BF7E3F
usr_uuid=031FFA75-00BB-49B6-A70D-911D2D82A5B7
architecture="loongarch64"
elif [ "${machine}" = "ia64" ]; then
root_guid=993D8D3D-F80E-4225-855A-9DAF8ED7EA97
root_uuid=DCF33449-0896-4EA9-BC24-7D58AEEF522D
root_uuid2=C2A6CAB7-ABEA-4FBA-8C48-CB4C52E6CA38
usr_guid=4301D2A6-4E3B-4B2A-BB94-9E0B2C4225EA
usr_uuid=BC2BCCE7-80D6-449A-85CC-637424CE5241
architecture="ia64"
elif [ "${machine}" = "s390x" ]; then
root_guid=5EEAD9A9-FE09-4A1E-A1D7-520D00531306
root_uuid=7EBE0C85-E27E-48EC-B164-F4807606232E
root_uuid2=2A074E1C-2A19-4094-A0C2-24B1A5D52FCB
usr_guid=8A4F5770-50AA-4ED3-874A-99B710DB6FEA
usr_uuid=51171D30-35CF-4A49-B8B5-9478B9B796A5
architecture="s390x"
elif [ "${machine}" = "ppc64le" ]; then
root_guid=C31C45E6-3F39-412E-80FB-4809C4980599
root_uuid=061E67A1-092F-482F-8150-B525D50D6654
root_uuid2=A6687CEF-4E4F-44E7-90B3-CDA52EA81739
usr_guid=15BB03AF-77E7-4D4A-B12B-C0D084F7491C
usr_uuid=C0D0823B-8040-4C7C-A629-026248E297FB
architecture="ppc64-le"
@ -70,6 +89,337 @@ else
exit 1
fi
test_basic() {
local defs imgs output
local loop volume
defs="$(mktemp --directory "/tmp/test-repart.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
# 1. create an empty image
systemd-repart --empty=create \
--size=1G \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118"
# 2. Testing with root, root2, home, and swap
cat >"$defs/root.conf" <<EOF
[Partition]
Type=root
EOF
ln -s root.conf "$defs/root2.conf"
cat >"$defs/home.conf" <<EOF
[Partition]
Type=home
Label=home-first
Label=home-always-too-long-xxxxxxxxxxxxxx-%v
EOF
cat >"$defs/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
PaddingMinBytes=92M
EOF
systemd-repart --definitions="$defs" \
--dry-run=no \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118
$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
# 3. Testing with root, root2, home, swap, and another partition
cat >"$defs/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
EOF
cat >"$defs/extra.conf" <<EOF
[Partition]
Type=linux-generic
Label=custom_label
UUID=a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
EOF
echo "Label=ignored_label" >>"$defs/home.conf"
echo "UUID=b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" >>"$defs/home.conf"
systemd-repart --definitions="$defs" \
--dry-run=no \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 2097118
$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
$imgs/zzz5 : start= 1908696, size= 188416, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\""
# 4. Resizing to 2G
systemd-repart --definitions="$defs" \
--size=2G \
--dry-run=no \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 4194270
$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\""
# 5. Testing with root, root2, home, swap, another partition, and partition copy
dd if=/dev/urandom of="$imgs/block-copy" bs=4096 count=10240
cat >"$defs/extra2.conf" <<EOF
[Partition]
Type=linux-generic
Label=block-copy
UUID=2a1d97e1d0a346cca26eadc643926617
CopyBlocks=$imgs/block-copy
EOF
systemd-repart --definitions="$defs" \
--size=3G \
--dry-run=no \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 6291422
$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\"
$imgs/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name=\"block-copy\""
cmp --bytes=$((4096*10240)) --ignore-initial=0:$((512*4194264)) "$imgs/block-copy" "$imgs/zzz"
# 6. Testing Format=/Encrypt=/CopyFiles=
cat >"$defs/extra3.conf" <<EOF
[Partition]
Type=linux-generic
Label=luks-format-copy
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Format=ext4
Encrypt=yes
CopyFiles=$defs:/def
SizeMinBytes=48M
EOF
systemd-repart --definitions="$defs" \
--size=auto \
--dry-run=no \
--seed="$seed" \
"$imgs/zzz"
output=$(sfdisk -d "$imgs/zzz" | grep -v -e 'sector-size' -e '^$')
assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/zzz
unit: sectors
first-lba: 2048
last-lba: 6389726
$imgs/zzz1 : start= 2048, size= 591856, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start= 593904, size= 591856, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/zzz3 : start= 1185760, size= 591864, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/zzz4 : start= 1777624, size= 131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\"
$imgs/zzz5 : start= 1908696, size= 2285568, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A0A1A2A3-A4A5-A6A7-A8A9-AAABACADAEAF, name=\"custom_label\"
$imgs/zzz6 : start= 4194264, size= 2097152, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=2A1D97E1-D0A3-46CC-A26E-ADC643926617, name=\"block-copy\"
$imgs/zzz7 : start= 6291416, size= 98304, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name=\"luks-format-copy\""
loop="$(losetup -P --show --find "$imgs/zzz")"
udevadm wait --timeout 60 --settle "${loop:?}"
volume="test-repart-$RANDOM"
touch "$imgs/empty-password"
cryptsetup open --type=luks2 --key-file="$imgs/empty-password" "${loop}p7" "$volume"
mkdir -p "$imgs/mount"
mount -t ext4 "/dev/mapper/$volume" "$imgs/mount"
# Use deferred closing on the mapper and autoclear on the loop, so they are cleaned up on umount
cryptsetup close --deferred "$volume"
losetup -d "$loop"
diff -r "$imgs/mount/def" "$defs" >/dev/null
umount "$imgs/mount"
}
test_dropin() {
local defs imgs output
defs="$(mktemp --directory "/tmp/test-repart.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
cat >"$defs/root.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
EOF
mkdir -p "$defs/root.conf.d"
cat >"$defs/root.conf.d/override1.conf" <<EOF
[Partition]
Label=label1
SizeMaxBytes=32M
EOF
cat >"$defs/root.conf.d/override2.conf" <<EOF
[Partition]
Label=label2
EOF
output=$(systemd-repart --definitions="$defs" --empty=create --size=100M --json=pretty "$imgs/zzz")
diff <(echo "$output") - <<EOF
[
{
"type" : "swap",
"label" : "label2",
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
"file" : "root.conf",
"node" : "$imgs/zzz1",
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "→ 32.0M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "→ 0B",
"activity" : "create",
"drop-in_files" : [
"$defs/root.conf.d/override1.conf",
"$defs/root.conf.d/override2.conf"
]
}
]
EOF
}
test_multiple_definitions() {
local defs imgs output
defs="$(mktemp --directory "/tmp/test-repart.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
mkdir -p "$defs/1"
cat >"$defs/1/root1.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Label=label1
EOF
mkdir -p "$defs/2"
cat >"$defs/2/root2.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
Label=label2
EOF
output=$(systemd-repart --definitions="$defs/1" --definitions="$defs/2" --empty=create --size=100M --json=pretty "$imgs/zzz")
diff <(echo "$output") - <<EOF
[
{
"type" : "swap",
"label" : "label1",
"uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
"file" : "root1.conf",
"node" : "$imgs/zzz1",
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "→ 32.0M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "→ 0B",
"activity" : "create"
},
{
"type" : "swap",
"label" : "label2",
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
"file" : "root2.conf",
"node" : "$imgs/zzz2",
"offset" : 34603008,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "→ 32.0M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "→ 0B",
"activity" : "create"
}
]
EOF
}
test_copy_blocks() {
local defs imgs output
@ -271,6 +621,9 @@ EOF
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"
}
test_basic
test_dropin
test_multiple_definitions
test_copy_blocks
test_unaligned_partition
test_issue_21817