-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_qemu.sh
executable file
·199 lines (180 loc) · 4.6 KB
/
run_qemu.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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
set -eo pipefail
RAM="2G"
SMP="2"
HOST_ARCH="$(uname -m)"
ARCH=""
VM_IMAGE=""
TELNET_PORT=""
SSH_PORT="10022"
DAEMONISE=""
PID_FILE=""
NO_EFI_STORAGE_PERSIST=""
OVERWRITE_EFI=""
function print_usage {
echo "$(basename "${0}"): run an EFI-based NemOS image using QEMU"
echo "Made by the NemOS Team <[email protected]>"
echo ""
echo "Usage: $(basename "${0}") [-h][-?] -f <path> [-a arch] [-m memory] [-c cpus] [-s port] [-t port] [-p pid] [-d] [-e] [-o]"
echo ""
echo " -h/-?: Print this help message"
echo " -f: Path to VM image (required)"
echo " -a: Guest architecture (defaults to ${HOST_ARCH})"
echo " -m: Memory size (defaults to ${RAM})"
echo " -c: Number of virtual CPUs (defaults to ${SMP})"
echo " -s: Enable SSH access on the given port number (defaults to ${SSH_PORT})"
echo " -t: Enable telnet console access on the given port number"
echo " -p: Write the process PID to the given file"
echo " -d: Daemonise the VM process"
echo " -e: Do not use persistent EFI storage (normally it will be saved to /tmp/<VM image file name>.efivars)"
echo " -o: Overwrite any existing EFI variable storage"
}
while getopts "h?a:f:c:m:s:t:p:deo" opt; do
case "${opt}" in
h | \?)
print_usage
exit 0
;;
a)
ARCH="${OPTARG}"
;;
f)
VM_IMAGE="${OPTARG}"
;;
m)
RAM="${OPTARG}"
;;
c)
SMP="${OPTARG}"
;;
t)
TELNET_PORT="${OPTARG}"
;;
s)
SSH_PORT="${OPTARG}"
;;
p)
PID_FILE="${OPTARG}"
;;
d)
DAEMONISE="1"
;;
e)
NO_EFI_STORAGE_PERSIST="1"
;;
o)
OVERWRITE_EFI="1"
;;
esac
done
if [ -z "${VM_IMAGE}" ]; then
print_usage
exit 1
fi
# Use the host architecture if another wasn't specified
if [ -z "${ARCH}" ]; then
ARCH="${HOST_ARCH}"
fi
case "${ARCH}" in
arm64)
# Correct for mismatched arch naming scheme
ARCH="aarch64"
;&
aarch64)
NVRAM_PATH="/usr/share/AAVMF/AAVMF_VARS.fd"
EFI_PATH="/usr/share/AAVMF/AAVMF_CODE.fd"
MACHINE="virt"
QEMU_CPU="cortex-a53"
TPM_DEVICE="tpm-tis-device"
;;
amd64)
# Correct for mismatched arch naming scheme
ARCH="x86_64"
;&
x86_64)
NVRAM_PATH="/usr/share/OVMF/OVMF_VARS.fd"
EFI_PATH="/usr/share/OVMF/OVMF_CODE.fd"
MACHINE="q35"
QEMU_CPU="qemu64"
TPM_DEVICE="tpm-tis"
;;
*)
echo "Invalid architecture: ${ARCH}"
echo "Please pick one of:"
echo " - x86_64"
echo " - aarch64"
exit 1
;;
esac
QEMU_CMD="qemu-system-${ARCH}"
# Enable acceleration if the host architecture matches and KVM is available
if [ "${HOST_ARCH}" = "${ARCH}" ] && kvm-ok > /dev/null; then
echo "Enabling KVM acceleration"
QEMU_ARGS="--enable-kvm --cpu host"
else
echo "KVM acceleration not available"
QEMU_ARGS="--cpu ${QEMU_CPU}"
fi
if [ -n "${NO_EFI_STORAGE_PERSIST}" ]; then
NVRAM=$(mktemp)
echo "Using temporary EFI variable storage"
else
NVRAM="/tmp/$(basename "${VM_IMAGE}").efivars"
echo "Using semi-persistent EFI variable storage (${NVRAM})"
fi
if [ ! -f "${NVRAM}" ] || [ -n "${OVERWRITE_EFI}" ]; then
echo "Copying EFI NVRAM base (${NVRAM_PATH}) to ${NVRAM}"
cp "${NVRAM_PATH}" "${NVRAM}"
fi
if [ -z "${TELNET_PORT}" ]; then
QEMU_ARGS="${QEMU_ARGS}
-serial mon:stdio"
else
echo "Telnet console access will be available on localhost port ${TELNET_PORT}"
QEMU_ARGS="${QEMU_ARGS}
-serial telnet:127.0.0.1:${TELNET_PORT},server,nowait"
fi
if [ -n "${PID_FILE}" ]; then
QEMU_ARGS="${QEMU_ARGS}
-pidfile ${PID_FILE}"
fi
if [ -n "${DAEMONISE}" ]; then
QEMU_ARGS="${QEMU_ARGS}
-daemonize"
fi
if command -v swtpm > /dev/null; then
SWTPM_DIR=$(mktemp -d)
SWTPM_PID=$(mktemp)
QEMU_ARGS="${QEMU_ARGS}
-chardev socket,id=chrtpm,path=${SWTPM_DIR}/swtpm-sock
-tpmdev emulator,id=tpm0,chardev=chrtpm
-device ${TPM_DEVICE},tpmdev=tpm0"
swtpm socket --tpm2 -d -t \
--tpmstate dir="${SWTPM_DIR}" \
--ctrl type=unixio,path="${SWTPM_DIR}/swtpm-sock"
else
echo "swtpm was not found; not adding a TPM device"
fi
echo "SSH access will be available on localhost port ${SSH_PORT}"
echo "Press ctrl-a then x to exit"
${QEMU_CMD} \
${QEMU_ARGS} \
-m "${RAM}" \
--smp "${SMP}" \
-M "${MACHINE}" \
-parallel null -monitor none -display none -vga none \
-drive file="${EFI_PATH}",readonly=on,if=pflash,format=raw,unit=0 \
-drive file="${NVRAM}",if=pflash,format=raw,unit=1 \
-netdev user,id=user0,hostfwd=tcp:127.0.0.1:"${SSH_PORT}"-:22 \
-device virtio-net-pci,netdev=user0 \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \
-drive file="${VM_IMAGE}",if=virtio \
-device i6300esb,id=watchdog0 -watchdog-action reset
if [ -n "${NO_EFI_STORAGE_PERSIST}" ]; then
rm "${NVRAM}"
fi
if [ -n "${SWTPM_DIR}" ]; then
rm -rf "${SWTPM_DIR}"
fi