#!/bin/bash # $HeadURL$ $LastChangedRevision$ set -e PROGNAME=$(basename $0) # Load support functions . $(pcms-config PCMS_SHARE_PREFIX)/scripts/support.sh main() { # Defaults for options VERBOSELEVEL=20 # Process options while [ $# -ge 1 ]; do case $1 in -d) VERBOSELEVEL=$2; shift ;; --debug=*) VERBOSELEVEL=${1#*=} ;; -v|--verbose) VERBOSELEVEL=3 ;; --) shift; break ;; -*) error "main: $1: bad option" ;; *) break ;; esac shift done # Process arguments [ $# = 1 ] || usage eval set -- $(encode --decode "$1") [ $# -ge 3 ] || usage LV="$1" VG="$2" DESIRED_SIZE="$3" shift 3 debug 10 "main: LV=$LV, VG=$VG, DESIRED_SIZE=$DESIRED_SIZE" # Create LV if necessary debug 10 "main: checking if LV needs to be created ..." if ! lvs /dev/$VG/$LV > /dev/null 2>&1; then debug 10 "main: creating LV ..." lvcreate -y --name=$LV --size=${DESIRED_SIZE}m /dev/$VG fi # Error if it exists and is wrong size debug 10 "main: checking size of LV ..." ACTUAL_SIZE=$(lvs /dev/$VG/$LV -o size --noheadings --units m --nosuffix | sed -e 's/\.00*$//' -e 's/^ *//' ) if [ $ACTUAL_SIZE != $DESIRED_SIZE ]; then error "/dev/$VG/$LV: exists already but actual size (${ACTUAL_SIZE}MB) does not match desired size (${DESIRED_SIZE}MB) (hint: fix manually)" fi # Get fstype info [ $# -ge 1 ] || usage FSTYPE="$1" shift debug 10 "main: FSTYPE=$FSTYPE" [[ $FSTYPE =~ ^(encrypted|swap|ext4|xfs)$ ]] || error "$FSTYPE: bad type (1)" DEVICE=/dev/$VG/$LV # If desired fs is encrypted then get info and set up device if [ $FSTYPE = encrypted ]; then [ $# -ge 2 ] || usage PW="$1" ID="$2" shift 2 debug 10 "main: PW=$PW, ID=$ID" [[ $ID =~ private ]] || error "$ID: temporarily only allowing device name 'private'" if ! cryptsetup isLuks $DEVICE; then debug 10 "main: $DEVICE: device is not LUKS, so formatting ..." echo -n "$PW" | cryptsetup -d - -q luksFormat $DEVICE # Immediately after the format, the disk is open! Wait for it # to close, otherwise we can't whether it's open below! STILL_OPEN_FLAG=true for I in $(seq 1 5); do sleep 1 lvs -o attr /dev/$VG/$ID --noheadings | grep -q o || { STILL_OPEN_FLAG=false; break; } done ! $STILL_OPEN_FLAG || error "$DEVICE: is still open after ${I}s timeout!" else debug 10 "main: $DEVICE: device is LUKS, so not formatting" fi if ! lvs -o attr /dev/$VG/$ID --noheadings | grep -q o; then debug 10 "main: $DEVICE: device is not open, so opening ..." echo -n "$PW" | cryptsetup -d - -q luksOpen $DEVICE $ID else debug 10 "main: $DEVICE: device is open, so not opening" fi DEVICE=/dev/mapper/$ID [ $# -ge 1 ] || usage FSTYPE="$1" shift debug 10 "main: FSTYPE=$FSTYPE" [[ $FSTYPE =~ ^(swap|ext4|xfs)$ ]] || error "$FSTYPE: bad type (2)" fi # Make filesystem (either on non-encrypted or encrypted) device debug 10 "main: checking existing blkid of LV/device ..." EXISTING_TYPE=$(file -bsL $DEVICE) case $FSTYPE in ext4) if ! [[ $EXISTING_TYPE =~ ext4 ]]; then debug 10 "main: $DEVICE: device is not ext4, so formatting ..." mkfs -t ext4 -F -q $DEVICE else debug 10 "main: $DEVICE: device is ext4, so not formatting" fi ;; xfs) if ! [[ $EXISTING_TYPE =~ XFS ]]; then debug 10 "main: $DEVICE: device is not xfs, so formatting ..." mkfs -t xfs -f -q $DEVICE else debug 10 "main: $DEVICE: device is xfs, so not formatting" fi ;; swap) [[ $EXISTING_TYPE =~ swap ]] || mkswap $DEVICE ;; *) error "$FSTYPE: unknown FSTYPE" ;; esac } usage() { echo "Usage: $PROGNAME @@@[@@]@" >&2; exit 1; } main "$@"