summaryrefslogtreecommitdiff
path: root/distrib/utils/embedded/files/resize_disklabel
blob: 5f05eeb176bbed069477697a0113902048f59085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/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"