diff --git a/src/etc/rc.be_functions.sh b/src/etc/rc.be_functions.sh index 625ca123ec6844fbe3f44e5fcea5ed3c47e59f86..a0f0bc56d2e154d5f05d0b8905d8bb2e4a4b8776 100644 --- a/src/etc/rc.be_functions.sh +++ b/src/etc/rc.be_functions.sh @@ -37,14 +37,14 @@ existing_base_dir() be_remount_subordinate() { /sbin/zfs list -rH -o mountpoint,name,canmount,mounted -s mountpoint -t filesystem "${1}" | \ - while read _mp _name _canmount _mounted ; do - # skip filesystems that must *not* be mounted - [ "${_canmount}" = "off" -o "${_mp}" = "/" ] && continue - # unmount the dataset if mounted... - [ "${_mounted}" = "yes" ] && /sbin/zfs umount -f "${_name}" - # finally, mount the dataset - /sbin/zfs mount "${_name}" - done + while read _mp _name _canmount _mounted ; do + # skip filesystems that must *not* be mounted + [ "${_canmount}" = "off" -o "${_mp}" = "/" ] && continue + # unmount the dataset if mounted... + [ "${_mounted}" = "yes" ] && /sbin/zfs umount -f "${_name}" + # finally, mount the dataset + /sbin/zfs mount "${_name}" + done } # @@ -62,27 +62,29 @@ be_mount_zfs() { } # -# Test if root on ZFS +# Get the active root dataset # -zfs_on_root() +# Example: pfSense/ROOT/default +# +zfs_root_dataset() { - local _mountfrom="$(/bin/kenv -q vfs.root.mountfrom)" - [ "${_mountfrom%%:*}" = "zfs" ] + local _mountfrom _rootds + + _mountfrom="$(/bin/kenv -q vfs.root.mountfrom)" + [ "${_mountfrom%%:*}" = "zfs" ] || return 1 + + _rootds="${_mountfrom##*:}" + [ -n "${_rootds}" ] || return 1 + + echo "${_rootds}" } # -# Get the active root dataset -# -# Example: pfSense/ROOT/default +# Test if root on ZFS # -zfs_root() +zfs_on_root() { - if zfs_on_root; then - /sbin/mount | while read _name _on _mp _more; do - [ ! "${_mp}" = "/" ] && continue - echo "${_name}" - done - fi + zfs_root_dataset >/dev/null 2>&1 } # @@ -92,9 +94,24 @@ zfs_root() # zfs_root_pool() { - local _rootds="$(zfs_root)" - [ $? -ne 0 ] && return 1 - echo "${_rootds%%/*}" + local _rootds _pool + + _rootds="$(zfs_root_dataset)" + _pool="${_rootds%%/*}" + + [ -n "${_pool}" ] || return 1 + + echo "${_pool}" +} + +# +# DEPRECATED: Get the active root dataset +# +# Example: pfSense/ROOT/default +# +zfs_root() +{ + zfs_root_dataset } # @@ -114,8 +131,8 @@ zfs_be_root() # zfs_path_dataset() { - zfs_on_root || return 1 - /sbin/zfs list -H -d 0 -t filesystem -o name "$(existing_base_dir "${1}")" 2>/dev/null + zfs_on_root && \ + /sbin/zfs list -H -d 0 -t filesystem -o name "$(existing_base_dir "${1}")" 2>/dev/null } # @@ -141,8 +158,8 @@ zfs_ds_exists() # Set a ZFS user property zfs_set_prop() { - zfs_on_root || return 1 - /sbin/zfs set "${2}=${3}" "${1}" 1>/dev/null 2>&1 + zfs_on_root && \ + /sbin/zfs set "${2}=${3}" "${1}" 1>/dev/null 2>&1 } # @@ -150,16 +167,17 @@ zfs_set_prop() # zfs_unset_prop() { - zfs_on_root || return 1 - /sbin/zfs inherit -r "${2}" "${1}" 1>/dev/null 2>&1 + zfs_on_root && \ + /sbin/zfs inherit -r "${2}" "${1}" 1>/dev/null 2>&1 } + # # Get a ZFS user property # zfs_get_prop() { - zfs_on_root || return 1 - /sbin/zfs get -H -o value "${2}" "${1}" 2>/dev/null + zfs_on_root && \ + /sbin/zfs get -H -o value "${2}" "${1}" 2>/dev/null } # @@ -167,8 +185,8 @@ zfs_get_prop() # zfs_test_prop_eq() { - zfs_on_root || return 1 - [ "$(zfs_get_prop "${1}" "${2}")" = "${3}" ] + zfs_on_root && \ + [ "$(zfs_get_prop "${1}" "${2}")" = "${3}" ] } # @@ -440,6 +458,9 @@ be_recent_name() echo "${_recent}" } +# +# Find a BE by ZFS property ($1) and optional value ($2) +# be_find_by_prop() { local _p="${1}" @@ -447,10 +468,10 @@ be_find_by_prop() if zfs_on_root; then /sbin/zfs list -r -d 1 -H -o name,${_p} -S ${_p} "$(zfs_be_root)" | \ - while read _name _previous; do - [ "${_previous}" = "${_v}" ] || continue; - echo ${_name##$(zfs_be_root)/} - return # we are done - done + while read _name _previous; do + [ "${_previous}" = "${_v}" ] || continue; + echo ${_name##$(zfs_be_root)/} + return # we are done + done fi } diff --git a/src/etc/pfSense-rc b/src/etc/pfSense-rc index 6ab0bc59d7187fb40f9dd7ff7fbcb35197216925..7c670f0b5d6829c2264f9a44ce400a2203bd7e0f 100755 --- a/src/etc/pfSense-rc +++ b/src/etc/pfSense-rc @@ -440,9 +440,11 @@ if /sbin/kldstat -qm zfs; then errexit_on else USE_ZFS=1 - ! ZFSROOT="$(zfs_root)" - if [ -n "$ZFSROOT" ]; then - /sbin/zfs set readonly=off "$ZFSROOT" + errexit_off + ZFSROOT="$(zfs_root_dataset)" + errexit_on + if [ -n "${ZFSROOT}" ]; then + /sbin/zfs set readonly=off "${ZFSROOT}" fi /sbin/zfs mount -a # If /bootpool is present, then there is an additional zfs pool to import @@ -813,11 +815,13 @@ if [ -f "$varrunpath/booting" ]; then /bin/rm "$varrunpath/booting" fi +unset ZPOOL if [ -n "${USE_ZFS}" ]; then + errexit_off + ZPOOL="$(zfs_root_pool)" + errexit_on # Create ZFS reservation if [ ! -f /.no_zfs_reservation ]; then - zpool list -o name pfSense > /dev/null 2>&1 && ZPOOL=pfSense - zpool list -o name zroot > /dev/null 2>&1 && ZPOOL=zroot if [ -n "${ZPOOL}" ]; then errexit_off # This will always fail if the reservation isn't already present if ! zfs list -Hp -o name -t filesystem | grep -q "${ZPOOL}/reservation"; then @@ -836,8 +840,6 @@ if [ -n "${USE_ZFS}" ]; then # Enable zpool trimming if [ ! -f /.no_zpool_autotrim ]; then - /sbin/zpool list -o name pfSense > /dev/null 2>&1 && ZPOOL=pfSense - /sbin/zpool list -o name zroot > /dev/null 2>&1 && ZPOOL=zroot if [ -n "${ZPOOL}" ]; then errexit_off # Not all platforms support TRIM AUTOTRIM=$( /sbin/zpool get -H -o value autotrim "${ZPOOL}" ) @@ -851,10 +853,8 @@ if [ -n "${USE_ZFS}" ]; then fi fi - # Make sure the config dataset is always synced - if [ "$(/sbin/zfs get -H -o value sync pfSense/ROOT/default/cf)" != "always" ]; then - zfs set sync=always pfSense/ROOT/default/cf - fi + # Make sure the config dataset is "always" synced for safety + zfs_set_prop "${ZFSROOT}/cf" sync always fi errexit_off # Could already be running