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