xref: /freebsd/libexec/rc/rc.d/growfs (revision f99f0ee14e3af81c23150a6a340259ca8a33d01a)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
34a30d7bbSMike Karels# Copyright 2022 Michael J. Karels
40696600cSBjoern A. Zeeb# Copyright 2014 John-Mark Gurney
50696600cSBjoern A. Zeeb# All rights reserved.
60696600cSBjoern A. Zeeb#
70696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
80696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
90696600cSBjoern A. Zeeb# are met:
100696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
110696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
120696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
130696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
140696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
150696600cSBjoern A. Zeeb#
160696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260696600cSBjoern A. Zeeb# SUCH DAMAGE.
270696600cSBjoern A. Zeeb#
280696600cSBjoern A. Zeeb#
290696600cSBjoern A. Zeeb
300696600cSBjoern A. Zeeb# PROVIDE: growfs
31b68e6569SEdward Tomasz Napierala# REQUIRE: fsck
32b68e6569SEdward Tomasz Napierala# BEFORE: root
330696600cSBjoern A. Zeeb# KEYWORD: firstboot
340696600cSBjoern A. Zeeb
354a30d7bbSMike Karels# Grow root partition to fill available space, optionally adding a swap
364a30d7bbSMike Karels# partition at the end.  This allows us to distribute an image and
374a30d7bbSMike Karels# have it work on essentially any size drive.
38b68e6569SEdward Tomasz Napierala
39b68e6569SEdward Tomasz Napierala# Note that this uses awk(1), and thus will not work if /usr is on a separate
40b68e6569SEdward Tomasz Napierala# filesystem.  We need to run early, because there might be not enough free
41b68e6569SEdward Tomasz Napierala# space on rootfs for the boot to succeed, and on images we ship - which are
42b68e6569SEdward Tomasz Napierala# the primary purpose of this script - there is no separate /usr anyway.
430696600cSBjoern A. Zeeb
440696600cSBjoern A. Zeeb. /etc/rc.subr
450696600cSBjoern A. Zeeb
460696600cSBjoern A. Zeebname="growfs"
470696600cSBjoern A. Zeebdesc="Grow root partition to fill device"
480696600cSBjoern A. Zeebstart_cmd="growfs_start"
490696600cSBjoern A. Zeebstop_cmd=":"
500696600cSBjoern A. Zeebrcvar="growfs_enable"
510696600cSBjoern A. Zeeb
5204500107SScott Longgrowfs_get_diskdev()
5304500107SScott Long{
5404500107SScott Long	local _search=${1}
5504500107SScott Long	sysctl -b kern.geom.conftxt |
5604500107SScott Long	while read x1 _type _dev line
5704500107SScott Long	do
5804500107SScott Long		if [ "${_type}" = "DISK" -a -n "$(echo ${_search} | grep ${_dev})" ]; then
5904500107SScott Long			echo -n ${_dev}
6004500107SScott Long			break
6104500107SScott Long		fi
6204500107SScott Long	done
6304500107SScott Long}
6404500107SScott Long
654a30d7bbSMike Karels# Compute upper bound on swap partition size (if added), based on physmem
664a30d7bbSMike Karels# and vm.swap_maxpages / 2 (the limit that elicits a warning).
674a30d7bbSMike Karels# Rule for swap size based on memory size:
684a30d7bbSMike Karels#	up to 4 GB	twice memory size
694a30d7bbSMike Karels#	4 GB - 8 GB	8 GB
704a30d7bbSMike Karels#	over 8 GB	memory size
714a30d7bbSMike Karelsgrowfs_swap_max()
724a30d7bbSMike Karels{
734a30d7bbSMike Karels	memsize=$(sysctl -n hw.physmem)
744a30d7bbSMike Karels	memsizeMB=$(($memsize / (1024 * 1024)))
754a30d7bbSMike Karels
764a30d7bbSMike Karels	if  [ $memsizeMB -lt 4096 ]
774a30d7bbSMike Karels	then
784a30d7bbSMike Karels		swapmax=$(($memsize * 2))
794a30d7bbSMike Karels	elif  [ $memsizeMB -lt 8192 ]
804a30d7bbSMike Karels	then
814a30d7bbSMike Karels		swapmax=$((8192 * 1024 * 1024))
824a30d7bbSMike Karels	else
834a30d7bbSMike Karels		swapmax=$memsize
844a30d7bbSMike Karels	fi
854a30d7bbSMike Karels
864a30d7bbSMike Karels	pagesize=$(sysctl -n hw.pagesize)
874a30d7bbSMike Karels	vm_swap_max=$(($(sysctl -n vm.swap_maxpages) / 2 * $pagesize))
884a30d7bbSMike Karels
894a30d7bbSMike Karels	if [ $swapmax -gt $vm_swap_max ]
904a30d7bbSMike Karels	then
910e2fdab2SMike Karels		swapmax=$vm_swap_max
924a30d7bbSMike Karels	fi
934a30d7bbSMike Karels	echo -n "$swapmax"
944a30d7bbSMike Karels}
954a30d7bbSMike Karels
964a30d7bbSMike Karels# Find newly-added swap partition on parent device ($1).
974a30d7bbSMike Karelsgrowfs_last_swap()
984a30d7bbSMike Karels{
994a30d7bbSMike Karels	swapdev=$(gpart list $1 | awk '
1004a30d7bbSMike Karels		$2 == "Name:" { dev = $3 }
1014a30d7bbSMike Karels		$1 == "type:" && $2 == "freebsd-swap" { swapdev = dev }
1024a30d7bbSMike Karels		END { print swapdev }
1034a30d7bbSMike Karels	    ')
1044a30d7bbSMike Karels	echo -n $swapdev
1054a30d7bbSMike Karels}
1064a30d7bbSMike Karels
1070696600cSBjoern A. Zeebgrowfs_start()
1080696600cSBjoern A. Zeeb{
1094a30d7bbSMike Karels	verbose=0
1100696600cSBjoern A. Zeeb	echo "Growing root partition to fill device"
111efed7c6dSColin Percival	FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
112efed7c6dSColin Percival	FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
113efed7c6dSColin Percival	case "$FSTYPE" in
114efed7c6dSColin Percival	ufs)
115efed7c6dSColin Percival		rootdev=${FSDEV#/dev/}
116efed7c6dSColin Percival		;;
117efed7c6dSColin Percival	zfs)
118efed7c6dSColin Percival		pool=${FSDEV%%/*}
119c65b552fSKurt Lidl		rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
120efed7c6dSColin Percival		;;
121efed7c6dSColin Percival	*)
122efed7c6dSColin Percival		echo "Don't know how to grow root filesystem type: $FSTYPE"
123efed7c6dSColin Percival		return
124efed7c6dSColin Percival	esac
1250696600cSBjoern A. Zeeb	if [ x"$rootdev" = x"${rootdev%/*}" ]; then
1260696600cSBjoern A. Zeeb		# raw device
1270696600cSBjoern A. Zeeb		rawdev="$rootdev"
1280696600cSBjoern A. Zeeb	else
12904500107SScott Long		rawdev=$(glabel status | awk -v rootdev=$rootdev 'index(rootdev, $1) { print $3; }')
1300696600cSBjoern A. Zeeb		if [ x"$rawdev" = x"" ]; then
1310696600cSBjoern A. Zeeb			echo "Can't figure out device for: $rootdev"
1320696600cSBjoern A. Zeeb			return
1330696600cSBjoern A. Zeeb		fi
1340696600cSBjoern A. Zeeb	fi
1350696600cSBjoern A. Zeeb
13604500107SScott Long	if [ x"diskid" = x"${rootdev%/*}" ]; then
13704500107SScott Long		search=$rootdev
13804500107SScott Long	else
13904500107SScott Long		search=$rawdev
14004500107SScott Long	fi
14104500107SScott Long
14204500107SScott Long	diskdev=$(growfs_get_diskdev ${search})
14304500107SScott Long	if [ -z "${diskdev}" ]; then
14404500107SScott Long		diskdev=${rootdev}
14504500107SScott Long	fi
14604500107SScott Long
1474a30d7bbSMike Karels	# Check kenv for growfs_swap_size; if not present,
1484a30d7bbSMike Karels	# check $growfs_swap_size from /etc/rc.conf.
1494a30d7bbSMike Karels	# A value of 0 suppresses swap addition,
1504a30d7bbSMike Karels	# "" (or unset) specifies the default;
1514a30d7bbSMike Karels	# other values indicate the size in bytes.
1524a30d7bbSMike Karels	# If default, check whether swap is already in fstab;
1534a30d7bbSMike Karels	# if so, don't add another.
1544a30d7bbSMike Karels	addswap=1
1554a30d7bbSMike Karels	swapsize="$(kenv -q growfs_swap_size 2>/dev/null)"
1564a30d7bbSMike Karels	case "$swapsize" in
1574a30d7bbSMike Karels	"0")	addswap=0
1584a30d7bbSMike Karels		;;
1594a30d7bbSMike Karels	"")	case "$growfs_swap_size" in
1604a30d7bbSMike Karels		"0")	addswap=0
1614a30d7bbSMike Karels			;;
1624a30d7bbSMike Karels		"")
1634a30d7bbSMike Karels			if ! awk '
1644a30d7bbSMike Karels				/^#/ { next }
1654a30d7bbSMike Karels				$3 == "swap" { exit 1 }
1664a30d7bbSMike Karels			    ' < /etc/fstab
1674a30d7bbSMike Karels			then
1684a30d7bbSMike Karels				addswap=0
1694a30d7bbSMike Karels			fi
1704a30d7bbSMike Karels			;;
1714a30d7bbSMike Karels		*)	swapsize="$growfs_swap_size"
1724a30d7bbSMike Karels			;;
1734a30d7bbSMike Karels		esac
1744a30d7bbSMike Karels		;;
1754a30d7bbSMike Karels	*)	;;
1764a30d7bbSMike Karels	esac
1774a30d7bbSMike Karels
1784a30d7bbSMike Karels	swaplim=$(growfs_swap_max)
1794a30d7bbSMike Karels
1804a30d7bbSMike Karels	[ $verbose -eq 1 ] && {
1814a30d7bbSMike Karels		echo "diskdev is $diskdev"
1824a30d7bbSMike Karels		echo "search is $search"
1834a30d7bbSMike Karels		echo "swapsize is $swapsize"
1844a30d7bbSMike Karels		echo "swaplim is $swaplim"
1854a30d7bbSMike Karels	}
1864a30d7bbSMike Karels
1870696600cSBjoern A. Zeeb	sysctl -b kern.geom.conftxt | awk '
1880696600cSBjoern A. Zeeb{
1894a30d7bbSMike Karels	verbose = 0
1900696600cSBjoern A. Zeeb	lvl=$1
1910696600cSBjoern A. Zeeb	device[lvl] = $3
1920696600cSBjoern A. Zeeb	type[lvl] = $2
1930696600cSBjoern A. Zeeb	idx[lvl] = $7
1944a30d7bbSMike Karels	offset[lvl] = $9
1950696600cSBjoern A. Zeeb	parttype[lvl] = $13
1964a30d7bbSMike Karels	size[lvl] = $4
1974a30d7bbSMike Karels	if (verbose) print lvl, type[lvl], $3
1984a30d7bbSMike Karels	if (type[lvl] == "DISK") {
1994a30d7bbSMike Karels		disksize = size[lvl]
2004a30d7bbSMike Karels		if (verbose)
2014a30d7bbSMike Karels			print "disksize ", disksize
2024c8a2578SMike Karels		# Do not add swap on disks under 15 GB (decimal) by default.
2034a30d7bbSMike Karels		if (addswap == 1 && (size[lvl] > 15000000000 || swapsize > 0))
2044a30d7bbSMike Karels			doing_swap = 1
2054a30d7bbSMike Karels		else
2064a30d7bbSMike Karels			doing_swap = 0
2074a30d7bbSMike Karels	} else if (type[lvl] == "PART" && $11 == "freebsd-swap" && \
2084a30d7bbSMike Karels	    int(swapsize) == 0) {
2094a30d7bbSMike Karels		# This finds swap only if it precedes root, e.g. preceding disk.
2104a30d7bbSMike Karels		addswap = 0
2114a30d7bbSMike Karels		doing_swap = 0
2124a30d7bbSMike Karels		print "swap device exists, not adding swap"
2134a30d7bbSMike Karels	}
2140696600cSBjoern A. Zeeb	if (dev == $3) {
2150696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
2160696600cSBjoern A. Zeeb			# resize
2170696600cSBjoern A. Zeeb			if (type[i] == "PART") {
2180696600cSBjoern A. Zeeb				pdev = device[i - 1]
2194a30d7bbSMike Karels				if (verbose)
2204a30d7bbSMike Karels					print i, pdev, addswap, disksize, \
2214a30d7bbSMike Karels					    doing_swap
2224a30d7bbSMike Karels				swapcmd = ""
2234a30d7bbSMike Karels				# Allow swap if current root is < 40% of disk.
2244a30d7bbSMike Karels				if (parttype[i] != "MBR" && doing_swap == 1 && \
2254a30d7bbSMike Karels				    (size[i] / disksize < 0.4 || \
2264a30d7bbSMike Karels				    swapsize > 0)) {
2274a30d7bbSMike Karels					print "Adding swap partition"
2284a30d7bbSMike Karels					if (int(swapsize) == 0) {
2294a30d7bbSMike Karels						swapsize = int(disksize / 10)
2304a30d7bbSMike Karels						if (swapsize > swaplim)
2314a30d7bbSMike Karels							swapsize = swaplim
2324a30d7bbSMike Karels					}
2334a30d7bbSMike Karels					sector = $5
2344a30d7bbSMike Karels					swapsize /= sector
2354a30d7bbSMike Karels					if (verbose)
2364a30d7bbSMike Karels						print "swapsize sectors",
2374a30d7bbSMike Karels						    swapsize
2384a30d7bbSMike Karels					align = 4 * 1024 * 1024 / sector
2394a30d7bbSMike Karels
2404a30d7bbSMike Karels					# Estimate offset for swap; let
2414a30d7bbSMike Karels					# gpart compute actual start and size.
2424a30d7bbSMike Karels					# Assume expansion all goes into this
2434a30d7bbSMike Karels					# partition for MBR case.
2444a30d7bbSMike Karels					if (parttype[i - 1] == "MBR") {
2454a30d7bbSMike Karels					    if (verbose)
2464a30d7bbSMike Karels						print "sz ", size[i - 1], \
2474a30d7bbSMike Karels						    " off ", offset[i - 1]
2484a30d7bbSMike Karels					    expand = size[0] - \
2494a30d7bbSMike Karels						(size[i - 1] + offset[i - 1])
2504a30d7bbSMike Karels					} else {
2514a30d7bbSMike Karels					    if (verbose)
2524a30d7bbSMike Karels						print "sz ", size[i], \
2534a30d7bbSMike Karels						    " off ", offset[i]
2544a30d7bbSMike Karels					    expand = size[0] - \
2554a30d7bbSMike Karels						(size[i] + offset[i])
2564a30d7bbSMike Karels					}
2574a30d7bbSMike Karels					if (verbose)
2584a30d7bbSMike Karels					    print "expand ", expand, \
2594a30d7bbSMike Karels						" sz ", size[i]
2604a30d7bbSMike Karels					swapbase = (expand + size[i]) / sector
2614a30d7bbSMike Karels					swapbase -= swapsize + align
2620e2fdab2SMike Karels					swapcmd = "gpart add -t freebsd-swap -a " align " -b " int(swapbase) " " pdev " && kenv growfs_swap_pdev=" pdev " >/dev/null; "
2634a30d7bbSMike Karels					if (verbose)
2640e2fdab2SMike Karels						swapcmd = "set -x; gpart show; " swapcmd
2654a30d7bbSMike Karels				}
2664a30d7bbSMike Karels				cmd[i] = swapcmd "gpart resize -i " idx[i] " " pdev
2670696600cSBjoern A. Zeeb				if (parttype[i] == "GPT")
2680696600cSBjoern A. Zeeb					cmd[i] = "gpart recover " pdev " ; " cmd[i]
2690696600cSBjoern A. Zeeb			} else if (type[i] == "LABEL") {
2700696600cSBjoern A. Zeeb				continue
2710696600cSBjoern A. Zeeb			} else {
2720696600cSBjoern A. Zeeb				print "unhandled type: " type[i]
2730696600cSBjoern A. Zeeb				exit 1
2740696600cSBjoern A. Zeeb			}
2750696600cSBjoern A. Zeeb		}
2760696600cSBjoern A. Zeeb		for (i = 1; i <= lvl; i++) {
2770696600cSBjoern A. Zeeb			if (cmd[i])
2780696600cSBjoern A. Zeeb				system(cmd[i])
2790696600cSBjoern A. Zeeb		}
2800696600cSBjoern A. Zeeb		exit 0
2810696600cSBjoern A. Zeeb	}
2824a30d7bbSMike Karels}' dev="$search" addswap="$addswap" swapsize="$swapsize" swaplim="$swaplim"
28304500107SScott Long	gpart commit "$diskdev" 2> /dev/null
284efed7c6dSColin Percival	case "$FSTYPE" in
285efed7c6dSColin Percival	ufs)
2860696600cSBjoern A. Zeeb		growfs -y /dev/"$rootdev"
287efed7c6dSColin Percival		;;
288efed7c6dSColin Percival	zfs)
289efed7c6dSColin Percival		zpool online -e $pool $rootdev
290efed7c6dSColin Percival		;;
291efed7c6dSColin Percival	esac
2924a30d7bbSMike Karels
2934a30d7bbSMike Karels	# Get parent device of swap partition if one was added;
2944a30d7bbSMike Karels	# if so, find swap device and label it.
2954a30d7bbSMike Karels	pdev=$(kenv -q growfs_swap_pdev)
2964a30d7bbSMike Karels	if [ -n "$pdev" ]
2974a30d7bbSMike Karels	then
2984a30d7bbSMike Karels		dev=$(growfs_last_swap "$pdev")
2994a30d7bbSMike Karels		if [ -z "$dev" ]
3004a30d7bbSMike Karels		then
3014a30d7bbSMike Karels			echo "Swap partition not found on $pdev"
3024a30d7bbSMike Karels			exit 0
3034a30d7bbSMike Karels		fi
3044a30d7bbSMike Karels		glabel label -v growfs_swap $dev
3054a30d7bbSMike Karels	fi
3060696600cSBjoern A. Zeeb}
3070696600cSBjoern A. Zeeb
3080696600cSBjoern A. Zeebload_rc_config $name
309*f99f0ee1SAlexander Leidinger
310*f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: config setting
311*f99f0ee1SAlexander Leidingergrowfs_svcj="NO"
312*f99f0ee1SAlexander Leidinger
3130696600cSBjoern A. Zeebrun_rc_command "$1"
314