#!/bin/bash # $HeadURL: https://svn.pasta.freemyip.com/main/miniade/trunk/bin/nop-sh $ $LastChangedRevision: 10100 $ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Globals # Configurable stuff # Other globals #MODROOT=$(cd $(dirname $(readlink -f $0))/.. && pwd) MODROOT=/root/opt/virttools # Hard-coded 'cos invoked via hardlink that doesn't have rest of module close by main() { local MY_ARGS local PROGNAME VM STEP BEGIN_OR_END HYPHEN MODE PROGRAM XMLFILE DRYRUN local ASSIGNMENTS TYPE DEVICE FILE MNTPNT # Process options miniade_process_options --help-handler=help MY_ARGS "$@" && set -- "${MY_ARGS[@]}" #miniade_set_verboselevel 10 #miniade_set_progname "mounter" # Process arguments [ $# = 4 ] || miniade_bad_usage VM=$1 STEP=$2 BEGIN_OR_END=$3 HYPHEN=$4 miniade_debug 10 "main: VM=$VM, STEP=$STEP, BEGIN_OR_END=$BEGIN_OR_END, HYPHEN=$HYPHEN" # Sanity checks and derivations [ "X$LOGNAME" = X ] || miniade_error "it looks like you are trying to call this script directly; it is not meant to be used like that" if [ $STEP = prepare -a $BEGIN_OR_END = begin ]; then MODE=mount elif [ $STEP = release -a $BEGIN_OR_END = end ]; then MODE=umount else miniade_debug 10 "main: ignoring step/begin_or_end combination '$STEP/$BEGIN_OR_END'" exit 0 fi for PROGRAM in xmlstarlet; do type -p $PROGRAM > /dev/null || miniade_error "$PROGRAM: not found" done miniade_get_progname PROGNAME # Guts XMLFILE=$(mktemp) cat > $XMLFILE for DRYRUN in true false; do miniade_debug 10 "main: DRYRUN=$DRYRUN" while read ASSIGNMENTS; do TYPE=; FILE=; DEVICE=; MNTPNT= eval "$ASSIGNMENTS" miniade_debug 10 "main: TYPE=$TYPE, DEVICE=$DEVICE, FILE=$FILE, MNTPNT=$MNTPNT" if [ $TYPE = loop ]; then $TYPE "$MODE" "$DRYRUN" "$FILE" "$MNTPNT" elif [ $TYPE = block ]; then $TYPE "$MODE" "$DRYRUN" "$DEVICE" "$MNTPNT" else miniade_error "$TYPE: unknown type" fi done < <(xmlstarlet select --template --match "//domain/metadata/*[local-name() = '$PROGNAME']/mounts/mount" --value-of "concat('TYPE=\"',@type,'\";FILE=\"',@file,'\";DEVICE=\"',@device,'\";MNTPNT=\"',@mntpnt,'\"')" -n < $XMLFILE) done rm -f $XMLFILE } help() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] {begin|end} -" exit 0 } loop() { local MODE DRYRUN FILE MNTPNT [ $# = 4 ] || miniade_internal "loop: $#: bad argument count" MODE=$1 DRYRUN=$2 FILE=$3 MNTPNT=$4 miniade_debug 10 "loop: MODE=$MODE, DRYRUN=$DRYRUN, FILE=$FILE, MNTPNT=$MNTPNT" if [ $MODE = umount ]; then miniade_debug 10 "loop: delegating to generic ..." generic "$MODE" "$DRYRUN" "$MNTPNT" elif ! $DRYRUN; then miniade_debug 10 "loop: calling [mount -o loop \"$FILE\" \"$MNTPNT\"] ..." if ! mount -o loop "$FILE" "$MNTPNT"; then miniade_internal "$MNTPNT: failed to mount despite dryrun thinking it would be okay" fi elif [ "X$FILE" = X ]; then miniade_error "file not specified" elif [[ $FILE =~ ^/dev/ ]]; then miniade_error "$FILE: looks like it might be a device rather than a file" elif [ "X$MNTPNT" = X ]; then miniade_error "mntpnt not specified" elif ! mkdir -p "$MNTPNT"; then miniade_error "$MNTPNT: failed to create it" elif ! mount --fake -o loop "$FILE" "$MNTPNT"; then miniade_error "$FILE: mount would fail (see messages above)" fi } block() { local MODE DRYRUN DEVICE MNTPNT [ $# = 4 ] || miniade_internal "block: $#: bad argument count" MODE=$1 DRYRUN=$2 DEVICE=$3 MNTPNT=$4 miniade_debug 10 "block: MODE=$MODE, DRYRUN=$DRYRUN, DEVICE=$DEVICE, MNTPNT=$MNTPNT" if [ $MODE = umount ]; then miniade_debug 10 "block: delegating to generic ..." generic "$MODE" "$DRYRUN" "$MNTPNT" elif ! $DRYRUN; then miniade_debug 10 "block: calling [mount \"$DEVICE\" \"$MNTPNT\"] ..." if ! mount "$DEVICE" "$MNTPNT"; then miniade_internal "$MNTPNT: failed to mount despite dryrun thinking it would be okay" fi elif [ "X$DEVICE" = X ]; then miniade_error "device not specified" elif ! [[ $DEVICE =~ ^/dev/ ]]; then miniade_error "$DEVICE: looks like it might not be a device" elif [ ! -b "$DEVICE" ]; then miniade_error "$DEVICE: not a block device" elif [ "X$MNTPNT" = X ]; then miniade_error "mntpnt not specified" elif ! mkdir -p "$MNTPNT"; then miniade_error "$MNTPNT: failed to create it" elif ! mount --fake "$DEVICE" "$MNTPNT"; then miniade_error "$DEVICE: mount would fail (see messages above)" fi } generic() { local DRYRUN MNTPNT [ $# = 3 ] || miniade_internal "generic: $#: bad argument count" MODE=$1 DRYRUN=$2 MNTPNT=$3 if [ $MODE != umount ]; then miniade_internal "generic: $MODE: invalid mode (hint: the generic handler can only handle mode 'umount')" elif ! $DRYRUN; then miniade_debug 10 "generic: calling [umount \"$MNTPNT\"] ..." if ! umount "$MNTPNT"; then miniade_internal "$MNTPNT: failed to mount despite dryrun thinking it would be okay" fi elif [ "X$MNTPNT" = X ]; then miniade_error "mntpnt not specified" elif [ ! -d "$MNTPNT" ]; then miniade_error "$MNTPNT: does not exist" elif ! umount --fake "$MNTPNT"; then miniade_error "$MNTPNT: umount would fail (see messages above)" fi } # At the time of writing, miniade did not support changing the # writer functions dynamically. Since, I needed the logfile writer # I redefined the existing writer function. #eval "$(echo "orig__miniade_msg_writer()"; declare -f _miniade_msg_writer | tail -n +2)" #_miniade_msg_writer() { orig__miniade_msg_writer "$@"; _miniade_msg_writer_logfile "$3"; } main "$@"