-
Notifications
You must be signed in to change notification settings - Fork 19
/
snap-guest
executable file
·323 lines (290 loc) · 10.5 KB
/
snap-guest
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
usage() {
cat <<USAGE
usage: $0 options
Tool for ultra-fast copy-on-write image provisioning. Prepare a base image (or
download a cloud image) and then spawn an COW instance. Then again, and again.
OPTIONS:
--help | -h
Show this message
--list | -l
List avaiable images (with "base" in the name)
--list-all
List all images
--base [image] | -b [image]
Base image name (template) - required
--target [name] | -t [name]
Target image name (and hostname) - required
--network [opts] | -n [opts]
Network options for virt-install (default: "network=default")
--network2 [opts]
Second network NIC settings (none by default)
--memory [MB] | -m [MB]
Memory (default: 800 MiB)
--cpus [CPUs] | -c [CPUs]
Number of CPUs (default: 1)
--image-dir [path] | -p [path]
Target images path (default: /var/lib/libvirt/images/)
--base-image-dir [path]
Base images path (default: /var/lib/libvirt/images/)
--domain [domain] | -d [domain]
Domain suffix like "mycompany.com" (default: none)
--domain-prefix [prefix]
Domain prefix like "test-" -> "test-NAME.lan" (default: none)
--force | -f
Force creating new guest (destroy existing, no questions, DANGEROUS!)
--add-ip | -w
Add IP address to /etc/hosts on the host machine (works only with NAT)
--graphics [opts] | -g [opts]
Graphics options passed to virt-install via --graphics
(default is vnc,listen=0.0.0.0)
--swap [MBs] | -s [MBs]
Creates RAW disk and connects and mounts it as swap of given size.
The created virtual disc is NOT parititioned (it is used raw).
For cloud-image provisioning swap is created but ignored and
you need to do this manually in user-data script (swapon /dev/vdb).
--firstboot [command] | -1 [command]
Command to execute during first boot in /root dir
(logfile available in /root/firstboot.log)
--cloud-image
Disables image manipulation and enables cloud-init seed via CD-ROM
--cloud-preconf
Run image manipulation (MAC address, hostname setting, fstab) even
when operating in the cloud mode
--user-data-file [file]
Reads cloud-init user-data from file
--user-data-ssh
Generate primitive user-data file with only your public ssh key
--user-data-stdin
Reads cloud-init user-data from standard input
(overrides all --user-data-* options)
--boot-script [filename] | -i [filename]
Appends a bash bootstrip to the user-data. Can be used multiple times.
--boot-script-prefix [directory]
Prepend directory to each boot script (so -i options can be shorter).
--static-ipaddr
It is possible to configure static network setup (only Fedora/RHEL).
--static-netmask
It is possible to configure static network setup (only Fedora/RHEL).
--static-gateway
It is possible to configure static network setup (only Fedora/RHEL).
--cpu-feature
Configure the CPU model and CPU features exposed to the guest.
EXAMPLE:
$0 --list
$0 -p /mnt/data/images --list-all
$0 -b fedora-17-base -t test-vm -s 4098
$0 -b fedora-17-base -t test-vm2 -n bridge=br0 -d example.com
$0 -b rhel-6-base -t test-vm -m 2048 -c 4 -p /mnt/data/images
echo "my user data" | $0 -b f19 -t aa --cloud-image --user-data-stdin \\
-n bridge=br0 -d xxx.redhat.com --domain-prefix 'lzap-' -s 2048 -f
USAGE
}
# determine script directory
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE_NAME=""
TARGET_NAME=""
NETWORK="network=default"
NETWORK2=""
MEM="800"
SWAP=0
CPUS="1"
IMAGE_DIR="/var/lib/libvirt/images"
BASE_IMAGE_DIR="/var/lib/libvirt/images"
DOMAIN=""
PREFIX=""
FORCE=0
LIST=0
LIST_ALL=0
ADD_IP=0
GRAPHICS_OPTS="vnc,listen=0.0.0.0"
COMMAND=""
CLOUD=0
CLOUD_PRECONF=0
USER_DATA_STDIN=0
USER_DATA_FILE=""
USER_DATA_SSH=0
BOOT_SCRIPTS=()
BOOT_SCRIPT_PREFIX=""
STATIC_IPADDR=""
STATIC_NETMASK=""
STATIC_GATEWAY=""
CPU_FEATURE=""
if ! options=$(getopt -o hlfwb:t:n:m:c:d:p:s:1:g:i: -l list,list-all,force,add-ip,image-dir:,base-image-dir:,base:,graphics:,target:,network:,network2:,memory:,cpus:,domain:,domain-prefix:,swap:,firstboot:,cloud-image,cloud-preconf,user-data-ssh,user-data-stdin,user-data-file:,boot-script,boot-script-prefix:,static-ipaddr:,static-netmask:,static-gateway:,cpu-feature:,help -- "$@"); then
exit 1
fi
eval set -- $options
while [ $# -gt 0 ]; do
case $1 in
--list|-l) LIST=1 ;;
--list-all) LIST_ALL=1 ;;
--force|-f) FORCE=1 ;;
--add-ip|-w) ADD_IP=1 ;;
--image-dir|-p) IMAGE_DIR="$2" ; shift ;;
--base-image-dir) BASE_IMAGE_DIR="$2" ; shift ;;
--base|-b) SOURCE_NAME="$2" ; shift ;;
--graphics|-g) GRAPHICS_OPTS="$2" ; shift ;;
--target|-t) TARGET_NAME="$2" ; shift ;;
--network|-n) NETWORK="$2" ; shift ;;
--network2) NETWORK2="--network=$2" ; shift ;;
--memory|-m) MEM="$2" ; shift ;;
--cpus|-c) CPUS="$2" ; shift ;;
--domain|-d) DOMAIN=".$2" ; shift ;;
--domain-prefix) PREFIX="$2" ; shift ;;
--swap|-s) SWAP="$2" ; shift ;;
--firstboot|-1) COMMAND="$2" ; shift ;;
--cloud-image) CLOUD=1 ;;
--cloud-preconf) CLOUD_PRECONF=1 ;;
--user-data-stdin) USER_DATA_STDIN=1 ;;
--user-data-ssh) USER_DATA_SSH=1 ;;
--user-data-file) USER_DATA_FILE="$2" ; shift ;;
--boot-script|-i) BOOT_SCRIPTS+=("$BOOT_SCRIPT_PREFIX$2:text/x-shellscript") ; shift ;;
--boot-script-prefix) BOOT_SCRIPT_PREFIX="$2/" ; shift ;;
--static-ipaddr) STATIC_IPADDR="$2" ; shift ;;
--static-netmask) STATIC_NETMASK="$2" ; shift ;;
--static-gateway) STATIC_GATEWAY="$2" ; shift ;;
--cpu-feature) CPU_FEATURE="--cpu=$2" ; shift ;;
--help|-h) usage; exit ;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
[ $LIST -eq 1 ] && ls "$BASE_IMAGE_DIR" | grep '\.img' | grep base | sed 's/\.img//g' && exit 0
[ $LIST_ALL -eq 1 ] && ls "$BASE_IMAGE_DIR" | grep '\.img' | sed 's/\.img//g' && exit 0
if [ -z "$SOURCE_NAME" -o -z "$TARGET_NAME" ]; then
usage; exit 1
fi
# Check prerequisites
for BINARY in getopt virsh qemu-img mkswap sed tail openssl hexdump perl genisoimage virt-install; do
if ! type -p $BINARY >/dev/null ; then
echo "Please install '$BINARY' first to use snap-guest!" 1>&2
echo "See https://github.com/lzap/snap-guest#installation" 1>&2
exit 1
fi
done
SOURCE_IMG=$(ls -v1 $BASE_IMAGE_DIR/${SOURCE_NAME}*.img | tail -n1)
TARGET_IMG=$IMAGE_DIR/$TARGET_NAME.img
TARGET_IMG_SWAP=$IMAGE_DIR/$TARGET_NAME-swap.img
MAC="52:54:00$(echo "$(hostname)$TARGET_NAME" | openssl dgst -md5 -binary | hexdump -e '/1 ":%02x"' -n 3)"
TARGET_HOSTNAME="$PREFIX$TARGET_NAME$DOMAIN"
if [ ! -f "$SOURCE_IMG" ]; then
echo "Base image $SOURCE_IMG not found!"
exit 1
fi
if [ -f "$TARGET_IMG" ]; then
echo "Target image already exists!"
if [ $FORCE -eq 1 ]; then
REPLY="y"
else
read -p "Shutdown and remove host with this name first (y/[n])? "
fi
[ "$REPLY" != "y" ] && \
# need to do it twice (sometimes this fail)
virsh destroy "$TARGET_NAME" 2>/dev/null; sleep 2s; \
virsh destroy "$TARGET_NAME" 2>/dev/null; sleep 1s; \
virsh undefine "$TARGET_NAME"
fi
echo "Creating snapshot guest $TARGET_NAME"
# -- Image Creation --------------
echo "Creating snapshot image"
qemu-img create -f qcow2 -b $SOURCE_IMG $TARGET_IMG
# -- Image Manipulation ----------
if [ $CLOUD -eq 0 -o $CLOUD_PRECONF -eq 1 ]; then
export COMMAND TARGET_HOSTNAME MAC SOURCE_NAME SWAP TARGET_IMG TARGET_NAME \
STATIC_IPADDR STATIC_NETMASK STATIC_GATEWAY
perl -w "$SCRIPTDIR/prepare-image.pl"
fi
# -- Cloud Init -----------------
if [ $CLOUD -eq 1 ]; then
echo "Generating cloud-init seed"
TARGET_IMG_SEED="$IMAGE_DIR/seed-$TARGET_NAME.iso"
CITEMP=$(mktemp -d)
trap "rm -rf $CITEMP" EXIT
# prepare metadata
cat > "$CITEMP/meta-data" <<EOS
instance-id: $TARGET_HOSTNAME
local-hostname: $TARGET_HOSTNAME
EOS
# prepare userdata
[ ! -z "$USER_DATA_FILE" ] && cp "$USER_DATA_FILE" "$CITEMP/user-data"
[ $USER_DATA_SSH -eq 1 ] && cat > "$CITEMP/user-data" <<EOS
#cloud-config
disable_root: 0
ssh_authorized_keys:
- $(cat ~/.ssh/id_rsa.pub)
EOS
[ $USER_DATA_STDIN -eq 1 ] && cat > "$CITEMP/user-data" </dev/stdin
if [ ! -f "$CITEMP/user-data" ]; then
echo "Please provide at least one --user-data option" 1>&2; exit 1
fi
# concatenate user-data
if [ ${#BOOT_SCRIPTS[@]} -gt 0 ]; then
mv "$CITEMP/user-data" "$CITEMP/user-data-main"
write-mime-multipart ${BOOT_SCRIPTS[@]} "$CITEMP/user-data-main" > "$CITEMP/user-data"
fi
pushd "$CITEMP" 2>/dev/null
genisoimage -output $TARGET_IMG_SEED -volid cidata -joliet -rock user-data meta-data
popd 2>/dev/null
cat "$CITEMP/user-data"
DISK_SEED="--disk $TARGET_IMG_SEED,device=cdrom"
else
DISK_SEED=""
fi
# -- Swap Creation --------------
if [ $SWAP -gt 0 ]; then
qemu-img create -f raw $TARGET_IMG_SWAP ${SWAP}M
mkswap -f $TARGET_IMG_SWAP
DISK_SWAP="--disk $TARGET_IMG_SWAP,device=disk,bus=virtio,format=raw"
else
DISK_SWAP=""
fi
# -- Image Provisioning ---------
echo "Provisioning guest $TARGET_NAME"
echo "Hostname: $TARGET_HOSTNAME"
echo "CPUs: $CPUS"
echo "Memory: $MEM MB"
echo "Swap: $MEM MB"
echo "MAC: $MAC"
virt-install --vcpus $CPUS \
--ram $MEM --import \
--name $TARGET_NAME \
--disk $TARGET_IMG,device=disk,bus=virtio,format=qcow2 $DISK_SWAP $DISK_SEED \
--graphics "$GRAPHICS_OPTS" \
--noautoconsole --force \
--network=$NETWORK,mac=$MAC \
$NETWORK2 \
$CPU_FEATURE
# -- IP Determining -------------
# TODO: Use this instead: http://rwmj.wordpress.com/2010/10/26/tip-find-the-ip-address-of-a-virtual-machine/
if [ $ADD_IP -eq 1 ]; then
echo "Waiting for IP"
i=0
until [ $i -ge 100 ] || IP_LINE=`cat /var/lib/libvirt/dnsmasq/${NETWORK/network=}.leases /var/lib/dnsmasq/dnsmasq.leases 2>/dev/null | grep "$MAC"`; do
i=$((i + 1))
sleep 1
done
if [[ -n $IP_LINE ]]; then
IP=`echo $IP_LINE | awk '{print $3}'`
if ! grep -q "$TARGET_HOSTNAME\$" /etc/hosts; then
echo "Writing into /etc/hosts ip: $IP"
echo "$IP $TARGET_HOSTNAME $TARGET_NAME" >> /etc/hosts
else
echo "Host $TARGET_HOSTNAME already in hosts"
OLD_IP=`grep "$TARGET_HOSTNAME\$" /etc/hosts | head -n 1 | awk '{print $1}'`
if [[ $IP = $OLD_IP ]]; then
echo "and these ip's match"
else
echo "with different ip: $OLD_IP instead of $IP"
fi
fi
fi
fi
# -- EOF --