Skip to content

Commit

Permalink
Amend Dracut module to export ZFS root on shutdown
Browse files Browse the repository at this point in the history
Make use of Dracut's ability to restore the initramfs on shutdown and
pivot to it, allowing for a clean unmount and export of the ZFS root.
No need to force-import on every reboot anymore.

Signed-off-by: Lukas Wunner <[email protected]>
Signed-off-by: Brian Behlendorf <[email protected]>
Issue openzfs#2195
Issue openzfs#2476
Issue openzfs#2498
Issue openzfs#2556
Issue openzfs#2563
Issue openzfs#2575
Issue openzfs#2600
Issue openzfs#2755
Issue openzfs#2766
  • Loading branch information
l1k authored and behlendorf committed Oct 7, 2014
1 parent 8ac9b5e commit 07a3312
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions dracut/90zfs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export-zfs.sh
module-setup.sh
mount-zfs.sh
parse-zfs.sh
2 changes: 2 additions & 0 deletions dracut/90zfs/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pkgdracutdir = $(dracutdir)/modules.d/90zfs
pkgdracut_SCRIPTS = \
$(top_srcdir)/dracut/90zfs/export-zfs.sh \
$(top_srcdir)/dracut/90zfs/module-setup.sh \
$(top_srcdir)/dracut/90zfs/mount-zfs.sh \
$(top_srcdir)/dracut/90zfs/parse-zfs.sh

EXTRA_DIST = \
$(top_srcdir)/dracut/90zfs/export-zfs.sh.in \
$(top_srcdir)/dracut/90zfs/module-setup.sh.in \
$(top_srcdir)/dracut/90zfs/mount-zfs.sh.in \
$(top_srcdir)/dracut/90zfs/parse-zfs.sh.in
Expand Down
29 changes: 29 additions & 0 deletions dracut/90zfs/export-zfs.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

_do_zpool_export() {
local ret=0
local final=$1
local force

if [ "x$final" != "x" ]; then
force="-f"
fi

info "Exporting ZFS storage pools"
zpool list -H | while read fs rest ; do
zpool export $force "$fs" || ret=$?
done

if [ "x$final" != "x" ]; then
info "zpool list"
zpool list 2>&1 | vinfo
fi

return $ret
}

if command -v zpool >/dev/null; then
_do_zpool_export $1
else
:
fi
1 change: 1 addition & 0 deletions dracut/90zfs/module-setup.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ install() {
dracut_install hostid
inst_hook cmdline 95 "$moddir/parse-zfs.sh"
inst_hook mount 98 "$moddir/mount-zfs.sh"
inst_hook shutdown 30 "$moddir/export-zfs.sh"

if [ -e @sysconfdir@/zfs/zpool.cache ]; then
inst @sysconfdir@/zfs/zpool.cache
Expand Down
2 changes: 2 additions & 0 deletions dracut/90zfs/mount-zfs.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ case "$root" in
else
mount -o zfsutil -t zfs "$zfsbootfs" "$NEWROOT" && ROOTFS_MOUNTED=yes
fi

need_shutdown
;;
esac
25 changes: 25 additions & 0 deletions dracut/README.dracut.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ command line and determine if ZFS is the active root filesystem.
* `mount-zfs.sh`: Run later in initramfs boot process after udev has settled
to mount the root dataset.

* `export-zfs.sh`: Run on shutdown after dracut has restored the initramfs
and pivoted to it, allowing for a clean unmount and export of the ZFS root.

`module-setup.sh`
---------------

Expand Down Expand Up @@ -164,3 +167,25 @@ import can lead to serious data corruption and loss of pools, so this option
should be used with extreme caution. Note that even with this flag set, if
the required zpool was auto-imported by the kernel module, no additional
`zpool import` commands are run, so nothing is forced.

`export-zfs.sh`
-------------

Normally the zpool containing the root dataset cannot be exported on
shutdown as it is still in use by the init process. To work around this,
Dracut is able to restore the initramfs on shutdown and pivot to it.
All remaining process are then running from a ramdisk, allowing for a
clean unmount and export of the ZFS root. The theory of operation is
described in detail in the [Dracut manual](https://www.kernel.org/pub/linux/utils/boot/dracut/dracut.html#_dracut_on_shutdown).

This script will try to export all remaining zpools after Dracut has
pivoted to the initramfs. If an initial regular export is not successful,
Dracut will call this script once more with the `final` option,
in which case a forceful export is attempted.

Other Dracut modules include similar shutdown scripts and Dracut
invokes these scripts round-robin until they succeed. In particular,
the `90dm` module installs a script which tries to close and remove
all device mapper targets. Thus, if there are ZVOLs containing
dm-crypt volumes or if the zpool itself is backed by a dm-crypt
volume, the shutdown scripts will try to untangle this.

0 comments on commit 07a3312

Please sign in to comment.