Module Name:    src
Committed By:   roy
Date:           Sun Feb 23 05:15:54 UTC 2020

Modified Files:
        src/distrib/common: list.zfsroot mtree.zfsroot zfsroot.rc

Log Message:
zfs: Allow modules to be loaded once in the ZFS chroot

Rather than copying the needed modules from FFS to the ramdisk,
load then directly from FFS instead.

This way, we can symlink ramdisk stand into /altroot and let nature take
it's course like loading firmware.

Set shell exit on error rather than using chained commands.
zpool import seems to SIGBUS randomly on amd64 and if we didn't do the
former then zpool would hang the init. Now at least we exit to the shell.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/distrib/common/list.zfsroot \
    src/distrib/common/mtree.zfsroot src/distrib/common/zfsroot.rc

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/common/list.zfsroot
diff -u src/distrib/common/list.zfsroot:1.1 src/distrib/common/list.zfsroot:1.2
--- src/distrib/common/list.zfsroot:1.1	Sat Feb 22 09:53:47 2020
+++ src/distrib/common/list.zfsroot	Sun Feb 23 05:15:54 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: list.zfsroot,v 1.1 2020/02/22 09:53:47 roy Exp $
+#	$NetBSD: list.zfsroot,v 1.2 2020/02/23 05:15:54 roy Exp $
 #
 # list file (c.f. parselist.awk) for ZFS on root.
 #
@@ -12,6 +12,11 @@ ARGVLN	zfs mount_zfs
 # We need sysctl to set init.root=/altroot
 PROG	sbin/sysctl
 
+# We need modload as we cannot build CDDL modules in the kernel
+PROG	sbin/modload
+PROG	sbin/modunload
+PROG	sbin/modstat
+
 LIBS	-lnvpair
 LIBS	-luutil
 LIBS	-lzfs
@@ -25,5 +30,6 @@ LIBS	-lzfs_core
 
 COPY	${NETBSDSRCDIR}/distrib/common/zfsroot.rc etc/rc
 
-# Make firmware images available.
+# Make modules and firmware images available
+SYMLINK	altroot/stand stand
 SYMLINK	altroot/libdata libdata
Index: src/distrib/common/mtree.zfsroot
diff -u src/distrib/common/mtree.zfsroot:1.1 src/distrib/common/mtree.zfsroot:1.2
--- src/distrib/common/mtree.zfsroot:1.1	Sat Feb 22 09:53:47 2020
+++ src/distrib/common/mtree.zfsroot	Sun Feb 23 05:15:54 2020
@@ -1,8 +1,7 @@
-#	$NetBSD: mtree.zfsroot,v 1.1 2020/02/22 09:53:47 roy Exp $
+#	$NetBSD: mtree.zfsroot,v 1.2 2020/02/23 05:15:54 roy Exp $
 
 .
 ./altroot
 ./etc
 ./etc/zfs
 ./rpool
-./stand
Index: src/distrib/common/zfsroot.rc
diff -u src/distrib/common/zfsroot.rc:1.1 src/distrib/common/zfsroot.rc:1.2
--- src/distrib/common/zfsroot.rc:1.1	Sat Feb 22 09:53:47 2020
+++ src/distrib/common/zfsroot.rc	Sun Feb 23 05:15:54 2020
@@ -1,7 +1,11 @@
 #/bin/sh
 #
-#	$NetBSD: zfsroot.rc,v 1.1 2020/02/22 09:53:47 roy Exp $
-#	ZFS on Root boot strapper
+#	$NetBSD: zfsroot.rc,v 1.2 2020/02/23 05:15:54 roy Exp $
+
+echo "Starting root on ZFS boot strapper"
+
+# Abort on any error
+set -e
 
 # Configurable - define the ZFS root pool and ROOT.
 # XXX Can these be set in boot.cfg?
@@ -10,7 +14,7 @@ rpool=rpool
 rroot=ROOT
 
 # Assumption - the boot device is named boot.
-# Could use dk0, wd0a, etc instead.
+# Could use /dev/dk0, /dev/wd0a, etc instead.
 # XXX Can be exposed by sysctl kern.boot_device?
 bootdev="NAME=boot"
 
@@ -19,46 +23,33 @@ export HOME=/
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
 umask 022
 
-echo
-echo "Starting ZFS on root boot strapper"
-
 # Avoid having the solaris and zfs modules in ramdisk directly.
 # Means we don't need to update the ramdisk with the kernel modules
 # or load them from boot.cfg so it's less pain for the user.
 #bootdev="$(/sbin/sysctl -n kern.boot_device)"
 modpath="$(/sbin/sysctl -n kern.module.path)"
-modmnt=/mnt
-echo "Copying needed kernel modules from $bootdev:$modpath"
-case "$bootdev" in
-*=*|"/"*)	;;
-*)		bootdev="/dev/$bootdev";;
-esac
-/sbin/mount -o ro "$bootdev" "$modmnt"
-/sbin/mount -t tmpfs none /stand
+echo "Loading needed kernel modules from $bootdev:$modpath"
+/sbin/mount -o ro "$bootdev" /mnt
 for m in solaris zfs; do
-	p="$modpath/$m"
-	if [ ! -e "$modmnt/$p/$m.kmod" ]; then
-		echo "$modmnt/$p/$m.kmod not found!" >&2
-		continue
-	fi
-	echo "	$m.kmod"
-	/bin/mkdir -p "$p"
-	/bin/cp "$modmnt/$p/$m.kmod" "$p"
+	/sbin/modload "/mnt/$modpath/$m/$m.kmod"
 done
-# zpool list will load the needed modules, then we can dispose of the mounts.
-/sbin/zpool list >/dev/null 2>&1
-/sbin/umount /stand
-/sbin/umount "$modmnt"
-echo
-
-echo "Importing $rpool, mounting and pivoting"
-# If we can mount the ZFS root partition to /altroot
-# then chroot to it and start /etc/rc
-if /sbin/zpool import -f -N "$rpool" && \
-   /sbin/mount -t zfs "$rpool/$rroot" /altroot; then
-	# This ensures that mountall mounts all ZFS mounts set to automount.
-	if [ ! -e /altroot/etc/zfs/zpool.cache ]; then
-		echo >/altroot/etc/zfs/zpool.cache
-	fi
-	/sbin/sysctl -w init.root=/altroot
+/sbin/umount /mnt
+
+# FIXME XXX Sometimes zpool import gets SIGBUS
+# Ensure that we are in a writable directory to try and capture a coredump
+# Not that we actually get a coredump ....
+cd /tmp
+
+echo "Importing $rpool"
+/sbin/zpool import -f -N "$rpool"
+
+echo "Mounting $rpool/$rroot to /altroot"
+/sbin/mount -t zfs "$rpool/$rroot" /altroot;
+
+# This ensures that rc.d/mountall mounts all ZFS mounts set to automount.
+if [ ! -e /altroot/etc/zfs/zpool.cache ]; then
+	echo >/altroot/etc/zfs/zpool.cache
 fi
+
+echo "Pivoting to /altroot, welcome to root on ZFS"
+/sbin/sysctl -w init.root=/altroot

Reply via email to