#!/bin/sh # # $NetBSD: resize_disklabel,v 1.3 2018/10/06 09:58:55 jmcneill 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() { partno=$(/sbin/sysctl -n kern.rawpartition) test $partno = 2 && echo c || echo d } get_total_sectors() { disk=$1 /sbin/drvctl -p $disk disk-info/geometry/sectors-per-unit } get_rawpart_sectors() { disk=$1 rawpart=$2 /sbin/disklabel $disk | grep "^ $rawpart:" | awk '{ print $2; }' } grow_mbrpart() { 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=$(($(get_total_sectors $disk) - ${PART1START})) rs=${PART1SIZE} if [ "$ts" = "$rs" ]; then return fi oldsize=$(($rs * 512 / 1024 / 1024)) newsize=$(($ts * 512 / 1024 / 1024)) echo "Growing $disk MBR partition #1 (${oldsize}MB -> ${newsize}MB)" /sbin/fdisk -f -u -1 -s 169/${PART1START}/${ts} ${disk} } grow_disklabel() { 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 * 512 / 1024 / 1024)) newsize=$(($ts * 512 / 1024 / 1024)) 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" |