From 38d68d81bd80cb22fbd888312858a9b1efaf2110 Mon Sep 17 00:00:00 2001 From: Mark Verlinde Date: Fri, 7 Aug 2026 11:16:36 +0200 Subject: [PATCH 1/2] fix(ns-storage): make no assumptions on partition naming scheme Instead of guessing the separator, read back the kname of the newly-created partition via lsblk and verify its number matches what was just created. --- .../ns-storage/files/ns-storage-setup-disk | 16 +++++++++++---- .../files/ns-storage-setup-partition | 20 ++++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/ns-storage/files/ns-storage-setup-disk b/packages/ns-storage/files/ns-storage-setup-disk index 381e53b72..085bcccd7 100644 --- a/packages/ns-storage/files/ns-storage-setup-disk +++ b/packages/ns-storage/files/ns-storage-setup-disk @@ -6,7 +6,7 @@ # # Create a single ext4 partition on the given device. -# Paramters: +# Parameters: # - device, like '/dev/sdb' # @@ -26,7 +26,15 @@ mount | grep -q "$device" && eerror "Device '$device' is already mounted" # cleanup partitions dd if=/dev/zero of="$device" bs=512 count=1 conv=notrunc 2>/dev/null -# create a single ext4 partition +# create a single partition parted -s -a optimal "$device" mklabel gpt -- mkpart primary 1 -1 >/dev/null -mkfs.ext4 -q -F -L ns_data "$device"1 >/dev/null -echo '{"device": "'${device}1'"}' +# mkfs on newly created partition number 1 +partition=$(lsblk $device -o KNAME | sed -n \$p) +partition_no=$(echo $partition | sed -E 's/.*(.)/\1/') +if [ "$partition_no" -eq 1 ] +then + mkfs.ext4 -q -F -L ns_data /dev/"$partition" >/dev/null +else + eerror "Wrong partition number '$partition_no'" +fi +echo '{"device": "/dev/'$partition'"}' diff --git a/packages/ns-storage/files/ns-storage-setup-partition b/packages/ns-storage/files/ns-storage-setup-partition index f8fce1554..737eafd97 100644 --- a/packages/ns-storage/files/ns-storage-setup-partition +++ b/packages/ns-storage/files/ns-storage-setup-partition @@ -29,14 +29,16 @@ end=$(echo $free_part | awk -F':' '{print $3}') start_new=${start_current::-3} # preserve some space for future use start_new=$((start_new + preserve)) - -# handle nvme -if [[ "$dev" =~ ^/dev/nvme ]]; then - part="${dev}p3" +# create new partition +parted -s --fix -a optimal ${dev} unit MiB mkpart primary ext4 ${start_new}MiB 100% &>/dev/null +# mkfs on newly created partition number 3 +part=$(lsblk $dev -o KNAME | sed -n \$p) +part_no=$(echo $part | sed -E 's/.*(.)/\1/') +if [ ${part_no} == 3 ] +then + mkfs.ext4 -q -F -L ns_data /dev/${part} >/dev/null else - part="${dev}3" + echo "ERROR: Wrong partition number ${part_no}" + exit 1 fi - -parted -s --fix -a optimal ${dev} unit MiB mkpart ext4 ${start_new}MiB 100% &>/dev/null -mkfs.ext4 -q -F -L ns_data "${part}" &>/dev/null -echo '{"device": "'${part}'"}' +echo '{"device": "/dev/'${part}'"}' From 0cd185e348fa27b894f01edcc69725d23429c5c8 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Fri, 7 Aug 2026 11:37:01 +0200 Subject: [PATCH 2/2] fix(ns-storage): identify the created partition by number The previous attempt at dropping the naming-scheme assumption picked the last line of `lsblk -o KNAME` and read its last character as the partition number. NethSecurity images carry a bios_grub partition 128, which sorts last, so setup-partition computed number 8 and aborted with "Wrong partition number 8"; add-storage in partition mode always failed with setup_partition_failed. Diff the partition numbers reported by parted before and after mkpart to learn the number actually assigned, then resolve it to a kernel name via /sys/class/block//partition. This handles both sdX3 and nvme0n1p3 without guessing the separator and without assuming the data partition is number 3. Assisted-by: Claude Code:claude-opus-5[1m] --- .../ns-storage/files/ns-storage-setup-disk | 25 +++++++----- .../files/ns-storage-setup-partition | 40 ++++++++++++++----- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/packages/ns-storage/files/ns-storage-setup-disk b/packages/ns-storage/files/ns-storage-setup-disk index 085bcccd7..da6330f2f 100644 --- a/packages/ns-storage/files/ns-storage-setup-disk +++ b/packages/ns-storage/files/ns-storage-setup-disk @@ -28,13 +28,20 @@ mount | grep -q "$device" && eerror "Device '$device' is already mounted" dd if=/dev/zero of="$device" bs=512 count=1 conv=notrunc 2>/dev/null # create a single partition parted -s -a optimal "$device" mklabel gpt -- mkpart primary 1 -1 >/dev/null -# mkfs on newly created partition number 1 -partition=$(lsblk $device -o KNAME | sed -n \$p) -partition_no=$(echo $partition | sed -E 's/.*(.)/\1/') -if [ "$partition_no" -eq 1 ] -then - mkfs.ext4 -q -F -L ns_data /dev/"$partition" >/dev/null -else - eerror "Wrong partition number '$partition_no'" -fi +partprobe "$device" >/dev/null 2>&1 || : + +# the disk has just been wiped, so the partition we created is number 1: +# look up its kernel name, without guessing the naming scheme +disk=$(basename "$device") +partition='' +for pfile in /sys/block/"$disk"/*/partition; do + [ -e "$pfile" ] || continue + if [ "$(cat "$pfile")" -eq 1 ]; then + partition=$(basename "$(dirname "$pfile")") + break + fi +done +[ -z "$partition" ] && eerror "Partition 1 not found on '$device'" + +mkfs.ext4 -q -F -L ns_data /dev/"$partition" >/dev/null echo '{"device": "/dev/'$partition'"}' diff --git a/packages/ns-storage/files/ns-storage-setup-partition b/packages/ns-storage/files/ns-storage-setup-partition index 737eafd97..ad323b098 100644 --- a/packages/ns-storage/files/ns-storage-setup-partition +++ b/packages/ns-storage/files/ns-storage-setup-partition @@ -11,9 +11,16 @@ set -e part=$(lsblk -ln | grep "/rom" | uniq | awk '{print $1}') -dev="/dev/$(lsblk -lno pkname /dev/$part)" +disk=$(lsblk -lno pkname /dev/$part) +dev="/dev/$disk" preserve=${1:-100} +# List the partition numbers currently present on $dev +list_partitions() +{ + parted -s -m "$dev" print 2>/dev/null | awk -F':' '/^[0-9]+:/ {print $1}' | sort -n +} + # find free space free_part=$(parted -s -m $dev unit MiB print free 2>/dev/null | grep free | tail -n -1) @@ -30,15 +37,30 @@ start_new=${start_current::-3} # preserve some space for future use start_new=$((start_new + preserve)) # create new partition +before=$(list_partitions) parted -s --fix -a optimal ${dev} unit MiB mkpart primary ext4 ${start_new}MiB 100% &>/dev/null -# mkfs on newly created partition number 3 -part=$(lsblk $dev -o KNAME | sed -n \$p) -part_no=$(echo $part | sed -E 's/.*(.)/\1/') -if [ ${part_no} == 3 ] -then - mkfs.ext4 -q -F -L ns_data /dev/${part} >/dev/null -else - echo "ERROR: Wrong partition number ${part_no}" +partprobe "$dev" >/dev/null 2>&1 || : + +# the partition number is assigned by parted, so read back the one that appeared +part_no=$(printf '%s\n%s\n' "$before" "$(list_partitions)" | sort -n | uniq -u) +if [ "$(echo "$part_no" | wc -l)" -ne 1 ] || [ -z "$part_no" ]; then + echo "ERROR: Cannot identify the partition created on ${dev}" + exit 1 +fi + +# map the partition number to its kernel name, without guessing the naming scheme +part='' +for pfile in /sys/block/"$disk"/*/partition; do + [ -e "$pfile" ] || continue + if [ "$(cat "$pfile")" == "$part_no" ]; then + part=$(basename "$(dirname "$pfile")") + break + fi +done +if [ -z "$part" ]; then + echo "ERROR: Partition ${part_no} not found on ${dev}" exit 1 fi + +mkfs.ext4 -q -F -L ns_data /dev/${part} >/dev/null echo '{"device": "/dev/'${part}'"}'