xref: /freebsd/libexec/rc/rc.d/growfs (revision 0edc114ac0b998b06235da32bec24d55c10206cd)
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 an 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	FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
53	FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
54	case "$FSTYPE" in
55	ufs)
56		rootdev=${FSDEV#/dev/}
57		;;
58	zfs)
59		pool=${FSDEV%%/*}
60		rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
61		;;
62	*)
63		echo "Don't know how to grow root filesystem type: $FSTYPE"
64		return
65	esac
66	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
67		# raw device
68		rawdev="$rootdev"
69	else
70		rawdev=$(glabel status | awk '$1 == "'"$rootdev"'" { print $3 }')
71		if [ x"$rawdev" = x"" ]; then
72			echo "Can't figure out device for: $rootdev"
73			return
74		fi
75	fi
76
77	sysctl -b kern.geom.conftxt | awk '
78{
79	lvl=$1
80	device[lvl] = $3
81	type[lvl] = $2
82	idx[lvl] = $7
83	parttype[lvl] = $13
84	if (dev == $3) {
85		for (i = 1; i <= lvl; i++) {
86			# resize
87			if (type[i] == "PART") {
88				pdev = device[i - 1]
89				cmd[i] = "gpart resize -i " idx[i] " " pdev
90				if (parttype[i] == "GPT")
91					cmd[i] = "gpart recover " pdev " ; " cmd[i]
92			} else if (type[i] == "LABEL") {
93				continue
94			} else {
95				print "unhandled type: " type[i]
96				exit 1
97			}
98		}
99		for (i = 1; i <= lvl; i++) {
100			if (cmd[i])
101				system(cmd[i])
102		}
103		exit 0
104	}
105}' dev="$rawdev"
106	gpart commit "$rootdev"
107	case "$FSTYPE" in
108	ufs)
109		growfs -y /dev/"$rootdev"
110		;;
111	zfs)
112		zpool online -e $pool $rootdev
113		;;
114	esac
115}
116
117load_rc_config $name
118run_rc_command "$1"
119