HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/units/TEST-58-REPART.sh

119,614 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: 2a6c369b38488296167b647fad74b8af7f70be19

Lines 1–1000 of 3,527

#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# shellcheck disable=SC2317
set -eux
set -o pipefail
 
if ! command -v systemd-repart >/dev/null; then
    echo "no systemd-repart" >/skipped
    exit 77
fi
 
# shellcheck source=test/units/test-control.sh
. "$(dirname "$0")"/test-control.sh
# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh
 
export SYSTEMD_LOG_LEVEL=debug
export PAGER=cat
 
# Disable use of special glyphs such as →
export SYSTEMD_UTF8=0
 
# Sanitizer runs are significantly slower, so give udevadm wait 3 times longer timeouts
if [[ -v ASAN_OPTIONS || -v UBSAN_OPTIONS ]]; then
    UDEVADM_WAIT_TIMEOUT=180
else
    UDEVADM_WAIT_TIMEOUT=60
fi
 
seed=750b6cd5c4ae4012a15e7be3c29e6a47
 
esp_guid=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
xbootldr_guid=BC13C2FF-59E6-4262-A352-B275FD6F7172
 
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"
else
    echo "Unexpected uname -m: ${machine} in TEST-58-REPART.sh, please fix me"
    exit 1
fi
 
testcase_cow() {
    local attrs cow_image default_nocow defs image imgs nocow_image probe
 
    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
 
    # Skip the checks entirely if the underlying filesystem does not support the attribute.
    if ! chattr -C "$imgs"; then
        echo "NOCOW is not supported on $imgs, skipping tests"
        return
    fi
 
    probe="$imgs/probe"
    touch "$probe"
    if ! chattr +C "$probe"; then
        echo "NOCOW is not supported on $imgs, skipping tests"
        return
    fi
    if ! chattr -C "$probe"; then
        echo "COW is not supported on $imgs, skipping tests"
        return
    fi
    rm "$probe"
 
    chattr +C "$imgs"
 
    image="$imgs/inherit-nocow.raw"
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=16M \
                   --cow=auto \
                   --dry-run=no \
                   "$image"
 
    attrs="$(lsattr -d -- "$image")"
    assert_neq "$attrs" ""
    read -r attrs _ <<<"$attrs"
    assert_in "C" "$attrs"
 
    cow_image="$imgs/cow.raw"
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=16M \
                   --cow=yes \
                   --dry-run=no \
                   "$cow_image"
 
    attrs="$(lsattr -d -- "$cow_image")"
    assert_neq "$attrs" ""
    read -r attrs _ <<<"$attrs"
    assert_not_in "C" "$attrs"
 
    chattr -C "$imgs"
 
    probe="$imgs/probe"
    touch "$probe"
    attrs="$(lsattr -d -- "$probe")"
    assert_neq "$attrs" ""
    read -r attrs _ <<<"$attrs"
    if [[ "$attrs" == *C* ]]; then
        default_nocow=1
    else
        default_nocow=0
    fi
    rm "$probe"
 
    image="$imgs/inherit-cow.raw"
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=16M \
                   --dry-run=no \
                   "$image"
 
    attrs="$(lsattr -d -- "$image")"
    assert_neq "$attrs" ""
    read -r attrs _ <<<"$attrs"
    if (( default_nocow )); then
        assert_in "C" "$attrs"
    else
        assert_not_in "C" "$attrs"
    fi
 
    nocow_image="$imgs/nocow.raw"
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=16M \
                   --cow=no \
                   --dry-run=no \
                   "$nocow_image"
 
    attrs="$(lsattr -d -- "$nocow_image")"
    assert_neq "$attrs" ""
    read -r attrs _ <<<"$attrs"
    assert_in "C" "$attrs"
}
 
testcase_basic() {
    local defs imgs output
    local loop volume
 
    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 "*** 1. create an empty image ***"
 
    systemd-repart --offline="$OFFLINE" \
                   --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"
 
    echo "*** 2. Testing with root, root2, home, and swap ***"
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root
Format=vfat
EOF
 
    ln -s root.conf "$defs/root2.conf"
 
    tee "$defs/home.conf" <<EOF
[Partition]
Type=home
Label=home-first
Label=home-always-too-long-xxxxxxxxxxxxxx-%v
Format=vfat
EOF
 
    tee "$defs/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
PaddingMinBytes=92M
EOF
 
    systemd-repart --definitions="$defs" \
                   -n \
                   --seed="$seed" \
                   --include-partitions=home,swap \
                   "-"
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --dry-run=no \
                   --seed="$seed" \
                   --include-partitions=home,swap \
                   --offline="$OFFLINE" \
                   "$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=     1775576, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/zzz2 : start=     1777624, size=      131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=50M \
                   --seed="$seed" \
                   --include-partitions=root,home \
                   "$imgs/qqq"
 
    sfdisk -d "$imgs/qqq" | grep -v -e 'sector-size' -e '^$'
 
    systemd-repart --offline="$OFFLINE" \
                   --empty=create \
                   --size=1G \
                   --dry-run=no \
                   --seed="$seed" \
                   --definitions "" \
                   --copy-from="$imgs/qqq" \
                   --copy-from="$imgs/qqq" \
                   "$imgs/copy"
 
    output=$(sfdisk -d "$imgs/copy" | grep -v -e 'sector-size' -e '^$')
 
    assert_eq "$output" "label: gpt
label-id: 1D2CE291-7CCE-4F7D-BC83-FDB49AD74EBD
device: $imgs/copy
unit: sectors
first-lba: 2048
last-lba: 2097118
$imgs/copy1 : start=        2048, size=       33432, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/copy2 : start=       35480, size=       33440, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/copy3 : start=       68920, size=       33440, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\"
$imgs/copy4 : start=      102360, size=       33432, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915, uuid=4980595D-D74A-483A-AA9E-9903879A0EE5, name=\"home-first\", attrs=\"GUID:59\"
$imgs/copy5 : start=      135792, size=       33440, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"
$imgs/copy6 : start=      169232, size=       33440, type=${root_guid}, uuid=${root_uuid2}, name=\"root-${architecture}-2\", attrs=\"GUID:59\""
 
    rm "$imgs/qqq" "$imgs/copy" # Save disk space
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --dry-run=no \
                   --seed="$seed" \
                   --empty=force \
                   --defer-partitions=home,root \
                   "$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/zzz4 : start=     1777624, size=      131072, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=78C92DB8-3D2B-4823-B0DC-792B78F66F1E, name=\"swap\""
 
    systemd-repart --offline="$OFFLINE" \
                   --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\""
 
    echo "*** 3. Testing with root, root2, home, swap, and another partition ***"
 
    tee "$defs/swap.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
EOF
 
    tee "$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 --offline="$OFFLINE" \
                   --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\""
 
    echo "*** 4. Resizing to 2G ***"
 
    systemd-repart --offline="$OFFLINE" \
                   --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\""
 
    echo "*** 5. Testing with root, root2, home, swap, another partition, and partition copy ***"
 
    dd if=/dev/urandom of="$imgs/block-copy" bs=4096 count=10240
 
    tee "$defs/extra2.conf" <<EOF
[Partition]
Type=linux-generic
Label=block-copy
UUID=2a1d97e1d0a346cca26eadc643926617
CopyBlocks=$imgs/block-copy
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --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"
 
    echo "*** 6. Testing Format=/Encrypt=/CopyFiles= ***"
 
    tee "$defs/extra3.conf" <<EOF
[Partition]
Type=srv
Label=luks-format-copy
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Format=ext4
Encrypt=yes
EncryptKDF=pbkdf2
CopyFiles=$defs:/def
SizeMinBytes=48M
VolumeLabel=schrupfel
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --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: 6422487
$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=      131072, type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name=\"luks-format-copy\", attrs=\"GUID:59\""
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping encrypt mount tests in container."
        return
    fi
 
    loop="$(losetup -P --show --find "$imgs/zzz")"
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p7"
 
    cryptsetup luksDump "${loop}p7" | grep 'Flags:[[:space:]]*allow-discards' >/dev/null
 
    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"
 
    # Validate that the VolumeLabel= had the desired effect
    PASSWORD="" systemd-dissect "$imgs/zzz" -M "$imgs/mount"
    udevadm info /dev/disk/by-label/schrupfel | grep ID_FS_TYPE=crypto_LUKS >/dev/null
    systemd-dissect -U "$imgs/mount"
 
    echo "*** 7. Testing Discard=no ***"
 
    tee "$defs/extra4.conf" <<EOF
[Partition]
Type=var
Label=luks-no-discards
UUID=329b9db2-dfd9-4f39-8ebf-53b582b05fcd
Format=ext4
Encrypt=yes
CopyFiles=$defs:/def
SizeMinBytes=48M
Discard=no
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --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: 6553559
$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=      131072, type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8, uuid=7B93D1F2-595D-4CE3-B0B9-837FBD9E63B0, name=\"luks-format-copy\", attrs=\"GUID:59\"
$imgs/zzz8 : start=     6422488, size=      131072, type=4D21B016-B534-45C2-A9FB-5C16E091FD2D, uuid=329B9DB2-DFD9-4F39-8EBF-53B582B05FCD, name=\"luks-no-discards\", attrs=\"GUID:59\""
 
    loop="$(losetup -P --show --find "$imgs/zzz")"
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p8"
 
    cryptsetup luksDump "${loop}p8" | grep 'Flags:[[:space:]]*(no flags)' >/dev/null
    losetup -d "$loop"
}
 
testcase_empty_force_block_device() {
    local defs imgs loop before partition_fd
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping block device overwrite tests in container."
        return
    fi
 
    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
 
    # Repart enforces a 32M minimum for ext4, even if SizeMaxBytes= is smaller.
    tee "$defs/10-data.conf" <<EOF
[Partition]
Type=linux-generic
Label=original
Format=ext4
SizeMinBytes=32M
SizeMaxBytes=32M
EOF
 
    tee "$defs/20-extra.conf" <<EOF
[Partition]
Type=linux-generic
Label=extra
SizeMinBytes=32M
SizeMaxBytes=32M
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --empty=create \
                   --size=128M \
                   --dry-run=no \
                   "$imgs/force.img"
 
    loop="$(losetup --partscan --show --find "$imgs/force.img")"
    # shellcheck disable=SC2064
    trap "losetup -d '$loop'; rm -rf '$defs' '$imgs'" RETURN ERR
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1" "${loop:?}p2"
 
    # Test dry run mode
    before="$(sfdisk -d "$loop")"
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --empty=force \
                   --discard=no \
                   --dry-run=yes \
                   "$loop"
 
    assert_eq "$(sfdisk -d "$loop")" "$before"
    assert_eq "$(blockdev --getsize64 "${loop}p1")" "$((32 * 1024 * 1024))"
    assert_eq "$(blockdev --getsize64 "${loop}p2")" "$((32 * 1024 * 1024))"
 
    # An open partition prevents the initial BLKRRPART, but must not prevent an offline overwrite.
    if [[ "$OFFLINE" == yes ]]; then
        exec {partition_fd}<"${loop}p2"
    fi
 
    # Recreating even the same layout must not collide with the old kernel partition devices.
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --empty=force \
                   --discard=no \
                   --dry-run=no \
                   "$loop" 2>&1 | tee "$imgs/recreate.log"
 
    if [[ "$OFFLINE" == yes ]]; then
        exec {partition_fd}<&-
        if built_with_musl; then
            grep -F "Failed to reread partition table after wiping, ignoring: Resource busy" "$imgs/recreate.log"
        else
            grep -F "Failed to reread partition table after wiping, ignoring: Device or resource busy" "$imgs/recreate.log"
        fi
    fi
 
    assert_eq "$(grep -Fc 'Informing kernel about changed partitions...' "$imgs/recreate.log")" 1
 
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop}p1" "${loop}p2"
    assert_eq "$(sfdisk -d "$loop")" "$before"
    assert_eq "$(blkid -p -s TYPE -o value "${loop}p1")" ext4
    assert_eq "$(blkid -p -s LABEL -o value "${loop}p1")" original
 
    # Also replace the layout with a partition overlapping both old partitions.
    rm "$defs/20-extra.conf"
    sed -i -e 's/Label=original/Label=replacement/' -e 's/32M/64M/g' "$defs/10-data.conf"
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --empty=force \
                   --discard=no \
                   --dry-run=no \
                   "$loop"
 
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop}p1"
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --removed --settle "${loop}p2"
    assert_eq "$(blockdev --getsize64 "${loop}p1")" "$((64 * 1024 * 1024))"
    assert_eq "$(blkid -p -s TYPE -o value "${loop}p1")" ext4
    assert_eq "$(blkid -p -s LABEL -o value "${loop}p1")" replacement
    assert_not_in "${loop}p2 :" "$(sfdisk -d "$loop")"
}
 
testcase_copy_from_grain_padding() {
    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"
 
    truncate -s 80MiB "$imgs/copy_from"
    sfdisk "$imgs/copy_from" <<EOF
label: gpt
grain: 4194304
 
size=40961, type=${root_guid}, uuid=837c3d67-21b3-478e-be82-7e7f83bf96d3
size=10240, type=${xbootldr_guid}, uuid=4985c03e-eecb-4fe0-9f65-3f6345782214
EOF
 
    output=$(sfdisk --dump "$imgs/copy_from")
 
    # Padding between partition 1 and 2 is 8191 sectors
    assert_in "$imgs/copy_from1 : start=        8192, size=       40961, type=${root_guid}," "$output"
    assert_in "$imgs/copy_from2 : start=       57344, size=       10240, type=${xbootldr_guid}," "$output"
 
    truncate -s 100MiB "$imgs/copy_to"
    output=$(systemd-repart --offline="$OFFLINE" \
                            --empty=allow \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --json=pretty \
                            --copy-from="$imgs/copy_from" \
                            --grain-size=512 \
                            "$imgs/copy_to")
 
    output=$(sfdisk --dump "$imgs/copy_to")
 
    assert_in "$imgs/copy_to1 : start=        2048, size=       40961, type=${root_guid}," "$output"
    # We set new grain-size to 1 sector, so padding now should be 8191 sectors again
    assert_in "$imgs/copy_to2 : start=       51200, size=       10240, type=${xbootldr_guid}," "$output"
}
 
testcase_copy_from_respects_new_grain() {
    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"
 
    truncate -s 80MiB "$imgs/copy_from"
    sfdisk "$imgs/copy_from" <<EOF
label: gpt
 
size=20480, type=${root_guid}, uuid=837c3d67-21b3-478e-be82-7e7f83bf96d3
size=10240, type=${xbootldr_guid}, uuid=4985c03e-eecb-4fe0-9f65-3f6345782214
EOF
 
    truncate -s 100MiB "$imgs/copy_to"
    output=$(systemd-repart --offline="$OFFLINE" \
                            --empty=allow \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --json=pretty \
                            --copy-from="$imgs/copy_from" \
                            --grain-size=4194304 \
                            "$imgs/copy_to")
 
    output=$(sfdisk --dump "$imgs/copy_to")
 
    assert_in "$imgs/copy_to1 : start=        8192, size=       24576, type=${root_guid}," "$output"
    assert_in "$imgs/copy_to2 : start=       32768, size=       16384, type=${xbootldr_guid}," "$output"
}
 
testcase_copy_from_no_padding_at_beginning_and_end() {
    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"
 
    tee "$defs/03-esp.conf" <<EOF
[Partition]
Type=esp
EOF
 
    truncate -s 80MiB "$imgs/copy_from"
    sfdisk "$imgs/copy_from" <<EOF
label: gpt
 
start=2280, size=20480, type=${root_guid}, uuid=837c3d67-21b3-478e-be82-7e7f83bf96d3
start=22760, size=10240, type=${xbootldr_guid}, uuid=4985c03e-eecb-4fe0-9f65-3f6345782214
EOF
 
    truncate -s 60MiB "$imgs/copy_to"
    output=$(systemd-repart --offline="$OFFLINE" \
                            --empty=allow \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --json=pretty \
                            --copy-from="$imgs/copy_from" \
                            "$imgs/copy_to")
 
    output=$(sfdisk --dump "$imgs/copy_to")
 
    assert_in "first-lba: 2048" "$output"
    assert_in "last-lba: 122846" "$output"
    assert_in "$imgs/copy_to1 : start=        2048, size=       20480, type=${root_guid}," "$output"
    assert_in "$imgs/copy_to2 : start=       22528, size=       10240, type=${xbootldr_guid}," "$output"
    assert_in "$imgs/copy_to3 : start=       32768, size=       90072, type=${esp_guid}," "$output"
}
 
testcase_size_auto_with_grain_size() {
    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"
 
    tee "$defs/01-esp.conf" <<EOF
[Partition]
Type=esp
SizeMinBytes=10M
EOF
 
    tee "$defs/02-usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
SizeMinBytes=10M
EOF
 
    tee "$defs/03-root.conf" <<EOF
[Partition]
Type=root-${architecture}
SizeMinBytes=10M
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --empty=create \
                   --size=auto \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   --grain-size=2097152 \
                   "$imgs/auto"
 
    output=$(sfdisk --dump "$imgs/auto")
 
    assert_in "first-lba: 2048" "$output"
    assert_in "last-lba: 65535" "$output"
    assert_in "$imgs/auto1 : start=        4096, size=       20480, type=${esp_guid}," "$output"
    assert_in "$imgs/auto2 : start=       24576, size=       20480, type=${usr_guid}," "$output"
    assert_in "$imgs/auto3 : start=       45056, size=       20480, type=${root_guid}," "$output"
}
 
testcase_size_auto_foreign_partitions() {
    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 "*** Adding a partition with --size=auto to an image with foreign partitions ***"
 
    # --size=auto reloads the partition table, which must not lose the UUID and label of the existing
    # partitions that have no definition.
    truncate -s 12M "$imgs/foreign.img"
    sfdisk "$imgs/foreign.img" <<EOF
label: gpt
size=10M, type=${esp_guid}, uuid=${root_uuid2}, name="foreign",
EOF
 
    tee "$defs/usr.conf" <<EOF
[Partition]
Type=usr
Label=new
SizeMinBytes=10M
SizeMaxBytes=10M
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --size=auto \
                   --dry-run=no \
                   "$imgs/foreign.img"
 
    output=$(sfdisk --dump "$imgs/foreign.img")
 
    assert_in "$imgs/foreign.img1 : start=        2048, size=       20480, type=${esp_guid}, uuid=${root_uuid2}, name=\"foreign\"" "$output"
    assert_in "$imgs/foreign.img2 : start=       22528, size=       20480, type=${usr_guid}, uuid=${usr_uuid}, name=\"new\"" "$output"
}
 
testcase_dropin() {
    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"
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=64M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
EOF
 
    mkdir -p "$defs/root.conf.d"
    tee "$defs/root.conf.d/override1.conf" <<EOF
[Partition]
Label=label1
SizeMaxBytes=32M
EOF
 
    tee "$defs/root.conf.d/override2.conf" <<EOF
[Partition]
Label=label2
EOF
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --empty=create \
                            --size=100M \
                            --json=pretty \
                            "$imgs/zzz")
 
    diff -u - <<EOF <(echo "$output")
[
	{
		"type" : "swap",
		"label" : "label2",
		"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
		"partno" : 0,
		"file" : "$defs/root.conf",
		"node" : "$imgs/zzz1",
		"offset" : 1048576,
		"old_size" : 0,
		"raw_size" : 33554432,
		"size" : "-> 32M",
		"old_padding" : 0,
		"raw_padding" : 70234112,
		"padding" : "-> 66.9M",
		"activity" : "create",
		"drop-in_files" : [
			"$defs/root.conf.d/override1.conf",
			"$defs/root.conf.d/override2.conf"
		]
	}
]
EOF
}
 
testcase_multiple_definitions() {
    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"
 
    mkdir -p "$defs/1"
    tee "$defs/1/root1.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
Label=label1
EOF
 
    mkdir -p "$defs/2"
    tee "$defs/2/root2.conf" <<EOF
[Partition]
Type=swap
SizeMaxBytes=32M
UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
Label=label2
EOF
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs/1" \
                            --definitions="$defs/2" \
                            --empty=create \
                            --size=100M \
                            --json=pretty \
                            "$imgs/zzz")
 
    diff -u - <<EOF <(echo "$output")
[
	{
		"type" : "swap",
		"label" : "label1",
		"uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
		"partno" : 0,
		"file" : "$defs/1/root1.conf",
		"node" : "$imgs/zzz1",
		"offset" : 1048576,
		"old_size" : 0,
		"raw_size" : 33554432,
		"size" : "-> 32M",
		"old_padding" : 0,
		"raw_padding" : 0,
		"padding" : "-> 0B",
		"activity" : "create"
	},
	{
		"type" : "swap",
		"label" : "label2",
		"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
		"partno" : 1,
		"file" : "$defs/2/root2.conf",
		"node" : "$imgs/zzz2",
		"offset" : 34603008,
		"old_size" : 0,
		"raw_size" : 33554432,
		"size" : "-> 32M",
		"old_padding" : 0,
		"raw_padding" : 36679680,
		"padding" : "-> 34.9M",
		"activity" : "create"
	}
]
EOF
}