git: 3d3f2fb069d9 - main - stand: Make makeroot.sh use makefs for ZFS

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 16 Aug 2022 21:39:44 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=3d3f2fb069d972785763e7ed4244d62e4e85cce0

commit 3d3f2fb069d972785763e7ed4244d62e4e85cce0
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-08-16 17:29:25 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-08-16 21:36:55 +0000

    stand: Make makeroot.sh use makefs for ZFS
    
    Start to use makefs for ZFS. This covers the gpt nogeli variants.  ZFS
    MBR booting is tricky and complicated, so will need some additional
    tweaks that makefs/mkimg isn't able to do at the moment. This means that
    all gpt nogeli amd64 combinations can be built w/o root.
    
    In addition, tweak the generated qemu.sh files to use stdio for the
    console. We grep the output for SUCCESS and report each of the booting
    types. Create a all.sh that will run these automatically. These all can
    also run w/o root.
    
    In the future, I'll add support for a make univers followed by this
    script to create other architectures' tests and/or generate stand tests
    for /usr/tests...
    
    Sponsored by: Netflix
---
 tools/boot/rootgen.sh | 173 ++++++++++++++++++++++++--------------------------
 1 file changed, 82 insertions(+), 91 deletions(-)

diff --git a/tools/boot/rootgen.sh b/tools/boot/rootgen.sh
index 80554fb1b77d..f087afa5ad49 100755
--- a/tools/boot/rootgen.sh
+++ b/tools/boot/rootgen.sh
@@ -35,9 +35,9 @@ cpsys() {
 }
 
 ufs_fstab() {
-    src=$1
+    dir=$1
 
-    cat > ${src}/etc/fstab <<EOF
+    cat > ${dir}/etc/fstab <<EOF
 /dev/ufs/root	/		ufs	rw	1	1
 EOF
 }
@@ -83,6 +83,23 @@ mk_nogeli_gpt_ufs_both() {
     rm -f ${src}/etc/fstab
 }
 
+# XXX should not assume host == target
+zfs_extra()
+{
+    src=$1
+    dst=$2
+
+    mkdir -p $dst
+    mkdir -p $dst/boot/kernel
+    cat > ${dst}/boot/loader.conf.local <<EOF
+cryptodev_load=YES
+zfs_load=YES
+EOF
+    cp /boot/kernel/acl_nfs4.ko ${dst}/boot/kernel/acl_nfs4.ko
+    cp /boot/kernel/cryptodev.ko ${dst}/boot/kernel/cryptodev.ko
+    cp /boot/kernel/zfs.ko ${dst}/boot/kernel/zfs.ko
+}
+
 mk_nogeli_gpt_zfs_legacy() {
     src=$1
     img=$2
@@ -92,34 +109,17 @@ mk_nogeli_gpt_zfs_legacy() {
     fs=$6
     bios=$7
     pool=nogeli-gpt-zfs-legacy
+    dst=$img.extra
 
-    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
-    md=$(mdconfig -f ${img})
-    gpart create -s gpt ${md}
-    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
-    gpart add -t freebsd-zfs -l root $md
-    # install-boot will make this bootable
-    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p2
-    zpool set bootfs=${pool} ${pool}
-    zfs create -po mountpoint=/ ${pool}/ROOT/default
-    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
-    cpsys ${src} ${mntpt}
-    # need to make a couple of tweaks
-    cat >> ${mntpt}/boot/loader.conf <<EOF
-cryptodev_load=YES
-zfs_load=YES
-EOF
-    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
-    cp /boot/kernel/cryptodev.ko ${mntpt}/boot/kernel/cryptodev.ko
-    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
-    # end tweaks
-    zfs umount -f ${pool}/ROOT/default
-    zfs set mountpoint=none ${pool}/ROOT/default
-    zpool set bootfs=${pool}/ROOT/default ${pool}
-    zpool set autoexpand=on ${pool}
-    zpool export ${pool}
-    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
-    mdconfig -d -u ${md}
+    zfs_extra $src $dst
+    makefs -t zfs -s 200m \
+	-o poolname=${pool} -o bootfs=${pool} -o rootpath=/ \
+	${img}.p2 ${src} ${dst}
+    mkimg -b ${src}/boot/pmbr -s gpt \
+	  -p freebsd-boot:=/boot/gptzfsboot \
+	  -p freebsd-zfs:=${img}.p2 \
+	  -o ${img}
+    rm -rf ${dst}
 }
 
 mk_nogeli_gpt_zfs_uefi() {
@@ -131,34 +131,18 @@ mk_nogeli_gpt_zfs_uefi() {
     fs=$6
     bios=$7
     pool=nogeli-gpt-zfs-uefi
+    dst=$img.extra
 
-    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
-    md=$(mdconfig -f ${img})
-    gpart create -s gpt ${md}
-    gpart add -t efi -s ${espsize}k -a 4k ${md}
-    gpart add -t freebsd-zfs -l root $md
-    # install-boot will make this bootable
-    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p2
-    zpool set bootfs=${pool} ${pool}
-    zfs create -po mountpoint=/ ${pool}/ROOT/default
-    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
-    cpsys ${src} ${mntpt}
-    # need to make a couple of tweaks
-    cat >> ${mntpt}/boot/loader.conf <<EOF
-cryptodev_load=YES
-zfs_load=YES
-EOF
-    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
-    cp /boot/kernel/cryptodev.ko ${mntpt}/boot/kernel/cryptodev.ko
-    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
-    # end tweaks
-    zfs umount -f ${pool}/ROOT/default
-    zfs set mountpoint=none ${pool}/ROOT/default
-    zpool set bootfs=${pool}/ROOT/default ${pool}
-    zpool set autoexpand=on ${pool}
-    zpool export ${pool}
-    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
-    mdconfig -d -u ${md}
+    zfs_extra $src $dst
+    make_esp_file ${img}.p1 ${espsize} ${src}/boot/loader.efi
+    makefs -t zfs -s 200m \
+	-o poolname=${pool} -o bootfs=${pool} -o rootpath=/ \
+	${img}.p2 ${src} ${dst}
+    mkimg -b ${src}/boot/pmbr -s gpt \
+	  -p efi:=${img}.p1 \
+	  -p freebsd-zfs:=${img}.p2 \
+	  -o ${img}
+    rm -rf ${dst}
 }
 
 mk_nogeli_gpt_zfs_both() {
@@ -170,35 +154,19 @@ mk_nogeli_gpt_zfs_both() {
     fs=$6
     bios=$7
     pool=nogeli-gpt-zfs-both
+    dst=$img.extra
 
-    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
-    md=$(mdconfig -f ${img})
-    gpart create -s gpt ${md}
-    gpart add -t efi -s ${espsize}k -a 4k ${md}
-    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
-    gpart add -t freebsd-zfs -l root $md
-    # install-boot will make this bootable
-    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p3
-    zpool set bootfs=${pool} ${pool}
-    zfs create -po mountpoint=/ ${pool}/ROOT/default
-    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
-    cpsys ${src} ${mntpt}
-    # need to make a couple of tweaks
-    cat >> ${mntpt}/boot/loader.conf <<EOF
-cryptodev_load=YES
-zfs_load=YES
-EOF
-    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
-    cp /boot/kernel/cryptodev.ko ${mntpt}/boot/kernel/cryptodev.ko
-    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
-    # end tweaks
-    zfs umount -f ${pool}/ROOT/default
-    zfs set mountpoint=none ${pool}/ROOT/default
-    zpool set bootfs=${pool}/ROOT/default ${pool}
-    zpool set autoexpand=on ${pool}
-    zpool export ${pool}
-    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
-    mdconfig -d -u ${md}
+    zfs_extra $src $dst
+    make_esp_file ${img}.p2 ${espsize} ${src}/boot/loader.efi
+    makefs -t zfs -s 200m \
+	-o poolname=${pool} -o bootfs=${pool} -o rootpath=/ \
+	${img}.p3 ${src} ${dst}
+    mkimg -b ${src}/boot/pmbr -s gpt \
+	  -p freebsd-boot:=/boot/gptzfsboot \
+	  -p efi:=${img}.p2 \
+	  -p freebsd-zfs:=${img}.p3 \
+	  -o ${img}
+    rm -rf ${dst}
 }
 
 mk_nogeli_mbr_ufs_legacy() {
@@ -631,7 +599,7 @@ mk_geli_mbr_zfs_both() {
 # u-boot
 # powerpc
 
-qser="-serial telnet::4444,server -nographic"
+qser="-monitor telnet::4444,server,nowait -serial stdio -nographic"
 
 # https://wiki.freebsd.org/QemuRecipes
 # aarch64
@@ -650,13 +618,22 @@ qemu_aarch64_uefi()
 #       -netdev user,id=net0
 }
 
+log_for()
+{
+    dir=$(dirname $1)
+    fn=$(basename $1 .sh)
+    echo $dir/$fn.log
+}
+
 # Amd64 qemu
 qemu_amd64_legacy()
 {
     img=$1
     sh=$2
+    log=$(log_for $2)
 
-    echo "qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser}" > $sh
+    echo "echo -n $(basename $sh .sh):' '" > $sh
+    echo "(qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser} | tee $log 2>&1 | grep -q SUCCESS) && echo legacy pass || echo legacy fail" >> $sh
     chmod 755 $sh
 }
 
@@ -664,8 +641,10 @@ qemu_amd64_uefi()
 {
     img=$1
     sh=$2
+    log=$(log_for $2)
 
-    echo "qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" > $sh
+    echo "echo -n $(basename $sh .sh):' '" > $sh
+    echo "(qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser} | tee $log 2>&1 | grep -q SUCCESS) && echo uefi pass || echo uefi fail" >> $sh
     chmod 755 $sh
 }
 
@@ -673,9 +652,12 @@ qemu_amd64_both()
 {
     img=$1
     sh=$2
+    log=$(log_for $2)
 
-    echo "qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser}" > $sh
-    echo "qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" >> $sh
+    echo "echo -n $(basename $sh .sh):' '" > $sh
+    echo "(qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser} | tee $log 2>&1 | grep -q SUCCESS) && echo legacy pass || echo legacy fail" >> $sh
+    echo "echo -n $(basename $sh .sh):' '" >> $sh
+    echo "(qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser} | tee -a $log 2>&1 | grep -q SUCCESS) && echo uefi pass || echo uefi fail" >> $sh
     chmod 755 $sh
 }
 
@@ -724,6 +706,8 @@ make_one_image()
     # Create sparse file and mount newly created filesystem(s) on it
     img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
     sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
+    echo "$sh" >> ${IMGDIR}/all.sh
+    echo date >> ${IMGDIR}/all.sh
     echo "vvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvv"
     rm -f ${img}*
     eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
@@ -753,6 +737,7 @@ cp /boot/kernel/kernel ${DESTDIR}/boot/kernel
 echo -h -D -S115200 > ${DESTDIR}/boot.config
 cat > ${DESTDIR}/boot/loader.conf <<EOF
 comconsole_speed=115200
+autoboot_delay=0
 EOF
 # XXX
 cp /boot/device.hints ${DESTDIR}/boot/device.hints
@@ -779,17 +764,22 @@ EOF
 
 # If we were given exactly 5 args, go make that one image.
 
+rm -f ${IMGDIR}/all.sh
+echo date > ${IMGDIR}/all.sh
+chmod +x  ${IMGDIR}/all.sh
+
 if [ $# -eq 5 ]; then
     make_one_image $*
+    echo ${IMGDIR}/all.sh
     exit
 fi
 
 # OK. Let the games begin
 
 for arch in amd64; do
-    for geli in nogeli geli; do
+    for geli in nogeli; do # geli
 	for scheme in gpt mbr; do
-	    for fs in ufs zfs; do
+	    for fs in ufs ; do # zfs
 		for bios in legacy uefi both; do
 		    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
 		done
@@ -797,6 +787,7 @@ for arch in amd64; do
 	done
     done
 done
+echo ${IMGDIR}/all.sh
 
 rmdir ${MNTPT}