-
Notifications
You must be signed in to change notification settings - Fork 19
/
snap-guest-template
executable file
·101 lines (83 loc) · 2.3 KB
/
snap-guest-template
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
usage() {
cat <<USAGE
usage: $0 options
Takes image of an existing machine and creates a base image from it. Then it
creates new copy-on-write image based on this base image and replaces the
original file (to save disk space).
It's useful for creating a base image from a machine that was already created
using the snap-guest tool.
When using with -c convert, the resulting base image is not copy-on-write any
more and it's independent from the original base.
OPTIONS:
-h Show this message
-s [image] Source image name (and hostname) - required
-b [image] Base image name (template) to be created - required
-c (rename|convert) Command to use when creating a base image from the
original file. Option rename (default) means the file
is only renamed, convert transforms the source image to
raw format, making it independed from other images.
base images.
EXAMPLE:
# create new image f16-1-base based on f16-05 image
$0 -s f16-05 -b f16-1-base
USAGE
}
IMAGE_DIR="/var/lib/libvirt/images"
COMMAND="rename"
while getopts "hb:s:c:" opt; do
case $opt in
s)
SOURCE_NAME="$OPTARG"
;;
b)
BASE_NAME="$OPTARG"
;;
c)
COMMAND="$OPTARG"
;;
h)
usage
exit
;;
?)
echo "Invalid option: $OPTARG" >&2
usage
exit 5
;;
esac
done
if [ -z "$SOURCE_NAME" -o -z "$BASE_NAME" ]; then
usage
exit 1
fi
BASE_IMG=$IMAGE_DIR/$BASE_NAME.img
SOURCE_IMG=$IMAGE_DIR/$SOURCE_NAME.img
if [ -f "$BASE_IMG" ]; then
echo "Base image already exists!"
exit 2
fi
if virsh list | grep "$SOURCE_NAME"; then
echo "Machine of the source image is running! Stop the machine and try it again."
exit 3
fi
case $COMMAND in
rename)
echo "Renaming the source image"
mv "$SOURCE_IMG" "$BASE_IMG" || exit 4
;;
convert)
echo "Creating base image based on the source"
qemu-img convert "$SOURCE_IMG" -O raw "$BASE_IMG" || exit 5
;;
*)
echo "Supported commands are only rename|convert"
exit 1
;;
esac
echo "Using new image as base for the original machine"
qemu-img create -f qcow2 -b "$BASE_IMG" "$SOURCE_IMG.new" || exit 6
if [[ -e "$SOURCE_IMG" ]]; then
rm "$SOURCE_IMG"
fi
mv "$SOURCE_IMG.new" "$SOURCE_IMG"