2011-02-26 03:42:55 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Usage: block-snapshot add|remove img-file cow-file
|
|
|
|
#
|
|
|
|
# This creates dm-snapshot device on given arguments
|
|
|
|
|
|
|
|
dir=$(dirname "$0")
|
2011-06-10 18:34:17 +02:00
|
|
|
if [ "$1" = "prepare" ] || [ "$1" = "cleanup" ]; then
|
|
|
|
. "$dir/xen-hotplug-common.sh"
|
|
|
|
command=$1
|
|
|
|
else
|
|
|
|
. "$dir/block-common.sh"
|
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
2011-06-10 18:29:56 +02:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2012-04-25 23:41:48 +02:00
|
|
|
if [ -n "$XENBUS_PATH" ]; then
|
|
|
|
HOTPLUG_STORE="/var/run/xen-hotplug/${XENBUS_PATH//\//-}"
|
|
|
|
fi
|
2011-06-09 14:06:24 +02:00
|
|
|
|
2011-02-26 03:42:55 +01:00
|
|
|
get_dev() {
|
|
|
|
dev=$1
|
|
|
|
|
|
|
|
if [ -L "$dev" ]; then
|
|
|
|
dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "$dev" ]; then
|
|
|
|
file=$dev
|
|
|
|
|
2013-02-09 01:05:45 +01:00
|
|
|
loopdev=$(losetup -j $file | head -1 | cut -d : -f 1)
|
|
|
|
if [ -n "$loopdev" ]; then
|
2011-02-26 03:42:55 +01:00
|
|
|
# found existing loop to this file
|
2011-02-28 18:52:55 +01:00
|
|
|
echo $loopdev
|
2011-02-26 03:42:55 +01:00
|
|
|
return
|
2013-02-09 01:05:45 +01:00
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
# assign new loop device
|
|
|
|
loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev)
|
|
|
|
if [ "$loopdev" = '' ]
|
|
|
|
then
|
|
|
|
release_lock "block"
|
|
|
|
fatal 'Failed to find an unused loop device'
|
|
|
|
fi
|
|
|
|
|
|
|
|
do_or_die losetup "$loopdev" "$file"
|
|
|
|
echo $loopdev
|
|
|
|
else
|
|
|
|
test -e "$dev" || fatal "$dev does not exist."
|
|
|
|
test -b "$dev" || fatal "$dev is not a block device nor file."
|
2015-05-27 23:52:57 +02:00
|
|
|
echo "$dev"
|
2011-02-26 03:42:55 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_dm_snapshot_name() {
|
2016-11-30 23:39:04 +01:00
|
|
|
local base cow cow2
|
|
|
|
|
2011-02-26 03:42:55 +01:00
|
|
|
base=$1
|
|
|
|
cow=$2
|
2016-11-30 23:39:04 +01:00
|
|
|
cow2=$3
|
2011-02-26 03:42:55 +01:00
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
name="snapshot-$(stat -c '%D:%i' "$base")-$(stat -c '%D:%i' "$cow")"
|
|
|
|
if [ -n "$cow2" ]; then
|
|
|
|
name="$name-$(stat -c '%D:%i' "$cow2")"
|
|
|
|
fi
|
|
|
|
echo "$name"
|
2011-02-26 03:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
create_dm_snapshot() {
|
2016-11-30 23:39:04 +01:00
|
|
|
local base_dev cow_dev base_sz base cow dm_devname
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
dm_devname=$1
|
|
|
|
base=$2
|
|
|
|
cow=$3
|
|
|
|
|
|
|
|
if [ ! -e /dev/mapper/$dm_devname ]; then
|
|
|
|
# prepare new snapshot device
|
|
|
|
base_dev=$(get_dev $base)
|
|
|
|
cow_dev=$(get_dev $cow)
|
|
|
|
base_sz=$(blockdev --getsz $base_dev)
|
|
|
|
do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
create_dm_snapshot_origin() {
|
2016-11-30 23:39:04 +01:00
|
|
|
local base_dev base_sz dm_devname base
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
dm_devname=$1
|
|
|
|
base=$2
|
|
|
|
|
|
|
|
if [ ! -e /dev/mapper/$dm_devname ]; then
|
|
|
|
# prepare new snapshot-origin device
|
|
|
|
base_dev=$(get_dev $base)
|
|
|
|
base_sz=$(blockdev --getsz $base_dev)
|
|
|
|
do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot-origin $base_dev"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-05-04 04:45:55 +02:00
|
|
|
t=$(basename $0)
|
|
|
|
t=${t#block-}
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
case "$command" in
|
|
|
|
add)
|
|
|
|
case $t in
|
|
|
|
snapshot|origin)
|
2012-04-25 23:41:48 +02:00
|
|
|
p=$(xenstore_read_default "$XENBUS_PATH/params" 'MISSING')
|
|
|
|
if [ "$p" == "MISSING" ]; then
|
|
|
|
fatal "Missing device parameters ($t $XENBUS_PATH/params)"
|
|
|
|
fi
|
2013-05-04 04:45:55 +02:00
|
|
|
echo $p > "$HOTPLUG_STORE-params"
|
|
|
|
echo $t > "$HOTPLUG_STORE-type"
|
2016-11-30 23:39:04 +01:00
|
|
|
base=${p%%:*}
|
|
|
|
cow=${p#*:}
|
|
|
|
cow2=${p##*:}
|
|
|
|
if [ "$cow" != "$cow2" ]; then
|
|
|
|
cow=${cow%:*}
|
|
|
|
else
|
|
|
|
cow2=""
|
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
if [ -L "$base" ]; then
|
|
|
|
base=$(readlink -f "$base") || fatal "$base link does not exist."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -L "$cow" ]; then
|
|
|
|
cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
|
|
|
|
fi
|
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
if [ -L "$cow2" ]; then
|
|
|
|
cow2=$(readlink -f "$cow2") || fatal "$cow2 link does not exist."
|
|
|
|
fi
|
|
|
|
|
2011-02-26 03:42:55 +01:00
|
|
|
# first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
|
|
|
|
dm_devname=$(get_dm_snapshot_name "$base" "$cow")
|
|
|
|
|
|
|
|
claim_lock "block"
|
|
|
|
|
|
|
|
# prepare snapshot device
|
|
|
|
create_dm_snapshot $dm_devname "$base" "$cow"
|
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
if [ -n "$cow2" ]; then
|
|
|
|
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
|
|
|
create_dm_snapshot "$dm_devname_full" "/dev/mapper/$dm_devname" "$cow2"
|
|
|
|
dm_devname="$dm_devname_full"
|
|
|
|
fi
|
|
|
|
|
2011-02-26 03:42:55 +01:00
|
|
|
if [ "$t" == "snapshot" ]; then
|
|
|
|
#that's all for snapshot, store name of prepared device
|
|
|
|
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
2011-06-09 14:06:24 +02:00
|
|
|
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
2011-02-26 03:42:55 +01:00
|
|
|
write_dev /dev/mapper/$dm_devname
|
|
|
|
elif [ "$t" == "origin" ]; then
|
|
|
|
# for origin - prepare snapshot-origin device and store its name
|
|
|
|
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
|
|
|
create_dm_snapshot_origin $dm_devname "$base"
|
|
|
|
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
2011-06-09 14:06:24 +02:00
|
|
|
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
2011-02-26 03:42:55 +01:00
|
|
|
write_dev /dev/mapper/$dm_devname
|
|
|
|
fi
|
2011-06-09 14:06:24 +02:00
|
|
|
# Save domain name for template commit on device remove
|
2015-02-22 14:44:09 +01:00
|
|
|
domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
|
|
|
|
# Store name of target domain in case of stubdom
|
|
|
|
domid=$(xenstore_read_default "/local/domain/$domid/target" "$domid")
|
|
|
|
domain=$(xl domname $domid)
|
2011-06-09 14:06:24 +02:00
|
|
|
echo $domain > "$HOTPLUG_STORE-domain"
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
release_lock "block"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
2011-06-10 18:34:17 +02:00
|
|
|
prepare)
|
|
|
|
t=$2
|
2011-02-26 03:42:55 +01:00
|
|
|
case $t in
|
|
|
|
snapshot|origin)
|
2011-06-10 18:34:17 +02:00
|
|
|
p=$3
|
2016-11-30 23:39:04 +01:00
|
|
|
base=${p%%:*}
|
|
|
|
cow=${p#*:}
|
|
|
|
cow2=${p##*:}
|
|
|
|
if [ "$cow" != "$cow2" ]; then
|
|
|
|
cow=${cow%:*}
|
|
|
|
else
|
|
|
|
cow2=""
|
|
|
|
fi
|
2011-06-10 18:34:17 +02:00
|
|
|
|
|
|
|
if [ -L "$base" ]; then
|
|
|
|
base=$(readlink -f "$base") || fatal "$base link does not exist."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -L "$cow" ]; then
|
|
|
|
cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
|
|
|
|
fi
|
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
if [ -L "$cow2" ]; then
|
|
|
|
cow2=$(readlink -f "$cow2") || fatal "$cow2 link does not exist."
|
|
|
|
fi
|
|
|
|
|
2011-06-10 18:34:17 +02:00
|
|
|
# first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
|
|
|
|
dm_devname=$(get_dm_snapshot_name "$base" "$cow")
|
|
|
|
|
|
|
|
claim_lock "block"
|
|
|
|
|
|
|
|
# prepare snapshot device
|
|
|
|
create_dm_snapshot $dm_devname "$base" "$cow"
|
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
if [ -n "$cow2" ]; then
|
|
|
|
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
|
|
|
create_dm_snapshot "$dm_devname_full" "/dev/mapper/$dm_devname" "$cow2"
|
|
|
|
dm_devname="$dm_devname_full"
|
|
|
|
fi
|
|
|
|
|
2011-06-10 18:34:17 +02:00
|
|
|
if [ "$t" == "snapshot" ]; then
|
|
|
|
#that's all for snapshot, store name of prepared device
|
|
|
|
echo "/dev/mapper/$dm_devname"
|
|
|
|
elif [ "$t" == "origin" ]; then
|
|
|
|
# for origin - prepare snapshot-origin device and store its name
|
|
|
|
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
|
|
|
create_dm_snapshot_origin $dm_devname "$base"
|
|
|
|
echo "/dev/mapper/$dm_devname"
|
|
|
|
fi
|
|
|
|
|
|
|
|
release_lock "block"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
remove|cleanup)
|
|
|
|
if [ "$command" = "cleanup" ]; then
|
|
|
|
t=$2
|
|
|
|
else
|
2012-04-25 23:41:48 +02:00
|
|
|
t=$(cat $HOTPLUG_STORE-type 2>/dev/null || echo 'MISSING')
|
2011-06-10 18:34:17 +02:00
|
|
|
fi
|
2012-04-25 23:41:48 +02:00
|
|
|
case "$t" in
|
2011-06-10 18:34:17 +02:00
|
|
|
snapshot|origin)
|
|
|
|
if [ "$command" = "cleanup" ]; then
|
|
|
|
node=$3
|
|
|
|
else
|
2012-04-25 23:41:48 +02:00
|
|
|
node=$(cat "$HOTPLUG_STORE-node" 2> /dev/null)
|
2011-06-10 18:34:17 +02:00
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
if [ -z "$node" ]; then
|
2012-04-25 23:41:48 +02:00
|
|
|
#fatal "No device node to remove"
|
|
|
|
#Most likely already removed
|
|
|
|
exit 0
|
2011-02-26 03:42:55 +01:00
|
|
|
fi
|
|
|
|
|
2011-02-28 18:52:55 +01:00
|
|
|
if [ ! -e "$node" ]; then
|
|
|
|
fatal "Device $node does not exists"
|
|
|
|
fi
|
|
|
|
|
2011-02-26 03:42:55 +01:00
|
|
|
claim_lock "block"
|
|
|
|
|
2015-02-22 14:44:09 +01:00
|
|
|
use_count=$(dmsetup info $node 2>/dev/null|grep Open|awk '{print $3}')
|
|
|
|
|
|
|
|
if [ -z "$use_count" ]; then
|
|
|
|
log info "Device $node already removed"
|
|
|
|
release_lock "block"
|
|
|
|
exit 0
|
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
|
|
|
# do not remove snapshot if snapshot origin is still present
|
2011-02-28 18:52:55 +01:00
|
|
|
if [ "${node/snapshot/}" != "$node" -a -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
|
|
|
|
use_count=1
|
2011-02-26 03:42:55 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$use_count" -gt 0 ]; then
|
|
|
|
log info "Device $node still in use - not removing"
|
2011-02-28 18:52:55 +01:00
|
|
|
release_lock "block"
|
2011-02-26 03:42:55 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get list of used (loop) devices
|
2016-11-30 23:39:04 +01:00
|
|
|
deps="$(dmsetup deps $node -o blkdevname | cut -d: -f2 | sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
2011-02-28 18:52:55 +01:00
|
|
|
|
|
|
|
# if this is origin
|
|
|
|
if [ "${node/origin/}" != "$node" ]; then
|
|
|
|
# remove unused snapshots
|
|
|
|
for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
|
|
|
|
use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
|
|
|
|
if [ "$use_count" -eq 0 ]; then
|
|
|
|
# unused snapshot - remove it
|
2016-11-30 23:39:04 +01:00
|
|
|
deps="$deps $(dmsetup deps $snap -o blkdevname | cut -d: -f2 |\
|
|
|
|
sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
2011-02-28 18:52:55 +01:00
|
|
|
log debug "Removing $snap"
|
|
|
|
dmsetup remove $snap
|
|
|
|
fi
|
|
|
|
done
|
2011-06-10 18:34:17 +02:00
|
|
|
if [ "$command" = "remove" ]; then
|
|
|
|
# Commit template changes
|
|
|
|
domain=$(cat "$HOTPLUG_STORE-domain")
|
|
|
|
if [ "$domain" ]; then
|
|
|
|
# Dont stop on errors
|
2016-08-09 04:22:48 +02:00
|
|
|
if [ -r /var/lib/qubes/qubes-test.xml -a \
|
|
|
|
"${domain#test-}" != "$domain" ]; then
|
|
|
|
export QUBES_XML_PATH=/var/lib/qubes/qubes-test.xml
|
|
|
|
fi
|
2015-02-22 04:01:18 +01:00
|
|
|
/usr/bin/qvm-template-commit --offline-mode "$domain" || true
|
2011-06-10 18:34:17 +02:00
|
|
|
fi
|
2011-03-18 23:54:14 +01:00
|
|
|
fi
|
2011-02-28 18:52:55 +01:00
|
|
|
fi
|
2011-06-09 14:06:24 +02:00
|
|
|
|
2011-03-18 23:24:55 +01:00
|
|
|
if [ -e $node ]; then
|
2011-02-28 18:52:55 +01:00
|
|
|
log debug "Removing $node"
|
|
|
|
dmsetup remove $node
|
2011-03-18 23:24:55 +01:00
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
|
2016-11-30 23:39:04 +01:00
|
|
|
# try to free unused devices
|
2011-02-26 03:42:55 +01:00
|
|
|
for dev in $deps; do
|
|
|
|
if [ -b "$dev" ]; then
|
2011-02-28 18:52:55 +01:00
|
|
|
log debug "Removing $dev"
|
2016-11-30 23:39:04 +01:00
|
|
|
case $dev in
|
|
|
|
/dev/loop*)
|
|
|
|
losetup -d $dev 2> /dev/null || true
|
|
|
|
;;
|
|
|
|
/dev/dm-*)
|
|
|
|
dmsetup remove $dev 2> /dev/null || true
|
|
|
|
;;
|
|
|
|
esac
|
2011-02-26 03:42:55 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2012-04-25 23:41:48 +02:00
|
|
|
if [ -n "$HOTPLUG_STORE" ]; then
|
|
|
|
rm $HOTPLUG_STORE-*
|
|
|
|
fi
|
2011-02-26 03:42:55 +01:00
|
|
|
release_lock "block"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# vim:sw=2:et:
|