#!/bin/bash # https://bitbucket.org/dewoodruff/zfs-on-linux-luks-mountvolumes/src/5836def278a3e462f1f508ba02b7fa236dd28717/mountVolumes.sh . /etc/zfs_mount_settings.sh # the real work happens below activePools=() date >> $LOG function getPoolStatus { echo "Checking pool status:" | tee -a $LOG for pool in "${pools[@]}" do echo -en "\t$pool: " | tee -a $LOG status=`zpool status $pool 2>&1 | grep "state:" | cut -f2 -d:` if [ -z "$status" ]; then echo "unknown - not imported" | tee -a $LOG else echo $status | tee -a $LOG activePools+=($pool) fi done } function exportActivePools { if [ -n "$activePools" ]; then echo -n "Exporting pools... " | tee -a $LOG for pool in "${activePools[@]}" do zpool export -f $pool 2>&1 1>>$LOG || { echo "Problem exporting $pool!" | tee -a $LOG; exit 0; } done echo " done." fi } function importPools { echo -n "Importing pools..." for pool in "${pools[@]}" do zpool import $pool 2>&1 1>>$LOG || { echo "Problem importing $pool!" | tee -a $LOG; exit 0; } done echo " done." } function closeAllLUKS { echo "Making sure all LUKS disks are closed..." for dev in "${devs[@]}" do #echo $dev cryptsetup close $dev 2>&1 | 1>>$LOG || { echo "Problem closing $dev!" | tee -a $LOG; exit 0; } done echo "Done." } function openAllLUKS { read -s -p "Enter LUKS passphrase: " pass echo "" for dev in "${!devs[@]}" do echo "Opening $dev to ${devs["$dev"]}" | tee -a $LOG echo "$pass" | cryptsetup luksOpen $dev ${devs[$dev]} 2>&1 1>>$LOG || { echo "Problem opening $dev!" | tee -a $LOG; exit 0; } done pass="" } function LUKSStatus { for dev in "${devs[@]}" do cryptsetup status $dev | head -1 | tee -a $LOG done | sort } function umount { zfs unshare -a service nfs-kernel-server stop 2> /dev/null getPoolStatus exportActivePools closeAllLUKS getPoolStatus } if [ "$1" = "status" ]; then LUKSStatus getPoolStatus elif [ "$1" = "mount" ]; then getPoolStatus exportActivePools closeAllLUKS openAllLUKS importPools getPoolStatus zfs share -a elif [ "$1" = "umount" ]; then umount elif [ "$1" = "reboot" ]; then umount reboot elif [ "$1" = "shutdown" ]; then umount shutdown -h now elif [ "$1" = "freespace" ]; then zfs list else echo "Usage: ./mountVolumes.sh [status|mount|umount|reboot|shutdown|freespace]" fi