xref: /freebsd/libexec/rc/rc.d/growfs (revision b68e6569221ecade8899eaab8e1b088947ef2f8a)
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
31*b68e6569SEdward Tomasz Napierala# REQUIRE: fsck
32*b68e6569SEdward Tomasz Napierala# BEFORE: root
330696600cSBjoern A. Zeeb# KEYWORD: firstboot
340696600cSBjoern A. Zeeb
353ed737ebSJohn-Mark Gurney# This allows us to distribute an image
360696600cSBjoern A. Zeeb# and have it work on essentially any size drive.
37*b68e6569SEdward Tomasz Napierala
38*b68e6569SEdward Tomasz Napierala# Note that this uses awk(1), and thus will not work if /usr is on a separate
39*b68e6569SEdward Tomasz Napierala# filesystem.  We need to run early, because there might be not enough free
40*b68e6569SEdward Tomasz Napierala# space on rootfs for the boot to succeed, and on images we ship - which are
41*b68e6569SEdward Tomasz Napierala# the primary purpose of this script - there is no separate /usr anyway.
420696600cSBjoern A. Zeeb
430696600cSBjoern A. Zeeb. /etc/rc.subr
440696600cSBjoern A. Zeeb
450696600cSBjoern A. Zeebname="growfs"
460696600cSBjoern A. Zeebdesc="Grow root partition to fill device"
470696600cSBjoern A. Zeebstart_cmd="growfs_start"
480696600cSBjoern A. Zeebstop_cmd=":"
490696600cSBjoern A. Zeebrcvar="growfs_enable"
500696600cSBjoern A. Zeeb
510696600cSBjoern A. Zeebgrowfs_start ()
520696600cSBjoern A. Zeeb{
530696600cSBjoern A. Zeeb	echo "Growing root partition to fill device"
54efed7c6dSColin Percival	FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
55efed7c6dSColin Percival	FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
56efed7c6dSColin Percival	case "$FSTYPE" in
57efed7c6dSColin Percival	ufs)
58efed7c6dSColin Percival		rootdev=${FSDEV#/dev/}
59efed7c6dSColin Percival		;;
60efed7c6dSColin Percival	zfs)
61efed7c6dSColin Percival		pool=${FSDEV%%/*}
62c65b552fSKurt Lidl		rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
63efed7c6dSColin Percival		;;
64efed7c6dSColin Percival	*)
65efed7c6dSColin Percival		echo "Don't know how to grow root filesystem type: $FSTYPE"
66efed7c6dSColin Percival		return
67efed7c6dSColin Percival	esac
680696600cSBjoern A. Zeeb	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
690696600cSBjoern A. Zeeb		# raw device
700696600cSBjoern A. Zeeb		rawdev="$rootdev"
710696600cSBjoern A. Zeeb	else
720696600cSBjoern A. Zeeb		rawdev=$(glabel status | awk '$1 == "'"$rootdev"'" { print $3 }')
730696600cSBjoern A. Zeeb		if [ x"$rawdev" = x"" ]; then
740696600cSBjoern A. Zeeb			echo "Can't figure out device for: $rootdev"
750696600cSBjoern A. Zeeb			return
760696600cSBjoern A. Zeeb		fi
770696600cSBjoern A. Zeeb	fi
780696600cSBjoern A. Zeeb
790696600cSBjoern A. Zeeb	sysctl -b kern.geom.conftxt | awk '
800696600cSBjoern A. Zeeb{
810696600cSBjoern A. Zeeb	lvl=$1
820696600cSBjoern A. Zeeb	device[lvl] = $3
830696600cSBjoern A. Zeeb	type[lvl] = $2
840696600cSBjoern A. Zeeb	idx[lvl] = $7
850696600cSBjoern A. Zeeb	parttype[lvl] = $13
860696600cSBjoern A. Zeeb	if (dev == $3) {
870696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
880696600cSBjoern A. Zeeb			# resize
890696600cSBjoern A. Zeeb			if (type[i] == "PART") {
900696600cSBjoern A. Zeeb				pdev = device[i - 1]
910696600cSBjoern A. Zeeb				cmd[i] = "gpart resize -i " idx[i] " " pdev
920696600cSBjoern A. Zeeb				if (parttype[i] == "GPT")
930696600cSBjoern A. Zeeb					cmd[i] = "gpart recover " pdev " ; " cmd[i]
940696600cSBjoern A. Zeeb			} else if (type[i] == "LABEL") {
950696600cSBjoern A. Zeeb				continue
960696600cSBjoern A. Zeeb			} else {
970696600cSBjoern A. Zeeb				print "unhandled type: " type[i]
980696600cSBjoern A. Zeeb				exit 1
990696600cSBjoern A. Zeeb			}
1000696600cSBjoern A. Zeeb		}
1010696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
1020696600cSBjoern A. Zeeb			if (cmd[i])
1030696600cSBjoern A. Zeeb				system(cmd[i])
1040696600cSBjoern A. Zeeb		}
1050696600cSBjoern A. Zeeb		exit 0
1060696600cSBjoern A. Zeeb	}
1070696600cSBjoern A. Zeeb}' dev="$rawdev"
1080696600cSBjoern A. Zeeb	gpart commit "$rootdev"
109efed7c6dSColin Percival	case "$FSTYPE" in
110efed7c6dSColin Percival	ufs)
1110696600cSBjoern A. Zeeb		growfs -y /dev/"$rootdev"
112efed7c6dSColin Percival		;;
113efed7c6dSColin Percival	zfs)
114efed7c6dSColin Percival		zpool online -e $pool $rootdev
115efed7c6dSColin Percival		;;
116efed7c6dSColin Percival	esac
1170696600cSBjoern A. Zeeb}
1180696600cSBjoern A. Zeeb
1190696600cSBjoern A. Zeebload_rc_config $name
1200696600cSBjoern A. Zeebrun_rc_command "$1"
121