/
vm_env_setup.sh
executable file
·422 lines (375 loc) · 12.1 KB
/
vm_env_setup.sh
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
#!/bin/bash
cleanup(){
echo "cleaning up; removing related files.."
rm -rf ${PROJECT_ROOT%/}/
xx=`virsh --version`
if [ $? -eq 0 ]; then
if [[ ! -z $(virsh list | grep running) ]]; then
virsh destroy $vm
fi
fi
if [[ ! -z $BENCH_DIR ]]; then
# make sure we're not busting root :P
if [[ ! $BENCH_DIR == / ]]; then
rm -rf $BENCH_DIR
echo "removed $BENCH_DIR"
fi
fi
}
user_interrupt(){
echo -e "\e[1;31m \n\nKeyboard Interrupt detected. \e[0m"
echo -e "\e[1;31m Stopping KVM env bootstrap script... \e[0m"
cleanup
exit 1
}
trap user_interrupt SIGINT
trap user_interrupt SIGTSTP
while getopts "h?v:d:l:r:s:p:o:" opt; do
case "$opt" in
h|\?)
echo "Usage: # $0 [OPTIONS]"
echo "Following are optional args, defaults of which are present in script itself.."
echo "[-o specify functionalities as Following (default: 0)"
echo -e "\t0 -> setup requirements\n\t1 -> bootstrap virsh env\n\t3 -> run workload\n\t4 -> postprocess data (visualize)]"
echo "[-v vm name (default: vm1)]"
echo "[-d distro name (default: fedora22) ]"
echo "[-l location (iso or url to /os) (default: network based)]"
echo "[-r project root (default: /src ) ]"
echo "[-s directory to store latency results to.. (default: /latency_results) ]"
echo "[-p path to latency results (<LATENCY_RESULT_DIR>/<benchmark results>) ]"
exit 0
;;
o) OPTION=$OPTARG
;;
v) vm=$OPTARG
;;
d) dist=$OPTARG
;;
l) ISO_LOC=$OPTARG
;;
r) PROJECT_ROOT=$OPTARG
;;
s) DIR_SRC=$OPTARG
;;
p) BENCH_PATH=$OPTARG
;;
esac
done
if [[ -z $OPTION ]]; then
OPTION=0
fi
if [[ -z $vm ]]; then
vm=vm1
fi
if [[ -z $dist ]]; then
dist=fedora22
fi
if [[ -z $ISO_LOC ]]; then
ISO_LOC="https://dl.fedoraproject.org/pub/fedora/linux/releases/22/Server/x86_64/os/"
fi
if [[ -z $PROJECT_ROOT ]]; then
PROJECT_ROOT="/src"
fi
if [[ -z $DIR_SRC ]]; then
LATENCY_RESULT_DIR="/latency_results"
fi
# define client name here, ex: virbr0-xxx-xx
CLIENT="virbr0"
IMAGE_PATH="/var/lib/libvirt/images/$vm.qcow2"
DISK_PATH="/var/lib/libvirt/images/$vm.disk.qcow2"
XML_PATH="${PROJECT_ROOT%/}/$vm.xml"
echo `ps -aef | egrep 'qemu-kvm|qemu-system-x86_64'`
install_requirements(){
cleanup
echo -e "\e[1;33m Initiating requirements installation..\e[0m"
# create project location and download $vm.xml
mkdir $PROJECT_ROOT
dnf install -q -y wget
wget -q https://raw.githubusercontent.com/arcolife/latency_analyzer/master/$dist-vm.ks -O ${PROJECT_ROOT%/}/$dist-vm.ks
if [ -f ${PROJECT_ROOT%/}/$dist-vm.ks ]; then
# get the kickstart file
echo -e "\e[1;42m $PROJECT_ROOT was created; \nAlso downloaded kickstart file to ${PROJECT_ROOT%/}/$dist-vm.ks.. \e[0m"
else
echo -e "\e[1;31m failed to download kickstart file \e[0m"
exit 1
fi
# install virsh components
rpm -q libvirt qemu-kvm
if [ ! $? -eq 0 ]; then
dnf install -q -y @virt* #libvirt-daemon
systemctl start libvirtd
systemctl enable libvirtd
virsh --version
if [ $? -eq 0 ]; then
echo -e "\e[1;42m virtualization components installed.. \e[0m"
else
echo -e "\e[1;31m FAILED! virtualization modules could not be installed.. \e[0m"
exit 1
fi
fi
# install pbench and fio
rpm -q dnf-plugins-core
if [ ! $? -eq 0 ]; then
dnf install -q -y dnf-plugins-core
fi
rpm -q pbench-agent pbench-fio #fio
if [ ! $? -eq 0 ]; then
dnf copr enable -y ndokos/configtools
dnf copr enable -y ndokos/pbench
# for testing in containers, use --nogpgcheck with dnf install -q of COPR repos
dnf install -q -y pbench-agent
dnf install -q -y pbench-fio #fio
FIO_VERSION=$(/usr/local/bin/fio --version | sed s/fio-//g)
sed -i -r s/ver=.*/ver=$FIO_VERSION/g /opt/pbench-agent/bench-scripts/pbench-fio
source /etc/profile.d/pbench-agent.sh
pbench-register-tool-set
which pbench-fio
if [ $? -eq 0 ]; then
echo -e "\e[1;42m pbench/fio installed.. \e[0m"
else
echo -e "\e[1;31m FAILED! Pbench could not be installed.. \e[0m"
exit 1
fi
fi
echo -e "\e[1;36m ..attempting to install perf-script-postprocessor.\e[0m"
echo -e "\e[1;36m This might take a few mins, given the deps. Enjoy a cup of coffee meanwhile.\e[0m"
# install perf-script-postprocessor
rpm -q gcc lapack lapack-devel blas blas-devel gcc-gfortran gcc-c++ liblas \
redhat-rpm-config libffi-devel libxml-devel libxml2-devel libxslt-devel
if [ ! $? -eq 0 ]; then
dnf install -y gcc lapack lapack-devel blas blas-devel gcc-gfortran gcc-c++ liblas
dnf install -y libffi-devel libxml-devel libxml2-devel libxslt-devel
dnf install -y redhat-rpm-config
fi
pip install perf-script-postprocessor
if [ $? -eq 0 ]; then
perf_script_processor -h
if [ $? -eq 0 ]; then
echo -e "\e[1;42m perf-script-postprocessor installed.. \e[0m"
else
echo -e "\e[1;31m FAILED! perf-script-postprocessor could not be installed.. \e[0m"
exit 1
fi
else
echo -e "\e[1;31m FAILED! pip not working correctly.. \e[0m"
exit 1
fi
# setup git and clone https://github.com/psuriset/kvm_io.git
rpm -q git
if [ ! $? -eq 0 ]; then
dnf install -q -y git
fi
# check for kvm_io repo
if [ -f ${PROJECT_ROOT%/}/kvm_io/bench_iter.sh ]; then
echo -e "\e[1;42m ${PROJECT_ROOT%/}/kvm_io exists.. \e[0m"
else
git clone -q https://github.com/psuriset/kvm_io.git ${PROJECT_ROOT%/}/kvm_io
if [ -f ${PROJECT_ROOT%/}/kvm_io/bench_iter.sh ]; then
echo -e "\e[1;42m ${PROJECT_ROOT%/}/kvm_io was created.. \e[0m"
else
echo -e "\e[1;31m failed to create ${PROJECT_ROOT%/}/kvm_io \e[0m"
exit 1
fi
fi
cp ${PROJECT_ROOT%/}/kvm_io/avg-stddev /usr/local/bin/avg-stddev
cp ${PROJECT_ROOT%/}/kvm_io/bench_iter.sh /usr/local/bin/bench_iter
chmod +x /usr/local/bin/{bench_iter,avg-stddev}
# install blockIO trace/debug tools
# strace, perf trace, perf record, perf trace record
# pbench installs perf; but just to be sure..
rpm -q strace perf
if [ ! $? -eq 0 ]; then
dnf install -q -y strace perf
xx=`perf --help && strace -h`
if [ $? -eq 0 ]; then
echo -e "\e[1;42m Debuggers: perf & strace installed.. \e[0m"
else
echo -e "\e[1;31m FAILED! perf/strace could not be installed.. \e[0m"
exit 1
fi
fi
# rpm -q fping
# if [ ! $? -eq 0 ]; then
# dnf install -q -y fping
# xx=`fping -h`
# if [ $? -eq 0 ]; then
# echo -e "\e[1;42m fping installed.. \e[0m"
# else
# echo -e "\e[1;31m FAILED! fping could not be installed.. \e[0m"
# exit 1
# fi
# fi
echo -e "\e[1;32m ALL requirements satisfied.. \e[0m"
}
attach_disk(){
wget -q https://raw.githubusercontent.com/arcolife/latency_analyzer/master/disk-native.xml -O ${PROJECT_ROOT%/}/disk-native.xml
while :; do
# get the IP and check if machine is up and then issue attach disk command
echo -e "\e[1;33m Attempting to get IP of $vm.. \e[0m"
VM_IPS=$(arp -e | grep $(virsh domiflist $vm | tail -n 2 | head -n 1 | awk -F' ' '{print $NF}') | tail -n 1 | awk -F' ' '{print $1}')
if [[ ! -z $VM_IPS ]]; then
array=($VM_IPS)
for CURR_IP in "${array[@]}"; do
echo -e "\e[1;33m Attempting to contact $vm at $CURR_IP.. \e[0m"
# IS_ALIVE=$(fping $CURR_IP | grep alive)
IS_ALIVE=$(ping $CURR_IP -c 1 -W 2 | grep "1 received")
if [[ ! -z $IS_ALIVE ]]; then
sed -i "s/vm1/$vm/g" ${PROJECT_ROOT%/}/disk-native.xml
virsh attach-device $vm ${PROJECT_ROOT%/}/disk-native.xml --persistent
break
else
echo "VM not ready yet (can't attach disk); sleeping for 2 secs"
sleep 2
fi
# done
done
else
echo "No IP found for $vm yet.. sleeping for 2 secs."
sleep 5
continue
fi
break
done
}
bootstrap_it(){
# cd ${PROJECT_ROOT%/}/ && wget $ISO_LOC
echo -e "\e[1;33m Starting bootstrap process..\e[0m"
virsh undefine $vm
if [[ ! -f $IMAGE_PATH ]]; then
XML_FLAG=0
qemu-img create -q -f qcow2 $IMAGE_PATH 10G
chown -R qemu:qemu $IMAGE_PATH
echo -e "\e[1;32m created qcow2 image of 10G. Next: kicking off virt-install..\e[0m"
virt-install --name=$vm \
--virt-type=kvm \
--disk format=qcow2,path=$IMAGE_PATH \
--vcpus=1 \
--ram=1024 \
--network bridge=$CLIENT \
--os-type=linux \
--os-variant=$dist \
--graphics none \
--extra-args="ks=file:/$dist-vm.ks console=ttyS0,115200" \
--initrd-inject=${PROJECT_ROOT%/}/$dist-vm.ks \
--serial pty \
--location=$ISO_LOC \
--noreboot
else
XML_FLAG=1
wget -q https://raw.githubusercontent.com/arcolife/latency_analyzer/master/$vm.xml -O $XML_PATH
if [ -f $XML_PATH ]; then
echo -e "\e[1;42m $PROJECT_ROOT was created; VM's XML definition saved to $XML_PATH.. \e[0m"
else
echo -e "\e[1;31m failed to create $XML_PATH \e[0m"
fi
sed -i "s/vm1/$vm/g" $XML_PATH
virsh -q define $XML_PATH
fi
# dnf install libguestfs-tools-c
# virt-builder fedora-23 -o /var/lib/libvirt/images/$vm.qcow2 --format qcow2 --update --selinux-relabel --size 5G
if [[ ! -f $DISK_PATH ]]; then
qemu-img create -q -f qcow2 $DISK_PATH 100M
chown -R qemu:qemu $DISK_PATH
echo -e "\e[1;32m created /dev/vdb (additional disk)..\e[0m"
fi
virsh -q start $vm
echo -e "\e[1;32m started $vm..\e[0m"
if [ $XML_FLAG -eq 0 ]; then
attach_disk
fi
if [[ $(virsh list | grep $vm) ]]; then
PID=`pgrep 'qemu-kvm|qemu-system-x86' | tail -n 1`
echo "qemu PID: $PID"
# virsh -q save $vm ${PROJECT_ROOT%/}/$vm_snapshot
# echo -e "\e[1;42m $vm was running with PID $PID ..snapshot saved to $PROJECT_ROOT \e[0m"
else
echo -e "\e[1;31m FAILED! $vm couldn't start up. \e[0m"
exit 1
fi
echo -e "\e[1;33m sleeping for 2 seconds before shutting down..\e[0m"
sleep 2
# TODO: figure out why it takes time to exec shutdown signal;
# destroy for now. Use XML to define next time.
# virsh shutdown $vm
# OR use this:
# /src/kvm_io/shutdown-all-vms
virsh destroy $vm
# virsh undefine $vm
}
run_workload(){
# run kvm_io/bench_iter.sh
echo -e "\e[1;33m Running workload..\e[0m"
virsh start $vm
RESULT_FLAG=0
while :; do
# get the IP and check if machine is up and then issue attach disk command
echo -e "\e[1;33m Attempting to get IP of $vm.. \e[0m"
VM_IPS=$(arp -e | grep $(virsh domiflist $vm | tail -n 2 | head -n 1 | awk -F' ' '{print $NF}') | tail -n 1 | awk -F' ' '{print $1}')
if [[ ! -z $VM_IPS ]]; then
array=($VM_IPS)
for CURR_IP in "${array[@]}"; do
echo -e "\e[1;33m Attempting to contact $vm at $CURR_IP.. \e[0m"
IS_ALIVE=$(ping $CURR_IP -c 1 -W 2 | grep "1 received")
if [[ ! -z $IS_ALIVE ]]; then
bench_ext='native'
DIR_TAG="`date +"%m-%d-%y-%H-%M-%S_"`$bench_ext"
BENCH_DIR=${LATENCY_RESULT_DIR%/}/$DIR_TAG
/usr/local/bin/bench_iter -i $CURR_IP -b $BENCH_DIR -r 5
RESULT_FLAG=1
break
else
echo "VM not ready yet (can't attach disk); sleeping for 2 secs"
sleep 2
continue
fi
# done
done
if [[ $RESULT_FLAG -eq 1 ]]; then
break
else
continue
fi
else
echo "No IP found for $vm yet.. sleeping for 2 secs."
sleep 5
continue
fi
done
# virsh restore ${PROJECT_ROOT%/}/$vm_snapshot
# qemu-img info master.qcow2
virsh destroy $vm
}
process_data(){
# see if we can graph the results nicely
echo -e "\e[1;33m Processing data (analyzing latency)..\e[0m"
# substitute with custom conf file
echo -e "\e[1;33m backing up and updating /etc/delta_processor.conf .. \e[0m"
cp /etc/delta_processor.conf /etc/delta_processor.conf.bkup
wget -q https://raw.githubusercontent.com/arcolife/latency_analyzer/master/delta_processor.conf -O /etc/delta_processor.conf
# TODO: set the below paths / commands to run in loop over a debug data:
if [[ -z $BENCH_PATH ]]; then
# in case, this runs in an automated fashion, i.e.,
# directly after run_workload() (..without user's input for -p)
TARGET_DIR=$BENCH_DIR
else
TARGET_DIR=$BENCH_PATH
fi
echo "processing sample: ${TARGET_DIR%/}/1/perf_kvm_record.data"
perf_script_processor -t 0 -p ${TARGET_DIR%/}/1/perf_kvm_record.data
}
if [ $OPTION -eq 0 ]; then
install_requirements
elif [ $OPTION -eq 1 ]; then
install_requirements
bootstrap_it
elif [ $OPTION -eq 2 ]; then
run_workload
elif [ $OPTION -eq 3 ]; then
process_data
else
echo -e "\e[1;33m option selected is out of choice. Running all steps now then..\e[0m"
install_requirements
bootstrap_it
run_workload
process_data
fi