3 # Copyright 2022 Michael J. Karels
4 # Copyright 2014 John-Mark Gurney
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 # Grow root partition to fill available space, optionally adding a swap
36 # partition at the end. This allows us to distribute an image and
37 # have it work on essentially any size drive.
39 # Note that this uses awk(1), and thus will not work if /usr is on a separate
40 # filesystem. We need to run early, because there might be not enough free
41 # space on rootfs for the boot to succeed, and on images we ship - which are
42 # the primary purpose of this script - there is no separate /usr anyway.
47 desc="Grow root partition to fill device"
48 start_cmd="growfs_start"
55 sysctl -b kern.geom.conftxt |
56 while read x1 _type _dev line
58 if [ "${_type}" = "DISK" -a -n "$(echo ${_search} | grep ${_dev})" ]; then
65 # Compute upper bound on swap partition size (if added), based on physmem
66 # and vm.swap_maxpages / 2 (the limit that elicits a warning).
67 # Rule for swap size based on memory size:
68 # up to 4 GB twice memory size
70 # over 8 GB memory size
73 memsize=$(sysctl -n hw.physmem)
74 memsizeMB=$(($memsize / (1024 * 1024)))
76 if [ $memsizeMB -lt 4096 ]
78 swapmax=$(($memsize * 2))
79 elif [ $memsizeMB -lt 8192 ]
81 swapmax=$((8192 * 1024 * 1024))
86 pagesize=$(sysctl -n hw.pagesize)
87 vm_swap_max=$(($(sysctl -n vm.swap_maxpages) / 2 * $pagesize))
89 if [ $swapmax -gt $vm_swap_max ]
96 # Find newly-added swap partition on parent device ($1).
99 swapdev=$(gpart list $1 | awk '
100 $2 == "Name:" { dev = $3 }
101 $1 == "type:" && $2 == "freebsd-swap" { swapdev = dev }
102 END { print swapdev }
110 echo "Growing root partition to fill device"
111 FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
112 FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
115 rootdev=${FSDEV#/dev/}
119 rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
122 echo "Don't know how to grow root filesystem type: $FSTYPE"
125 if [ x"$rootdev" = x"${rootdev%/*}" ]; then
129 rawdev=$(glabel status | awk -v rootdev=$rootdev 'index(rootdev, $1) { print $3; }')
130 if [ x"$rawdev" = x"" ]; then
131 echo "Can't figure out device for: $rootdev"
136 if [ x"diskid" = x"${rootdev%/*}" ]; then
142 diskdev=$(growfs_get_diskdev ${search})
143 if [ -z "${diskdev}" ]; then
147 # Check kenv for growfs_swap_size; if not present,
148 # check $growfs_swap_size from /etc/rc.conf.
149 # A value of 0 suppresses swap addition,
150 # "" (or unset) specifies the default;
151 # other values indicate the size in bytes.
152 # If default, check whether swap is already in fstab;
153 # if so, don't add another.
155 swapsize="$(kenv -q growfs_swap_size 2>/dev/null)"
159 "") case "$growfs_swap_size" in
165 $3 == "swap" { exit 1 }
171 *) swapsize="$growfs_swap_size"
178 swaplim=$(growfs_swap_max)
180 [ $verbose -eq 1 ] && {
181 echo "diskdev is $diskdev"
182 echo "search is $search"
183 echo "swapsize is $swapsize"
184 echo "swaplim is $swaplim"
187 sysctl -b kern.geom.conftxt | awk '
197 if (verbose) print lvl, type[lvl], $3
198 if (type[lvl] == "DISK") {
201 print "disksize ", disksize
202 # Do not add swap on disks under 15 GB (decimal) by default.
203 if (addswap == 1 && (size[lvl] > 15000000000 || swapsize > 0))
207 } else if (type[lvl] == "PART" && $11 == "freebsd-swap" && \
208 int(swapsize) == 0) {
209 # This finds swap only if it precedes root, e.g. preceding disk.
212 print "swap device exists, not adding swap"
215 for (i = 1; i <= lvl; i++) {
217 if (type[i] == "PART") {
220 print i, pdev, addswap, disksize, \
223 # Allow swap if current root is < 40% of disk.
224 if (parttype[i] != "MBR" && doing_swap == 1 && \
225 (size[i] / disksize < 0.4 || \
227 print "Adding swap partition"
228 if (int(swapsize) == 0) {
229 swapsize = int(disksize / 10)
230 if (swapsize > swaplim)
236 print "swapsize sectors",
238 align = 4 * 1024 * 1024 / sector
240 # Estimate offset for swap; let
241 # gpart compute actual start and size.
242 # Assume expansion all goes into this
243 # partition for MBR case.
244 if (parttype[i - 1] == "MBR") {
246 print "sz ", size[i - 1], \
247 " off ", offset[i - 1]
249 (size[i - 1] + offset[i - 1])
252 print "sz ", size[i], \
255 (size[i] + offset[i])
258 print "expand ", expand, \
260 swapbase = (expand + size[i]) / sector
261 swapbase -= swapsize + align
262 swapcmd = "gpart add -t freebsd-swap -a " align " -b " int(swapbase) " " pdev " && kenv growfs_swap_pdev=" pdev " >/dev/null; "
264 swapcmd = "set -x; gpart show; " swapcmd
266 cmd[i] = swapcmd "gpart resize -i " idx[i] " " pdev
267 if (parttype[i] == "GPT")
268 cmd[i] = "gpart recover " pdev " ; " cmd[i]
269 } else if (type[i] == "LABEL") {
272 print "unhandled type: " type[i]
276 for (i = 1; i <= lvl; i++) {
282 }' dev="$search" addswap="$addswap" swapsize="$swapsize" swaplim="$swaplim"
283 gpart commit "$diskdev" 2> /dev/null
286 growfs -y /dev/"$rootdev"
289 zpool online -e $pool $rootdev
293 # Get parent device of swap partition if one was added;
294 # if so, find swap device and label it.
295 pdev=$(kenv -q growfs_swap_pdev)
298 dev=$(growfs_last_swap "$pdev")
301 echo "Swap partition not found on $pdev"
304 glabel label -v growfs_swap $dev