Skip to content

Commit

Permalink
Relax insmod spl failures, try to load zfs.ko
Browse files Browse the repository at this point in the history
Instead of manually loading spl.ko every time, try to load it via
modprobe. Iff that fails, use modinfo to find the path to spl.ko and try
to load it with spl_hostid=0. Since udevd could have already loaded zfs
(and by extension, spl), insmod failures are not hard failures. Warn
that the module couldn't be loaded along with the error text and then
try to load zfs.ko anyway.

This moves all of the on-disk / /sys hostid manipulation to after the
modules have been loaded. This ensures that regardless of how spl was
loaded, /sys/module/spl/parameters/spl_hostid is set to 0 to force
/etc/hostid to take precedence.
  • Loading branch information
zdykstra committed Jul 3, 2021
1 parent f657717 commit 2a9a6ab
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions 90zfsbootmenu/zfsbootmenu-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ fi

mkdir -p "${BASE}"

# Attempt to load spl normally
if ! _modload="$( modprobe spl 2>&1 )" ; then
zdebug "${_modload}"

# Capture the filename for spl.ko
_modfilename="$( modinfo -F filename spl )"

if [ -n "${_modfilename}" ] ; then
zinfo "loading ${_modfilename}"

# Load with a hostid of 0, so that /etc/hostid takes precedence and
# invalid spl.spl_hostid values are ignored

# There's a race condition between udev and insmod spl
# insmod failures are no longer a hard failure - they can be because
# 1. spl.ko is already loaded because of the race condition
# 2. there's an invalid parameter or value for spl.ko

if ! _modload="$( insmod "${_modfilename}" "spl_hostid=0" 2>&1 )" ; then
zwarn "${_modload}"
zwarn "unable to load SPL kernel module; attempting to load ZFS anyway"
fi
fi
fi

if ! _modload="$( modprobe zfs 2>&1 )" ; then
zerror "${_modload}"
emergency_shell "unable to load ZFS kernel modules"
fi

udevadm settle

# Write out a default or overridden hostid
if [ -n "${spl_hostid}" ] ; then
if write_hostid "${spl_hostid}" ; then
Expand All @@ -42,26 +74,6 @@ elif [ ! -e /etc/hostid ]; then
write_hostid "${default_hostid}"
fi

# only load spl.ko if it isn't already loaded
if ! lsmod | grep -E -q "^spl" ; then
# Capture the filename for spl.ko
_modfilename="$( modinfo -F filename spl )"
zinfo "loading ${_modfilename}"

# Load with a hostid of 0, so that /etc/hostid takes precedence
if ! _modload="$( insmod "${_modfilename}" "spl_hostid=0" 2>&1 )" ; then
zdebug "${_modload}"
emergency_shell "unable to load SPL kernel module"
fi
fi

if ! _modload="$( modprobe zfs 2>&1 )" ; then
zdebug "${_modload}"
emergency_shell "unable to load ZFS kernel modules"
fi

udevadm settle

# Prefer a specific pool when checking for a bootfs value
# shellcheck disable=SC2154
if [ "${root}" = "zfsbootmenu" ]; then
Expand Down

0 comments on commit 2a9a6ab

Please sign in to comment.