-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path,run-qemu
executable file
·171 lines (157 loc) · 3.97 KB
/
,run-qemu
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
#!/bin/sh
#
# ,run-qemu -- run a netbsd vm with qemu.
#
# command line flags and options
# -h | --help - print usage message and exit
# -n - dry run: print the commands that would run but do not execute them
# -v | --verbose - show actual commands being run
# -c [nographic|curses|serial]
# - select console. default: serial
# --root-image file - use 'file' as root image
# --root-size size - create root image with size 'size' e.g. 10g
# --iso-image file - use 'file' as image for CD-ROM
# -m | --mem size - allocate 'size" RAM to the VM
# --ssh - forward localhost port 2022 to port 22 so ssh works
# --fsdev dir[,(ro|rw}] - make directory 'dir' available via 9P, default: rw
# -- - stop option processing and pass remaining arguments to qemu
#
# exit status:
# - 1 for errors running commands or missing files/directories
# - 2 for missing arguments to command line options
# - 3 when 'usage' was invoked
#
set -eu
dryrun=
verbose=false
nbvers=10.1
memsz=2g
numcpu=2
pfx=vm
vmrootsz=10g
vmrootfile=${pfx}/netbsd-${nbvers}-root
isoimage=${pfx}/NetBSD-${nbvers}-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 [-h | --help ] [-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
;;
-h | --help)
usage # exits
;;
-n)
dryrun=: ; 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 1 "error creating ${vmrootfile}: $?"
fi
run $qemucmd "$@"