-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathvirt-vol
executable file
·61 lines (47 loc) · 1.04 KB
/
virt-vol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
ARGS=$(getopt -o s: --long size: -n virt-boot -- "$@")
if [ $? -ne 0 ]; then
echo "usage: $myname <baseimage> <name>"
exit 2
fi
eval set -- "$ARGS"
while :; do
case "$1" in
-s|--size)
image_size="$2"
shift 2
;;
--) shift
break
;;
esac
done
base_image="$1"
vol_name="$2"
shift 2
case $base_image in
*/*) :
;;
*) echo "ERROR: base image must be specified as pool/volume" >&2
exit 1
;;
esac
base_image_pool=${base_image%/*}
base_image_name=${base_image#*/}
if [ -z "$image_size" ]; then
image_size=$(virsh vol-info $base_image_name $base_image_pool |
awk '$1 == "Capacity:" {print $2}' |
cut -f1 -d.)
fi
if ! virsh vol-key $base_image_name --pool $base_image_pool > /dev/null 2>&1; then
echo "ERROR: base image $base_image does not exist" >&2
exit 1
fi
(
virsh vol-delete $vol_name --pool $base_image_pool
) > /dev/null 2>&1
virsh vol-create-as $base_image_pool $vol_name ${image_size}G \
--format qcow2 \
--backing-vol $base_image_name \
--backing-vol-format qcow2 >&2
echo $base_image_pool/$vol_name