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
|
#!/bin/sh -e
#
# Copyright (C) 2007-2011 Canonical, Ltd.
# Author: Jamie Strandboge <jamie@canonical.com>
# License: GPLv3
#
# Simple starter script for when virt-manager won't do.
#
ustconf="$HOME/.uqt-vm-tools.conf"
if [ -s "$ustconf" ]; then
. "$ustconf"
else
echo "Could not find '$ustconf'"
exit 1
fi
# Find out our current directory
if [ -z "$UQT_VM_TOOLS" ]; then
UQT_VM_TOOLS=$(dirname $(readlink -f $0))
fi
. $UQT_VM_TOOLS/libvm.sh
abort_if_root
help() {
cat << EOM
Usage: vm-start [-v] [-w] [-s] <vm1> <vm2>
vm-start [-v] [-w] [-s] -p PREFIX [-a ARCH]
-v Do not launch VNC viewer on started VMs
-w Wait for VMs to be SSH-available before exiting
-s Create new snapshot, overwriting any existing snapshots
-t SECONDS Time to wait between starting VMs (default 5 seconds)
EOM
}
start_machine() {
machine="$1"
if ! vm_exists $machine ; then
echo "'$machine' does not exist" >&2
return 1
fi
if vm_running $machine ; then
echo "'$machine' is already running" >&2
return 0
fi
disks=`get_disks $machine`
if [ -z "$disks" ]; then
echo "Could not find any disks for '$machine'. Skipping" >&2
return
fi
if [ "$can_snapshot" = "no" ]; then
for d in $disks ; do
real=`readlink -f "$d"`
dir=`dirname "$real"`
bn=`basename "$real" .qcow2`
pristine="$dir/$bn.pristine.qcow2"
if [ ! -e "$real" ]; then
echo -n "ERROR: '$real' does not exist. Aborting" >&2
if [ -e "$pristine" ]; then
echo ". Try with '-s'" >&2
else
echo "" >&2
fi
return 1
fi
done
fi
this_vm_can_snapshot="$can_snapshot"
if [ "$can_snapshot" = "yes" ] && ! vm_can_snapshot "$machine" ; then
echo "Not using snapshot for '$m'" >&2
this_vm_can_snapshot="no"
fi
if [ "$this_vm_can_snapshot" = "yes" ]; then
uuid=`virsh --connect ${vm_connect} domuuid "$machine" 2>/dev/null | head -1`
if [ -z "$uuid" ]; then
echo "ERROR: could not find uuid" >&2
return 1
fi
# need to update the apparmor profile for the pristine disks on 9.10
update_apparmor="no"
aa_profile="/etc/apparmor.d/libvirt/libvirt-$uuid"
if `lsb_release -r | grep -q '9\.10'` && `virsh --connect ${vm_connect} dominfo "$machine" 2>/dev/null | grep -q 'Security model: apparmor'` ; then
update_apparmor="yes"
if [ ! -e "$aa_profile" ]; then
echo "INFO: Creating non-existent apparmor profile" >&2
vah="/usr/lib/libvirt/virt-aa-helper"
if [ ! -x "$vah" ]; then
vah="virt-aa-helper"
fi
# create it, then unload it
virsh --connect ${vm_connect} dumpxml "$machine" 2>/dev/null | sudo $vah -c --uuid "libvirt-$uuid"
virsh --connect ${vm_connect} dumpxml "$machine" 2>/dev/null | sudo $vah -R --uuid "libvirt-$uuid"
fi
fi
for d in $disks ; do
real=`readlink -f "$d"`
dir=`dirname "$real"`
bn=`basename "$real" .qcow2`
pristine="$dir/$bn.pristine.qcow2"
if [ ! -e "$pristine" ]; then
echo "ERROR: '$pristine' does not exist. Aborting" >&2
return 1
fi
if [ "$update_apparmor" = "yes" ] && [ -e "$aa_profile" ]; then
if ! grep -q "$pristine" "$aa_profile" ; then
echo "INFO: Updating '$aa_profile' to have '$pristine rw,'" >&2
tmp=`mktemp`
sed "s<^}< \"$pristine\" rw,<" "$aa_profile" > "$tmp"
echo "}" >> "$tmp"
sudo mv -f "$tmp" "$aa_profile"
fi
fi
if [ -e "$real" ]; then
echo "INFO: Removing old snapshot '$real'" >&2
rm -f "$real"
fi
echo "INFO: snapshotting '$pristine' -> '$d'" >&2
qemu-img create -F qcow2 -b "$pristine" -f qcow2 "$real" >/dev/null || {
echo "ERROR: could not create snapshot" >&2
return 1
}
done
fi
virsh --connect ${vm_connect} start "$machine" 1>&2 || return 1
if [ -n "$opt_viewer" ]; then
vm_view --wait "$machine"
fi
echo "Sleeping '$wait_seconds' seconds to give '$machine' a chance to start" >&2
sleep $wait_seconds
return 0
}
arch=
prefix=
opt_viewer=1
opt_wait=
can_snapshot="no"
wait_seconds=5
while getopts "hswva:p:t:" opt
do
case "$opt" in
a) arch="$OPTARG";;
p) prefix="$OPTARG";;
v) opt_viewer=;;
w) opt_wait=1;;
s) can_snapshot="yes";;
t) wait_seconds="$OPTARG"
if ! echo "$wait_seconds" | grep -q '^[0-9]\+$' ; then
echo "Must use a number with -t" >&2
exit 1
fi
;;
?|h) help
exit 1
;;
esac
done
shift $(($OPTIND - 1))
machines=
if [ -z "$prefix" ]; then
if [ -z "$1" ]; then
help
exit 1
fi
for i in "$@" ; do
machines="$machines $i"
done
else
args=""
if [ -n "$arch" ]; then
args="-a $arch"
fi
if [ -n "$prefix" ]; then
args="$args -p $prefix"
fi
machines=`vm-list -s $args`
fi
# Start each machine
started=""
for m in $machines ; do
start_machine "$m" && started="$started $m"
done
# Wait for them to come up
if [ -n "$opt_wait" ]; then
for m in $started ; do
vm_wait "$m"
done
fi
exit 0
|