Handle arbitrary raw partitions.

Fetch geometry from disklabel to be consistent.
Use disktab format instead of the normal human-readable output for parsing
the disklabel.
Avoid overflows in size computation.
This commit is contained in:
mlelstv 2023-02-18 07:58:34 +00:00
parent 469d9aaeac
commit 9921d00860
1 changed files with 21 additions and 9 deletions

View File

@ -1,6 +1,6 @@
#!/bin/sh
#
# $NetBSD: resize_disklabel,v 1.3 2018/10/06 09:58:55 jmcneill Exp $
# $NetBSD: resize_disklabel,v 1.4 2023/02/18 07:58:34 mlelstv Exp $
#
# PROVIDE: resize_disklabel
@ -16,25 +16,35 @@ stop_cmd=":"
get_rawpart()
{
local partno
partno=$(/sbin/sysctl -n kern.rawpartition)
test $partno = 2 && echo c || echo d
printf "$(printf \\\\%o $((partno + 97)))"
}
get_total_sectors()
{
local disk
disk=$1
/sbin/drvctl -p $disk disk-info/geometry/sectors-per-unit
/sbin/disklabel -t "$disk" \
| sed -ne 's/.*:su#\([0-9]*\):.*/\1/p'
}
get_rawpart_sectors()
{
local disk rawpart
disk=$1
rawpart=$2
/sbin/disklabel $disk | grep "^ $rawpart:" | awk '{ print $2; }'
/sbin/disklabel -t "$disk" \
| sed -ne 's/.*:p'"$rawpart"'#\([0-9]*\):.*/\1/p'
}
grow_mbrpart()
{
local disk rawpart ts rs oldsize newsize
disk=$1
rawpart=$(get_rawpart)
@ -44,21 +54,23 @@ grow_mbrpart()
return
fi
ts=$(($(get_total_sectors $disk) - ${PART1START}))
ts=$((${DLSIZE} - ${PART1START}))
rs=${PART1SIZE}
if [ "$ts" = "$rs" ]; then
return
fi
oldsize=$(($rs * 512 / 1024 / 1024))
newsize=$(($ts * 512 / 1024 / 1024))
oldsize=$(($rs / 2048))
newsize=$(($ts / 2048))
echo "Growing $disk MBR partition #1 (${oldsize}MB -> ${newsize}MB)"
/sbin/fdisk -f -u -1 -s 169/${PART1START}/${ts} ${disk}
}
grow_disklabel()
{
local disk part rawpart ts rs oldsize newsize
disk=$1
part=$2
rawpart=$(get_rawpart)
@ -70,8 +82,8 @@ grow_disklabel()
return
fi
oldsize=$(($rs * 512 / 1024 / 1024))
newsize=$(($ts * 512 / 1024 / 1024))
oldsize=$(($rs / 2048))
newsize=$(($ts / 2048))
echo "Growing $disk disklabel (${oldsize}MB -> ${newsize}MB)"
printf "A\ny\n$part\n\n\n\$\nc\n\n\n\$\nd\n\n\n\$\nW\ny\nQ\n" | \
disklabel -i $disk >/dev/null