1273955Sjmg#!/bin/sh
2273955Sjmg#
3273955Sjmg# Copyright 2014 John-Mark Gurney
4273955Sjmg# All rights reserved.
5273955Sjmg#
6273955Sjmg# Redistribution and use in source and binary forms, with or without
7273955Sjmg# modification, are permitted provided that the following conditions
8273955Sjmg# are met:
9273955Sjmg# 1. Redistributions of source code must retain the above copyright
10273955Sjmg#    notice, this list of conditions and the following disclaimer.
11273955Sjmg# 2. Redistributions in binary form must reproduce the above copyright
12273955Sjmg#    notice, this list of conditions and the following disclaimer in the
13273955Sjmg#    documentation and/or other materials provided with the distribution.
14273955Sjmg#
15273955Sjmg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16273955Sjmg# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17273955Sjmg# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18273955Sjmg# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19273955Sjmg# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20273955Sjmg# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21273955Sjmg# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22273955Sjmg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23273955Sjmg# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24273955Sjmg# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25273955Sjmg# SUCH DAMAGE.
26273955Sjmg#
27273955Sjmg# $FreeBSD: releng/10.2/etc/rc.d/growfs 284009 2015-06-05 00:46:49Z cperciva $
28273955Sjmg#
29273955Sjmg
30273955Sjmg# PROVIDE: growfs
31273955Sjmg# BEFORE: sysctl
32273955Sjmg# KEYWORD: firstboot
33273955Sjmg
34273955Sjmg# This allows us to distribute a image
35273955Sjmg# and have it work on essentially any size drive.
36273955Sjmg#
37273955Sjmg# TODO: Figure out where this should really be ordered.
38284009Scperciva# I suspect it should go just after fsck but before mountcritlocal.
39273955Sjmg# 
40273955Sjmg
41273955Sjmg. /etc/rc.subr
42273955Sjmg
43273955Sjmgname="growfs"
44273955Sjmgstart_cmd="growfs_start"
45273955Sjmgstop_cmd=":"
46273955Sjmgrcvar="growfs_enable"
47273955Sjmg
48273955Sjmggrowfs_start ()
49273955Sjmg{
50284009Scperciva	echo "Growing root partition to fill device"
51284009Scperciva	rootdev=$(df / | tail -n 1 | awk '{ sub("/dev/", "", $1); print $1 }')
52284009Scperciva	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
53284009Scperciva		# raw device
54284009Scperciva		rawdev="$rootdev"
55284009Scperciva	else
56284009Scperciva		rawdev=$(glabel status | awk '$1 == "'"$rootdev"'" { print $3 }')
57284009Scperciva		if [ x"$rawdev" = x"" ]; then
58284009Scperciva			echo "Can't figure out device for: $rootdev"
59284009Scperciva			return
60284009Scperciva		fi
61273955Sjmg	fi
62273955Sjmg
63284009Scperciva	sysctl -b kern.geom.conftxt | awk '
64273955Sjmg{
65273955Sjmg	lvl=$1
66273955Sjmg	device[lvl] = $3
67273955Sjmg	type[lvl] = $2
68273955Sjmg	idx[lvl] = $7
69273955Sjmg	parttype[lvl] = $13
70273955Sjmg	if (dev == $3) {
71273955Sjmg		for (i = 1; i <= lvl; i++) {
72273955Sjmg			# resize
73273955Sjmg			if (type[i] == "PART") {
74273955Sjmg				pdev = device[i - 1]
75273955Sjmg				cmd[i] = "gpart resize -i " idx[i] " " pdev
76273955Sjmg				if (parttype[i] == "GPT")
77273955Sjmg					cmd[i] = "gpart recover " pdev " ; " cmd[i]
78273955Sjmg			} else if (type[i] == "LABEL") {
79273955Sjmg				continue
80273955Sjmg			} else {
81273955Sjmg				print "unhandled type: " type[i]
82273955Sjmg				exit 1
83273955Sjmg			}
84273955Sjmg		}
85273955Sjmg		for (i = 1; i <= lvl; i++) {
86273955Sjmg			if (cmd[i])
87273955Sjmg				system(cmd[i])
88273955Sjmg		}
89273955Sjmg		exit 0
90273955Sjmg	}
91273955Sjmg}' dev="$rawdev"
92284009Scperciva	growfs -y /dev/"$rootdev"
93273955Sjmg}
94273955Sjmg
95273955Sjmgload_rc_config $name
96273955Sjmgrun_rc_command "$1"
97