git: 52f9722e6fff - main - bsdinstall: Limit default swap to maximum supported by kernel

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Wed, 15 Oct 2025 17:32:12 UTC
The branch main has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=52f9722e6ffff6fee309e6040b7b5313499a03ef

commit 52f9722e6ffff6fee309e6040b7b5313499a03ef
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2023-03-06 20:24:05 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-10-15 16:29:56 +0000

    bsdinstall: Limit default swap to maximum supported by kernel
    
    PR:             251993
    Reviewed by:    cperciva
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D53106
---
 usr.sbin/bsdinstall/partedit/part_wizard.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/bsdinstall/partedit/part_wizard.c b/usr.sbin/bsdinstall/partedit/part_wizard.c
index 90a8da1c3c9b..9146a2af782f 100644
--- a/usr.sbin/bsdinstall/partedit/part_wizard.c
+++ b/usr.sbin/bsdinstall/partedit/part_wizard.c
@@ -27,6 +27,7 @@
  */
 
 #include <sys/param.h>
+#include <sys/sysctl.h>
 
 #include <errno.h>
 #include <inttypes.h>
@@ -34,6 +35,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <libgeom.h>
 #include <bsddialog.h>
@@ -41,10 +43,29 @@
 #include "partedit.h"
 
 #define MIN_FREE_SPACE		(1023*1024*1024) /* Just under 1 GB */
-#define SWAP_SIZE(available)	MIN(available/20, 4*1024*1024*1024LL)
 
 static char *wizard_partition(struct gmesh *mesh, const char *disk);
 
+/*
+ * Determine default swap (partition) size in bytes for a given amount of free
+ * disk space in bytes.  The algorithm should likely be revisited in light of
+ * contemporary memory and disk sizes.
+ */
+static intmax_t
+swap_size(intmax_t available)
+{
+	intmax_t swapsize;
+	unsigned long swap_maxpages;
+	size_t sz;
+
+	swapsize = MIN(available/20, 4*1024*1024*1024LL);
+	sz = sizeof(swap_maxpages);
+	if (sysctlbyname("vm.swap_maxpages", &swap_maxpages, &sz, NULL, 0) == 0)
+		swapsize = MIN(swapsize, (intmax_t)swap_maxpages * getpagesize());
+
+	return (swapsize);
+}
+
 int
 part_wizard(const char *fsreq)
 {
@@ -383,7 +404,7 @@ wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
 		return (!retval); /* Editor -> return 0 */
 	}
 
-	swapsize = SWAP_SIZE(available);
+	swapsize = swap_size(available);
 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
 	    HN_NOSPACE | HN_DECIMAL);
 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,