xref: /freebsd/libexec/rc/rc.d/growfs (revision c65b552f895bcb8ea931da6304108a3e008ff669)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright 2014 John-Mark Gurney
40696600cSBjoern A. Zeeb# All rights reserved.
50696600cSBjoern A. Zeeb#
60696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
70696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
80696600cSBjoern A. Zeeb# are met:
90696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
100696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
110696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
120696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
130696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
140696600cSBjoern A. Zeeb#
150696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250696600cSBjoern A. Zeeb# SUCH DAMAGE.
260696600cSBjoern A. Zeeb#
270696600cSBjoern A. Zeeb# $FreeBSD$
280696600cSBjoern A. Zeeb#
290696600cSBjoern A. Zeeb
300696600cSBjoern A. Zeeb# PROVIDE: growfs
310696600cSBjoern A. Zeeb# BEFORE: sysctl
320696600cSBjoern A. Zeeb# KEYWORD: firstboot
330696600cSBjoern A. Zeeb
340696600cSBjoern A. Zeeb# This allows us to distribute a image
350696600cSBjoern A. Zeeb# and have it work on essentially any size drive.
360696600cSBjoern A. Zeeb#
370696600cSBjoern A. Zeeb# TODO: Figure out where this should really be ordered.
380696600cSBjoern A. Zeeb# I suspect it should go just after fsck but before mountcritlocal.
390696600cSBjoern A. Zeeb#
400696600cSBjoern A. Zeeb
410696600cSBjoern A. Zeeb. /etc/rc.subr
420696600cSBjoern A. Zeeb
430696600cSBjoern A. Zeebname="growfs"
440696600cSBjoern A. Zeebdesc="Grow root partition to fill device"
450696600cSBjoern A. Zeebstart_cmd="growfs_start"
460696600cSBjoern A. Zeebstop_cmd=":"
470696600cSBjoern A. Zeebrcvar="growfs_enable"
480696600cSBjoern A. Zeeb
490696600cSBjoern A. Zeebgrowfs_start ()
500696600cSBjoern A. Zeeb{
510696600cSBjoern A. Zeeb	echo "Growing root partition to fill device"
52efed7c6dSColin Percival	FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
53efed7c6dSColin Percival	FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
54efed7c6dSColin Percival	case "$FSTYPE" in
55efed7c6dSColin Percival	ufs)
56efed7c6dSColin Percival		rootdev=${FSDEV#/dev/}
57efed7c6dSColin Percival		;;
58efed7c6dSColin Percival	zfs)
59efed7c6dSColin Percival		pool=${FSDEV%%/*}
60*c65b552fSKurt Lidl		rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
61efed7c6dSColin Percival		;;
62efed7c6dSColin Percival	*)
63efed7c6dSColin Percival		echo "Don't know how to grow root filesystem type: $FSTYPE"
64efed7c6dSColin Percival		return
65efed7c6dSColin Percival	esac
660696600cSBjoern A. Zeeb	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
670696600cSBjoern A. Zeeb		# raw device
680696600cSBjoern A. Zeeb		rawdev="$rootdev"
690696600cSBjoern A. Zeeb	else
700696600cSBjoern A. Zeeb		rawdev=$(glabel status | awk '$1 == "'"$rootdev"'" { print $3 }')
710696600cSBjoern A. Zeeb		if [ x"$rawdev" = x"" ]; then
720696600cSBjoern A. Zeeb			echo "Can't figure out device for: $rootdev"
730696600cSBjoern A. Zeeb			return
740696600cSBjoern A. Zeeb		fi
750696600cSBjoern A. Zeeb	fi
760696600cSBjoern A. Zeeb
770696600cSBjoern A. Zeeb	sysctl -b kern.geom.conftxt | awk '
780696600cSBjoern A. Zeeb{
790696600cSBjoern A. Zeeb	lvl=$1
800696600cSBjoern A. Zeeb	device[lvl] = $3
810696600cSBjoern A. Zeeb	type[lvl] = $2
820696600cSBjoern A. Zeeb	idx[lvl] = $7
830696600cSBjoern A. Zeeb	parttype[lvl] = $13
840696600cSBjoern A. Zeeb	if (dev == $3) {
850696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
860696600cSBjoern A. Zeeb			# resize
870696600cSBjoern A. Zeeb			if (type[i] == "PART") {
880696600cSBjoern A. Zeeb				pdev = device[i - 1]
890696600cSBjoern A. Zeeb				cmd[i] = "gpart resize -i " idx[i] " " pdev
900696600cSBjoern A. Zeeb				if (parttype[i] == "GPT")
910696600cSBjoern A. Zeeb					cmd[i] = "gpart recover " pdev " ; " cmd[i]
920696600cSBjoern A. Zeeb			} else if (type[i] == "LABEL") {
930696600cSBjoern A. Zeeb				continue
940696600cSBjoern A. Zeeb			} else {
950696600cSBjoern A. Zeeb				print "unhandled type: " type[i]
960696600cSBjoern A. Zeeb				exit 1
970696600cSBjoern A. Zeeb			}
980696600cSBjoern A. Zeeb		}
990696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
1000696600cSBjoern A. Zeeb			if (cmd[i])
1010696600cSBjoern A. Zeeb				system(cmd[i])
1020696600cSBjoern A. Zeeb		}
1030696600cSBjoern A. Zeeb		exit 0
1040696600cSBjoern A. Zeeb	}
1050696600cSBjoern A. Zeeb}' dev="$rawdev"
1060696600cSBjoern A. Zeeb	gpart commit "$rootdev"
107efed7c6dSColin Percival	case "$FSTYPE" in
108efed7c6dSColin Percival	ufs)
1090696600cSBjoern A. Zeeb		growfs -y /dev/"$rootdev"
110efed7c6dSColin Percival		;;
111efed7c6dSColin Percival	zfs)
112efed7c6dSColin Percival		zpool online -e $pool $rootdev
113efed7c6dSColin Percival		;;
114efed7c6dSColin Percival	esac
1150696600cSBjoern A. Zeeb}
1160696600cSBjoern A. Zeeb
1170696600cSBjoern A. Zeebload_rc_config $name
1180696600cSBjoern A. Zeebrun_rc_command "$1"
119