Skip to content

Commit

Permalink
Change to use sfdisk
Browse files Browse the repository at this point in the history
Use sfdisk instead of lsblk to get the partition table. This requires
some changes in how the size of partitions are checked since sfdisk
records disk size in sectors, not MiB.
  • Loading branch information
lukewarmtemp committed Nov 28, 2023
1 parent 13a364c commit bb22c43
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions tests/kola/disks/partition-scheme
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ set -xeuo pipefail

root_part=$(findmnt -n -o SOURCE /sysroot)
disk_name=$(lsblk --json -o PKNAME --path "$root_part" | jq --raw-output '.blockdevices[].pkname')
diskData=$(lsblk --json --bytes -o NAME,FSTYPE,START,SIZE,MOUNTPOINT,PARTLABEL,PARTTYPENAME,LOG-SEC "$disk_name" | jq '.blockdevices[]' )
partitionData=$(echo $diskData | jq '.children[]')
totalPartitions=$(echo $diskData | jq '.children | length')
sector_size=$(echo $diskData | jq '.["log-sec"]')
diskData=$(sfdisk --json "$disk_name" | jq '.partitiontable' )
partitionData=$(echo $diskData | jq '.partitions[]')
totalPartitions=$(echo $diskData | jq '.partitions | length')

# lsblk always reports `START` in sectors that are 512 bytes even if
# the disk is a 4k native disk with 4096 byte sectors. Let's hard code
# the value here and do our math based on that. See
# https://github.com/karelzak/util-linux/commit/61c40f484563881420e1606a27663aee930cbe53
sector_size=512

if [[ $totalPartitions -ne 4 ]]; then
fatal "Expected 4 partitions, got $totalPartitions"
fi

# check sizes of each parition
ONE_MiB=$(( 1024 * 1024 ))
SECTOR_UNIT=$(( $ONE_MiB / $sector_size ))

# An associative array that maps the partition label to the size (in MiB)
# An associative array that maps the partition label to the size (in Sector Units)
# of the partition. For the root partition we set it to "" to skip the check
# there because the growfs service runs on first boot. Checking start
# should be enough there.
Expand All @@ -39,23 +45,22 @@ expected_start=$(( 1 * $ONE_MiB / $sector_size ))

# Iterate over the partitions and check their
for key in ${!expected[@]}; do
echo "${key}, ${expected[${key}]}"
size_MiB="${expected[${key}]}"
start=$(echo "$partitionData" | jq "select ( .partlabel == \"$key\") | .start")
size=$(echo "$partitionData" | jq "select ( .partlabel == \"$key\") | .size")
size_in_sectors="${expected[${key}]}"
start=$(echo "$partitionData" | jq "select ( .name == \"$key\") | .start")
size=$(echo "$partitionData" | jq "select ( .name == \"$key\") | .size")
if [[ "$start" -ne "$expected_start" ]]; then
fatal "Expected $key partition start sector of $expected_start, got $start"
fi
if [ ! -z "$size_MiB" ]; then
expected_size=$(($size_MiB * $ONE_MiB))
if [ ! -z "$size_in_sectors" ]; then
expected_size=$(($size_in_sectors * $SECTOR_UNIT))
if [[ "$expected_size" -ne "$size" ]]; then
fatal "Expected $key partition of size $expected_size, got $size"
fi
fi
# The expected start of the next partition will be the start of this partition
# plus the size of this partition.
expected_start=$(($expected_start + $size / $sector_size))
expected_start=$(($expected_start + $size))
done

ok partition scheme
exit 0
exit 0

0 comments on commit bb22c43

Please sign in to comment.