Created
July 27, 2026 04:11
-
-
Save robmcmullen/902e9c9cbc2461bb595cf644f6f932da to your computer and use it in GitHub Desktop.
OpenRC script to prompt for ZFS passwords on boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/sbin/openrc-run | |
| # Distributed under the terms of the GNU General Public License v2 | |
| depend() { | |
| before zfs-mount | |
| after zfs-import | |
| keyword -timeout | |
| } | |
| start() { | |
| retval=0 | |
| ebegin "Loading ZFS encryption key(s)" | |
| OLDIFS="$IFS" | |
| IFS=$'\n' | |
| for line in $(zfs list -Ho name,encryptionroot,keystatus,keylocation); do | |
| name=$(echo "$line" | cut -f1) | |
| encryptionroot=$(echo "$line" | cut -f2) | |
| keystatus=$(echo "$line" | cut -f3) | |
| keylocation=$(echo "$line" | cut -f4) | |
| if [ "$encryptionroot" != "-" ] && | |
| [ "$name" = "$encryptionroot" ] && | |
| [ "$keystatus" = "unavailable" ] && | |
| [ "$keylocation" != "none" ] | |
| then | |
| if [ "$keylocation" = "prompt" ]; then | |
| args="-L prompt" | |
| else | |
| args="" | |
| fi | |
| if ! eval zfs load-key $args \"$encryptionroot\"; then | |
| retval=1 | |
| fi | |
| fi | |
| done | |
| IFS="$OLDIFS" | |
| eend $retval | |
| return $retval | |
| } | |
| stop() { | |
| retval=0 | |
| ebegin "Unloading ZFS encryption key(s)" | |
| OLDIFS="$IFS" | |
| IFS=$'\n' | |
| for line in $(zfs list -Ho name,encryptionroot,keystatus,keylocation); do | |
| name=$(echo "$line" | cut -f1) | |
| encryptionroot=$(echo "$line" | cut -f2) | |
| keystatus=$(echo "$line" | cut -f3) | |
| keylocation=$(echo "$line" | cut -f4) | |
| if [ "$encryptionroot" != "-" ] && | |
| [ "$name" = "$encryptionroot" ] && | |
| [ "$keystatus" = "available" ] && | |
| [ "$keylocation" != "none" ] | |
| then | |
| if ! zfs unload-key $encryptionroot; then | |
| retval=1 | |
| fi | |
| fi | |
| done | |
| IFS="$OLDIFS" | |
| eend $retval | |
| return $retval | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment