-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulation_configure.bash
446 lines (349 loc) · 13.2 KB
/
emulation_configure.bash
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
# Copyright 2015-2016 Hewlett Packard Enterprise Development LP
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2 as
# published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Configurator script for the Fabic-Attached Memory Emulation for
# The Machine from Hewlett Packard Enterprise.
# See http://github.com/FabricAttachedMemory for more details.
# Set these in your environment to override the following defaults.
# Note: ARTDIR was originally TMPDIR, but that variable is suppressed
# by glibc on setuid programs which breaks under certain uses of sudo.
export ARTDIR=${ARTDIR:-/tmp} # ARTifact DIRectory
export MIRROR=${MIRROR:-http://ftp.us.debian.org/debian}
export PROXY=${PROXY:-} # Default: no proxy override
export VERBOSE=${VERBOSE:-} # Default: mostly quiet; "yes" overrides
###########################################################################
# Hardcoded to match content in external config files
HOSTUSERBASE=fabric
PROJECT=${HOSTUSERBASE}_emulation
LOG=$ARTDIR/$PROJECT.log
NETWORK=${HOSTUSERBASE}_emul # libvirt: occasional name length limits
MACBASE="52:54:48:50:45:"
TEMPLATE=$ARTDIR/${HOSTUSERBASE}_template.img
TARBALL=$ARTDIR/${HOSTUSERBASE}_template.tar
###########################################################################
# Helpers
function sep() {
SEP=". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
echo -e "$SEP\n$*\n"
return 0
}
# Early calls (before SUDO is set up) may not make it into $LOG
function die() {
echo -e "Error: $*" >&2
echo -e "\n$0 failed:\n$*\n" >> $LOG
[ "$VERBOSE" ] && $SUDO env | sort | $SUDO tee --append $LOG >/dev/null
echo -e "\n$LOG may have more details" >&2
exit 1
}
function quiet() {
[ "$VERBOSE" ] && echo $*
eval $* >/dev/null 2>&1
return $?
}
function yesno() {
while true; do
read -p "$* (yes/no) ? " RSP
[ "$RSP" = yes ] && return 0
[ "$RSP" = no ] && return 1
done
}
###########################################################################
# Helper for qemu-bridge-helper, contained in package qemu-system-common
function verify_QBH() {
QBH=/usr/lib/qemu/qemu-bridge-helper # qemu-system-common
ACL=/etc/qemu/bridge.conf # ACL mechanism: allow/deny
[ -x $QBH ] || die "$QBH is missing"
[ `stat -c "%u:%g" $QBH` != "0:0" ] && die "$QBH is not owned by root"
if [ `stat -c "%A" $QBH | cut -c4` != 's' ]; then
cat << EOMSG
$QBH must be setuid root for
this script to work. There is some concern that presents a
security hole, but running QEMU as root is worse. See
http://wiki.qemu.org/Features-Done/HelperNetworking#Detailed_Summary
EOMSG
yesno "Change $QBH to setuid root"
[ $? -eq 1 ] && echo "No configuration for you." && exit 0
$SUDO chown root:root $QBH
$SUDO chmod 4755 $QBH
fi
# Always fix $ACL whether missing entirely or just missing the entry.
quiet $SUDO mkdir -m 755 -p `dirname $ACL`
PATTERN="allow $NETWORK"
quiet $SUDO grep -q "^$PATTERN\$" $ACL 2>/dev/null
[ $? -ne 0 ] && echo $PATTERN | quiet $SUDO tee --append $ACL >/dev/null
# "Failed to parse default acl file" if these are wrong.
quiet $SUDO chown root:libvirt-qemu $ACL
quiet $SUDO chmod 640 $ACL
return 0
}
###########################################################################
# vmdebootstrap requires root, but first insure other commands exist.
# Ass-u-me coreutils is installed.
SUDO=
function verify_host_environment() {
sep Verifying host environment
# Chicken and egg: sudo has not yet been verified
if [ -f $LOG ]; then
PREV=$LOG.previous
quiet rm -f $PREV
[ $? -ne 0 ] && echo "Cannot rm $PREV, please do so" >&2 && exit 1
quiet mv -f $LOG $LOG.previous # vmdebootstrap will rewrite it
[ $? -ne 0 ] && echo "Cannot mv $LOG, please remove it" >&2 && exit 1
fi
echo -e "`date`\n" > $LOG
chmod 666 $LOG
# Close some obvious holes before SUDO
unset LD_LIBRARY_PATH
export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
[ -x /bin/which -o -x /usr/bin/which ] || die "Missing command 'which'"
NEED="awk brctl grep qemu-img sudo virsh vmdebootstrap"
MISSING=
for CMD in $NEED; do
quiet which $CMD || MISSING="$CMD $MISSING"
done
[ "$MISSING" ] && die "The following command(s) are needed:\n$MISSING"
if [ "$USER" = root ]; then
SUDO_USER=${SUDO_USER:-root}
else
sudo echo || die "sudo access is needed by this script."
SUDO='sudo -E'
MAYBE=`$SUDO env | grep SUDO_USER`
[ "$MAYBE" ] && eval $MAYBE
SUDO_USER=${SUDO_USER:-root}
fi
verify_QBH
# Space for 2 raw image files, the tarball, all qcows, and slop
[ ! -d "$ARTDIR" ] && die "$ARTDIR is not a directory"
let GNEEDED=16+1+$NODES+1
let KNEEDED=1000000*$GNEEDED
TMPFREE=`df "$ARTDIR" | awk '/^\// {print $4}'`
[ $TMPFREE -lt $KNEEDED ] && die "$ARTDIR has less than $GNEEDED G free"
return 0
}
###########################################################################
# libvirt / virsh / qemu / kvm stuff
function libvirt_bridge() {
sep Configure libvirt network bridge \"$NETWORK\"
XML=$PROJECT.net.xml
[ ! -f $XML ] && die "Missing local file $XML"
# Some installations set up LIBVIRT_DEFAULT, but the virsh man page
# says LIBVIRT_DEFAULT_URI. Get the deprecated version, too..
export LIBVIRT_DEFAULT="${LIBVIRT_DEFAULT:-qemu:///system}"
export LIBVIRT_DEFAULT_URI=$LIBVIRT_DEFAULT
export LIBVIRT_DEFAULT_CONNECT_URI=$LIBVIRT_DEFAULT
export VIRSH_DEBUG=0
export VIRSH_LOG_FILE=$LOG
VIRSH="$SUDO virsh"
quiet $VIRSH connect
[ $? -ne 0 ] && die "'virsh connect' failed"
$VIRSH net-list --all | grep -q '^ Name.*State.*Autostart.*Persistent$'
[ $? -ne 0 ] && die "virsh net-list command is not working as expected"
for CMD in stop destroy; do
quiet $VIRSH net-$CMD $NETWORK
done
quiet $VIRSH net-undefine $NETWORK
# virsh will defin a net with a loooong name, but fail on starting it.
quiet $VIRSH net-define $XML
[ $? -ne 0 ] && die "Cannot define the network $NETWORK:\n`cat $XML`"
for CMD in net-start net-autostart; do
quiet $VIRSH $CMD $NETWORK
[ $? -ne 0 ] && die "virsh $CMD $NETWORK failed"
done
return 0
}
###########################################################################
MNT=/mnt/$PROJECT
function mount_image() {
quiet $SUDO umount $MNT # Singleton
if [ $# -eq 0 ]; then
quiet $SUDO rmdir -f $MNT # Leave no traces
return 0
fi
LOCALIMG="$*"
quiet $SUDO mkdir $MNT
quiet $SUDO mount -oloop,offset=1M "$LOCALIMG" $MNT
return $?
}
###########################################################################
function validate_template_image() {
[ -f $TEMPLATE ] || return 1
mount_image $TEMPLATE || return 1
test -d $MNT/home/$HOSTUSERBASE
RET=$?
mount_image
return $RET
}
###########################################################################
# vmdebootstrap uses debootstrap which uses wget to retrieve packages.
# wget obeys (lower case) "http_proxy" but can be overridden by
# or $HOME/.wgetrc or /etc/wgetrc. Try to help; export http_proxy to
# avoid a reported issue in the VMD variable.
function expose_proxy() {
for RC in $HOME/.wgetrc /etc/wgetrc; do
TMP=`grep '^[[:space:]]*http_proxy' $RC 2>/dev/null | sed 's/[[:space:]]//g'`
if [ "$TMP" ]; then
eval "$TMP"
export http_proxy
echo "http_proxy=$http_proxy (from $RC)"
return 0
fi
done
if [ "$PROXY" ]; then # override for this script
http_proxy=$PROXY
export http_proxy
echo "http_proxy=$http_proxy (from override by PROXY=)"
return 0
fi
if [ "${http_proxy:-}" ]; then # already there
echo "http_proxy=$http_proxy (existing environment)"
return 0
fi
echo "No proxy setting can be ascertained"
return 0
}
###########################################################################
# This takes about six minutes if the mirror is unproxied on a LAN. YMMV.
function manifest_template_image() {
sep Creating pristine VM file system image $TEMPLATE
if validate_template_image; then
yesno "Re-use existing $TEMPLATE"
[ $? -eq 0 ] && return 0
fi
expose_proxy
CFG=$PROJECT.vmd # local
VMD="$SUDO vmdebootstrap --no-default-configs --config=$CFG"
# Does this vintage of vmdeboostrap eat "variant" or "debootstrapopts"?
$VMD --dump-config | grep -q '^variant ='
if [ $? -eq 0 ]; then
VAROPT='--variant=buildd'
else
VAROPT='--debootstrapopts=variant=buildd' # per KP
fi
# vmdebootstrap calls debootstrap which makes a loopback mount for
# the image under construction, like /dev/mapper/loop0p1/. It should
# be possible to construct a status bar based on df of that mount.
# NOTE: killing the script here may leave a dangling mount that
# interferes with subsequent runs, but doesn't complain properly.
quiet $VMD $VAROPT --log=$LOG --image=$TEMPLATE \
--mirror=$MIRROR --owner=$SUDO_USER
RET=$?
quiet $SUDO chown $SUDO_USER "/$ARTDIR/${HOSTUSERBASE}.*" # --owner bug
if [ $RET -ne 0 ]; then
BAD=`mount | grep '/dev/loop[[:digit:]]+p[[:digit:]]+'`
[ $BAD ] && echo "mount of $BAD may be a problem" | tee -a $LOG
die "Build of $TEMPLATE failed"
fi
validate_template_image || die "Validation of $TEMPLATE failed"
return 0
}
###########################################################################
# Helper for cloning into current image at $MNT
function emit_files() {
NEWHOST=$1
# One-liners
$SUDO cp hello_${HOSTUSERBASE}.c $MNT/home/$HOSTUSERBASE
echo $NEWHOST | $SUDO tee $MNT/etc/hostname >/dev/null
echo 'nameserver 192.168.42.254' | \
$SUDO tee $MNT/etc/resolv.conf >/dev/null
# With the simple invocation, this is always true
$SUDO ln -s /sys/bus/pci/devices/0000:00:04.0/resource2 $MNT/$MNT
#------------------------------------------------------------------
$SUDO tee $MNT/etc/hosts >/dev/null << EOHOSTS
127.0.0.1 localhost
127.1.0.1 NEWHOST
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.42.1 ${HOSTUSERBASE}1
192.168.42.2 ${HOSTUSERBASE}2
192.168.42.3 ${HOSTUSERBASE}3
192.168.42.4 ${HOSTUSERBASE}4
192.168.42.5 ${HOSTUSERBASE}5
192.168.42.6 ${HOSTUSERBASE}6
192.168.42.7 ${HOSTUSERBASE}7
192.168.42.8 ${HOSTUSERBASE}8
192.168.42.9 ${HOSTUSERBASE}9
192.168.42.10 ${HOSTUSERBASE}10
192.168.42.11 ${HOSTUSERBASE}11
192.168.42.12 ${HOSTUSERBASE}12
192.168.42.13 ${HOSTUSERBASE}13
192.168.42.14 ${HOSTUSERBASE}14
192.168.42.15 ${HOSTUSERBASE}15
192.168.42.16 ${HOSTUSERBASE}16
192.168.42.254 vmhost `hostname`
EOHOSTS
return 0
}
###########################################################################
# Copy, emit, convert: the 8G raw disk will drop to 800M qcow2.
function clone_VMs()
{
sep Generating file system images for $NODES virtual machines
for N in `seq $NODES`; do
NEWHOST=${HOSTUSERBASE}`printf "%d" $N`
NEWIMG="$ARTDIR/$NEWHOST.img"
QCOW2="$ARTDIR/$NEWHOST.qcow2"
echo "Customize $NEWHOST..."
quiet cp $TEMPLATE $NEWIMG
mount_image $NEWIMG || die "Cannot mount $NEWIMG"
emit_files $NEWHOST
mount_image
echo Converting $NEWIMG into $QCOW2
quiet qemu-img convert -f raw -O qcow2 $NEWIMG $QCOW2
quiet rm -f $NEWIMG
done
return 0
}
###########################################################################
# When in doubt, "qemu-system-x86_64 -device ?" or "-device virtio-net,?"
function emit_invocations() {
DOIT=$ARTDIR/$PROJECT.bash
sep "\nVM invocation script is $DOIT"
cat >$DOIT <<EODOIT
#!/bin/bash
# Invoke VMs created by `basename $0`
# `date`
# SUDO="sudo -E" # Uncomment this if your system needs it
QEMU="\$SUDO qemu-system-x86_64 -enable-kvm"
[ -z "\$DISPLAY" ] && NODISPLAY="-display none"
EODOIT
exec 3>&1 # Save stdout before...
exec >>$DOIT # ...hijacking it
for N in `seq $NODES`; do
NODE=$HOSTUSERBASE$N
MAC=$MACBASE`printf "%02d" $N`
echo "nohup \$QEMU -name $NODE \\"
echo " -netdev bridge,id=$NETWORK,br=$NETWORK,helper=$QBH \\"
echo " -device virtio-net,mac=$MAC,netdev=$NETWORK \\"
echo " -device ivshmem,shm=$PROJECT,size=1024 \\"
echo " \$NODISPLAY $ARTDIR/$NODE.qcow2 &"
echo
done
exec 1>&3 # Restore stdout
exec 3>&-
chmod +x $DOIT
return 0
}
###########################################################################
# MAIN
[ $# -ne 1 -o "${1:0:1}" = '-' ] && die "usage: `basename $0` [ -h ] VMcount"
typeset -ir NODES=$1 # will evaluate to zero if non-integer
set -u
[ "$NODES" -lt 1 -o "$NODES" -gt 16 ] && die "'$1' VMs is not in range 1-16"
trap "rm -f debootstrap.log; exit 0" TERM QUIT INT HUP EXIT # always empty
verify_host_environment
libvirt_bridge
manifest_template_image
clone_VMs
emit_invocations
exit 0