xref: /freebsd/libexec/rc/rc.d/growfs (revision 0696600c41600d80bcd993bfd8e675d0ae6951fe)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# Copyright 2014 John-Mark Gurney
4*0696600cSBjoern A. Zeeb# All rights reserved.
5*0696600cSBjoern A. Zeeb#
6*0696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
7*0696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
8*0696600cSBjoern A. Zeeb# are met:
9*0696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
10*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
11*0696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
12*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
13*0696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
14*0696600cSBjoern A. Zeeb#
15*0696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*0696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*0696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*0696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*0696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*0696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*0696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*0696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*0696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*0696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*0696600cSBjoern A. Zeeb# SUCH DAMAGE.
26*0696600cSBjoern A. Zeeb#
27*0696600cSBjoern A. Zeeb# $FreeBSD$
28*0696600cSBjoern A. Zeeb#
29*0696600cSBjoern A. Zeeb
30*0696600cSBjoern A. Zeeb# PROVIDE: growfs
31*0696600cSBjoern A. Zeeb# BEFORE: sysctl
32*0696600cSBjoern A. Zeeb# KEYWORD: firstboot
33*0696600cSBjoern A. Zeeb
34*0696600cSBjoern A. Zeeb# This allows us to distribute a image
35*0696600cSBjoern A. Zeeb# and have it work on essentially any size drive.
36*0696600cSBjoern A. Zeeb#
37*0696600cSBjoern A. Zeeb# TODO: Figure out where this should really be ordered.
38*0696600cSBjoern A. Zeeb# I suspect it should go just after fsck but before mountcritlocal.
39*0696600cSBjoern A. Zeeb#
40*0696600cSBjoern A. Zeeb
41*0696600cSBjoern A. Zeeb. /etc/rc.subr
42*0696600cSBjoern A. Zeeb
43*0696600cSBjoern A. Zeebname="growfs"
44*0696600cSBjoern A. Zeebdesc="Grow root partition to fill device"
45*0696600cSBjoern A. Zeebstart_cmd="growfs_start"
46*0696600cSBjoern A. Zeebstop_cmd=":"
47*0696600cSBjoern A. Zeebrcvar="growfs_enable"
48*0696600cSBjoern A. Zeeb
49*0696600cSBjoern A. Zeebgrowfs_start ()
50*0696600cSBjoern A. Zeeb{
51*0696600cSBjoern A. Zeeb	echo "Growing root partition to fill device"
52*0696600cSBjoern A. Zeeb	rootdev=$(df / | tail -n 1 | awk '{ sub("/dev/", "", $1); print $1 }')
53*0696600cSBjoern A. Zeeb	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
54*0696600cSBjoern A. Zeeb		# raw device
55*0696600cSBjoern A. Zeeb		rawdev="$rootdev"
56*0696600cSBjoern A. Zeeb	else
57*0696600cSBjoern A. Zeeb		rawdev=$(glabel status | awk '$1 == "'"$rootdev"'" { print $3 }')
58*0696600cSBjoern A. Zeeb		if [ x"$rawdev" = x"" ]; then
59*0696600cSBjoern A. Zeeb			echo "Can't figure out device for: $rootdev"
60*0696600cSBjoern A. Zeeb			return
61*0696600cSBjoern A. Zeeb		fi
62*0696600cSBjoern A. Zeeb	fi
63*0696600cSBjoern A. Zeeb
64*0696600cSBjoern A. Zeeb	sysctl -b kern.geom.conftxt | awk '
65*0696600cSBjoern A. Zeeb{
66*0696600cSBjoern A. Zeeb	lvl=$1
67*0696600cSBjoern A. Zeeb	device[lvl] = $3
68*0696600cSBjoern A. Zeeb	type[lvl] = $2
69*0696600cSBjoern A. Zeeb	idx[lvl] = $7
70*0696600cSBjoern A. Zeeb	parttype[lvl] = $13
71*0696600cSBjoern A. Zeeb	if (dev == $3) {
72*0696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
73*0696600cSBjoern A. Zeeb			# resize
74*0696600cSBjoern A. Zeeb			if (type[i] == "PART") {
75*0696600cSBjoern A. Zeeb				pdev = device[i - 1]
76*0696600cSBjoern A. Zeeb				cmd[i] = "gpart resize -i " idx[i] " " pdev
77*0696600cSBjoern A. Zeeb				if (parttype[i] == "GPT")
78*0696600cSBjoern A. Zeeb					cmd[i] = "gpart recover " pdev " ; " cmd[i]
79*0696600cSBjoern A. Zeeb			} else if (type[i] == "LABEL") {
80*0696600cSBjoern A. Zeeb				continue
81*0696600cSBjoern A. Zeeb			} else {
82*0696600cSBjoern A. Zeeb				print "unhandled type: " type[i]
83*0696600cSBjoern A. Zeeb				exit 1
84*0696600cSBjoern A. Zeeb			}
85*0696600cSBjoern A. Zeeb		}
86*0696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
87*0696600cSBjoern A. Zeeb			if (cmd[i])
88*0696600cSBjoern A. Zeeb				system(cmd[i])
89*0696600cSBjoern A. Zeeb		}
90*0696600cSBjoern A. Zeeb		exit 0
91*0696600cSBjoern A. Zeeb	}
92*0696600cSBjoern A. Zeeb}' dev="$rawdev"
93*0696600cSBjoern A. Zeeb	gpart commit "$rootdev"
94*0696600cSBjoern A. Zeeb	growfs -y /dev/"$rootdev"
95*0696600cSBjoern A. Zeeb}
96*0696600cSBjoern A. Zeeb
97*0696600cSBjoern A. Zeebload_rc_config $name
98*0696600cSBjoern A. Zeebrun_rc_command "$1"
99