zfsroot.rc revision 1.1
1#/bin/sh
2#
3#	$NetBSD: zfsroot.rc,v 1.1 2020/02/22 09:53:47 roy Exp $
4#	ZFS on Root boot strapper
5
6# Configurable - define the ZFS root pool and ROOT.
7# XXX Can these be set in boot.cfg?
8# Assumption - the root pool is set to legacy mount.
9rpool=rpool
10rroot=ROOT
11
12# Assumption - the boot device is named boot.
13# Could use dk0, wd0a, etc instead.
14# XXX Can be exposed by sysctl kern.boot_device?
15bootdev="NAME=boot"
16
17# Setup some stuff incase things go south and we drop to the shell
18export HOME=/
19export PATH=/sbin:/bin:/usr/sbin:/usr/bin
20umask 022
21
22echo
23echo "Starting ZFS on root boot strapper"
24
25# Avoid having the solaris and zfs modules in ramdisk directly.
26# Means we don't need to update the ramdisk with the kernel modules
27# or load them from boot.cfg so it's less pain for the user.
28#bootdev="$(/sbin/sysctl -n kern.boot_device)"
29modpath="$(/sbin/sysctl -n kern.module.path)"
30modmnt=/mnt
31echo "Copying needed kernel modules from $bootdev:$modpath"
32case "$bootdev" in
33*=*|"/"*)	;;
34*)		bootdev="/dev/$bootdev";;
35esac
36/sbin/mount -o ro "$bootdev" "$modmnt"
37/sbin/mount -t tmpfs none /stand
38for m in solaris zfs; do
39	p="$modpath/$m"
40	if [ ! -e "$modmnt/$p/$m.kmod" ]; then
41		echo "$modmnt/$p/$m.kmod not found!" >&2
42		continue
43	fi
44	echo "	$m.kmod"
45	/bin/mkdir -p "$p"
46	/bin/cp "$modmnt/$p/$m.kmod" "$p"
47done
48# zpool list will load the needed modules, then we can dispose of the mounts.
49/sbin/zpool list >/dev/null 2>&1
50/sbin/umount /stand
51/sbin/umount "$modmnt"
52echo
53
54echo "Importing $rpool, mounting and pivoting"
55# If we can mount the ZFS root partition to /altroot
56# then chroot to it and start /etc/rc
57if /sbin/zpool import -f -N "$rpool" && \
58   /sbin/mount -t zfs "$rpool/$rroot" /altroot; then
59	# This ensures that mountall mounts all ZFS mounts set to automount.
60	if [ ! -e /altroot/etc/zfs/zpool.cache ]; then
61		echo >/altroot/etc/zfs/zpool.cache
62	fi
63	/sbin/sysctl -w init.root=/altroot
64fi
65