-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-qemu.sh
executable file
·148 lines (134 loc) · 3.02 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
#!/bin/sh
#
# run-qemu.sh -- run the netbsd vm with qemu.
#
set -eu
dryrun=
verbose=false
memsz=2g
numcpu=2
pfx=vm
vmrootsz=10g
vmrootfile=${pfx}/netbsd-10.0
isoimage=${pfx}/NetBSD-10.0-amd64.iso
hostfwdssh=",hostfwd=tcp:127.1:2222-:22"
console="-display none -serial mon:stdio"
fsdev=""
case `uname -s` in
Linux)
accel=kvm
;;
Darwin)
accel=hvf
;;
NetBSD)
accel=nvmm
;;
*)
err 2 "unkown operating system. can't choose accellerator"
;;
esac
usage() {
cat 1>&2 <<EOF
usage: $0 [-n] [-v | --verbose] [-c [nographic|curses|serial]] [--root-image file] [--root-size sz] [--iso-image file] [[-m|--mem] size] [--ssh] [--fsdev dir[,ro|rw]]... [--] [ qemu-options]
EOF
exit 3
}
err() {
code=$1; shift
echo "$@" 1>&2
exit ${code}
}
run() {
${verbose} && echo "$@" 1>&2
${dryrun} "$@"
}
# parse args
while [ $# -ge 1 ]; do
case "$1" in
--)
shift; break # no more options
;;
-n)
dryrun=echo; shift
;;
-v | --verbose)
verbose=true; shift;
;;
-c)
shift
[ $# -ge 1 ] || err 2 "-c requires an argument (nographic|curses|serial)"
case $1 in
nographic)
console="-nographic"
;;
curses)
console="-display curses"
;;
serial)
console="-display none -serial mon:stdio"
;;
*)
err 2 "unknown console option '$1'"
;;
esac
shift
;;
--root-image)
shift
[ $# -ge 1 ] || err 2 "--root-image requires a file name argument"
vmrootfile=$1; shift
;;
--root-size)
shift
[ $# -ge 1 ] || err 2 "--root-size requires a size argument"
vmrootsz=$1; shift
;;
--iso-image)
shift
[ $# -ge 1 ] || err 2 "--iso-image requires a file name argument"
isoimage="$1"; shift
;;
-m | --mem)
shift
[ $# -ge 1 ] || err 2 "-m | --mem requires a size argument"
memsz=$1; shift
;;
--ssh)
shift
fwdssh=true
;;
--fsdev)
shift
[ $# -ge 1 ] || err 2 "-m | --fsdev requires a directory name"
roflag=$(expr "$1" : '.*,\(r[ow]\)$' || true)
case ${roflag:-rw} in # default to read-write as qemu does
ro) roflag=",readonly=on";;
rw) roflag=;;
esac
dir=$(echo "$1"| sed -e 's/,.*//') # strip everything from ',' on
[ ! -d "$dir" ] && err 1 "directory $dir doesn't exist"
shift
tag=$(basename "$dir")
fsdev="$fsdev -fsdev local,id=${tag},security_model=none,path=${dir}${roflag} -device virtio-9p-pci,fsdev=${tag},mount_tag=${tag}"
;;
*)
echo "unkown option: $1" 1>&2
usage
;;
esac
done
qemucmd="qemu-system-x86_64 -M q35 -cpu host -accel $accel
-smp ${numcpu} -m ${memsz}
-device virtio-rng,rng=rng0
-object rng-random,id=rng0,filename=/dev/urandom
-nic user,model=virtio-net-pci${fwdssh:+$hostfwdssh}
-drive if=ide,index=0,id=wd0,media=disk,file=${vmrootfile}
-cdrom ${isoimage}
${fsdev}
${console}"
if [ ! -f ${vmrootfile} ]; then
run qemu-img create -f qcow2 ${vmrootfile} ${vmrootsz} ||
err 2 "error creating ${vmrootfile}: $!"
fi
run $qemucmd "$@"