HALF ECHO LEARN / REFERENCE COLLECTION
v262.2test/units/TEST-58-REPART.sh
Git blob: 2a6c369b38488296167b647fad74b8af7f70be19
Lines 3001–3527 of 3,527
dot_linux=$(fat_dot_entry '::/EFI/Linux')
if [[ "$OFFLINE" == "yes" ]]; then
# mmd creates them and respects SOURCE_DATE_EPOCH
assert_in "$time_epoch" "$dot_efi"
assert_in "$time_epoch" "$dot_linux"
else
# The kernel's vfat driver creates them with wallclock mtime. Our utimensat()
# afterwards only rewrites the directory's entry in its *parent*, never this pair.
# Out of reach from userspace, remains unreproducible. Just log them.
echo "'.' entries carry the wall clock, as expected online: $dot_efi / $dot_linux"
fi
# Without an epoch, both files keep their own mtime.
build_image wallclock.img -u SOURCE_DATE_EPOCH
assert_in "$time_new" "$(fat_entry '::/EFI/Linux/new.efi')"
assert_in "$time_old" "$(fat_entry '::/EFI/Linux/old.efi')"
# A value we cannot parse is dropped rather than passed on
build_image bogus.img SOURCE_DATE_EPOCH=99999999999999999999
assert_in "$time_new" "$(fat_entry '::/EFI/Linux/new.efi')"
assert_in "$time_old" "$(fat_entry '::/EFI/Linux/old.efi')"
# SOURCE_DATE_EPOCH in hex - we do parse that, but mtools doesn't
build_image hex.img SOURCE_DATE_EPOCH=0x6553F100
assert_in "$time_epoch" "$(fat_entry '::/EFI*')"
# Note the images still are not reproducible byte for byte: the volume label entry carries
# mkfs.fat's wall clock, in *both* modes, and that needs a dosfstools with $SOURCE_DATE_EPOCH
# support (https://github.com/dosfstools/dosfstools/commit/8da7bc93315c, release > 4.2). Once
# that lands, offline mode can move to a `cmp` like in testcase_ext_reproducibility; online
# mode cannot, because of the "." and ".." entries above.
}
testcase_luks2_keyhash() {
local defs imgs output root
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
root="$(mktemp --directory "/var/test-repart.root.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs' '$root'" RETURN
chmod 0755 "$defs"
echo "*** testcase for fixate-volume-key ***"
volume="test-repart-lukskeyhash-$RANDOM"
tee "$defs/root.conf" <<EOF
[Partition]
Type=linux-generic
Format=ext4
Encrypt=key-file
EncryptedVolume=$volume:::fixate-volume-key
EOF
systemd-repart --pretty=yes \
--definitions "$defs" \
--empty=create \
--size=100M \
--seed="$seed" \
--dry-run=no \
--offline="$OFFLINE" \
--generate-crypttab="$imgs/crypttab" \
"$imgs/enckeyhash.img"
loop="$(losetup -P --show --find "$imgs/enckeyhash.img")"
udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1"
touch "$imgs/empty-password"
# Check that the volume can be attached with the correct hash
expected_hash="$(grep UUID= "$imgs/crypttab" | sed s,.*fixate-volume-key=,,)"
echo "Expected hash: $expected_hash"
echo "Trying to attach the volume"
systemd-cryptsetup attach $volume "${loop}p1" "$imgs/empty-password" "fixate-volume-key=$expected_hash"
echo "Trying to detach the volume"
systemd-cryptsetup detach $volume
echo "Success!"
# Check that the volume cannot be attached with incorrect hash
echo "Trying to attach the volume with wrong hash"
systemd-cryptsetup attach $volume "${loop}p1" "$imgs/empty-password" "fixate-volume-key=aaaaaabbbbbbccccccddddddeeeeeeffffff1111112222223333334444445555" && exit 1
# Verify the volume is not attached
[ ! -f "/dev/mapper/$volume" ] || exit 1
losetup -d "$loop"
}
testcase_fstab_crypttab_in_repart() {
local defs imgs root volume
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
root="$(mktemp --directory "/var/test-repart.root.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs' '$root'" RETURN
chmod 0755 "$defs"
echo "*** testcase for including fstab/crypttab into repart created volume ***"
volume="test-repart-fstab-crypttab-$RANDOM"
mkdir -p "$root/etc"
tee "$defs/root.conf" <<EOF
[Partition]
Type=linux-generic
Format=ext4
CopyFiles=/etc
Encrypt=key-file
EncryptedVolume=$volume
MountPoint=/mnt/volume
EOF
systemd-repart --pretty=yes \
--definitions "$defs" \
--empty=create \
--size=100M \
--seed="$seed" \
--dry-run=no \
--offline="$OFFLINE" \
--generate-fstab="/etc/fstab" \
--generate-crypttab="/etc/crypttab" \
--root="$root" \
"$imgs/fstabcrypttabrepart.img"
loop="$(losetup -P --show --find "$imgs/fstabcrypttabrepart.img")"
udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1"
touch "$imgs/empty-password"
mkdir -p "$imgs/mount"
systemd-cryptsetup attach "$volume" "${loop}p1" "$imgs/empty-password"
mount -t ext4 "/dev/mapper/$volume" "$imgs/mount"
echo "Testing /etc/fstab presence"
test -f "$imgs/mount/etc/fstab"
grep -q "/mnt/volume" "$imgs/mount/etc/fstab"
echo "Testing /etc/crypttab presence"
test -f "$imgs/mount/etc/crypttab"
grep -q "$volume" "$imgs/mount/etc/crypttab"
umount "$imgs/mount"
systemd-cryptsetup detach "$volume"
losetup -d "$loop"
}
testcase_generate_fstab_dry_run() {
local defs root
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
root="$(mktemp --directory "/var/test-repart.root.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$root'" RETURN
chmod 0755 "$defs"
echo "*** testcase for skipping generated fstab in dry-run mode ***"
mkdir -p "$root/etc"
tee "$defs/root.conf" <<EOF
[Partition]
Type=root
Format=ext4
MountPoint=/
EOF
systemd-repart --pretty=yes \
--definitions "$defs" \
--dry-run=yes \
--seed="$seed" \
--generate-fstab="$root/etc/fstab" \
-
test ! -e "$root/etc/fstab"
}
testcase_encrypted_volume_empty_name() {
local defs imgs
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
chmod 0755 "$defs"
echo "*** testcase for EncryptedVolume= with empty volume name ***"
tee "$defs/root.conf" <<EOF
[Partition]
Type=linux-generic
Format=ext4
Encrypt=key-file
EncryptedVolume=:none:discard
EOF
systemd-repart --pretty=yes \
--definitions "$defs" \
--empty=create \
--size=100M \
--seed="$seed" \
--dry-run=no \
--offline="$OFFLINE" \
--generate-crypttab="$imgs/crypttab" \
"$imgs/emptyvolname.img"
# systemd-repart should fill in volume name as luks-UUID
grep -Eq '^luks-[0-9a-f-]{36} UUID=[0-9a-f-]{36} none discard$' \
"$imgs/crypttab"
}
testcase_block_device_replace() {
if [[ "$OFFLINE" == "yes" ]]; then
return 0
fi
if ! command -v btrfs >/dev/null; then
echo "btrfs not found, skipping."
return 0
fi
if ! command -v mkfs.btrfs >/dev/null; then
echo "mkfs.btrfs not found, skipping."
return 0
fi
local defs imgs btrfs_mntpoint_plain btrfs_mntpoint_encrypted
local loop loop_btrfs_plain loop_btrfs_encrypted
local dm_btrfs_encrypted encrypted_device
btrfs_mntpoint_plain="$(mktemp --directory "/tmp/test-repart.btrfs-mntpoint-plain.XXXXXXXXXX")"
btrfs_mntpoint_encrypted="$(mktemp --directory "/tmp/test-repart.btrfs-mntpoint-encrypted.XXXXXXXXXX")"
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
chmod 0755 "$defs"
truncate --size 500M "${imgs}/btrfs-plain"
mkfs.btrfs "${imgs}/btrfs-plain"
loop_btrfs_plain="$(losetup --show --find "$imgs/btrfs-plain")"
# shellcheck disable=SC2064
trap "losetup -d '${loop_btrfs_plain}'; rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
mount "${loop_btrfs_plain}" "${btrfs_mntpoint_plain}"
echo tada >"${btrfs_mntpoint_plain}/magic-plain"
# shellcheck disable=SC2064
trap "umount '${btrfs_mntpoint_plain}'; losetup -d '${loop_btrfs_plain}'; rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
truncate --size 500M "${imgs}/btrfs-encrypted"
mkfs.btrfs "${imgs}/btrfs-encrypted"
loop_btrfs_encrypted="$(losetup --show --find "$imgs/btrfs-encrypted")"
# shellcheck disable=SC2064
trap "losetup -d '${loop_btrfs_encrypted}'; umount '${btrfs_mntpoint_plain}'; losetup -d '${loop_btrfs_plain}'; rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
mount "${loop_btrfs_encrypted}" "${btrfs_mntpoint_encrypted}"
echo tada >"${btrfs_mntpoint_encrypted}/magic-encrypted"
# shellcheck disable=SC2064
trap "umount '${btrfs_mntpoint_encrypted}'; losetup -d '${loop_btrfs_encrypted}'; umount '${btrfs_mntpoint_plain}'; losetup -d '${loop_btrfs_plain}'; rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
truncate --size 2G "${imgs}/img"
tee "$defs/01-plain.conf" <<EOF
[Partition]
Type=linux-generic
Label=plain
BlockDeviceReplace=${btrfs_mntpoint_plain}
EOF
tee "$defs/02-encrypted.conf" <<EOF
[Partition]
Type=linux-generic
Label=encrypted
Encrypt=key-file
BlockDeviceReplace=${btrfs_mntpoint_encrypted}
VolumeName=btrfs-replace-encrypted
EOF
loop="$(losetup -P --show --find "${imgs}/img")"
# shellcheck disable=SC2064
trap "umount '${btrfs_mntpoint_encrypted}'; cryptsetup close btrfs-replace-encrypted || true; losetup -d '${loop_btrfs_encrypted}'; umount '${btrfs_mntpoint_plain}'; losetup -d '${loop_btrfs_plain}'; losetup -d '${loop}'; rm -rf '$defs' '$imgs' '$btrfs_mntpoint_plain' '$btrfs_mntpoint_encrypted'" RETURN
touch "${imgs}/empty-password"
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--empty=require \
--key-file="${imgs}/empty-password" \
--seed="$seed" \
--dry-run=no \
"${loop}"
assert_eq "$(findmnt "${btrfs_mntpoint_plain}" -o SOURCE -n)" "${loop}p1"
dm_btrfs_encrypted="$(findmnt "${btrfs_mntpoint_encrypted}" -o SOURCE -n)"
if [[ "$dm_btrfs_encrypted" != "/dev/mapper/btrfs-replace-encrypted" ]]; then
# When libdevmapper is built without UDEV_SYNC_SUPPORT (e.g. on Alpine/Nura),
# it creates a device node under /dev/mapper/ instead of relying on udev to create a symlink.
# In this case, verify that both device nodes refer to the same underlying device.
assert_eq "$(stat -c %Hr:%Lr "$dm_btrfs_encrypted")" "$(stat -c %Hr:%Lr /dev/mapper/btrfs-replace-encrypted)"
fi
encrypted_device="/sys/dev/block/$(dmsetup table /dev/mapper/btrfs-replace-encrypted | cut -d" " -f7)"
assert_eq "$(udevadm info --query=property --property=DEVNAME --value "${encrypted_device}")" "${loop}p2"
grep -q tada "${btrfs_mntpoint_plain}/magic-plain"
grep -q tada "${btrfs_mntpoint_encrypted}/magic-encrypted"
}
testcase_insert_into_gap() {
local defs imgs output
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
chmod 0755 "$defs"
echo "*** Inserting a new partition into a gap between two existing partitions ***"
truncate -s 71M "$imgs/gap.img"
sfdisk "$imgs/gap.img" <<EOF
label: gpt
size=10M, type=${esp_guid}, name="part-a",
start=60M, size=10M, type=${root_guid}, name="part-c",
EOF
tee "$defs/new.conf" <<EOF
[Partition]
Type=usr
Label=part-b
SizeMinBytes=10M
SizeMaxBytes=10M
EOF
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--dry-run=no \
"$imgs/gap.img"
output=$(sfdisk --dump "$imgs/gap.img")
assert_in "$imgs/gap.img1 : start= 2048, size= 20480," "$output"
assert_in "$imgs/gap.img2 : start= 122880, size= 20480," "$output"
# New partition B must start at the beginning of the gap (after A), not at
# the end (before C).
assert_in "$imgs/gap.img3 : start= 22528, size= 20480, type=$usr_guid, uuid=$usr_uuid, name=\"part-b\"" "$output"
}
testcase_distribute_leftover_space() {
local defs imgs output
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
chmod 0755 "$defs"
echo "*** Left over space after distributing according to weights should still be assigned ***"
tee "$defs/01-esp.conf" <<EOF
[Partition]
Type=esp
Weight=1
SizeMinBytes=5M
EOF
tee "$defs/02-usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
Weight=1000
SizeMinBytes=1M
SizeMaxBytes=2M
EOF
truncate -s 20M "$imgs/leftover.img"
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--dry-run=no \
--empty=allow \
"$imgs/leftover.img"
output=$(sfdisk --dump "$imgs/leftover.img")
# esp should get all the leftover space
assert_in "$imgs/leftover.img1 : start= 2048, size= 34776, type=$esp_guid," "$output"
assert_in "$imgs/leftover.img2 : start= 36824, size= 4096, type=$usr_guid," "$output"
rm "$defs/01-esp.conf"
rm "$defs/02-usr.conf"
rm "$imgs/leftover.img"
tee "$defs/01-xbootldr.conf" <<EOF
[Partition]
Type=xbootldr
SizeMaxBytes=10M
EOF
tee "$defs/02-usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
Weight=1000
SizeMinBytes=1M
SizeMaxBytes=2M
EOF
tee "$defs/03-esp.conf" <<EOF
[Partition]
Type=esp
Weight=1
SizeMinBytes=5M
EOF
truncate -s 25M "$imgs/leftover2.img"
sfdisk "$imgs/leftover2.img" <<EOF
label: gpt
size=3M, type=${xbootldr_guid},
EOF
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--dry-run=no \
--empty=allow \
"$imgs/leftover2.img"
output=$(sfdisk --dump "$imgs/leftover2.img")
# xbootldr should get the leftover space and grow to 10M, then esp should get the rest
assert_in "$imgs/leftover2.img1 : start= 2048, size= 20480, type=$xbootldr_guid," "$output"
assert_in "$imgs/leftover2.img2 : start= 22528, size= 4096, type=$usr_guid," "$output"
assert_in "$imgs/leftover2.img3 : start= 26624, size= 24536, type=$esp_guid," "$output"
}
testcase_partition_number_gap() {
local defs imgs image output before after
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs'" RETURN
chmod 0755 "$defs"
image="$imgs/gap.img"
tee "$defs/10-a.conf" <<EOF
[Partition]
Type=linux-generic
Label=a
SizeMinBytes=20M
SizeMaxBytes=20M
EOF
tee "$defs/20-b.conf" <<EOF
[Partition]
Type=linux-generic
Label=b
SizeMinBytes=10M
SizeMaxBytes=10M
EOF
# Initially create two partitions with consecutive partition numbers.
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--empty=create \
--size=64M \
--dry-run=no \
"$image"
output="$(sfdisk -d "$image")"
assert_in "${image}1 :" "$output"
assert_in "${image}2 :" "$output"
# Remove the first partition, leaving partition number 1 unused while
# partition number 2 remains occupied.
sfdisk --delete "$image" 1
output="$(sfdisk -d "$image")"
assert_not_in "${image}1 :" "$output"
assert_in "${image}2 :" "$output"
# Repart should append the new partition after the highest existing
# partition of the same type instead of filling the lower-numbered gap.
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--dry-run=no \
"$image"
output="$(sfdisk -d "$image")"
assert_not_in "${image}1 :" "$output"
assert_in "${image}2 : start= 43008, size= 40960, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4," "$output"
assert_in "${image}3 : start= 2048, size= 20480, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4," "$output"
# A subsequent invocation should not rematch the definitions and modify
# the partition table again.
before="$output"
systemd-repart --offline="$OFFLINE" \
--definitions="$defs" \
--seed="$seed" \
--dry-run=no \
"$image"
after="$(sfdisk -d "$image")"
assert_eq "$after" "$before"
}
OFFLINE="yes"
run_testcases
# Online image builds need loop devices so we can't run them in nspawn.
if ! systemd-detect-virt --container; then
OFFLINE="no"
run_testcases
fi
touch /testok