Files
ansible-fet/roles/ariane/files/zfs_mount.sh
Daniel A. Maierhofer 0d54fa3a8f Add ssd zpool config
2018-07-12 23:11:17 +02:00

134 lines
3.0 KiB
Bash

#!/bin/bash
# https://bitbucket.org/dewoodruff/zfs-on-linux-luks-mountvolumes/src/5836def278a3e462f1f508ba02b7fa236dd28717/mountVolumes.sh
#list our zpools to be mounted, one per line, no delimiter
pools=(
"zv1"
)
#list all devs and their aliases to be used with luksOpen
declare -A devs=(
["/dev/disk/by-id/ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PP32E-part1"]="ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PP32E-part1"
["/dev/disk/by-id/ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PPLPH-part1"]="ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PPLPH-part1"
["/dev/disk/by-id/ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PP0K1-part1"]="ata-WDC_WD60EFRX-68L0BN1_WD-WX21D36PP0K1-part1"
["/dev/disk/by-id/ata-WDC_WD60EFRX-68L0BN1_WD-WXB1HB4MJCMM-part1"]="ata-WDC_WD60EFRX-68L0BN1_WD-WXB1HB4MJCMM-part1"
)
#set your log file name
LOG=mountVolumes.log
# 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: " pass1
echo ""
read -s -p "Confirm LUKS passphrase: " pass2
echo ""
if [ "$pass1" = "$pass2" ];
then
for dev in "${!devs[@]}"
do
echo "Opening $dev to ${devs["$dev"]}" | tee -a $LOG
echo "$pass1" | cryptsetup luksOpen $dev ${devs[$dev]} 2>&1 1>>$LOG || { echo "Problem opening $dev!" | tee -a $LOG; exit 0; }
done
else
echo "ERROR: passphrases don't match!"
fi
pass1=""
pass2=""
}
function LUKSStatus {
for dev in "${devs[@]}"
do
cryptsetup status $dev | head -1 | tee -a $LOG
done | sort
}
function unmount {
zfs unshare -a
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" = "unmount" ];
then
unmount
elif [ "$1" = "reboot" ];
then
unmount
reboot
elif [ "$1" = "shutdown" ];
then
unmount
shutdown -h now
elif [ "$1" = "freespace" ];
then
zfs list
else
echo "Usage: ./mountVolumes.sh [status|mount|unmount|reboot|shutdown|freespace]"
fi