Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Basic LVM snapshot to file for Xen domU.
#!/bin/bash
[ $# -ne 2 ] && { echo "Usage: ./snap <lvm group> <domU>"; exit 1; }
##
# Export an LVM device to file
#
# @param string $1 LVM_GROUP The logical volume manager group (e.g. vg0)
# @param string $2 DOMU The guest domain to snap (e.g. w01)
#
function snaplvm() {
local LVM_GROUP=$1
local DOMU=$2
DEVICE=/dev/${LVM_GROUP}/${DOMU}-disk
SNAP_DEVICE=/dev/${LVM_GROUP}/${DOMU}-snap
SNAP_FILE=${HOME}/${DOMU}.snap
# Test block device exists
[ -b ${DEVICE} ] || {
echo "${DEVICE} does not exist!"
return 1
}
# Delete old snap
[ -f ${SNAP_FILE} ] && {
read -p "Delete previous snapshot? [y/n]" REPLY
[[ $REPLY == "y" ]] && rm ${SNAP_FILE} || {
echo "Aborting..."
return 1
}
}
echo "Creating snapshot volume..."
lvcreate -L 1G -s -n ${DOMU}-snap ${DEVICE} && {
echo "Taking snapshot of ${DEVICE}..."
dd if=${SNAP_DEVICE} of=${SNAP_FILE} && {
echo "Deleting snapshot volume..."
lvremove ${SNAP_DEVICE}
echo "Completed ${SNAP_FILE}"
return 0
}
}
}
snaplvm $@ && echo "Success!" || echo "Failed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment