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 1001–2000 of 3,527

testcase_copy_blocks() {
    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 "*** First, create a disk image and verify its in order ***"
 
    tee "$defs/esp.conf" <<EOF
[Partition]
Type=esp
SizeMinBytes=10M
Format=vfat
EOF
 
    tee "$defs/usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
SizeMinBytes=10M
Format=ext4
ReadOnly=yes
EOF
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root-${architecture}
SizeMinBytes=10M
Format=ext4
MakeDirectories=/usr /efi
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=auto \
                   --seed="$seed" \
                   "$imgs/zzz"
 
    output=$(sfdisk --dump "$imgs/zzz")
 
    assert_in "$imgs/zzz1 : start=        2048, size=      532480, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=39107B09-615D-48FB-BA37-C663885FCE67, name=\"esp\"" "$output"
    assert_in "$imgs/zzz2 : start=      534528, size=       65536, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"" "$output"
    assert_in "$imgs/zzz3 : start=      600064, size=       65536, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:60\"" "$output"
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping second part of copy blocks tests in container."
        return
    fi
 
    echo "*** Second, create another image with CopyBlocks=auto ***"
 
    tee "$defs/esp.conf" <<EOF
[Partition]
Type=esp
CopyBlocks=auto
EOF
 
    tee "$defs/usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
ReadOnly=yes
CopyBlocks=auto
EOF
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyBlocks=auto
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --empty=create \
                   --size=auto \
                   --seed="$seed" \
                   --image="$imgs/zzz" \
                   "$imgs/yyy"
 
    cmp "$imgs/zzz" "$imgs/yyy"
}
 
testcase_unaligned_partition() {
    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 "*** Operate on an image with unaligned partition ***"
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root-${architecture}
EOF
 
    truncate -s 10g "$imgs/unaligned"
    sfdisk "$imgs/unaligned" <<EOF
label: gpt
 
start=2048, size=69044
start=71092, size=3591848
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   "$imgs/unaligned"
 
    output=$(sfdisk --dump "$imgs/unaligned")
 
    assert_in "$imgs/unaligned1 : start=        2048, size=       69044," "$output"
    assert_in "$imgs/unaligned2 : start=       71092, size=     3591848," "$output"
    assert_in "$imgs/unaligned3 : start=     3662944, size=    17308536, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\", attrs=\"GUID:59\"" "$output"
}
 
testcase_output_order() {
    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 "*** Ensure the order of the partition list is correct ***"
 
    # make this one min size 20MiB so that it has to be assigned slot 4
    tee "$defs/01-home.conf" <<EOF
[Partition]
Type=home
SizeMinBytes=20971520
EOF
 
    tee "$defs/02-swap.conf" <<EOF
[Partition]
Type=swap
EOF
 
    tee "$defs/03-esp.conf" <<EOF
[Partition]
Type=esp
EOF
 
    tee "$defs/04-root.conf" <<EOF
[Partition]
Type=root-${architecture}
EOF
 
    truncate -s 80MiB "$imgs/order"
    sfdisk "$imgs/order" <<EOF
label: gpt
 
size=10M, type=${root_guid}, uuid=837c3d67-21b3-478e-be82-7e7f83bf96d3
size=5M, type=${xbootldr_guid}, uuid=4985c03e-eecb-4fe0-9f65-3f6345782214
start=30M, size=10M, type=${esp_guid}, uuid=91c30bc9-0187-4db6-81a2-c648294197f8
EOF
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --json=pretty \
                            "$imgs/order")
 
    diff -u - <<EOF <(echo "$output")
[
	{
		"type" : "root-$architecture",
		"label" : "root-$architecture",
		"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
		"partno" : 0,
		"file" : "$defs/04-root.conf",
		"node" : "$imgs/order1",
		"offset" : 1048576,
		"old_size" : 10485760,
		"raw_size" : 10485760,
		"size" : "10M",
		"old_padding" : 0,
		"raw_padding" : 0,
		"padding" : "0B",
		"activity" : "unchanged"
	},
	{
		"type" : "xbootldr",
		"label" : "xbootldr",
		"uuid" : "4985c03e-eecb-4fe0-9f65-3f6345782214",
		"partno" : 1,
		"file" : null,
		"node" : "$imgs/order2",
		"offset" : 11534336,
		"old_size" : 5242880,
		"raw_size" : 5242880,
		"size" : "5M",
		"old_padding" : 14680064,
		"raw_padding" : 0,
		"padding" : "14M -> 0B",
		"activity" : "unchanged"
	},
	{
		"type" : "swap",
		"label" : "swap",
		"uuid" : "78c92db8-3d2b-4823-b0dc-792b78f66f1e",
		"partno" : 3,
		"file" : "$defs/02-swap.conf",
		"node" : "$imgs/order4",
		"offset" : 16777216,
		"old_size" : 0,
		"raw_size" : 14680064,
		"size" : "-> 14M",
		"old_padding" : 0,
		"raw_padding" : 0,
		"padding" : "-> 0B",
		"activity" : "create"
	},
	{
		"type" : "esp",
		"label" : "esp",
		"uuid" : "91c30bc9-0187-4db6-81a2-c648294197f8",
		"partno" : 2,
		"file" : "$defs/03-esp.conf",
		"node" : "$imgs/order3",
		"offset" : 31457280,
		"old_size" : 10485760,
		"raw_size" : 26202112,
		"size" : "10M -> 24.9M",
		"old_padding" : 41922560,
		"raw_padding" : 0,
		"padding" : "39.9M -> 0B",
		"activity" : "resize"
	},
	{
		"type" : "home",
		"label" : "home",
		"uuid" : "4980595d-d74a-483a-aa9e-9903879a0ee5",
		"partno" : 4,
		"file" : "$defs/01-home.conf",
		"node" : "$imgs/order5",
		"offset" : 57659392,
		"old_size" : 0,
		"raw_size" : 26206208,
		"size" : "-> 24.9M",
		"old_padding" : 0,
		"raw_padding" : 0,
		"padding" : "-> 0B",
		"activity" : "create"
	}
]
EOF
 
    output=$(sfdisk --dump "$imgs/order")
 
    assert_in "$imgs/order1 : start=        2048, size=       20480, type=${root_guid}," "$output"
    assert_in "$imgs/order2 : start=       22528, size=       10240, type=${xbootldr_guid}," "$output"
    assert_in "$imgs/order3 : start=       61440, size=       51176, type=${esp_guid}," "$output"
    assert_in "$imgs/order4 : start=       32768, size=       28672, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F," "$output"
    assert_in "$imgs/order5 : start=      112616, size=       51184, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915," "$output"
}
 
testcase_issue_21817() {
    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 "*** testcase for #21817 ***"
 
    tee "$defs/test.conf" <<EOF
[Partition]
Type=root
EOF
 
    truncate -s 100m "$imgs/21817.img"
    sfdisk "$imgs/21817.img" <<EOF
label: gpt
 
size=50M, type=${root_guid}
,
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --pretty=yes \
                   --definitions "$imgs" \
                   --seed="$seed" \
                   --dry-run=no \
                   "$imgs/21817.img"
 
    output=$(sfdisk --dump "$imgs/21817.img")
 
    assert_in "$imgs/21817.img1 : start=        2048, size=      102400, type=${root_guid}," "$output"
    # Accept both unpadded (pre-v2.38 util-linux) and padded (v2.38+ util-linux) sizes
    assert_in "$imgs/21817.img2 : start=      104448, size=      (100319| 98304)," "$output"
}
 
testcase_issue_24553() {
    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 "*** testcase for #24553 ***"
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root
SizeMinBytes=10G
SizeMaxBytes=120G
EOF
 
    tee "$imgs/partscript" <<EOF
label: gpt
label-id: C9FFE979-A415-C449-B729-78C7AA664B10
unit: sectors
first-lba: 40
 
start=40, size=524288, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=F2E89C8A-DC5D-4C4C-A29C-6CFB643B74FD, name="ESP System Partition"
start=524328, size=14848000, type=${root_guid}, uuid=${root_uuid}, name="root-${architecture}"
EOF
 
    echo "*** 1. Operate on a small image compared with SizeMinBytes= ***"
    truncate -s 8g "$imgs/zzz"
    sfdisk "$imgs/zzz" <"$imgs/partscript"
 
    # This should fail, but not trigger assertions.
    assert_rc 1 systemd-repart --offline="$OFFLINE" \
                               --definitions="$defs" \
                               --seed="$seed" \
                               --dry-run=no \
                               "$imgs/zzz"
 
    output=$(sfdisk --dump "$imgs/zzz")
    assert_in "$imgs/zzz2 : start=      524328, size=    14848000, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
 
    echo "*** 2. Operate on an larger image compared with SizeMinBytes= ***"
    rm -f "$imgs/zzz"
    truncate -s 12g "$imgs/zzz"
    sfdisk "$imgs/zzz" <"$imgs/partscript"
 
    # This should succeed.
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   "$imgs/zzz"
 
    output=$(sfdisk --dump "$imgs/zzz")
    assert_in "$imgs/zzz2 : start=      524328, size=    24641456, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
 
    echo "*** 3. Multiple partitions with Priority= (small disk) ***"
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root
SizeMinBytes=10G
SizeMaxBytes=120G
Priority=100
EOF
 
    tee "$defs/usr.conf" <<EOF
[Partition]
Type=usr
SizeMinBytes=10M
Priority=10
EOF
 
    rm -f "$imgs/zzz"
    truncate -s 8g "$imgs/zzz"
    sfdisk "$imgs/zzz" <"$imgs/partscript"
 
    # This should also succeed, but root is not extended.
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   "$imgs/zzz"
 
    output=$(sfdisk --dump "$imgs/zzz")
    assert_in "$imgs/zzz2 : start=      524328, size=    14848000, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
    assert_in "$imgs/zzz3 : start=    15372328, size=     1404848, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:59\"" "$output"
 
    echo "*** 4. Multiple partitions with Priority= (large disk) ***"
    rm -f "$imgs/zzz"
    truncate -s 12g "$imgs/zzz"
    sfdisk "$imgs/zzz" <"$imgs/partscript"
 
    # This should also succeed, and root is extended.
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   "$imgs/zzz"
 
    output=$(sfdisk --dump "$imgs/zzz")
    assert_in "$imgs/zzz2 : start=      524328, size=    20971520, type=${root_guid}, uuid=${root_uuid}, name=\"root-${architecture}\"" "$output"
    assert_in "$imgs/zzz3 : start=    21495848, size=     3669936, type=${usr_guid}, uuid=${usr_uuid}, name=\"usr-${architecture}\", attrs=\"GUID:59\"" "$output"
}
 
testcase_zero_uuid() {
    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 "*** Test image with zero UUID ***"
 
    tee "$defs/root.conf" <<EOF
[Partition]
Type=root-${architecture}
UUID=null
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   --empty=create \
                   --size=auto \
                   "$imgs/zero"
 
    output=$(sfdisk --dump "$imgs/zero")
 
    assert_in "$imgs/zero1 : start=        2048, size=       20480, type=${root_guid}, uuid=00000000-0000-0000-0000-000000000000" "$output"
}
 
testcase_verity() {
    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 "*** dm-verity ***"
 
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Minimize=guess
EOF
 
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
Minimize=yes
EOF
 
    tee "$defs/verity-sig.conf" <<EOF
[Partition]
Type=root-${architecture}-verity-sig
Verity=signature
VerityMatchKey=root
EOF
 
    # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
    tee >"$defs/verity.openssl.cnf" <<EOF
[ req ]
prompt = no
distinguished_name = req_distinguished_name
 
[ req_distinguished_name ]
C = DE
ST = Test State
L = Test Locality
O = Org Name
OU = Org Unit Name
CN = Common Name
emailAddress = test@email.com
EOF
 
    openssl req \
            -config "$defs/verity.openssl.cnf" \
            -new -x509 \
            -newkey rsa:1024 \
            -keyout "$defs/verity.key" \
            -out "$defs/verity.crt" \
            -days 365 \
            -nodes
 
    mkdir -p /run/verity.d
    ln -sf "$defs/verity.crt" /run/verity.d/ok.crt
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --empty=create \
                            --size=auto \
                            --split=yes \
                            --json=pretty \
                            --private-key="$defs/verity.key" \
                            --certificate="$defs/verity.crt" \
                            "$imgs/verity")
 
    drh=$(jq -r ".[] | select(.type == \"root-${architecture}\") | .roothash" <<<"$output")
    hrh=$(jq -r ".[] | select(.type == \"root-${architecture}-verity\") | .roothash" <<<"$output")
    srh=$(jq -r ".[] | select(.type == \"root-${architecture}-verity-sig\") | .roothash" <<<"$output")
 
    assert_eq "$drh" "$hrh"
    assert_eq "$hrh" "$srh"
 
    # The split-out verity signature file should be a valid JSON document (i.e. trailing NUL padding
    # from the on-disk partition must be trimmed when writing the split file).
    sig_split=$(jq -r ".[] | select(.type == \"root-${architecture}-verity-sig\") | .split_path" <<<"$output")
    assert_neq "$sig_split" ""
    assert_neq "$sig_split" "null"
    jq . "$sig_split" >/dev/null
 
    # Check that offline signing works and the resulting image is valid
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --empty=create \
                            --size=auto \
                            --json=pretty \
                            --defer-partitions=root-${architecture}-verity-sig \
                            "$imgs/offline")
 
    offline_drh=$(jq -r ".[] | select(.type == \"root-${architecture}\") | .roothash" <<<"$output")
 
    echo -n "$offline_drh" | \
        openssl smime -sign -in /dev/stdin \
                      -inkey "$defs/verity.key" \
                      -signer "$defs/verity.crt" \
                      -noattr -binary -outform der \
                      -out "$imgs/offline.roothash.p7s"
 
    systemd-repart --offline "$OFFLINE" \
                   --definitions "$defs" \
                   --dry-run no \
                   --join-signature "$offline_drh:$imgs/offline.roothash.p7s" \
                   --certificate "$defs/verity.crt" \
                   "$imgs/offline"
 
    # Check that we can dissect, mount and unmount a repart verity image. (and that the image UUID is deterministic)
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping verity test dissect part in container."
        return
    fi
 
    systemd-dissect "$imgs/verity" --root-hash "$drh"
    systemd-dissect "$imgs/verity" --root-hash "$drh" --json=short | grep '"imageUuid":"1d2ce291-7cce-4f7d-bc83-fdb49ad74ebd"' >/dev/null
    systemd-dissect "$imgs/verity" --root-hash "$drh" -M "$imgs/mnt"
    systemd-dissect -U "$imgs/mnt"
 
    systemd-dissect "$imgs/offline" --root-hash "$offline_drh"
    systemd-dissect "$imgs/offline" --root-hash "$offline_drh" --json=short | grep '"imageUuid":"1d2ce291-7cce-4f7d-bc83-fdb49ad74ebd"' >/dev/null
    systemd-dissect "$imgs/offline" --root-hash "$offline_drh" -M "$imgs/mnt"
    systemd-dissect -U "$imgs/mnt"
}
 
testcase_verity_explicit_block_size() {
    local defs imgs loop
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping verity block size 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
    chmod 0755 "$defs"
 
    echo "*** varying-dm-verity-block-sizes ***"
 
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Minimize=guess
EOF
 
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
VerityHashBlockSizeBytes=1024
VerityDataBlockSizeBytes=4096
Minimize=yes
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   --empty=create \
                   --size=auto \
                   --json=pretty \
                   "$imgs/verity"
 
    loop="$(losetup --partscan --show --find "$imgs/verity")"
 
    # Make sure the loopback device gets cleaned up
    # shellcheck disable=SC2064
    trap "rm -rf '$defs' '$imgs' ; losetup -d '$loop'" RETURN ERR
 
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1" "${loop:?}p2"
 
    # Check that the verity block sizes are as expected
    veritysetup dump "${loop}p2" | grep 'Data block size:' | grep '4096' >/dev/null
    veritysetup dump "${loop}p2" | grep 'Hash block size:' | grep '1024' >/dev/null
}
 
testcase_verity_hash_size_from_data_size() {
    local defs imgs loop
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping verity hash size from data size test 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
    chmod 0755 "$defs"
 
    echo "*** dm-verity-hash-size-from-data-size ***"
 
    # create minimized data partition with SizeMaxBytes=
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Minimize=guess
SizeMaxBytes=10G
EOF
 
    # create hash partition, its size will be derived from SizeMaxBytes= of the data partition
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
VerityHashBlockSizeBytes=4096
VerityDataBlockSizeBytes=4096
EOF
 
    systemd-repart --offline="$OFFLINE" \
                   --definitions="$defs" \
                   --seed="$seed" \
                   --dry-run=no \
                   --empty=create \
                   --size=auto \
                   --json=pretty \
                   "$imgs/verity"
 
    loop="$(losetup --partscan --show --find "$imgs/verity")"
 
    # Make sure the loopback device gets cleaned up
    # shellcheck disable=SC2064
    trap "rm -rf '$defs' '$imgs' ; losetup -d '$loop'" RETURN ERR
 
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1" "${loop:?}p2"
 
    output=$(sfdisk -J "$loop")
 
    # size of the hash partition, as determined by calculate_verity_hash_size()
    # for 10GiB data partition and hash / data block size of 4096B
    hash_bytes=84557824
    hash_sectors_expected=$((hash_bytes / 512))
 
    hash_sectors_actual=$(jq -r ".partitiontable.partitions | map(select(.name == \"root-${architecture}-verity\")) | .[].size" <<<"$output")
 
    assert_eq "$hash_sectors_expected" "$hash_sectors_actual"
 
    data_sectors=$(jq -r ".partitiontable.partitions | map(select(.name == \"root-${architecture}\")) | .[].size" <<<"$output")
    data_bytes=$((data_sectors * 512))
    data_verity_blocks=$((data_bytes / 4096))
 
    # The actual data partition is much smaller than 10GiB, i.e. also smaller than 100MiB
    assert_rc 0 test $data_bytes -lt $((100 * 1024 * 1024))
 
    # Check that the verity hash tree is created from the actual on-disk data, not the custom size
    veritysetup dump "${loop}p2" | grep 'Data blocks:' | grep "$data_verity_blocks" >/dev/null
}
 
testcase_verity_encrypt() {
    local defs imgs output loop drh hrh part_size dm_devno verity_dep
 
    if ( . /etc/os-release && [[ "${ID_LIKE:-}" == "alpine" ]] ); then
        echo "Skipping verity+encrypt test on Alpine/Nura."
        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
    chmod 0755 "$defs"
 
    echo "*** dm-verity + LUKS2 (verity envelope around encrypted data) ***"
 
    echo -n "wetterfrosch" >"$imgs/key"
 
    # Encrypting verity hash partitions must be refused
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Encrypt=key-file
SizeMaxBytes=1G
EOF
 
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
Encrypt=key-file
EOF
 
    (! systemd-repart --offline="$OFFLINE" \
                      --definitions="$defs" \
                      --seed="$seed" \
                      --dry-run=yes \
                      --empty=create \
                      --size=auto \
                      --key-file="$imgs/key" \
                      "$imgs/refused")
 
    # Minimize= on the hash partition of an encrypted data partition must be refused. Set Minimize= on the
    # data partition as well, so that the generic "data partition does not set CopyBlocks= or Minimize="
    # check doesn't fire first and the encryption-specific check is actually reached.
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
Format=ext4
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Encrypt=key-file
Minimize=guess
SizeMaxBytes=1G
EOF
 
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
Minimize=yes
EOF
 
    (! systemd-repart --offline="$OFFLINE" \
                      --definitions="$defs" \
                      --seed="$seed" \
                      --dry-run=yes \
                      --empty=create \
                      --size=auto \
                      --key-file="$imgs/key" \
                      "$imgs/refused") |& grep "Minimize= cannot be set for verity hash partitions whose data partition is encrypted" >/dev/null
 
    # Now a valid combination: the hash partition is sized via SizeMaxBytes= of the data partition
    tee "$defs/verity-data.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=${defs}
Verity=data
VerityMatchKey=root
Encrypt=key-file
SizeMaxBytes=1G
EOF
 
    tee "$defs/verity-hash.conf" <<EOF
[Partition]
Type=root-${architecture}-verity
Verity=hash
VerityMatchKey=root
EOF
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --empty=create \
                            --size=auto \
                            --key-file="$imgs/key" \
                            --json=pretty \
                            "$imgs/verity-encrypt")
 
    drh=$(jq -r ".[] | select(.type == \"root-${architecture}\") | .roothash" <<<"$output")
    hrh=$(jq -r ".[] | select(.type == \"root-${architecture}-verity\") | .roothash" <<<"$output")
 
    assert_neq "$drh" "null"
    assert_eq "$drh" "$hrh"
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping verity+encrypt test dissect part in container."
        return
    fi
 
    loop="$(systemd-dissect --attach "$imgs/verity-encrypt")"
 
    # Make sure the loopback device gets cleaned up
    # shellcheck disable=SC2064
    trap "rm -rf '$defs' '$imgs' ; systemd-dissect --detach '$loop'" RETURN ERR
 
    # The data partition must contain LUKS ciphertext as its outermost layer ...
    blkid --output value --match-tag TYPE "${loop}p1" | grep -x crypto_LUKS
 
    # ... and the verity hash data must cover exactly that ciphertext
    veritysetup verify "${loop}p1" "${loop}p2" "$drh"
 
    # Dissection must set up verity as the outer envelope and LUKS inside of it. The LUKS passphrase is
    # picked up via the dissect.passphrase credential.
    mkdir -p "$imgs/creds"
    echo -n "wetterfrosch" >"$imgs/creds/dissect.passphrase"
 
    systemd-dissect --root-hash "$drh" "$imgs/verity-encrypt"
    systemd-dissect --root-hash "$drh" --validate --image-policy "root=encrypted+verity" "$imgs/verity-encrypt"
    # A policy that doesn't allow encryption must be refused
    (! systemd-dissect --root-hash "$drh" --validate --image-policy "root=verity" "$imgs/verity-encrypt")
 
    CREDENTIALS_DIRECTORY="$imgs/creds" systemd-dissect --root-hash "$drh" -M "$imgs/verity-encrypt" "$imgs/mnt"
 
    # shellcheck disable=SC2064
    trap "umount --quiet --recursive '$imgs/mnt' || : ; rm -rf '$defs' '$imgs' ; systemd-dissect --detach '$loop'" RETURN ERR
 
    # Check that both DM layers are stacked as expected: the mounted device is a LUKS volume backed by a
    # dm-verity device
    dm_devno=$(findmnt --noheadings --output MAJ:MIN "$imgs/mnt" | tr -d ' ')
    [[ "$(dmsetup table -j "${dm_devno%%:*}" -m "${dm_devno##*:}" | cut -d' ' -f3)" == "crypt" ]]
    verity_dep=$(dmsetup deps -o devname -j "${dm_devno%%:*}" -m "${dm_devno##*:}" | sed 's/.*(\(.*\))/\1/')
    [[ "$(dmsetup table "/dev/mapper/$verity_dep" | cut -d' ' -f3)" == "verity" ]]
 
    # The copied-in files must be intact
    cmp "$defs/verity-data.conf" "$imgs/mnt$defs/verity-data.conf"
 
    systemd-dissect -U "$imgs/mnt"
 
    # Now corrupt a block in the middle of the encrypted data partition and check that verification fails
    part_size=$(blockdev --getsize64 "${loop}p1")
    dd if=/dev/urandom of="${loop}p1" bs=4096 count=1 seek=$(( part_size / 2 / 4096 )) oflag=direct conv=notrunc
    (! veritysetup verify "${loop}p1" "${loop}p2" "$drh")
}
 
testcase_exclude_files() {
    local defs imgs root output
 
    defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
    imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
    root="$(mktemp --directory "/var/tmp/test-repart.root.XXXXXXXXXX")"
    # shellcheck disable=SC2064
    trap "rm -rf '$defs' '$imgs' '$root'" RETURN
    chmod 0755 "$defs"
 
    echo "*** file exclusion ***"
 
    touch "$root/abc"
    mkdir "$root/usr"
    touch "$root/usr/def"
    touch "$root/usr/qed"
    mkdir "$root/tmp"
    touch "$root/tmp/prs"
    mkdir "$root/proc"
    touch "$root/proc/prs"
    mkdir "$root/zzz"
    mkdir "$root/zzz/usr"
    touch "$root/zzz/usr/prs"
    mkdir "$root/zzz/proc"
    touch "$root/zzz/proc/prs"
 
    tee "$defs/00-root.conf" <<EOF
[Partition]
Type=root-${architecture}
CopyFiles=/
CopyFiles=/zzz:/
CopyFiles=/:/oiu
ExcludeFilesTarget=/oiu/usr
EOF
 
    tee "$defs/10-usr.conf" <<EOF
[Partition]
Type=usr-${architecture}
CopyFiles=/usr:/
ExcludeFiles=/usr/qed
EOF
 
    output=$(systemd-repart --offline="$OFFLINE" \
                            --definitions="$defs" \
                            --seed="$seed" \
                            --dry-run=no \
                            --empty=create \
                            --size=auto \
                            --json=pretty \
                            --root="$root" \
                            "$imgs/zzz")
 
    if systemd-detect-virt --quiet --container; then
        echo "Skipping issue 24786 test loop/mount parts in container."
        return
    fi
 
    loop=$(losetup -P --show -f "$imgs/zzz")
    udevadm wait --timeout="$UDEVADM_WAIT_TIMEOUT" --settle "${loop:?}p1" "${loop:?}p2"
 
    # Test that /usr/def did not end up in the root partition but other files did.
    mkdir "$imgs/mnt"
    mount -t ext4 "${loop}p1" "$imgs/mnt"
    assert_rc 0 ls "$imgs/mnt/abc"
    assert_rc 0 ls "$imgs/mnt/usr"
    assert_rc 2 ls "$imgs/mnt/usr/def"
 
    # Test that /zzz/usr/prs did not end up in the root partition under /usr but did end up in /zzz/usr/prs
    assert_rc 2 ls "$imgs/mnt/usr/prs"
    assert_rc 0 ls "$imgs/mnt/zzz/usr/prs"
 
    # Test that /tmp/prs did not end up in the root partition but /tmp did.
    assert_rc 0 ls "$imgs/mnt/tmp"
    assert_rc 2 ls "$imgs/mnt/tmp/prs"
 
    # Test that /usr/qed did not end up in the usr partition but /usr/def did.
    mount -t ext4 "${loop}p2" "$imgs/mnt/usr"
    assert_rc 0 ls "$imgs/mnt/usr/def"
    assert_rc 2 ls "$imgs/mnt/usr/qed"
 
    # Test that /zzz/proc/prs did not end up in the root partition but /proc did.
    assert_rc 0 ls "$imgs/mnt/proc"
    assert_rc 2 ls "$imgs/mnt/proc/prs"
 
    # Test that /zzz/usr/prs did not end up in the usr partition.
    assert_rc 2 ls "$imgs/mnt/usr/prs"
 
    # Test that /oiu/ and /oiu/zzz ended up in the root partition but /oiu/usr did not.
    assert_rc 0 ls "$imgs/mnt/oiu"
    assert_rc 0 ls "$imgs/mnt/oiu/zzz"
    assert_rc 2 ls "$imgs/mnt/oiu/usr"
 
    umount -R "$imgs/mnt"
    losetup -d "$loop"
}
 
testcase_minimize() {
    local defs imgs output
 
    echo "*** minimization ***"
 
    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
 
    for format in ext4 vfat erofs; do
        if ! command -v "mkfs.$format" >/dev/null; then
            continue
        fi
 
        tee "$defs/root-$format.conf" <<EOF
[Partition]
Type=root-${architecture}
Format=${format}
CopyFiles=${defs}
Minimize=guess
EOF
    done
 
    if command -v mkfs.btrfs >/dev/null; then
        for minimize in guess best; do
            tee "$defs/root-btrfs-${minimize}.conf" <<EOF
[Partition]
Type=root-${architecture}
Format=btrfs
CopyFiles=${defs}
Minimize=${minimize}
EOF
        done
    fi