forked from chazy/vm-loop-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop-test.sh
executable file
·134 lines (115 loc) · 2.62 KB
/
loop-test.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
#!/bin/bash
############################# Init and configuration ##################
RESULT=0
J=1
FS=debian-sid.qcow2
usage() {
U=""
if [[ -n "$1" ]]; then
U="${U}$1\n\n"
fi
U="${U}Usage: $0 [options [--]] [arguments]\n\n"
U="${U}Options:\n"
U="$U --fs <image>: Guest file system (default $FS)\n"
U="$U -j <i>: Run <i> guests in parallel (default: $J)\n"
U="$U -h | --help: Show this output\n"
U="${U}\n"
U="${U}Remaining arguments are passed down to the run script.\n"
U="${U}\n"
echo -e "$U" >&2
}
while :
do
case "$1" in
--fs)
FS="$2"
shift 2
;;
-j)
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
usage "Unrecognized number"
exit 1
fi
J=$2
shift 2
;;
-h | --help)
usage ""
exit 1
;;
--) # End of all options
shift
break
;;
-*) # Unknown option
break
;;
*)
break
;;
esac
done
#######################################################################
############################# Trap handler ############################
trap ctrl_c INT
RUN=true
function ctrl_c() {
if ! $RUN; then
echo "Pressed ctrl+c twice, killing all guests."
for i in `seq 0 $((J - 1))`; do
kill ${pids[$i]}
done
exit 1
fi
echo "Waiting for guests to exit current round of testing..."
RUN=false
}
#######################################################################
############################# Main Execution ##########################
EXIT_CODE1=0
EXIT_CODE2=0
mkdir -p /tmp/vm-loop-test
IMGDIR=`mktemp -d -p /tmp/vm-loop-test`
#IMGDIR=tmp-$$
#mkdir -p $IMGDIR
for i in `seq 0 $((J - 1))`; do
imgs[$i]=$IMGDIR/guest$i.qcow2
logs[$i]=$IMGDIR/guest$i.log
qemu-img create -b "`pwd`/${FS}" -F qcow2 -f qcow2 ${imgs[$i]}
done
iter=1
while $RUN;
do
echo "Running $J guest(s): Round: $iter"
for i in `seq 0 $((J - 1))`; do
setsid expect hackbench-shutdown.exp \
"guest$i" \
--fs ${imgs[$i]} \
--alt-console $((5000 + i)) $@ > ${logs[$i]} &
pids[$i]=$!
done
# Since the wait may be interrupted by the signal handler, we need another loop
i=0
while [[ $i -lt $J ]]; do
wait ${pids[$i]}
EXIT_CODE=$?
# The signal handler will return from wait and give us an exit code greater
# than 128 - see the bash manual and search for 'wait builtin'.
if [[ $EXIT_CODE -gt 128 ]]; then
continue
fi
exit_codes[$i]=$EXIT_CODE
i=$(($i+1))
done
for i in `seq 0 $((J - 1))`; do
if [[ ${exit_codes[$i]} != 0 ]]; then
echo "guest$i had non-zero exit-code: ${exit_codes[$i]}, see ${logs[$i]}"
RESULT=1
RUN=false
fi
done
iter=$(($iter+1))
sleep 1
done
#######################################################################
exit $RESULT