From nobody Sun Dec 11 18:52:08 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NVYlZ0FQbz4kB4D; Sun, 11 Dec 2022 18:52:10 +0000 (UTC) (envelope-from gbe@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NVYlY6pGfz3nJZ; Sun, 11 Dec 2022 18:52:09 +0000 (UTC) (envelope-from gbe@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670784730; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PWjP7iNd78Nh3L2eNNGVOIKlKxC10hmv/fxDBPBCgmI=; b=aegL8fqS/N5WIpyyB/8Te6dom3cSX2AcL6xz6loW+2PzwDjhlNAp4cgZNPlTvo0hoKHREI 9pIq+9o3sK2Ql20kwzimoikBLIoo85GRMFfs3BzR0xTu1EX6EiN5HsCCw4Z/YVVUJTr4nC 09VzaQF6hDMVKpH6qIQYPXxnljN3tbT3/3kYNTdmNw2kb8j/PkmATiToMwwMTtz6exLhS9 q4BzHoz4BYLSkTbVfbH1UdSZKlTqB9u5NmLv7cjZAGFqLnSqhhtlmGkSKrhe7xY/Yqfqlc 0NIP4lAQ00V9U3Kn3pVyYU5/iDdbqT7BfCeF3CgnuavqhEmqSFEwnrkXSg5BaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670784730; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PWjP7iNd78Nh3L2eNNGVOIKlKxC10hmv/fxDBPBCgmI=; b=J4Mr03cpTO31T8FSrW5jp+DbqP6vCXLG5viS/unB4trvVaivq896PMtdGMiFjBxaeG7E9+ 7Y7Kt18O3fT6Htyj6mSlwjABzoeyWk3qmuJOE1LuGhP/mZorDf1KWLX3BY2xjCpXUOAm0H Esb/qMLBtaHWpEu0cJQp6DcqEbof2HXs1zy9/FQayxZ2yfcQmvoqxG6pf/uEY5nPdVCZuF iLIWI9C19+ILLH6usSclQycg1ITEbwRwVQL09md9y7Pgsie5COsyMOiX3StX0fx0Ge+Bpq xO8wlK2u0Z9Ln52ulL+5h/Q9bM+KR2AN7BJrB4XcFS8XEljq+wT16SrwM5wA6A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1670784730; a=rsa-sha256; cv=none; b=ibRGkJrHxfaYaVNNRnRpMvSXmGW6JlpQfCp1ApyrgBWAf9qo6YWPGx3Vt+irVNL2uAGEk7 ifHrPYG6EwC98JOYype5JfIDrBJKQykCxKXHSmcumOVrxZaa5p+AsfyAOvaPVocgjCxo5n Vtt8njWf/yI5CLkIFufKCHPcFqBsG/UrcC5BImGGKWUmE+0cK3pnGwdU2StsEY5LNDMB1a TGBeHFYp4YbuJgLgVFF5dw2arYhoaKJquoCwqgtaXhsGlf6jQU4JhmHnS/B0MJQ8l+5Jcd sgf46X4dSd7m318BVHDErD7Q1kXFMXOFlf3DtlqndAX0AUh3izjmTedNFHOs5A== Received: from localhost (p200300cb870eb3fe39fa1400772ae971.dip0.t-ipconnect.de [IPv6:2003:cb:870e:b3fe:39fa:1400:772a:e971]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: gbe) by smtp.freebsd.org (Postfix) with ESMTPSA id 4NVYlY27kNzTPw; Sun, 11 Dec 2022 18:52:09 +0000 (UTC) (envelope-from gbe@freebsd.org) Date: Sun, 11 Dec 2022 19:52:08 +0100 From: Gordon Bergling To: Mike Karels Cc: Gordon Bergling , Mike Karels , src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 4a30d7bb373c - main - growfs script: add swap partition as well as growing root Message-ID: References: <202212101941.2BAJfDx0085242@gitrepo.freebsd.org> <219B1ADF-9000-4E68-9740-2D1888AAA234@karels.net> List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="t/2vo3T+oCZqq0p5" Content-Disposition: inline In-Reply-To: <219B1ADF-9000-4E68-9740-2D1888AAA234@karels.net> X-Url: X-Operating-System: FreeBSD 13.1-STABLE amd64 X-Host-Uptime: 7:49PM up 4 days, 2:51, 2 users, load averages: 0.04, 0.17, 0.16 X-ThisMailContainsUnwantedMimeParts: N --t/2vo3T+oCZqq0p5 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Mike On Sun, Dec 11, 2022 at 07:26:46AM -0600, Mike Karels wrote: > On 11 Dec 2022, at 4:09, Gordon Bergling wrote: > > thanks for implementation. After the MFC and the new -STABLE snapshots > > I am really looking forward to try this out an my RPi4B. >=20 > You are welcome, but I hadn=E2=80=99t decided yet whether this should be = MFC=E2=80=99d. > That depends in part on how it seems to work out, but I am interested > in opinions on that. >=20 > Mike a MFC to stable/13 would be very welcomed. Once I find the time I test this on -CURRENT. --Gordon > > On Sat, Dec 10, 2022 at 07:41:13PM +0000, Mike Karels wrote: > >> The branch main has been updated by karels: > >> > >> URL: https://cgit.FreeBSD.org/src/commit/?id=3D4a30d7bb373c08f42f953b9= cd1e793e236b4cd92 > >> > >> commit 4a30d7bb373c08f42f953b9cd1e793e236b4cd92 > >> Author: Mike Karels > >> AuthorDate: 2022-12-10 19:38:36 +0000 > >> Commit: Mike Karels > >> CommitDate: 2022-12-10 19:38:36 +0000 > >> > >> growfs script: add swap partition as well as growing root > >> > >> Add the ability to create a swap partition in the course of growing > >> the root file system on first boot, enabling by default. The defa= ult > >> rules are: add swap if the disk is at least 15 GB (decimal), and t= he > >> existing root is less than 40% of the disk. The default size is 1= 0% > >> of the disk, but is limited by the memory size. The limit is twice > >> memory size up to 4 GB, 8 GB up to 8 GB memory, and memory size ov= er > >> 8 GB memory. Swap size is clamped at vm.swap_maxpages/2 as well. > >> The new swap partition is labeled as "growfs_swap". > >> > >> The default behavior can be overridden by setting growfs_swap_size= in > >> /etc/rc.conf or in the kernel environment, with kenv taking priori= ty. > >> A value of 0 inhibits the addition of swap, an empty value specifi= es > >> the default, and other values indicate a swap size in bytes. > >> > >> By default, addition of swap is inhibited if a swap partition is f= ound > >> in the output of the sysctl kern.geom.conftxt before the current r= oot > >> partition, usually meaning that there is another disk present. > >> Swap space is not added if one is already present in /etc/fstab. > >> > >> The root partition is read-only when growfs runs, so /etc/fstab can > >> not be modified. That step is handled by a new growfs_fstab scrip= t, > >> added in a separate commit. Set the value "growfs_swap_pdev" in k= env > >> to indicate that this should be done, as well as for internal use. > >> > >> There is optional verbose output meant for debugging; it can only = be > >> enabled by modifying the script (in two places, for sh and awk). > >> This should be removed before release, after testing on -current. > >> > >> Discussed with: cperciva > >> Reviewed by: imp (previous version) > >> Differential Revision: https://reviews.freebsd.org/D37462 > >> --- > >> libexec/rc/rc.d/growfs | 178 ++++++++++++++++++++++++++++++++++++++++= +++++++-- > >> 1 file changed, 172 insertions(+), 6 deletions(-) > >> > >> diff --git a/libexec/rc/rc.d/growfs b/libexec/rc/rc.d/growfs > >> index 5402bd442279..3c48a7dca6b2 100755 > >> --- a/libexec/rc/rc.d/growfs > >> +++ b/libexec/rc/rc.d/growfs > >> @@ -1,5 +1,6 @@ > >> #!/bin/sh > >> # > >> +# Copyright 2022 Michael J. Karels > >> # Copyright 2014 John-Mark Gurney > >> # All rights reserved. > >> # > >> @@ -32,8 +33,9 @@ > >> # BEFORE: root > >> # KEYWORD: firstboot > >> > >> -# This allows us to distribute an image > >> -# and have it work on essentially any size drive. > >> +# Grow root partition to fill available space, optionally adding a sw= ap > >> +# partition at the end. This allows us to distribute an image and > >> +# have it work on essentially any size drive. > >> > >> # Note that this uses awk(1), and thus will not work if /usr is on a = separate > >> # filesystem. We need to run early, because there might be not enoug= h free > >> @@ -48,7 +50,7 @@ start_cmd=3D"growfs_start" > >> stop_cmd=3D":" > >> rcvar=3D"growfs_enable" > >> > >> -growfs_get_diskdev () > >> +growfs_get_diskdev() > >> { > >> local _search=3D${1} > >> sysctl -b kern.geom.conftxt | > >> @@ -61,8 +63,51 @@ growfs_get_diskdev () > >> done > >> } > >> > >> -growfs_start () > >> +# Compute upper bound on swap partition size (if added), based on phy= smem > >> +# and vm.swap_maxpages / 2 (the limit that elicits a warning). > >> +# Rule for swap size based on memory size: > >> +# up to 4 GB twice memory size > >> +# 4 GB - 8 GB 8 GB > >> +# over 8 GB memory size > >> +growfs_swap_max() > >> { > >> + memsize=3D$(sysctl -n hw.physmem) > >> + memsizeMB=3D$(($memsize / (1024 * 1024))) > >> + > >> + if [ $memsizeMB -lt 4096 ] > >> + then > >> + swapmax=3D$(($memsize * 2)) > >> + elif [ $memsizeMB -lt 8192 ] > >> + then > >> + swapmax=3D$((8192 * 1024 * 1024)) > >> + else > >> + swapmax=3D$memsize > >> + fi > >> + > >> + pagesize=3D$(sysctl -n hw.pagesize) > >> + vm_swap_max=3D$(($(sysctl -n vm.swap_maxpages) / 2 * $pagesize)) > >> + > >> + if [ $swapmax -gt $vm_swap_max ] > >> + then > >> + $swapmax=3D$vm_swap_max > >> + fi > >> + echo -n "$swapmax" > >> +} > >> + > >> +# Find newly-added swap partition on parent device ($1). > >> +growfs_last_swap() > >> +{ > >> + swapdev=3D$(gpart list $1 | awk ' > >> + $2 =3D=3D "Name:" { dev =3D $3 } > >> + $1 =3D=3D "type:" && $2 =3D=3D "freebsd-swap" { swapdev =3D dev } > >> + END { print swapdev } > >> + ') > >> + echo -n $swapdev > >> +} > >> + > >> +growfs_start() > >> +{ > >> + verbose=3D0 > >> echo "Growing root partition to fill device" > >> FSTYPE=3D$(mount -p | awk '{ if ( $2 =3D=3D "/") { print $3 }}') > >> FSDEV=3D$(mount -p | awk '{ if ( $2 =3D=3D "/") { print $1 }}') > >> @@ -100,19 +145,126 @@ growfs_start () > >> diskdev=3D${rootdev} > >> fi > >> > >> + # Check kenv for growfs_swap_size; if not present, > >> + # check $growfs_swap_size from /etc/rc.conf. > >> + # A value of 0 suppresses swap addition, > >> + # "" (or unset) specifies the default; > >> + # other values indicate the size in bytes. > >> + # If default, check whether swap is already in fstab; > >> + # if so, don't add another. > >> + addswap=3D1 > >> + swapsize=3D"$(kenv -q growfs_swap_size 2>/dev/null)" > >> + case "$swapsize" in > >> + "0") addswap=3D0 > >> + ;; > >> + "") case "$growfs_swap_size" in > >> + "0") addswap=3D0 > >> + ;; > >> + "") > >> + if ! awk ' > >> + /^#/ { next } > >> + $3 =3D=3D "swap" { exit 1 } > >> + ' < /etc/fstab > >> + then > >> + addswap=3D0 > >> + fi > >> + ;; > >> + *) swapsize=3D"$growfs_swap_size" > >> + ;; > >> + esac > >> + ;; > >> + *) ;; > >> + esac > >> + > >> + swaplim=3D$(growfs_swap_max) > >> + > >> + [ $verbose -eq 1 ] && { > >> + echo "diskdev is $diskdev" > >> + echo "search is $search" > >> + echo "swapsize is $swapsize" > >> + echo "swaplim is $swaplim" > >> + } > >> + > >> sysctl -b kern.geom.conftxt | awk ' > >> { > >> + verbose =3D 0 > >> lvl=3D$1 > >> device[lvl] =3D $3 > >> type[lvl] =3D $2 > >> idx[lvl] =3D $7 > >> + offset[lvl] =3D $9 > >> parttype[lvl] =3D $13 > >> + size[lvl] =3D $4 > >> + if (verbose) print lvl, type[lvl], $3 > >> + if (type[lvl] =3D=3D "DISK") { > >> + disksize =3D size[lvl] > >> + if (verbose) > >> + print "disksize ", disksize > >> + # Don't add swap on disks under 15 GB (decimal) by default. > >> + if (addswap =3D=3D 1 && (size[lvl] > 15000000000 || swapsize > 0)) > >> + doing_swap =3D 1 > >> + else > >> + doing_swap =3D 0 > >> + } else if (type[lvl] =3D=3D "PART" && $11 =3D=3D "freebsd-swap" && \ > >> + int(swapsize) =3D=3D 0) { > >> + # This finds swap only if it precedes root, e.g. preceding disk. > >> + addswap =3D 0 > >> + doing_swap =3D 0 > >> + print "swap device exists, not adding swap" > >> + } > >> if (dev =3D=3D $3) { > >> for (i =3D 1; i <=3D lvl; i++) { > >> # resize > >> if (type[i] =3D=3D "PART") { > >> pdev =3D device[i - 1] > >> - cmd[i] =3D "gpart resize -i " idx[i] " " pdev > >> + if (verbose) > >> + print i, pdev, addswap, disksize, \ > >> + doing_swap > >> + swapcmd =3D "" > >> + # Allow swap if current root is < 40% of disk. > >> + if (parttype[i] !=3D "MBR" && doing_swap =3D=3D 1 && \ > >> + (size[i] / disksize < 0.4 || \ > >> + swapsize > 0)) { > >> + print "Adding swap partition" > >> + if (int(swapsize) =3D=3D 0) { > >> + swapsize =3D int(disksize / 10) > >> + if (swapsize > swaplim) > >> + swapsize =3D swaplim > >> + } > >> + sector =3D $5 > >> + swapsize /=3D sector > >> + if (verbose) > >> + print "swapsize sectors", > >> + swapsize > >> + align =3D 4 * 1024 * 1024 / sector > >> + > >> + # Estimate offset for swap; let > >> + # gpart compute actual start and size. > >> + # Assume expansion all goes into this > >> + # partition for MBR case. > >> + if (parttype[i - 1] =3D=3D "MBR") { > >> + if (verbose) > >> + print "sz ", size[i - 1], \ > >> + " off ", offset[i - 1] > >> + expand =3D size[0] - \ > >> + (size[i - 1] + offset[i - 1]) > >> + } else { > >> + if (verbose) > >> + print "sz ", size[i], \ > >> + " off ", offset[i] > >> + expand =3D size[0] - \ > >> + (size[i] + offset[i]) > >> + } > >> + if (verbose) > >> + print "expand ", expand, \ > >> + " sz ", size[i] > >> + swapbase =3D (expand + size[i]) / sector > >> + swapbase -=3D swapsize + align > >> + swapcmd =3D "gpart add -t freebsd-swap -a " align " -b " swapbas= e " " pdev "; kenv growfs_swap_pdev=3D" pdev " >/dev/null; " > >> + if (verbose) > >> + swapcmd =3D "set -x; " swapcmd > >> + } > >> + cmd[i] =3D swapcmd "gpart resize -i " idx[i] " " pdev > >> if (parttype[i] =3D=3D "GPT") > >> cmd[i] =3D "gpart recover " pdev " ; " cmd[i] > >> } else if (type[i] =3D=3D "LABEL") { > >> @@ -128,7 +280,7 @@ growfs_start () > >> } > >> exit 0 > >> } > >> -}' dev=3D"$search" > >> +}' dev=3D"$search" addswap=3D"$addswap" swapsize=3D"$swapsize" swapli= m=3D"$swaplim" > >> gpart commit "$diskdev" 2> /dev/null > >> case "$FSTYPE" in > >> ufs) > >> @@ -138,6 +290,20 @@ growfs_start () > >> zpool online -e $pool $rootdev > >> ;; > >> esac > >> + > >> + # Get parent device of swap partition if one was added; > >> + # if so, find swap device and label it. > >> + pdev=3D$(kenv -q growfs_swap_pdev) > >> + if [ -n "$pdev" ] > >> + then > >> + dev=3D$(growfs_last_swap "$pdev") > >> + if [ -z "$dev" ] > >> + then > >> + echo "Swap partition not found on $pdev" > >> + exit 0 > >> + fi > >> + glabel label -v growfs_swap $dev > >> + fi > >> } > >> > >> load_rc_config $name > >> > > > > -- --=20 --t/2vo3T+oCZqq0p5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGTBAEBCgB9FiEEYbWI0KY5X7yH/Fy4OQX2V8rP09wFAmOWJtZfFIAAAAAALgAo aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDYx QjU4OEQwQTYzOTVGQkM4N0ZDNUNCODM5MDVGNjU3Q0FDRkQzREMACgkQOQX2V8rP 09zUaQf/easCcQeTvAjXQaCsuz2l/A2DY7xpZEPrn5cWvYxZ1Xuf7ax559oIQ0Zj H7zTjxAoRyjNgnVQwSbxcZVivWj6NZXCEjkA6eRoJD9y3N+F5/wv6gKdXGhZ/5Gl XmoWXQ1UeTPJlj3mrHy9TdlkpQrSezDzeocP5CnPzdXiDWxt9CSQ5cD3TRjaLrOo VHQXr3H9F8DBGh4BoWGK8q+Wv5MeTIyN4jI/6qJfAoj2xFuHzGqQfyoQH9i8zOuC Gyrbx6ik6l0eq0qJF7TGLsHYcJjIthMQwJuI6X5G8eJSGiYoDtDe1Vk2uDGBDPnq cSg2GITYWBR5rjE2pbK32wJZ2ta9uw== =cc+h -----END PGP SIGNATURE----- --t/2vo3T+oCZqq0p5--