#!/bin/sh #=============================================================================== # ossuary #=============================================================================== #=============================================================================== # script usage #=============================================================================== usage () { # if argument passed to function echo it [ -z "${1}" ] || echo "! ${1}" echo "\ $(basename "$0") -m mount -f container -k gelikey $(basename "$0") -m umount -f container" exit 1 } #=============================================================================== # error messages #=============================================================================== ROOT_ERR="$(basename "$0") must be run as root" WRONG_ARGS_ERR='wrong number of arguments passed to script' INVALID_OPT_ERR='Invalid option:' REQ_ARG_ERR='requires an argument' #=============================================================================== # check if script is run as root #=============================================================================== [ "$(id -u)" -eq 0 ] || usage "${ROOT_ERR}" #=============================================================================== # check number of aruments passed to script #=============================================================================== [ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}" #=============================================================================== # getopts check options passed to script #=============================================================================== while getopts ':m:f:k:h' opt do case ${opt} in m) action="${OPTARG}";; f) container="${OPTARG}";; k) gelikey="${OPTARG}";; h) usage;; \?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;; :) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;; esac done shift $((OPTIND-1)) #=============================================================================== # functions #=============================================================================== # mount function mount_container () { [ -f "${container}" ] || usage "Container file not found" [ -f "${gelikey}" ] || usage "GELI key not found" # mdconfig create vnode loop=$(mdconfig -a -t vnode -f "${container}") || exit 1 # geli attach geli attach -k "${gelikey}" "${loop}" || { mdconfig -du "${loop}"; exit 1; } loopcrypt="/dev/${loop}.eli" # find pool name poolname=$(zdb -l "${loopcrypt}" | awk -F\' '/[[:blank:]]name/ {print $2; exit;}') # import pool zpool import "${poolname}" } # umount function umount_container () { [ -n "${container}" ] || usage "Container path required (-f)" # find associated md device loopdevice=$(mdconfig -lv | grep "${container}" | awk '{print $1}') if [ -z "${loopdevice}" ]; then echo "Error: Container not found in mdconfig list." exit 1 fi loopcrypt="/dev/${loopdevice}.eli" poolname=$(zdb -l "${loopcrypt}" | awk -F\' '/[[:blank:]]name/ {print $2; exit;}') # clean tear down zfs umount "${poolname}" 2>/dev/null zpool export "${poolname}" sleep 1 geli detach "${loopcrypt}" mdconfig -du "${loopdevice}" } #=============================================================================== # mount and umount case statement #=============================================================================== case "${action}" in mount) mount_container;; umount) umount_container;; *) usage;; esac