-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_vm.sh
executable file
·137 lines (119 loc) · 4.24 KB
/
run_vm.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
##!/bin/bash
set -e
TESTS_PATH=$(realpath -s tests)
: "${ELMNTL_PREFIX:=}"
: "${ELMNTL_FIRMWARE:=/usr/share/qemu/ovmf-x86_64-smm-ms-code.bin}"
: "${ELMNTL_FIRMWARE_ORIG_VARS:=/usr/share/qemu/ovmf-x86_64-smm-ms-vars.bin}"
: "${ELMNTL_FIRMWARE_VARS:=${TESTS_PATH}/${ELMNTL_PREFIX}ovmf-x86_64-vars.bin}"
: "${ELMNTL_FWDIP:=127.0.0.1}"
: "${ELMNTL_FWDPORT:=2222}"
: "${ELMNTL_MEMORY:=4096}"
: "${ELMNTL_LOGFILE:=${TESTS_PATH}/${ELMNTL_PREFIX}serial.log}"
: "${ELMNTL_PIDFILE:=${TESTS_PATH}/${ELMNTL_PREFIX}testvm.pid}"
: "${ELMNTL_TESTDISK:=${TESTS_PATH}/${ELMNTL_PREFIX}testdisk.qcow2}"
: "${ELMNTL_VMSTDOUT:=${TESTS_PATH}/${ELMNTL_PREFIX}vmstdout}"
: "${ELMNTL_DISKSIZE:=16G}"
: "${ELMNTL_DISPLAY:=none}"
: "${ELMNTL_ACCEL:=kvm}"
: "${ELMNTL_TARGETARCH:=$(uname -p)}"
: "${ELMNTL_MACHINETYPE:=q35}"
: "${ELMNTL_CPU:=max}"
function _abort {
echo "$@" && exit 1
}
function start {
local base_disk=$1
local usrnet_arg="-netdev user,id=user0,hostfwd=tcp:${ELMNTL_FWDIP}:${ELMNTL_FWDPORT}-:22 -device virtio-net-pci,romfile=,netdev=user0"
local accel_arg
local memory_arg="-m ${ELMNTL_MEMORY}"
local global_arg="-global driver=cfi.pflash01,property=secure,value=on"
local firmware_arg="-drive if=pflash,format=raw,unit=0,readonly=on,file=${ELMNTL_FIRMWARE}"
local firwmare_vars_arg="-drive if=pflash,format=raw,unit=1,file="${ELMNTL_FIRMWARE_VARS}""
local disk_arg="-drive file=${ELMNTL_TESTDISK},if=none,id=disk,format=qcow2,media=disk -device virtio-blk-pci,drive=disk,bootindex=1"
local serial_arg="-serial file:${ELMNTL_LOGFILE}"
local pidfile_arg="-pidfile ${ELMNTL_PIDFILE}"
local display_arg="-display ${ELMNTL_DISPLAY}"
local machine_arg="-machine type=${ELMNTL_MACHINETYPE},smm=on"
local cdrom_arg
local cpu_arg
local vmpid
local kvm_arg
if [ -f "${ELMNTL_PIDFILE}" ]; then
vmpid=$(cat "${ELMNTL_PIDFILE}")
if ps -p ${vmpid} > /dev/null; then
echo "test VM is already running with pid ${vmpid}"
exit 0
else
echo "removing outdated pidfile ${ELMNTL_PIDFILE}"
rm "${ELMNTL_PIDFILE}"
fi
fi
if [ ! -e "${ELMNTL_FIRMWARE_ARGS}" ]; then
echo Copy "${ELMNTL_FIRMWARE_ORIG_VARS}" to "${ELMNTL_FIRMWARE_VARS}"
cp "${ELMNTL_FIRMWARE_ORIG_VARS}" "${ELMNTL_FIRMWARE_VARS}"
fi
[ -f "${base_disk}" ] || _abort "Disk not found: ${base_disk}"
case "${base_disk}" in
*.qcow2)
qemu-img create -f qcow2 -b "${base_disk}" -F qcow2 "${ELMNTL_TESTDISK}" > /dev/null
;;
*.iso)
qemu-img create -f qcow2 "${ELMNTL_TESTDISK}" "${ELMNTL_DISKSIZE}" > /dev/null
cdrom_arg="-drive file=${base_disk},readonly=on,if=none,id=cdrom,media=cdrom -device virtio-scsi-pci,id=scsi0 -device scsi-cd,bus=scsi0.0,drive=cdrom,bootindex=2"
;;
*)
_abort "Expected a *.qcow2 or *.iso file"
;;
esac
[ "hvf" == "${ELMNTL_ACCEL}" ] && accel_arg="-accel ${ELMNTL_ACCEL}" && firmware_arg="-bios ${ELMNTL_FIRMWARE} ${firmware_arg}" && cpu_arg="-cpu max,-pdpe1gb"
[ "kvm" == "${ELMNTL_ACCEL}" ] && cpu_arg="-cpu host" && kvm_arg="-enable-kvm"
qemu-system-${ELMNTL_TARGETARCH} ${kvm_arg} ${disk_arg} ${cdrom_arg} ${global_arg} ${firmware_arg} ${firwmare_vars_arg} \
${usrnet_arg} ${kvm_arg} ${memory_arg} ${graphics_arg} -serial stdio ${pidfile_arg} \
${display_arg} ${machine_arg} ${accel_arg} ${cpu_arg}
}
function stop {
local vmpid
local killprog
if [ -f "${ELMNTL_PIDFILE}" ]; then
vmpid=$(cat "${ELMNTL_PIDFILE}")
killprog=$(which kill)
if ${killprog} --version | grep -q util-linux; then
${killprog} --verbose --timeout 1000 TERM --timeout 5000 KILL --signal QUIT ${vmpid}
else
${killprog} -9 ${vmpid}
fi
rm -f "${ELMNTL_PIDFILE}"
else
echo "No pidfile ${ELMNTL_PIDFILE} found, nothing to stop"
fi
}
function clean {
([ -f "${ELMNTL_LOGFILE}" ] && rm -f "${ELMNTL_LOGFILE}") || true
([ -f "${ELMNTL_TESTDISK}" ] && rm -f "${ELMNTL_TESTDISK}") || true
([ -f "${ELMNTL_VMSTDOUT}" ] && rm -f "${ELMNTL_VMSTDOUT}") || true
}
function vmpid {
if [ -f "${ELMNTL_PIDFILE}" ]; then
cat "${ELMNTL_PIDFILE}"
fi
}
cmd=$1
disk=$2
case $cmd in
start)
start "${disk}"
;;
stop)
stop
;;
clean)
clean
;;
vmpid)
vmpid
;;
*)
_abort "Unknown command: ${cmd}"
;;
esac
exit 0