/
kantan
executable file
·385 lines (319 loc) · 9.73 KB
/
kantan
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
#!/bin/bash
# Filename: kantan
# Purpose: main interface to the kantan test suite
# Authors: (c) Michael Prokop <mika@grml.org>
# Bug-Reports: see http://grml.org/bugs/
# License: This file is licensed under the GPL v2 or any later version.
################################################################################
set -u
# global settings {{{
PN=$(basename $0)
VERSION="$(dpkg --list ${PN} 2>/dev/null| awk '/^i/ {print $3}')"
[ -n "${VERSION:-}" ] || VERSION="unknown"
# }}}
# helper functions {{{
info() {
printf "Info: $*\n"
}
warn() {
printf "Warn: $*\n"
}
error() {
printf "Error: $*\n">&2
}
cleanup() {
set +e
[ -n "${socat_pid:-}" ] && kill -9 "$socat_pid" &>/dev/null
[ -n "${http_pid_scripts:-}" ] && kill -9 "$http_pid_scripts" &>/dev/null
[ -n "${http_pid_debs:-}" ] && kill -9 "$http_pid_debs" &>/dev/null
[ -n "${kvm_pid:-}" ] && kill -9 "$kvm_pid" &>/dev/null
[ -n "${vde_pid:-}" ] && kill -9 "$vde_pid" &>/dev/null
[ -n "${terminate_pid:-}" ] && kill -9 "$terminate_pid" &>/dev/null
[ -f "${tmpfile:-}" ] && rm -f "$tmpfile"
if [ "${TYPE:-}" == "server" ] ; then
rm -rf /tmp/kantan_share/$(dirname ${PROFILE_DIRECTORY})
rmdir /tmp/kantan_share 2>/dev/null
fi
}
bailout() {
cleanup
[ -n "${1:-}" ] && exit "$1" || exit 1
}
usage() {
printf "${PN} ${VERSION} - test suite for autotesting software using Grml + KVM
Usage:
${PN} --profile <profile_directory> --type server --disk <disk.img> \
--iso <grml.iso> --bootdir </mnt/point/of/iso/>
or
${PN} --profile <profile_directory> --type client --disk <disk.img> \
[--name <name>] [--kvm <kvm_arguments>]
${PN} --help -- display usage instructions (this screen)
${PN} --version -- display version information and exit
Copyright (c) 2011, Michael Prokop <mika@grml.org>
"
}
# }}}
trap bailout 1 2 3 3 6 9 14 15
# server handling {{{
server_execution() {
VDE_SWITCH="/tmp/kantan_share/${PROFILE_DIRECTORY}/vde_switch"
mkdir -p "$VDE_SWITCH"
# checks
if ! [ -d "${BOOT_DIR}" ] ; then
error "boot directory $BOOT_DIR does not exist"
bailout
fi
if ! [ -d "${PROFILE_DIRECTORY}/share/" ] ; then
error "${PROFILE_DIRECTORY}/share/ not found."
bailout
fi
. "${PROFILE_DIRECTORY}/server.cfg"
current_working_dir=$(pwd)
[ -d /tmp/kantan_share ] || mkdir -m 700 /tmp/kantan_share
vde_switch -s ${VDE_SWITCH} &
vde_pid="$!"
KERNEL=$(find ${BOOT_DIR} -name linux26 -print0)
INITRD=$(find ${BOOT_DIR} -name initrd.gz -print0)
if [ -z "${KERNEL:-}" ] ; then
error "no kernel file linux26 found inside $BOOT_DIR" >&2
bailout
fi
if [ -z "${INITRD:-}" ] ; then
error "no initrd file initrd.gz found inside $BOOT_DIR" >&2
bailout
fi
info "Starting up kvm server instance"
info "Connect via VNC: 'vncviewer localhost'"
info "Connect via serial console: 'sudo screen -m /dev/pts/\$ID 115200 || cu -l /dev/pts/\$ID'"
info "Stop execution via entering 'quit'"
tmpfile=$(mktemp)
socat -u TCP4-LISTEN:8888,reuseaddr,fork - > "${tmpfile}" &
socat_pid="$!"
info "Serving ${PROFILE_DIRECTORY}/share/ to port 8000 for netscript bootoption."
cd "${PROFILE_DIRECTORY}/share/"
python -m SimpleHTTPServer 8000 &>/dev/null &
http_pid_scripts="$!"
cd "$current_working_dir"
if ! [ -d "${PROFILE_DIRECTORY}/debs" ] ; then
warn "No directory ${PROFILE_DIRECTORY}/debs/ found, will not serve local Debian packages."
else
cd "${PROFILE_DIRECTORY}/debs"
if ! which dpkg-scanpackages &>/dev/null ; then
error "dpkg-scanpackages not available, please install package dpkg-dev."
bailout
fi
dpkg-scanpackages . | gzip > Packages.gz
python -m SimpleHTTPServer 8080 &>/dev/null &
http_pid_debs="$!"
fi
cd "$current_working_dir"
# default to vde:
VLAN1="-net vde,sock=${VDE_SWITCH},vlan=1"
# but if VLAN1_DEVICE is configured then use tap devices instead:
if [ -z "${VLAN1_DEVICE:-}" ] ; then
info "Using vde_switch setup (configure VLAN1_DEVICE to use tap instead)."
else
info "Using tap setup as VLAN1_DEVICE is configured."
VLAN1="-net tap,vlan=1,ifname=$VLAN1_DEVICE"
fi
if [ -z "${KVMSERVER_APPEND:-}" ] ; then
KVMSERVER_APPEND="boot=live ignore_bootid noquickconfig netscript=http://10.0.2.2:8000/netscript.sh console=tty1 console=ttyS0"
fi
info "Starting KVM instance"
kvm -k en-us -m "${MEMORY:-1024}" -serial pty \
-net nic,macaddr=$(python /usr/share/kantan/scripts/random_mac.py),vlan=0,model=virtio \
-net user,vlan=0 \
-net nic,macaddr=$(python /usr/share/kantan/scripts/random_mac.py),vlan=1,model=virtio \
$VLAN1 \
-hda "${DISK}" -cdrom "${ISO_FILE}" -kernel "${KERNEL}" -initrd "${INITRD}" \
-append "${KVMSERVER_APPEND} ${KVM_ARGUMENTS:-}" -vnc :0 ${KVM_ARGS:-} &
kvm_pid="$!"
terminate_check $kvm_pid &
terminate_pid="$!"
console
}
# }}}
# client handling {{{
client_execution() {
VDE_SWITCH="/tmp/kantan_share/${PROFILE_DIRECTORY}/vde_switch"
if [ -n "$CLIENTNAME" ] ; then
if [ -r "${PROFILE_DIRECTORY}/${CLIENTNAME}.cfg" ] ; then
info "Reading configuration file ${PROFILE_DIRECTORY}/${CLIENTNAME}.cfg"
. "${PROFILE_DIRECTORY}/${CLIENTNAME}.cfg"
else
info "Configuration file ${PROFILE_DIRECTORY}/${CLIENTNAME}.cfg not found, using defaults."
fi
fi
if ! [ -r ${VDE_SWITCH} ] ; then
error "could not retrive vde_switch socket ${VDE_SWITCH}"
error "Server process not running yet?"
bailout
fi
# default if unset
if [ -z "${CLIENT_ISO:-}" ] ; then
CLIENT_ISO="${PROFILE_DIRECTORY}/ipxe.iso"
fi
if ! [ -r "${CLIENT_ARGS:-}" ] ; then
error "Client ISO $CLIENT_ISO can not be read."
bailout
fi
if [ -z "${KVM_CMDLINE:-}" ] ; then
KVM_CMDLINE="kvm -k en-us -m ${CLIENT_MEMORY:-512} \
-net nic,macaddr=$(python /usr/share/kantan/scripts/random_mac.py),vlan=1,model=virtio \
-net vde,sock=${VDE_SWITCH},vlan=1 \
-cdrom ${CLIENT_ISO} -hda ${DISK} ${KVM_ARGUMENTS:-}" # -boot once=d
fi
printf "Invoking KVM using the following cmdline:\n
$KVM_CMDLINE\n"
$KVM_CMDLINE &
kvm_pid="$!"
terminate_check $kvm_pid &
terminate_pid="$!"
console
}
# }}}
# termination secret {{{
terminate_check() {
if [ -n "${KANTAN_TERMINATE_SECRECT:-}" ] ; then
info "Termination secret $KANTAN_TERMINATE_SECRECT enabled, awaiting string."
else
return 0
fi
if ! [ -r "${tmpfile:-}" ] ; then
error "Could not read ${tmpfile:-}"
return 1
fi
terminate=false
while ! $terminate ; do
if grep -q "${KANTAN_TERMINATE_SECRECT}" ${tmpfile:-} ; then
info "Termination string ${KANTAN_TERMINATE_SECRECT} found, terminating KVM..."
terminate=true
cleanup
kill ${1:-} $$
fi
sleep 5
done
}
# }}}
# simple console prompt {{{
console() {
sleep 2
printf "\nEnter control command: [help|data|quit|pause|cont]\nprompt\$ "
while read s ; do
printf 'prompt$ '
case "$s" in
help)
printf "
Usage instructions:
quit Cancel execution of KVM and immediately exit .
pause Interrupt KVM process for later resuming using 'cont'.
cont Continue execution of paused KVM process.
data Display data that has been received from KVM server instance.\n"
printf 'prompt$ '
;;
data)
if [ -r "${tmpfile:-}" ] ; then
printf "Data from KVM instance:\n"
cat "$tmpfile"
printf 'prompt$ '
else
printf "No data from KVM instance received yet.\n"
printf 'prompt$ '
fi
;;
pause)
printf "Pausing KVM process (use 'cont' to continue execution)\n"
kill -s STOP $kvm_pid
printf 'prompt$ '
;;
cont)
printf "Continuing KVM process (use 'pause' to pause execution again)\n"
kill -s CONT $kvm_pid
printf 'prompt$ '
;;
exit|quit)
bailout 0
;;
esac
done
}
# }}}
# cmdline parsing {{{
_options=$(getopt --name kantan -o +b:d:i:k:n:p:t:hv --long \
bootdir:,disk:,iso:,kvm:,name:,profile:,type:,help,verbose,version -- "$@")
[ $? -ne 0 ] && { usage ; bailout ; }
eval set -- "$_options"
while :; do
case "${1:-}" in
--bootdir|-b)
shift ; BOOT_DIR="${1}"
;;
--disk|-d)
shift ; DISK="${1}"
;;
--iso|-i)
shift ; ISO_FILE="${1}"
;;
--kvm|-k)
shift ; KVM_ARGUMENTS="${1}"
;;
--name|-n)
shift ; CLIENTNAME="${1}"
;;
--profile|-p)
shift ; PROFILE_DIRECTORY="${1}"
;;
--type|-t)
shift ; TYPE="${1}"
;;
--verbose)
if [ "${DEBUG:-}" ]; then
DEBUG=$(expr $DEBUG + 1)
else
DEBUG=1;
fi
;;
--help|-h)
usage ; exit 0
;;
--version|-v)
echo "${PN}, version ${VERSION}" ; bailout 0
;;
--)
shift; break
;;
*)
error "internal getopt error."
bailout
;;
esac
shift
done
# }}}
# execution checks {{{
if [ -z "${PROFILE_DIRECTORY:-}" ] ; then
error "profile option is unset, --profile is mandatory." ; bailout
fi
if [ -z "${TYPE:-}" ] ; then
error "executionttype option is unset, --type is mandatory." ; bailout
fi
if [ -z "${DISK:-}" ] ; then
error "harddisk option is unset, --disk is mandatory." ; bailout
fi
if [ -z "${BOOT_DIR:-}" ] ; then
error "bootdir option is unset, --bootdir is mandatory." ; bailout
fi
if ! [ -d "${PROFILE_DIRECTORY:-}" ] ; then
error "profile directory $PROFILE_DIRECTORY does not exist." ; bailout
fi
# }}}
# main execution {{{
case "$TYPE" in
server) server_execution ;;
client) client_execution;;
*) error "unsupported --type argument, exiting." ; bailout ;;
esac
bailout 0
# }}}
## END OF FILE #################################################################
# vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2