-
Notifications
You must be signed in to change notification settings - Fork 1
/
qcl
executable file
·634 lines (537 loc) · 23.5 KB
/
qcl
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env bash
# NOTES:
# - Use VNC or QXL/SPICE so qemu can run as another user
function show_help {
cat <<EndOfHelp
usage: ${0} [- options...]
System options:
--nokvm|--nohvf Disable KVM/HVF accelleration
--efi <bits> Boot using <bits>-bit UEFI instead of BIOS
--pcfrom <year> Emulate hardware from <year>
--ram <MiB> Specify RAM available to VM
--sound Enable sound
--xport <1..99> Port offset for VNC, SPICE, and telnet
--monitor <monitor> Specify how qemu-monitor is accessed
<monitor> is one of the following:
stdio (default) Start monitor in current console
telnet Start monitor at telnet:localhost:61<port>
unix Start monitor as a unix socket
<bin> Start monitor in a new console process
If <monitor> is not stdio, telnet, or unix, then the argument
is assumed to be the binary of a terminal emulator.
CPU options:
--cores <n> Emulate <n> cores. Default is ([cores+2]/3)
--cpu <cpu> Emulate <cpu> model. Default is host.
<cpu> is one of the following:
pentium opteron core-gen1|nehalem
pentium2 core2duo westmere
pentium3 penryn core-gen2|sandybridge
pentium4|coreduo atom64|denverton core-gen3|ivybridge
atom32|n270 core-gen4|haswell
core-gen5|broadwell
Drive options:
--drive <file>:<interface>
<interface> is one of floppy, ide, scsi, ahci, or virtio
If <interface> is not specified then qcl will guess based on the file
extension and machine type.
Note that ahci cannot be combined with type=pc. There is no AHCI
controller for the i440FX chipset. PIIX provides IDE (ATA) only.
SATA is only a connector type -- drives still used the ATA (IDE) protocol
until Intel released AHCI in 2002.
Pointing device options:
QCL will use a PS/2 mouse up until --pcfrom 1999, and usb-tablet with
--pcfrom 2000 and later. VirtIO setups will use virtio-tablet-pci.
--mouse <qemu-pointer>
<qemu-pointer> can be anything QEMU accepts, plus "PS/2"
Common pointers relevant to QCL are:
ps/2 The standard PS/2 QEMU normally presents the guest
usb-mouse Standard USB mouse
usb-tablet Standard USB tablet device
usb-wacom-tablet QEMU PenPartner Tablet
vmmouse ISA bus VM mouse
Additionally, there are VirtIO devices
virtio-mouse-pci Mouse device utilizing VirtIO
virtio-tablet-pci USB tablet device utilizing VirtIO
GPU options
QCL uses cirrus-vga up to --pcfrom 2001 and VGA from --pcfrom 2002 forward
--gpu <qemu-gpu>
<qemu-gpu> can be anything QEMU accepts.
Common GPUs relevant to QCL are:
cirrus[-vga] Cirrus Logic GD-5446
rage128pro ATI Rage 128 Pro
radeon7000 ATI Radeon 7000
VGA Standard VESA VGA device
vmware[-svga] VMWare SVGA device
Network options:
--forward <port:port> Forward host's <port> to guest's <port>
--nic <qemu-nic>
<qemu-nic> can be one of the following
e1000 Intel Gigabit e1000-82540em
intel An alias for e1000
pcnet AMD PCnet Fast Ethernet Card
ne2k An NE2000-compatible Ethernet Card
tulip A DEC 21x4x-compatible Ethernet Card
realtek An alias for rtl8139
rtl8139 Realtek 8139 Ethernet Card
--vtap <mac-address> Use macvtap with <mac-address>
<mac-address> can be set to 'random' if you want a random MAC
address or just don't want to be bothered choosing an address.
Display options:
The default is to start a VNC host and launch a VNC viewer.
--gtk Use a GTK window (QEMU default)
--qxl Use QXL/SPICE instead of VNC
--daemon Don't launch SPICE/vncviewer directly
Utilties:
-h|--help This help text.
EndOfHelp
}
FLOPS=0
PATAS=0
SATAS=0
SCSIS=0
VIRTS=0
function add_drive {
DISC_IMG=${1%%:*}
INTERFACE=${1##*:}
case ${DISC_IMG##*.} in
dsk|DSK) DVC_TYPE='fd'; DVC_FRMT='raw'; MEDIATYPE='floppy';;
fd|FD) DVC_TYPE='fd'; DVC_FRMT='raw'; MEDIATYPE='floppy';;
iso|ISO) DVC_TYPE="cd"; DVC_FRMT='raw'; MEDIATYPE='cdrom';;
img|IMG) DVC_TYPE='hd'; DVC_FRMT='raw'; MEDIATYPE='disk';;
luks) DVC_TYPE='hd'; DVC_FRMT='luks'; MEDIATYPE='disk';;
qcow) DVC_TYPE='hd'; DVC_FRMT='guess'; MEDIATYPE='disk';;
qed) DVC_TYPE='hd'; DVC_FRMT='qed'; MEDIATYPE='disk';;
vdi) DVC_TYPE='hd'; DVC_FRMT='vdi'; MEDIATYPE='disk';;
vhdx) DVC_TYPE='hd'; DVC_FRMT='vhdx'; MEDIATYPE='disk';;
vmdk) DVC_TYPE='hd'; DVC_FRMT='vmdk'; MEDIATYPE='disk';;
vpc) DVC_TYPE='hd'; DVC_FRMT='vpc'; MEDIATYPE='disk';;
*) DVC_TYPE='hd'; DVC_FRMT='raw'; MEDIATYPE='disk';;
esac
if [ "${INTERFACE}" == "${DISC_IMG}" ]; then
printf 'Interface not specified for %s - ' "${DISC_IMG}"
if [ "${MEDIATYPE}" == 'floppy' ]; then
INTERFACE='floppy'
elif [ -n "${VBALLON}" ]; then
INTERFACE='virtio'
elif [[ "${MACHINE}" =~ q35 ]]; then
INTERFACE='ahci'
elif [[ "${MACHINE}" =~ pc ]]; then
INTERFACE='ide'
fi
/usr/bin/printf 'guessed %s\n' "${INTERFACE}"
fi
case ${INTERFACE} in
floppy)
S_DRIVES="${S_DRIVES} -drive id=floppy${FLOPS},media=${MEDIATYPE},file='${DISC_IMG}',cache=none,if=floppy,bus=$((FLOPS/2)),unit=$((FLOPS%2))"
(( FLOPS++ ));;
ide)
S_DRIVES="${S_DRIVES} -drive id=pata${PATAS},media=${MEDIATYPE},file='${DISC_IMG}',cache=none,if=ide,bus=$((PATAS/2)),unit=$((PATAS%2))"
(( PATAS++ ));;
ahci)
[ "${MACHINE}" == 'type=pc' ] && echo 'Cannot use ahci on legacy hardware -- only floppy, ide, and scsi' && exit 1
[ "${SATAS}" -eq 0 ] && S_DRIVES="${S_DRIVES} -device ahci,id=ahci"
S_DRIVES="${S_DRIVES} -device ide-${DVC_TYPE},bus=ahci.${SATAS},drive=sata${SATAS} -drive id=sata${SATAS},media=${MEDIATYPE},file='${DISC_IMG}',cache=none,if=none"
(( SATAS++ ));;
scsi)
[ "${SCSIS}" -eq 0 ] && S_DRIVES="${S_DRIVES} -device ${SCSICARD},id=scsi0"
S_DRIVES="${S_DRIVES} -device scsi-${DVC_TYPE},bus=scsi0.0,drive=scsi${SCSIS} -drive id=scsi${SCSIS},media=${MEDIATYPE},file='${DISC_IMG}',cache=none,if=none,bus=0,unit=${SCSIS}"
(( SCSIS++ ));;
virtio)
[ "${MACHINE}" == 'type=pc' ] && echo 'Cannot use virtio on legacy hardware -- only floppy, ide, and scsi' && exit 1
S_DRIVES="${S_DRIVES} -drive id=vd${VIRTS},media=${MEDIATYPE},file='${DISC_IMG}',cache=none,if=virtio"
(( VIRTS++ ));;
esac
[ "${DVC_FRMT}" = "guess" ] || S_DRIVES="${S_DRIVES},format=${DVC_FRMT}"
}
function set_hardware {
[ "${1}" -lt 1995 ] || [ "${1}" -gt 2015 ] && \
"${1} out of range -- expect 1995-2015" && exit 1
# This is our baseline (1995) and we'll change it depending on the year given
MACHINE='type=pc'
VIRT_CPU='pentium'
CORLIMIT='1'
SCSICARD='am53c974'
NET_CARD='ne2k_pci'
GPU_CARD='cirrus-vga'
POINTING=''
SND_CARD='sb16'
RAMLIMIT='64'
VBALLOON=''
USEACCEL='false'
[ "${1}" -ge 1996 ] && VIRT_CPU='pentium' && RAMLIMIT='128'
[ "${1}" -ge 1997 ] && VIRT_CPU='pentium2' && RAMLIMIT='256' && NET_CARD="pcnet"
[ "${1}" -ge 1998 ] && RAMLIMIT='384'
[ "${1}" -ge 1999 ] && VIRT_CPU='pentium3' && RAMLIMIT='448' && SCSICARD="dc390"
[ "${1}" -ge 2000 ] && VIRT_CPU='pentium4' && RAMLIMIT='768' && SND_CARD='ac97'
[ "${1}" -ge 2001 ] && RAMLIMIT='1024' && USEACCEL='true'
[ "${1}" -ge 2002 ] && RAMLIMIT='2048' && GPU_CARD="VGA" && NET_CARD='rtl8139'
[ "${1}" -ge 2003 ] && RAMLIMIT='3072'
[ "${1}" -ge 2004 ] && VIRT_CPU='opteron' && RAMLIMIT='4096' && POINTING='usb-tablet'
[ "${1}" -ge 2005 ] && SND_CARD='hda-ouput'
[ "${1}" -ge 2006 ] && VIRT_CPU='core2duo' && CORLIMIT='2' && RAMLIMIT='8192'
[ "${1}" -ge 2007 ] && VIRT_CPU='penryn' && CORLIMIT='4' && RAMLIMIT='999999999'
[ "${1}" -ge 2008 ] && VIRT_CPU='core-gen1' && CORLIMIT='8'
[ "${1}" -ge 2009 ] && MACHINE='type=q35' && SND_CARD='ich9-intel-hda'
[ "${1}" -ge 2010 ] && VIRT_CPU='westmere' && CORLIMIT='12'
[ "${1}" -ge 2011 ] && VIRT_CPU='core-gen2'
[ "${1}" -ge 2012 ] && VIRT_CPU='core-gen3'
[ "${1}" -ge 2013 ] && VIRT_CPU='core-gen4' && CORLIMIT='16' && NET_CARD='e1000e'
[ "${1}" -ge 2014 ] && VIRT_CPU='core-gen5'
[ "${CORLIMIT}" -lt "${VIRTCORS}" ] && VIRTCORS="${CORLIMIT}"
[ "${RAMLIMIT}" -lt "${RAM_SIZE}" ] && RAM_SIZE="${RAMLIMIT}"
}
function set_windows_hardware {
case "${1,,}" in
95|4.0) WIN_VRSN=400;;
95a|4.0a) WIN_VRSN=401;;
95b|4.0b) WIN_VRSN=402;;
95c|4.0c) WIN_VRSN=403;;
98|4.1) WIN_VRSN=410;;
98se|se) WIN_VRSN=411;;
me|4.9) WIN_VRSN=490;;
2000|2k|5.0) WIN_VRSN=500;;
xp|5.1) WIN_VRSN=510;;
xp64|5.2) WIN_VRSN=520;;
vista|6.0) WIN_VRSN=600;;
7|6.1) WIN_VRSN=610;;
8|6.2) WIN_VRSN=620;;
8.1|6.3) WIN_VRSN=630;;
10) WIN_VRSN=1000;;
11) WIN_VRSN=1100;;
*) echo "Unkown Windows version ${1}"; exit 1
esac
VBALLOON=''
[ "${WIN_VRSN}" -lt 402 ] && VIRT_CPU='pentium'
if [ "${WIN_VRSN}" -ge 402 ] && [ "${WIN_VRSN}" -lt 410 ]; then
[ ${VIRT_CPU} != "pentium" ] && [ ${VIRT_CPU} != "pentium2" ] && VIRT_CPU="pentium2"
elif [ "${WIN_VRSN}" -ge 410 ] && [ "${WIN_VRSN}" -lt 520 ]; then
[[ ${VIRT_CPU} =~ "pentium" ]] || VIRT_CPU="pentium4"
fi
[ "${WIN_VRSN}" -lt 410 ] && NET_CARD='ne2k_pci'
[ "${WIN_VRSN}" -ge 410 ] && [ "${WIN_VRSN}" -lt 500 ] && NET_CARD='pcnet'
if [ "${WIN_VRSN}" -lt 500 ]; then
VIRTCORS=1
POINTING=''
SND_CARD='sb16'
USEACCEL='false'
[ "${RAM_SIZE}" -gt 448 ] && RAM_SIZE=448
else
POINTING='usb-tablet'
SND_CARD='ac97'
USEACCEL='true'
NET_CARD='rtl8139'
fi
if [ "${WIN_VRSN}" -eq 500 ] || [ "${WIN_VRSN}" -eq 510 ]; then
[ "${RAM_SIZE}" -gt 4096 ] && RAM_SIZE=4096
fi
[ "${WIN_VRSN}" -eq 500 ] && [ "${VIRTCORS}" -gt 2 ] && VIRTCORS=2
[ "${WIN_VRSN}" -lt 600 ] && GPU_CARD='cirrus-vga' || GPU_CARD='VGA'
[ "${WIN_VRSN}" -lt 610 ] && MACHINE='type=pc' || MACHINE='type=q35'
}
function set_vga_resolution {
[ "${GPU_CARD:0:3}" != "VGA" ] && return
if [ -z "${1}" ]; then
SERCHSTR='Display.*primary.*\n(\s+[0-9]+x[0-9]+.*\n)+'
PDISPRES=$(xrandr | grep -Pzo "${SERCHSTR}" | grep -a '\*' | grep -o '[0-9]\+x[0-9]\+')
MXHRZNTL="$(( "${PDISPRES%x*}" - 64 ))"
MXVERTCL="$(( "${PDISPRES#*x}" - 64 ))"
else
MXHRZNTL="${1%x*}"
MXVERTCL="${1#*x}"
fi
for DISP_RES in 1920x1080 1680x1050 1280x1024 1600x960 1440x960 \
1280x960 1280x800 1360x768 1280x720 1024x768 800x600 640x480; do
HORZ_RES="${DISP_RES%x*}"
VERT_RES="${DISP_RES#*x}"
[ $((HORZ_RES)) -gt "${MXHRZNTL}" ] || [ $((VERT_RES)) -gt "${MXVERTCL}" ] && continue
GPU_CARD="VGA,edid=on,xmax=${HORZ_RES},ymax=${VERT_RES}"
break
done
}
# Before doing anything, make sure the qemu user exists and is set up
KVM_GRID="$(/bin/grep kvm /etc/group | /usr/bin/cut -d: -f3)"
# Make a "complete" match first so that we pass this check quickly if already correctly set up
if ! /bin/grep -q "^qemu:.*:[0-9]\+:${KVM_GRID}:.*:.*:\(/usr\)\?/s\?bin/nologin$" /etc/passwd; then
# If we're here, something isn't set up yet.
if ! /bin/grep -q '^qemu:' /etc/passwd; then
# 2nd most common scenario is that the user hasn't even been set up.
for LCOUNTER in {0..9}; do
IDNUMBER=$((KVM_GRID+LCOUNTER))
/bin/grep -q "${IDNUMBER}" /etc/passwd || break
IDNUMBER=$((KVM_GRID-LCOUNTER))
/bin/grep -q "${IDNUMBER}" /etc/passwd || break
done
/usr/sbin/useradd --no-create-home --uid "${IDNUMBER}" --gid kvm --groups kvm --shell /sbin/nologin qemu
else
# PEBKAC, now we check all of these
/bin/grep -q "^qemu:.*:[0-9]\+:${KVM_GRID}:" /etc/passwd || /usr/sbin/usermod --gid kvm qemu
/bin/grep -q "^qemu:.*:\(/usr\)\?/s\?bin/nologin$" /etc/passwd || /usr/sbin/usermod --shell /sbin/nologin qemu
fi
fi
/bin/grep -q "^kvm:.*:${KVM_GRID}.*[,:]qemu" /etc/group || /usr/sbin/usermod --append --groups kvm qemu
/bin/grep -q '^qemu:!' /etc/shadow || /usr/sbin/usermod --lock qemu
USEACCEL='true'
BASE='-nodefaults -parallel none -serial none'
MACHINE='type=q35'
FIRMWARE='BIOS'
ENABLESC='false'
SHOW_CFG='false'
MAXDSIZE=''
#CPU
VIRT_CPU="host"
VIRTCORS=$((($(//usr/bin/getconf _NPROCESSORS_ONLN)+2)/3))
#RAM (default if not specified)
for MEMORIES in /sys/devices/system/memory/memory*; do
if [[ $(< "${MEMORIES}/online") == 1 ]]; then
((TOTALRAM+=0x$(/bin/cat /sys/devices/system/memory/block_size_bytes)))
fi
done
POWEROF2="$(/usr/bin/awk "BEGIN {print int(log(${TOTALRAM}/3)/log(2))}")"
RAM_SIZE="$((2**POWEROF2/1024**2))"
# DISPLAY
PORT_NUM=0
VDISPLAY=vnc
V_LAUNCH=true
QMONITOR=stdio
# DEVICES
VBALLOON='virtio-balloon-pci'
POINTING='virtio-tablet-pci'
NET_CARD='virtio-net-pci'
NET_MODE='user'
SCSICARD='virtio-scsi-pci'
SND_CARD='hda-output'
GPU_CARD='VGA'
while [ ${#} -gt 0 ]; do
case "${1}" in
--cores) VIRTCORS="${2}" ; shift 2;;
--cpu) VIRT_CPU="${2}" ; shift 2;;
--daemon) V_LAUNCH=false ; shift 1;;
--drive) add_drive "${2}" ; shift 2;;
--efi) FIRMWARE="UEFI${2}" ; shift 2;;
--forward) PORT_FWD="${PORT_FWD},hostfwd=tcp::$(echo "${2}" | /usr/bin/cut -d: -f1)-:$(echo "${2}" | /usr/bin/cut -d: -f2)"; shift 2;;
--gpu) GPU_CARD="${2}" ; shift 2;;
--gtk) VDISPLAY='gtk' ; shift 1;;
-h|--help) show_help ; exit 0;;
--initrd) BOOTIRFS="${2}" ; shift 2;;
--kernel) BOOTKRNL="${2}" ; shift 2;;
--maxres) MAXDSIZE="${2}" ; shift 2;;
--monitor) QMONITOR="${2}" ; shift 2;;
--mouse) POINTING="${2}" ; shift 2;;
--nic) NET_CARD="${2}" ; shift 2;;
--nohvf) USEACCEL=false ; shift 1;;
--nokvm) USEACCEL=false ; shift 1;;
--ram) RAM_SIZE="${2}" ; shift 2;;
--pcfrom) set_hardware "${2}" ; shift 2;;
--qxl) VDISPLAY=spice; GPU_CARD=qxl-vga; shift 1;;
--sound) ENABLESC='true' ; shift 1;;
--showcfg) SHOW_CFG='true' ; shift 1;;
--term) QMONITOR="${2}"; echo '--term <bin> is deprecated, use --monitor <bin> instead' >&2; shift 2;;
--usehvf) USEACCEL='true' ; shift 1;;
--usekvm) USEACCEL='true' ; shift 1;;
--vtap) NET_MODE='tap'; MAC_ADDR="${2}"; shift 2;;
--windows) set_windows_hardware "${2}"; shift 2;;
--xport) PORT_NUM="${2}" ; shift 2;;
*) echo "Unkonw argument ${1}"; show_help >&2; exit 1;;
esac
done
shift $((OPTIND-1))
if [ -n "${PORT_FWD}" ] && [ -n "${MAC_ADDR}" ]; then
echo "Port forwarding makes no sense when using tap networking. Ignoring port fowards."
PORT_FWD=
fi
[ -n "${PKEXEC_UID}" ] && SUDO_USER=$(/usr/bin/getent passwd "${PKEXEC_UID}" | /usr/bin/cut -d: -f1)
[ -z "${SUDO_USER}" ] && echo "Must use sudo or pkexec to run as root." && exit 1
if [ "${FIRMWARE:0:4}" = "UEFI" ]; then
/usr/bin/printf 'Finding UEFI firmware...'
# Try to use locate, it's faster
if [ -e /usr/bin/plocate ]; then
FRMWRFIL="$(/usr/bin/plocate "/usr/*${FIRMWARE: -2}/OVMF_CODE.fd")"
elif [ -e /usr/bin/mlocate ]; then
FRMWRFIL="$(/usr/bin/mlocate "/usr/*${FIRMWARE: -2}/OVMF_CODE.fd")"
elif [ -e /bin/locate ]; then
FRMWRFIL="$(/bin/locate "/usr/*${FIRMWARE: -2}/OVMF_CODE.fd")"
else
# Maybe the system doesn't have a locate?
FRMWRFIL="$(/usr/bin/find /usr -type f -wholename '/usr/*${FIRMWARE: -2}/OVMF_CODE.fd')"
fi
case "${FRMWRFIL}" in
*OVMF_CODE.fd) UEFI_OPT="-bios ${FRMWRFIL}"; echo "${FRMWRFIL}" ;;
*) echo 'OVMF_CODE.fd not found' && exit 1 ;;
esac
fi
[ "${GPU_CARD}" == "VGA" ] && set_vga_resolution "${MAXDSIZE}"
if [ "${MACHINE}" == 'type=pc' ] && [ "${POINTING:0:3}" == 'usb' ]; then
DEVICES="-device piix4-usb-uhci"
fi
case "${VIRT_CPU}" in
pentium) true;;
pentium2) true;;
pentium3) true;;
pentium4) VIRT_CPU='coreduo';;
coreduo) true;;
opteron) VIRT_CPU='Opteron_G1';;
core2duo) true;;
penryn) VIRT_CPU='Penryn';;
n270) true;;
atom32) VIRT_CPU='n270';;
denverton) VIRT_CPU='Denverton';;
atom64) VIRT_CPU='Denverton';;
nehalem) VIRT_CPU='Nehalem-IBRS';;
core-gen1) VIRT_CPU='Nehalem-IBRS';;
westmere) VIRT_CPU='Westmere-IBRS';;
sandybridge) VIRT_CPU='SandyBridge-IBRS';;
core-gen2) VIRT_CPU='SandyBridge-IBRS';;
ivybridge) VIRT_CPU='IvyBridge-IBRS';;
core-gen3) VIRT_CPU='IvyBridge-IBRS';;
haswell) VIRT_CPU='Haswell-noTSX-IBRS';;
core-gen4) VIRT_CPU='Haswell-noTSX-IBRS';;
broadwell) VIRT_CPU='Broadwell-noTSX-IBRS';;
core-gen5) VIRT_CPU='Broadwell-noTSX-IBRS';;
host) true;;
*) echo "Unknown CPU ${VIRT_CPU}"; exit 1;;
esac
case "${GPU_CARD}" in
cirrus) GPU_CARD='cirrus-vga';;
cirrus-vga) true;;
rage128pro) GPU_CARD='ati-vga,model=rage128p';;
radeon7000) GPU_CARD='ati-vga,model=rv100';;
qxl-vga) true;;
VGA*) true;;
vmware) GPU_CARD='vmware-svga';;
vmware-svga) true;;
*) echo "Unknown GPU ${GPU_CARD}" && exit 1;;
esac
case "${NET_CARD}" in
e1000) true;;
intel) NET_CARD='e1000';;
pcnet) true;;
ne2k) NET_CARD='ne2k_pci';;
tulip) true;;
realtek) NET_CARD='rtl8139';;
rtl8139) true;;
virtio-net-*) true;;
*) echo "Unknown NIC ${NET_CARD}" && exit 1;;
esac
for DEVICE in "${GPU_CARD}" "${POINTING}" "${VBALLOON}"; do
[ -n "${DEVICE}" ] && DEVICES="${DEVICES} -device ${DEVICE}"
done
${ENABLESC} && DEVICES="${DEVICES} -device ${SND_CARD}"
if [ -n "${BOOTKRNL}" ] && [ -n "${BOOTIRFS}" ]; then
BOOTPARM="-kernel ${BOOTKRNL} -initrd ${BOOTIRFS}"
else
BOOTPARM="-boot menu=on"
fi
case "$(/bin/uname)" in
Linux)
ACELTYPE='KVM'
${USEACCEL} && KVMACCEL='-enable-kvm' && MACHINE="${MACHINE},accel=kvm"
VNC_CMND="/usr/bin/vncviewer localhost:$((5900+PORT_NUM))"
;;
Darwin)
ACELTYPE='HVF'
${USEACCEL} && MACHINE="${MACHINE},accel=hvf"
VNC_CMND="open vnc://localhost:$((5900+PORT_NUM))"
;;
esac
/usr/bin/lscpu | /bin/grep -q topoext && VIRT_CPU="${VIRT_CPU},topoext"
if [ "$(/usr/bin/lscpu | /bin/grep 'Thread(s) per core' | /bin/sed 's/.*: *//g')" = "2" ]; then
[ "$((VIRTCORS % 2))" = "0" ] && VTHREADS=2 || VTHREADS=1
else
VTHREADS=1
fi
VMCORES="$((VIRTCORS / VTHREADS))"
case "${VDISPLAY}" in
*vnc*)
VDISPLAY="egl-headless -vnc localhost:${PORT_NUM}"
! ${SHOW_CFG} && ${V_LAUNCH} && /bin/sh -c "/bin/sleep 1; /bin/su ${SUDO_USER} -c '/usr/bin/${VNC_CMND}'" > /dev/null 2>&1 &
;;
*spice*)
VDISPLAY="egl-headless -device virtio-serial-pci -spice port=$((6000+PORT_NUM)),disable-ticketing=on"
VDISPLAY+=" -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0"
VDISPLAY+=" -chardev spicevmc,id=spicechannel0,name=vdagent"
[ -e /usr/bin/spicy ] && SPICECMD="/usr/bin/spicy -h localhost -p $((6000+PORT_NUM))"
[ -e /usr/bin/remote-viewer ] && SPICECMD="/usr/bin/remote-viewer spice://localhost:$((6000+PORT_NUM))"
! ${SHOW_CFG} && ${V_LAUNCH} && /bin/sh -c "/bin/sleep 1; /bin/su ${SUDO_USER} -c '/usr/bin/${SPICECMD}'" > /dev/null 2>&1 &
;;
esac
case "${QMONITOR}" in
console) QMONITOR=stdio;;
stdio) true;;
telnet) QMONITOR="telnet:127.0.0.1:$((6100+PORT_NUM)),server,nowait";;
unix)
SCKTFILE="/tmp/qcl_$((6100+PORT_NUM)).sock"
QMONITOR="unix:${SCKTFILE},server,nowait"
{ until [ -e "${SCKTFILE}" ]; do /bin/sleep 1; done; /bin/chown qemu:kvm "${SCKTFILE}"; /bin/chmod 770 "${SCKTFILE}"; } &
;;
*)
[ ! -e "/usr/bin/${QMONITOR}" ] && echo "/usr/bin/${QMONITOR} not found" && exit 1
TERMINAL="${QMONITOR}"
;;
esac
TEMPFILE="$(mktemp)"
printf '\n>>> USE send_key TO SEND KEY COMBOS TO VM THAT THE HOST GRABS
>>> USE system_powerdown TO SEND ACPI POWER BUTTON SIGNAL TO VM
>>> USE quit TO TELL QEMU TO STOP IMMEDIATELY
>>> PRESS CTRL+C TO FORCE-QUIT QEMU\n\n' > "${TEMPFILE}"
if [ "${NET_CARD}" == 'pcnet' ] || [[ "${NET_CARD}" =~ 'ne2k' ]] || [ -z "${NET_CARD}" ]; then
if [ "${VIRT_CPU}" == '486' ] || [[ "${VIRT_CPU}" =~ 'pentium' ]]; then
if [ "${SND_CARD}" == 'sb16' ] || [ -z "${SND_CARD}" ]; then
if [[ ! "${POINTING}" =~ 'tablet' ]]; then
printf '>>> FOR WINDOWS 9X, ⚠PLUG & PLAY BIOS = PCI BUS\n\n' >> "${TEMPFILE}"
fi
fi
fi
fi
if [ "${NET_MODE}" = "tap" ]; then
SYSNETIF="$(/sbin/route | /bin/grep default | /usr/bin/rev | /usr/bin/cut -d\ -f1 | /usr/bin/rev)"
for NUMERATE in {0..99}; do
[ ! -e "/sys/devices/virtual/net/qclvtap${NUMERATE}" ] && break
done
! ${SHOW_CFG} && /bin/ip link add link "${SYSNETIF}" name "qclvtap${NUMERATE}" type macvtap mode bridge
if [ "${MAC_ADDR}" = "random" ]; then
MAC_ADDR=",mac=$(< "/sys/devices/virtual/net/qclvtap${NUMERATE}/address")"
else
! ${SHOW_CFG} && /bin/ip link set dev "qclvtap${NUMERATE}" address "${MAC_ADDR}"
MAC_ADDR=",mac=${MAC_ADDR}"
fi
! ${SHOW_CFG} && /bin/ip link set dev "qclvtap${NUMERATE}" up
NET_MODE="${NET_MODE},fd=30,vhost=on"
! ${SHOW_CFG} && NTWRK_FD="/dev/tap$(< "/sys/devices/virtual/net/qclvtap${NUMERATE}/ifindex")"
fi
QEMU_CMD="/usr/bin/qemu-system-x86_64 -runas qemu ${KVMACCEL} ${UEFI_OPT} -m ${RAM_SIZE} \
-machine ${MACHINE} ${BASE} ${W2K_HACK} -cpu ${VIRT_CPU} \
-smp cores=${VMCORES},threads=${VTHREADS} \
-monitor ${QMONITOR} -display ${VDISPLAY} \
-nic ${NET_MODE},model=${NET_CARD}${MAC_ADDR}${PORT_FWD} \
${DEVICES} ${S_DRIVES} ${BOOTPARM}"
if ${SHOW_CFG}; then
printf '%17s : %s\n' 'Machine Type' "$(echo "${MACHINE}" | /usr/bin/cut -d, -f1 | /usr/bin/cut -d= -f2)"
printf '%17s : %s\n' "${ACELTYPE} Acceleration" "${USEACCEL}"
printf '%17s : %s\n' 'CPU Type' "${VIRT_CPU}"
printf '%17s : %s\n' 'Multithreading' "${VMCORES} cores ${VTHREADS} threads"
printf '%17s : %s\n' 'System RAM' "${RAM_SIZE}"
printf '%17s : %s\n' 'Graphics Card' "${GPU_CARD%%,*}"
printf '%17s : %s\n' 'Network Card' "${NET_CARD}"
printf '%17s : %s\n' 'Network Type' "${NET_MODE%%,*}"
printf '%17s : %s\n' 'Sound Card' "$([ -z "${ENABLESC}" ] && echo "${SND_CARD}" || echo none)"
printf '%17s : %s\n' 'Pointing Device' "${POINTING:-PS/2}"
printf '%17s : %s\n' 'VirtIO Balloon' "$([ -n "${VBALLOON}" ] && echo yes || echo no)"
printf '%17s : %s\n' 'SCSI Card' "$([[ "${DEVICES}" =~ scsi ]] && echo "${SCSICARD}" || echo no)"
printf '%17s : %s\n' 'AHCI Controller' "$([[ "${DEVICES}" =~ ahci ]] && echo yes || echo no)"
echo
printf '%17s : %s\n' 'QEMU Display' "${VDISPLAY%% *}"
printf '%17s : %s\n' 'QEMU Monitor' "${TERMINAL:-${QMONITOR}}"
echo "Resulting Command : $(echo "${QEMU_CMD}" | /bin/sed 's/ \+/ /g')"
exit 0
fi
if [ -n "${TERMINAL}" ]; then
${TERMINAL} -e "/bin/cat '${TEMPFILE}'; exec 30<> ${NTWRK_FD}; ${QEMU_CMD}" &
/bin/sleep 2 && /bin/rm "${TEMPFILE}"
else
[ -t 1 ] && [ "${QMONITOR}" = "stdio" ] && /bin/cat "${TEMPFILE}"
/bin/rm "${TEMPFILE}"
[ -n "${NTWRK_FD}" ] && exec 30<> "${NTWRK_FD}"
[ -t 1 ] && { ( eval "set -x; ${QEMU_CMD}" ); } || eval "${QEMU_CMD}"
[ -n "${NTWRK_FD}" ] && /bin/ip link del link "${SYSNETIF}" name "qclvtap${NUMERATE}" && exec 30>&-
fi
[ -n "${SCKTFILE}" ] && [ -e "${SCKTFILE}" ] && /bin/rm "${SCKTFILE}"