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
36 # Grow root partition to fill available space, optionally adding a swap
37 # partition at the end. This allows us to distribute an image and
38 # have it work on essentially any size drive.
40 # Note that this uses awk(1), and thus will not work if /usr is on a separate
41 # filesystem. We need to run early, because there might be not enough free
42 # space on rootfs for the boot to succeed, and on images we ship - which are
43 # the primary purpose of this script - there is no separate /usr anyway.
48 desc="Grow root partition to fill device"
49 start_cmd="growfs_start"
56 sysctl -b kern.geom.conftxt |
57 while read x1 _type _dev line
59 if [ "${_type}" = "DISK" -a -n "$(echo ${_search} | grep ${_dev})" ]; then
66 # Compute upper bound on swap partition size (if added), based on physmem
67 # and vm.swap_maxpages / 2 (the limit that elicits a warning).
68 # Rule for swap size based on memory size:
69 # up to 4 GB twice memory size
71 # over 8 GB memory size
74 memsize=$(sysctl -n hw.physmem)
75 memsizeMB=$(($memsize / (1024 * 1024)))
77 if [ $memsizeMB -lt 4096 ]
79 swapmax=$(($memsize * 2))
80 elif [ $memsizeMB -lt 8192 ]
82 swapmax=$((8192 * 1024 * 1024))
87 pagesize=$(sysctl -n hw.pagesize)
88 vm_swap_max=$(($(sysctl -n vm.swap_maxpages) / 2 * $pagesize))
90 if [ $swapmax -gt $vm_swap_max ]
97 # Find newly-added swap partition on parent device ($1).
100 swapdev=$(gpart list $1 | awk '
101 $2 == "Name:" { dev = $3 }
102 $1 == "type:" && $2 == "freebsd-swap" { swapdev = dev }
103 END { print swapdev }
111 echo "Growing root partition to fill device"
112 FSTYPE=$(mount -p | awk '{ if ( $2 == "/") { print $3 }}')
113 FSDEV=$(mount -p | awk '{ if ( $2 == "/") { print $1 }}')
116 rootdev=${FSDEV#/dev/}
120 rootdev=$(zpool list -v $pool | awk 'END { print $1 }')
123 echo "Don't know how to grow root filesystem type: $FSTYPE"
126 if [ x"$rootdev" = x"${rootdev%/*}" ]; then
130 rawdev=$(glabel status | awk -v rootdev=$rootdev 'index(rootdev, $1) { print $3; }')
131 if [ x"$rawdev" = x"" ]; then
132 echo "Can't figure out device for: $rootdev"
137 if [ x"diskid" = x"${rootdev%/*}" ]; then
143 diskdev=$(growfs_get_diskdev ${search})
144 if [ -z "${diskdev}" ]; then
148 # Check kenv for growfs_swap_size; if not present,
149 # check $growfs_swap_size from /etc/rc.conf.
150 # A value of 0 suppresses swap addition,
151 # "" (or unset) specifies the default;
152 # other values indicate the size in bytes.
153 # If default, check whether swap is already in fstab;
154 # if so, don't add another.
156 swapsize="$(kenv -q growfs_swap_size 2>/dev/null)"
160 "") case "$growfs_swap_size" in
166 $3 == "swap" { exit 1 }
172 *) swapsize="$growfs_swap_size"
179 swaplim=$(growfs_swap_max)
181 [ $verbose -eq 1 ] && {
182 echo "diskdev is $diskdev"
183 echo "search is $search"
184 echo "swapsize is $swapsize"
185 echo "swaplim is $swaplim"
188 sysctl -b kern.geom.conftxt | awk '
198 if (verbose) print lvl, type[lvl], $3
199 if (type[lvl] == "DISK") {
202 print "disksize ", disksize
203 # Do not add swap on disks under 15 GB (decimal) by default.
204 if (addswap == 1 && (size[lvl] > 15000000000 || swapsize > 0))
208 } else if (type[lvl] == "PART" && $11 == "freebsd-swap" && \
209 int(swapsize) == 0) {
210 # This finds swap only if it precedes root, e.g. preceding disk.
213 print "swap device exists, not adding swap"
216 for (i = 1; i <= lvl; i++) {
218 if (type[i] == "PART") {
221 print i, pdev, addswap, disksize, \
224 # Allow swap if current root is < 40% of disk.
225 if (parttype[i] != "MBR" && doing_swap == 1 && \
226 (size[i] / disksize < 0.4 || \
228 print "Adding swap partition"
229 if (int(swapsize) == 0) {
230 swapsize = int(disksize / 10)
231 if (swapsize > swaplim)
237 print "swapsize sectors",
239 align = 4 * 1024 * 1024 / sector
241 # Estimate offset for swap; let
242 # gpart compute actual start and size.
243 # Assume expansion all goes into this
244 # partition for MBR case.
245 if (parttype[i - 1] == "MBR") {
247 print "sz ", size[i - 1], \
248 " off ", offset[i - 1]
250 (size[i - 1] + offset[i - 1])
253 print "sz ", size[i], \
256 (size[i] + offset[i])
259 print "expand ", expand, \
261 swapbase = (expand + size[i]) / sector
262 swapbase -= swapsize + align
263 swapcmd = "gpart add -t freebsd-swap -a " align " -b " int(swapbase) " " pdev " && kenv growfs_swap_pdev=" pdev " >/dev/null; "
265 swapcmd = "set -x; gpart show; " swapcmd
267 cmd[i] = swapcmd "gpart resize -i " idx[i] " " pdev
268 if (parttype[i] == "GPT")
269 cmd[i] = "gpart recover " pdev " ; " cmd[i]
270 } else if (type[i] == "LABEL") {
273 print "unhandled type: " type[i]
277 for (i = 1; i <= lvl; i++) {
283 }' dev="$search" addswap="$addswap" swapsize="$swapsize" swaplim="$swaplim"
284 gpart commit "$diskdev" 2> /dev/null
287 growfs -y /dev/"$rootdev"
290 zpool online -e $pool $rootdev
294 # Get parent device of swap partition if one was added;
295 # if so, find swap device and label it.
296 pdev=$(kenv -q growfs_swap_pdev)
299 dev=$(growfs_last_swap "$pdev")
302 echo "Swap partition not found on $pdev"
305 glabel label -v growfs_swap $dev