1#!/bin/bash
2
3usage () { echo "USAGE: xmsnap <VM ID> <Backing File>"; }
4
5#
6# Check Usage
7#
8if [ -n "$1" ]
9then
10	vmid=$1
11else
12	usage
13	exit 1
14fi
15
16if [ -n "$2" ]
17then
18	target=$2
19else
20	usage
21	exit 1
22fi
23
24if [ -e "$target" ]
25then
26    echo "Creating snapshot of file $target for VM $vmid."
27else
28    usage
29    echo "File $target not found."
30    exit 1
31fi
32
33#
34# Find the snapshot name
35#
36directory=`dirname "$target"`
37target=`basename "$target"`
38
39let maxidx=0
40if [ -e $directory/${target}.snap1 ]
41then
42	for idx in $(ls $directory/${target}.snap*)
43	do
44	    let idx=${idx#$directory/${target}.snap}
45	    if [ "$idx" -gt "$maxidx" ]
46	    then
47		let maxidx=$idx
48	    fi
49	done
50fi
51
52snap=${target}.snap`expr $maxidx + 1`
53
54#
55# Pause VM
56#
57xm pause $vmid
58if [ "$?" -ne "0" ]; then
59  exit 1
60fi
61
62
63#
64# Snap and reposition the files
65#
66mv $directory/$target $directory/$snap
67if [ "$?" -ne "0" ]; then
68  exit 1
69fi
70
71qcow-create 0 $directory/$target $directory/$snap
72
73#
74# Unpause
75#
76xm unpause $vmid
77
78exit