Skip to content

Instantly share code, notes, and snippets.

@fredmajor
Created November 26, 2017 22:18
Show Gist options
  • Save fredmajor/8d1f0b1c2fb0b1d8b3c4e5098b8451aa to your computer and use it in GitHub Desktop.
Save fredmajor/8d1f0b1c2fb0b1d8b3c4e5098b8451aa to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script starts a manjaro vm with following features:
# 1.higly optimized CPU setup with pinning
# 2. q35 machine type
# 3. RAM memory using hugepages
# 4. GPU-passthrough with vfio
# 5. usb-controller passthrough via PCI
# 6. bridged network
MACHINE_NAME="manjaro_studio"
# QEMU name and PID
OPTS="-name $MACHINE_NAME"
OPTS="$OPTS -pidfile /tmp/$MACHINE_NAME.pid"
# Processor core2duo qemu64 host
# OPTS="$OPTS -cpu qemu64,kvm=off,hv_vendor_id=123456789asx"
OPTS="$OPTS -cpu host,kvm=off,hv_vendor_id=123456789ab,hv_time,hv_relaxed,hv_vapic,hv_spinlocks=0x1fff"
OPTS="$OPTS -smp 6,sockets=1,cores=3,threads=2"
OPTS="$OPTS -vcpu vcpunum=0,affinity=1 -vcpu vcpunum=1,affinity=5"
OPTS="$OPTS -vcpu vcpunum=2,affinity=2 -vcpu vcpunum=3,affinity=6"
OPTS="$OPTS -vcpu vcpunum=4,affinity=3 -vcpu vcpunum=5,affinity=7"
OPTS="$OPTS -enable-kvm"
# Machine
# OPTS="$OPTS -machine type=pc-i440fx-2.1,accel=kvm"
# OPTS="$OPTS -machine type=q35,accel=kvm"
OPTS="$OPTS -machine type=q35"
# The following setting enables S3 (suspend to RAM). OVMF supports S3
# suspend/resume. Disable when using Q35
#OPTS="$OPTS -global PIIX4_PM.disable_s3=0"
# Memory
OPTS="$OPTS -m 8G"
OPTS="$OPTS -mem-path /dev/hugepages"
OPTS="$OPTS -mem-prealloc"
OPTS="$OPTS -balloon none"
# Hardware clock
OPTS="$OPTS -rtc clock=host,base=utc"
# Sound hardware
#QEMU_PA_SAMPLES=128
#export QEMU_AUDIO_DRV=pa
#OPTS="$OPTS -soundhw hda"
# Graphic card passthrough (Gigabyte GeForce GTX 980 G1 Gaming)
OPTS="$OPTS -device vfio-pci,host=01:00.0,multifunction=on"
OPTS="$OPTS -device vfio-pci,host=01:00.1"
# USB controller passthrough, must be accompanied by virsh nodeded-attach/reattach around the actual invocation
OPTS="$OPTS -device i82801b11-bridge,id=pci.1,bus=pcie.0,addr=0x1e"
OPTS="$OPTS -device pci-bridge,chassis_nr=2,id=pci.2,bus=pci.1,addr=0x0"
OPTS="$OPTS -device vfio-pci,host=00:14.0,id=hostdev0,bus=pci.2,addr=0x3"
# USB 1.0 passtrhough
# OPTS="$OPTS -usb"
# OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x2516,productid=0x001a" #CM keyboard
# OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x046d,productid=0xc24a" #logitech G600 mouse
# USB 2.0 passthrough
# OPTS="$OPTS -device usb-ehci,id=ehci"
# OPTS="$OPTS -device usb-host,bus=ehci.0,vendorid=0x194f,productid=0x0101" #presonus
# this not really used here
# USB 3.0 passthrough (NEC/Renesas)
# OPTS="$OPTS -device nec-usb-xhci,id=xhci"
# OPTS="$OPTS -device usb-host,bus=xhci.0,vendorid=0x194f,productid=0x0101" #presonus
# OPTS="$OPTS -device usb-host,bus=xhci.0,vendorid=0x046d,productid=0xc52f" #logitech receiver
#OPTS="$OPTS -device vfio-pci,host=03:00.0"
# Keyboard layout
#OPTS="$OPTS -k en-us"
# Boot priority
OPTS="$OPTS -boot order=c"
# OVMF
#cp "/blacknas/vm_$MACHINE_NAME/ovmf-x64/OVMF_VARS-pure-efi.fd" "/blacknas/vm_$MACHINE_NAME/my_vars.fd"
OPTS="$OPTS -drive if=pflash,format=raw,readonly,file=/blacknas/vm_$MACHINE_NAME/ovmf-x64/OVMF_CODE-pure-efi.fd"
OPTS="$OPTS -drive if=pflash,format=raw,file=/blacknas/vm_$MACHINE_NAME/my_vars.fd"
# System drive
OPTS="$OPTS -drive id=disk0,if=none,cache=unsafe,format=raw,file=/blacknas/vm_$MACHINE_NAME/hdd.img"
OPTS="$OPTS -device driver=virtio-scsi-pci,id=scsi0"
OPTS="$OPTS -device scsi-hd,drive=disk0"
# Other drive
#OPTS="$OPTS -drive id=disk1,if=none,cache=none,aio=native,format=raw,file=/dev/disk/by-id/ata-Hitachi_HDS721050CLA660_JP1570FR1ZWP7K"
#OPTS="$OPTS -device driver=virtio-scsi-pci,id=scsi1"
#OPTS="$OPTS -device scsi-hd,drive=disk1"
# Other drive 2
#OPTS="$OPTS -drive id=disk2,if=none,cache=none,aio=native,format=raw,file=/dev/disk/by-id/ata-Hitachi_HDS5C3020ALA632_ML0220F30NX2DD"
#OPTS="$OPTS -device driver=virtio-scsi-pci,id=scsi2"
#OPTS="$OPTS -device scsi-hd,drive=disk2"
# installer
# OPTS="$OPTS -drive id=cd0,if=none,format=raw,readonly,file=/blacknas/vm_$MACHINE_NAME/manjaro.iso"
# OPTS="$OPTS -device driver=ide-cd,bus=ide.0,drive=cd0"
# host's shares, used for 9p filesystem. NFS is way faster tho
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs0,path=/sharecentre/media -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/sharecentre/media,bus=pcie.0,addr=0x8"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs1,path=/blacknas/photos -device virtio-9p-pci,id=fs1,fsdev=fsdev-fs1,mount_tag=/blacknas/photos,bus=pcie.0,addr=0x9"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs2,path=/blacknas/show-archive -device virtio-9p-pci,id=fs2,fsdev=fsdev-fs2,mount_tag=/blacknas/show-archive,bus=pcie.0,addr=0xa"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs3,path=/blacknas/show-static-contents -device virtio-9p-pci,id=fs3,fsdev=fsdev-fs3,mount_tag=/blacknas/show-static-contents,bus=pcie.0,addr=0xb"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs4,path=/blacknas/show-workspace -device virtio-9p-pci,id=fs4,fsdev=fsdev-fs4,mount_tag=/blacknas/show-workspace,bus=pcie.0,addr=0xc "
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs5,path=/blacknas/backups -device virtio-9p-pci,id=fs5,fsdev=fsdev-fs5,mount_tag=/blacknas/backups,bus=pcie.0,addr=0xd"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs6,path=/blacknas/timemachine -device virtio-9p-pci,id=fs6,fsdev=fsdev-fs6,mount_tag=/blacknas/timemachine,bus=pcie.0,addr=0xe"
#OPTS="$OPTS -fsdev local,security_model=mapped,id=fsdev-fs7,path=/home/fred -device virtio-9p-pci,id=fs7,fsdev=fsdev-fs7,mount_tag=/host/fred,bus=pcie.0,addr=0xf"
# OVMF emits a number of info / debug messages to the QEMU debug console, at
# ioport 0x402. We configure qemu so that the debug console is indeed
# available at that ioport. We redirect the host side of the debug console to
# a file.
#OPTS="$OPTS -global isa-debugcon.iobase=0x402 -debugcon file:/tmp/windows_10_pro.ovmf.log"
# QEMU accepts various commands and queries from the user on the monitor
# interface. Connect the monitor with the qemu process's standard input and
# output.
#OPTS="$OPTS -monitor stdio"
# A USB tablet device in the guest allows for accurate pointer tracking
# between the host and the guest.
#OPTS="$OPTS -device piix3-usb-uhci -device usb-tablet"
# Network
#OPTS="$OPTS -netdev tap,vhost=on,ifname=$VM,script=/usr/local/bin/vm_ifup_brlan,id=brlan"
#OPTS="$OPTS -device virtio-net-pci,mac=52:54:00:xx:xx:xx,netdev=brlan"
OPTS="$OPTS -netdev bridge,id=net0,br=br_vms,helper=/usr/lib/qemu/qemu-bridge-helper -device virtio-net-pci,netdev=net0,id=net0,mac=52:54:00:b3:18:12"
# Disable display
#OPTS="$OPTS -vga cirrus"
OPTS="$OPTS -vga none"
OPTS="$OPTS -serial null"
OPTS="$OPTS -parallel null"
OPTS="$OPTS -monitor none"
OPTS="$OPTS -display none"
#OPTS="$OPTS -daemonize"
# QEMU Guest Agent
#OPTS="$OPTS -chardev socket,path=/tmp/qga.sock,server,nowait,id=qga0"
#OPTS="$OPTS -device virtio-serial"
#OPTS="$OPTS -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0"
virsh nodedev-detach pci_0000_00_14_0
taskset -c 0-5 qemu-system-x86_64 $OPTS
virsh nodedev-reattach pci_0000_00_14_0
#qemu-system-x86_64 $OPTS
#echo $OPTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment