zfsroot.rc revision 1.1
11.1Sroy#/bin/sh
21.1Sroy#
31.1Sroy#	$NetBSD: zfsroot.rc,v 1.1 2020/02/22 09:53:47 roy Exp $
41.1Sroy#	ZFS on Root boot strapper
51.1Sroy
61.1Sroy# Configurable - define the ZFS root pool and ROOT.
71.1Sroy# XXX Can these be set in boot.cfg?
81.1Sroy# Assumption - the root pool is set to legacy mount.
91.1Sroyrpool=rpool
101.1Sroyrroot=ROOT
111.1Sroy
121.1Sroy# Assumption - the boot device is named boot.
131.1Sroy# Could use dk0, wd0a, etc instead.
141.1Sroy# XXX Can be exposed by sysctl kern.boot_device?
151.1Sroybootdev="NAME=boot"
161.1Sroy
171.1Sroy# Setup some stuff incase things go south and we drop to the shell
181.1Sroyexport HOME=/
191.1Sroyexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
201.1Sroyumask 022
211.1Sroy
221.1Sroyecho
231.1Sroyecho "Starting ZFS on root boot strapper"
241.1Sroy
251.1Sroy# Avoid having the solaris and zfs modules in ramdisk directly.
261.1Sroy# Means we don't need to update the ramdisk with the kernel modules
271.1Sroy# or load them from boot.cfg so it's less pain for the user.
281.1Sroy#bootdev="$(/sbin/sysctl -n kern.boot_device)"
291.1Sroymodpath="$(/sbin/sysctl -n kern.module.path)"
301.1Sroymodmnt=/mnt
311.1Sroyecho "Copying needed kernel modules from $bootdev:$modpath"
321.1Sroycase "$bootdev" in
331.1Sroy*=*|"/"*)	;;
341.1Sroy*)		bootdev="/dev/$bootdev";;
351.1Sroyesac
361.1Sroy/sbin/mount -o ro "$bootdev" "$modmnt"
371.1Sroy/sbin/mount -t tmpfs none /stand
381.1Sroyfor m in solaris zfs; do
391.1Sroy	p="$modpath/$m"
401.1Sroy	if [ ! -e "$modmnt/$p/$m.kmod" ]; then
411.1Sroy		echo "$modmnt/$p/$m.kmod not found!" >&2
421.1Sroy		continue
431.1Sroy	fi
441.1Sroy	echo "	$m.kmod"
451.1Sroy	/bin/mkdir -p "$p"
461.1Sroy	/bin/cp "$modmnt/$p/$m.kmod" "$p"
471.1Sroydone
481.1Sroy# zpool list will load the needed modules, then we can dispose of the mounts.
491.1Sroy/sbin/zpool list >/dev/null 2>&1
501.1Sroy/sbin/umount /stand
511.1Sroy/sbin/umount "$modmnt"
521.1Sroyecho
531.1Sroy
541.1Sroyecho "Importing $rpool, mounting and pivoting"
551.1Sroy# If we can mount the ZFS root partition to /altroot
561.1Sroy# then chroot to it and start /etc/rc
571.1Sroyif /sbin/zpool import -f -N "$rpool" && \
581.1Sroy   /sbin/mount -t zfs "$rpool/$rroot" /altroot; then
591.1Sroy	# This ensures that mountall mounts all ZFS mounts set to automount.
601.1Sroy	if [ ! -e /altroot/etc/zfs/zpool.cache ]; then
611.1Sroy		echo >/altroot/etc/zfs/zpool.cache
621.1Sroy	fi
631.1Sroy	/sbin/sysctl -w init.root=/altroot
641.1Sroyfi
65