Use temporary pool name while installing

This will allow running the script when the OS is already running off
a pool named "zroot".
This commit is contained in:
Jørn Åne de Jong 2022-05-25 10:59:20 +02:00 committed by Nico Schottelius
parent 7bac053757
commit 2a0b713a78
1 changed files with 22 additions and 21 deletions

View File

@ -19,13 +19,14 @@ DIST_BASE="https://download.freebsd.org/ftp/releases/$ARCH/$RELEASE"
CLSETUP_COMMIT=82d7d16ff14e1893f06f39788bb7cd8604284583
CLSETUP_URL="https://git.sr.ht/~jornane/clsetup/archive/$CLSETUP_COMMIT.tar.gz"
ZPOOL=zroot
ZPOOL_TMP="zinstalling"
ZFSTARGET="$(mktemp -d /var/tmp/zfsbuild.XXXXX)"
UFSTARGET="$(mktemp -d /var/tmp/ufsbuild.XXXXX)"
CLSETUP_WORK="$(mktemp -d /var/tmp/clsetup.XXXXX)"
if zpool list -Ho name $ZPOOL 2>/dev/null; then
echo "The pool $ZPOOL is already imported." >&2
if zpool list -Ho name $ZPOOL_TMP 2>/dev/null; then
echo "The pool $ZPOOL_TMP is already imported." >&2
exit 1
fi
@ -35,7 +36,7 @@ cleanup() {
umount "$UFSTARGET/tmp" ||:
umount "$UFSTARGET/var/tmp" ||:
umount "$UFSTARGET" ||:
zpool export $ZPOOL ||:
zpool export $ZPOOL_TMP ||:
mdconfig -du md0 ||:
mdconfig -du md1 ||:
rm -rf "$CLSETUP_WORK"
@ -81,27 +82,27 @@ gpart create -s gpt /dev/md0
gpart add -t freebsd-boot -l bootfs -b 40 -s 512K md0
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 md0
gpart add -t freebsd-zfs -l zfs0 -b 1M -s 5G md0
zpool create -O compression=on -o ashift=12 -o altroot="$ZFSTARGET" -m none $ZPOOL md0p2
zpool create -O compression=on -o ashift=12 -o altroot="$ZFSTARGET" -m none -t $ZPOOL_TMP $ZPOOL md0p2
zfs create -o mountpoint=none $ZPOOL/ROOT
zfs create -o mountpoint=none $ZPOOL_TMP/ROOT
# We set zstd-19 so our image will become smaller, at the cost of a longer build time.
# At the end of the process, we disable zstd-19 again using zfs inherit compression,
# but all files already written will remain zstd-19 compressed
zfs create -o mountpoint=/ -o canmount=noauto $ZPOOL/ROOT/default
mount -t zfs $ZPOOL/ROOT/default "$ZFSTARGET"
zpool set bootfs=$ZPOOL/ROOT/default $ZPOOL
zfs create -o mountpoint=/ -o canmount=noauto $ZPOOL_TMP/ROOT/default
mount -t zfs $ZPOOL_TMP/ROOT/default "$ZFSTARGET"
zpool set bootfs=$ZPOOL_TMP/ROOT/default $ZPOOL_TMP
zfs create -o mountpoint=/tmp -o exec=on -o setuid=off $ZPOOL/tmp
zfs create -o canmount=off -o mountpoint=/usr $ZPOOL/usr
zfs create $ZPOOL/usr/home
zfs create -o exec=off -o setuid=off $ZPOOL/usr/src
zfs create -o mountpoint=/usr/ports -o setuid=off $ZPOOL/usr/ports
zfs create -o canmount=off -o mountpoint=/var $ZPOOL/var
zfs create -o exec=off -o setuid=off $ZPOOL/var/audit
zfs create -o exec=off -o setuid=off $ZPOOL/var/crash
zfs create -o exec=off -o setuid=off $ZPOOL/var/log
zfs create -o atime=on -o exec=off -o setuid=off $ZPOOL/var/mail
zfs create -o exec=on -o setuid=off $ZPOOL/var/tmp
zfs create -o mountpoint=/tmp -o exec=on -o setuid=off $ZPOOL_TMP/tmp
zfs create -o canmount=off -o mountpoint=/usr $ZPOOL_TMP/usr
zfs create $ZPOOL_TMP/usr/home
zfs create -o exec=off -o setuid=off $ZPOOL_TMP/usr/src
zfs create -o mountpoint=/usr/ports -o setuid=off $ZPOOL_TMP/usr/ports
zfs create -o canmount=off -o mountpoint=/var $ZPOOL_TMP/var
zfs create -o exec=off -o setuid=off $ZPOOL_TMP/var/audit
zfs create -o exec=off -o setuid=off $ZPOOL_TMP/var/crash
zfs create -o exec=off -o setuid=off $ZPOOL_TMP/var/log
zfs create -o atime=on -o exec=off -o setuid=off $ZPOOL_TMP/var/mail
zfs create -o exec=on -o setuid=off $ZPOOL_TMP/var/tmp
ln -s /usr/home "$ZFSTARGET/home"
chmod 1777 "$ZFSTARGET/var/tmp"
@ -169,13 +170,13 @@ rm -rf "$UFSTARGET/var/db/freebsd-update/"* ||:
# This will make the base image smaller, at the cost of taking longer to generate, as zstd-19 is slow to write
# Therefore, afterwards we restore compression to default, so written files stay zstd-19, which is fast to read,
# but files written by the user afterwards will be written with the default compression algorihtm.
zfs set compression=zstd-19 $ZPOOL/ROOT/default
zfs set compression=zstd-19 $ZPOOL_TMP/ROOT/default
umount "$UFSTARGET/dev" "$UFSTARGET/tmp" "$UFSTARGET/var/tmp"
rsync -aH --fileflags --inplace "$UFSTARGET/." "$ZFSTARGET"
sysrc -f "$UFSTARGET/boot/loader.conf" -x zfs_load
printf '%s\t%s\t\t%s\t%s\t%s\t%s\n' /dev/gpt/rootfs / ufs rw,noatime 1 1 >>"$UFSTARGET/etc/fstab"
sync ||:
zfs inherit compression $ZPOOL/ROOT/default
zfs inherit compression $ZPOOL_TMP/ROOT/default
trap : EXIT
cleanup