Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

#!/bin/sh
#
# $NetBSD: resize_disklabel,v 1.5 2023/03/18 11:36:23 mlelstv Exp $
#

# PROVIDE: resize_disklabel
# REQUIRE: fsck_root
# BEFORE: resize_root

$_rc_subr_loaded . /etc/rc.subr

name="resize_disklabel"
rcvar=$name
start_cmd="resize_disklabel_start"
stop_cmd=":"

get_rawpart()
{
	local partno

	partno=$(/sbin/sysctl -n kern.rawpartition)
	printf "$(printf \\\\%o $((partno + 97)))"
}

get_total_sectors()
{
	local disk

	disk=$1
	/sbin/drvctl -p "$disk" disk-info/geometry/sectors-per-unit
}

get_rawpart_sectors()
{
	local disk rawpart

	disk=$1
	rawpart=$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)

	eval $(/sbin/fdisk -S $disk)
	if [ ! "$PART1ID" = "169" ]; then
		warn "No NetBSD partition found in MBR partition #1"
		return
	fi

	ts=$((${DLSIZE} - ${PART1START}))
	rs=${PART1SIZE}

	if [ "$ts" = "$rs" ]; then
		return
	fi

	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)

	ts=$(get_total_sectors $disk)
	rs=$(get_rawpart_sectors $disk $rawpart)

	if [ "$ts" = "$rs" ]; then
		return
	fi

	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
}

resize_disklabel_start()
{
	if [ x"${resize_disklabel_disk}" = "x" ]; then
		resize_disklabel_disk="$(/sbin/sysctl -n kern.root_device)"
	fi
	if [ x"${resize_disklabel_part}" = "x" ]; then
		resize_disklabel_part=$(printf \\$(printf '%03o' $(( 97 + $(sysctl -n kern.root_partition) ))))
	fi

	grow_mbrpart "${resize_disklabel_disk}"
	grow_disklabel "${resize_disklabel_disk}" "${resize_disklabel_part}"
}

load_rc_config $name
run_rc_command "$1"