forked from BBVA/kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startvm
executable file
·374 lines (312 loc) · 11.3 KB
/
startvm
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/bash
# This entrypoint for KVM container configures and attaches network bridges to
# the VM and starts KVM with the selected options.
# See Dockerfile reference and README.md for further info.
# If no arguments, the VM will lauch with 4 cores and 1GB of memory
: ${DEBUG:='N'}
: ${USE_NET_BRIDGES:='N'}
: ${LAUNCHER:='/usr/libexec/qemu-kvm'}
: ${DNSMASQ_CONF_DIR:='/etc/dnsmasq.d'}
: ${DNSMASQ:='/usr/sbin/dnsmasq'}
: ${QEMU_CONF_DIR:='/etc/qemu-kvm'}
: ${ENABLE_DHCP:='Y'}
: ${DISABLE_VGA:='N'}
: ${KVM_BLK_OPTS:="-drive if=virtio,file=/image/image"}
: ${KVM_OPTS:="\
-nodefaults \
-device virtio-balloon-pci,id=balloon0 \
-realtime mlock=off \
-msg timestamp=on \
-chardev pty,id=charserial0 \
-device isa-serial,chardev=charserial0,id=serial0 \
-serial stdio \
-cpu host \
"}
# -serial telnet::4555,server,nowait \
: ${KVM_CPU_OPTS:="-m 4G -smp 2"}
log () {
case "$1" in
INFO | WARNING | ERROR )
echo "$1: ${@:2}"
;;
DEBUG)
[[ $DEBUG -eq 1 ]] && echo "$1: ${@:2}"
;;
*)
echo "-- $@"
;;
esac
}
# ContainsElement: checks if first parameter is among the array given as second parameter
# returns 0 if the element is found in the list and 1 if not
# usage: containsElement $item $list
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
# Generate random MAC address
genMAC () {
hexchars="0123456789ABCDEF"
end=$( for i in {1..8} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' )
echo "FE:05$end"
}
# atoi: Returns the integer representation of an IP arg, passed in ascii
# dotted-decimal notation (x.x.x.x)
atoi() {
IP=$1
IPnum=0
for (( i=0 ; i<4 ; ++i ))
do
((IPnum+=${IP%%.*}*$((256**$((3-${i}))))))
IP=${IP#*.}
done
echo $IPnum
}
# itoa: returns the dotted-decimal ascii form of an IP arg passed in integer
# format
itoa() {
echo -n $(($(($(($((${1}/256))/256))/256))%256)).
echo -n $(($(($((${1}/256))/256))%256)).
echo -n $(($((${1}/256))%256)).
echo $((${1}%256))
}
cidr2mask() {
local i mask=""
local full_octets=$(($1/8))
local partial_octet=$(($1%8))
for ((i=0;i<4;i+=1)); do
if [ $i -lt $full_octets ]; then
mask+=255
elif [ $i -eq $full_octets ]; then
mask+=$((256 - 2**(8-$partial_octet)))
else
mask+=0
fi
test $i -lt 3 && mask+=.
done
echo $mask
}
# Generates and returns a new IP and MASK in a superset (inmediate wider range)
# of the given IP/MASK
# usage: getNonConflictingIP IP MASK
# returns NEWIP MASK
getNonConflictingIP () {
local IP="$1"
local CIDR="$2"
let "newCIDR=$CIDR-1"
local i=$(atoi $IP)
let "j=$i^(1<<(32-$CIDR))"
local newIP=$(itoa j)
echo $newIP $newCIDR
}
# generates unused, random names for macvlan or bridge devices
# usage: generateNetDevNames DEVICETYPE
# DEVICETYPE must be either 'macvlan' or 'bridge'
# returns:
# - bridgeXXXXXX if DEVICETYPE is 'bridge'
# - macvlanXXXXXX, macvtapXXXXXX if DEVICETYPE is 'macvlan'
generateNetdevNames () {
devicetype=$1
local netdevinterfaces=($(ip link show | awk "/$devicetype/ { print \$2 }" | cut -d '@' -f 1 | tr -d :))
local randomID=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 6 | head -n 1)
# check if the device already exists and regenerate the name if so
while containsElement "$devicetype$randomID" "${netdevinterfaces[@]}"; do randomID=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 6 | head -n 1); done
echo "$randomID"
}
setupBridge () {
set -x
local iface="$1"
local mode="$2"
local deviceID=$(generateNetdevNames $mode)
local bridgeName="$mode$deviceID"
if [[ $mode == "bridge" ]]; then
brctl addbr "$bridgeName"
brctl addif "$bridgeName" "$iface"
else # use macvlan devices by default
vtapdev="macvtap${deviceID}"
until $(ip link add link $iface name $vtapdev type macvtap mode bridge); do
sleep 1
done
ip link set $vtapdev address "$MAC"
ip link set $vtapdev up
# create a macvlan device for the host
ip link add link $iface name $bridgeName type macvlan mode bridge
ip link set $bridgeName up
# create dev file (there is no udev in container: need to be done manually)
IFS=: read major minor < <(cat /sys/devices/virtual/net/$vtapdev/tap*/dev)
mknod "/dev/$vtapdev" c $major $minor
fi
set +x
# get a new IP for the guest machine in a broader network broadcast domain
if ! [[ -z $IP ]]; then
newIP=($(getNonConflictingIP $IP $CIDR))
ip address del "$IP/$CIDR" dev "$iface"
ip address add "${newIP[0]}/${newIP[1]}" dev "$bridgeName"
fi
ip link set dev "$bridgeName" up
echo $deviceID
}
setupDhcp () {
# dnsmasq configuration:
if [[ "$ENABLE_DHCP" == 1 ]]; then
log "INFO" "DHCP configured to serve IP $IP/$CIDR via ${bridgeName[0]} (attached to container's $iface)"
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$IP,$IP --dhcp-host=$MAC,,$IP,$(hostname -s),infinite --dhcp-option=option:netmask,$(cidr2mask $CIDR)"
else
log "INFO" "No DHCP enabled. The VM won't get the container IP(s)"
fi
}
# Setup macvtap device to connect later the VM and setup a new macvlan devide
# to connect the host machine to the network
configureNetworks () {
local i=0
local GATEWAY=$(ip r | grep default | awk '{print $3}')
local IP
for iface in "${local_ifaces[@]}"; do
IPs=$(ip address show dev $iface | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
IPs=($IPs)
MAC=$(ip link show $iface | awk '/ether/ { print $2 }')
log "DEBUG" "Container original MAC address: $MAC"
# If the container has more than one IP configured in a given interface,
# the user can select which one to use.
# The SELECTED_NETWORK environment variable is used to select that IP.
# This env variable must be in the form IP/MASK (e.g. 1.2.3.4/24).
#
# If this env variable is not set, the IP to be given to the VM is
# the first in the list for that interface (default behaviour).
if ! [[ -z "$SELECTED_NETWORK" ]]; then
local given_ip given_mask
IFS=/ read given_ip given_mask <<< $SELECTED_NETWORK
local given_addr=$(atoi $given_ip)
local given_mask=$((0xffffffff << (32 - $given_mask) & 0xffffffff))
local given_broadcast=$((given_addr | ~given_mask & 0xffffffff))
local given_network=$((given_addr & given_mask))
for configured_ip in "${IPs[@]}"; do
local configured_ip=$(atoi $configured_ip)
if [[ $configured_ip -gt $given_network && $configured_ip -lt $given_broadcast ]]; then
IP=$(itoa $configured_ip)
log "INFO" "SELECTED_NETWORK ($SELECTED_NETWORK) found with ip $IP in $iface interface."
fi
done
[[ -z "$IP" ]] && log "WARNING" "SELECTED_NETWORK ($SELECTED_NETWORK) not found in $iface interface."
else
IP=${IPs[0]}
fi
local CIDR=$(ip address show dev $iface | awk "/inet $IP/ { print \$2 }" | cut -f2 -d/)
# use container MAC address ($MAC) for tap device
# and generate a new one for the local interface
ip link set $iface down
ip link set $iface address $(genMAC)
ip link set $iface up
# setup the bridge or macvtap (default) devices for bridging the VM and the
# container
if [[ $USE_NET_BRIDGES == 1 ]]; then
deviceID=$(setupBridge $iface "bridge")
bridgeName="bridge$deviceID"
# kvm configuration:
echo allow $bridgeName >> $QEMU_CONF_DIR/bridge.conf
KVM_NET_OPTS="$KVM_NET_OPTS -netdev bridge,br=$bridgeName,id=net$i"
else
deviceID=($(setupBridge $iface "macvlan"))
bridgeName="macvlan$deviceID"
# kvm configuration:
let fd=$i+3
KVM_NET_OPTS="$KVM_NET_OPTS -netdev tap,id=net$i,vhost=on,fd=$fd ${fd}<>/dev/macvtap$deviceID"
fi
setupDhcp
log "DEBUG" "bridgeName: $bridgeName"
KVM_NET_OPTS=" -device virtio-net-pci,netdev=net$i,mac=$MAC $KVM_NET_OPTS"
let i++
done
}
# MAIN
#enable KVM support onl if the host supports it
if [[ $(grep -e vmx -e svm /proc/cpuinfo) ]]; then
log "INFO" "KVM acceleration enabled"
KVM_OPTS="$KVM_OPTS -enable-kvm -machine accel=kvm,usb=off "
else
log "INFO" "KVM acceleration disabled"
KVM_OPTS="$KVM_OPTS -machine usb=off "
fi
# Debugging mode
if [ "$1" = "bash" ]; then
export DEBUG=1
export LAUNCHER=$LAUNCHER
export QEMU_CONF_DIR=$QEMU_CONF_DIR
export DNSMASQ_CONF_DIR=$DNSMASQ_CONF_DIR
export DNSMASQ=$DNSMASQ
export ENABLE_DHCP=$ENABLE_DHCP
export DNS_SERVERS=$DNS_SERVERS
export KVM_BLK_OPTS=$KVM_BLK_OPTS
export KVM_OPTS="$KVM_OPTS -nographic"
alias launcher="$LAUNCHER $KVM_BLK_OPTS $KVM_OPTS $KVM_CPU_OPTS $KVM_NET_OPTS"
exec bash
fi
case "$DEBUG" in
[Yy1]* ) DEBUG=1;;
[Nn0]* ) DEBUG=0;;
* ) log "ERROR" "DEBUG incorrect or undefined. It must be one of [Yy1Nn0]"; exit 1;;
esac
case "$AUTO_ATTACH" in
[Yy1]* ) AUTO_ATTACH=1;;
[Nn0]* ) AUTO_ATTACH=0;;
* ) log "ERROR" "AUTO_ATTACH incorrect or undefined. It must be one of [Yy1Nn0]"; exit 1;;
esac
case "$ENABLE_DHCP" in
[Yy1]* ) ENABLE_DHCP=1;;
[Nn0]* ) ENABLE_DHCP=0;;
* ) log "ERROR" "ENABLE_DHCP incorrect or undefined. It must be one of [Yy1Nn0]"; exit 1;;
esac
case "$DISABLE_VGA" in
[Yy1]* ) DISABLE_VGA=1;;
[Nn0]* ) DISABLE_VGA=0;;
* ) log "ERROR" "DISABLE_VGA incorrect or undefined. It must be one of [Yy1Nn0]"; exit 1;;
esac
case "$USE_NET_BRIDGES" in
[Yy1]* ) USE_NET_BRIDGES=1;;
[Nn0]* ) USE_NET_BRIDGES=0;;
* ) log "ERROR" "USE_NET_BRIDGES incorrect or undefined. It must be one of [Yy1Nn0]"; exit 1;;
esac
if [[ "$DISABLE_VGA" -eq 0 ]]; then
: ${KVM_VIDEO_OPTS:="-vga qxl -display none"}
else
: ${KVM_VIDEO_OPTS:="-nographic"}
fi
if [[ $AUTO_ATTACH -eq 1 ]]; then
# Get all interfaces:
local_ifaces=($(ip link show | grep -v noop | grep state | grep -v LOOPBACK | awk '{print $2}' | tr -d : | sed 's/@.*$//'))
local_bridges=($(brctl show | tail -n +2 | awk '{print $1}'))
# Get non-bridge interfaces:
for i in "${local_bridges[@]}"
do
local_ifaces=(${local_ifaces[@]//*$i*})
done
else
local_ifaces=($ATTACH_IFACES)
fi
DEFAULT_ROUTE=$(ip route | grep default | awk '{print $3}')
configureNetworks
if [[ "$ENABLE_DHCP" == 1 ]]; then
# Hack for guest VMs complaining about "bad udp checksums in 5 packets"
/usr/sbin/iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill
# Build DNS options from container /etc/resolv.conf
nameservers=($(grep nameserver /etc/resolv.conf | sed 's/nameserver //'))
searchdomains=$(grep search /etc/resolv.conf | sed 's/search //' | sed 's/ /,/g')
domainname=$(echo $searchdomains | awk -F"," '{print $1}')
for nameserver in "${nameservers[@]}"; do
[[ -z $DNS_SERVERS ]] && DNS_SERVERS=$nameserver || DNS_SERVERS="$DNS_SERVERS,$nameserver"
done
DNSMASQ_OPTS="$DNSMASQ_OPTS \
--dhcp-option=option:dns-server,$DNS_SERVERS \
--dhcp-option=option:router,$DEFAULT_ROUTE \
--dhcp-option=option:domain-search,$searchdomains \
--dhcp-option=option:domain-name,$domainname \
"
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
log "INFO" "Lauching dnsmasq"
log "DEBUG" "dnsmasq options: $DNSMASQ_OPTS"
$DNSMASQ $DNSMASQ_OPTS
fi
log "INFO" "Launching qemu-kvm"
log "DEBUG" "Launching $LAUNCHER $KVM_BLK_OPTS $KVM_OPTS $KVM_VIDEO_OPTS $KVM_CPU_OPTS $KVM_ARGS $@ $KVM_NET_OPTS"
eval exec $LAUNCHER $KVM_BLK_OPTS $KVM_OPTS $KVM_VIDEO_OPTS $KVM_CPU_OPTS "$@" $KVM_NET_OPTS