Skip to content

Instantly share code, notes, and snippets.

@cmdrkotori
Last active March 1, 2023 07:52
Show Gist options
  • Save cmdrkotori/d4f78cd814e185b820b19f938392d58a to your computer and use it in GitHub Desktop.
Save cmdrkotori/d4f78cd814e185b820b19f938392d58a to your computer and use it in GitHub Desktop.
QEMU script for macOS
#!/usr/bin/env python3
from subprocess import Popen, call
from time import sleep
threads = 8
mem = 8
eth0mac = '52:54:00:12:34:56'
bootSplash = '/home/tux/vms/splash/boot.jpg'
ovmfCode = '/home/tux/src/1git/macos-kvm-pci-passthrough/OVMF_CODE.fd'
ovmfVars = '/home/tux/src/1git/macos-kvm-pci-passthrough/OVMF_VARS.fd'
drives = ['/home/tux/vms/vm/macos/disk.img',
'/home/tux/vms/vm/macos/disk-2.img',
'/home/tux/vms/img/mac-install.qcow2'
]
drivesRaw = []#'/dev/disk/by-id/usb-Kingston_DataTraveler_3.0_F46D04613D1EE0A069000026-0:0']
usbs = []
vgaSlot = '11'
glassSock = '/tmp/looking-glass.socket'
glassMem = '/dev/shm/looking-glass'
usevnc = False
def sep(u):
return [v for v in u.split() if v]
glass = sep(f'''
-spice unix,addr={glassSock},disable-ticketing
-device ivshmem-plain,memdev=ivshmem
-object memory-backend-file,id=ivshmem,share=on,mem-path={glassMem},size=32M
-device virtio-serial-pci
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0
-chardev spicevmc,id=spicechannel0,name=vdagent
''')
cirrus = sep('''
-device VGA,vgamem_mb=64
-vnc 127.0.0.1:0
''')
vga = sep(f'''
-vga none
-device ioh3420,bus=pcie.0,addr=1c,multifunction=on,port=1,chassis=1,id=root.1
-device vfio-pci,host={vgaSlot}:00.0,bus=root.1,addr=00.0,multifunction=on,x-vga=on
-device vfio-pci,host={vgaSlot}:00.1,bus=root.1,addr=00.1
''')
'Penryn,kvm=on,+invtsc,vmware-cpuid-freq=on'
qemu=sep(f'''
sudo -E qemu-system-x86_64
-enable-kvm
-cpu qemu64,kvm=on,+invtsc,vmware-cpuid-freq=on,+topoext,+ssse3,+sse4.1,+sse4.2,+x2apic
-smp threads={threads}
-M pc-q35-2.9
-m {mem}G
-boot menu=on,splash={bootSplash},splash-time=5000
-drive if=pflash,format=raw,unit=0,file={ovmfCode},readonly=on
-drive if=pflash,format=raw,file={ovmfVars}
-device ahci
-usb
-device usb-kbd
-device usb-mouse
-device e1000-82545em,netdev=n1,mac={eth0mac}
-netdev user,id=n1
-device isa-applesmc,osk=ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc
-audiodev spice,id=usba
-device usb-audio,audiodev=usba
-smbios type=2
-rtc base=utc,base=localtime
-uuid f5b8c05b-9c7a-3211-49b9-2bd635f7e3aa
-name macos
''')
for d in drives:
qemu.extend(['-drive', f'if=ide,file={d},media=disk'])
for r in drivesRaw:
qemu.extend(['-drive', f'if=ide,file={r},media=disk,format=raw'])
for u,v in [x.split(':') for x in usbs]:
qemu.extend(['-device', f'usb-host,vendorid={u},productid={v}'])
client = sep(f'looking-glass-client win:autoResize=yes win:borderless=yes opengl:amdPinnedMem=0 win:position=0x0 spice:port=0 spice:host={glassSock}')
viewer = sep('gvncviewer 127.0.0.1')
qemu.extend(glass)
if usevnc:
qemu.extend(cirrus)
ctrl = viewer
else:
call(sep(f'sudo touch {glassMem}'))
call(sep(f'sudo touch {glassSock}'))
qemu.extend(vga)
ctrl = client
print('qemu cmdline:\n' + ' '.join(qemu))
vm = Popen(qemu)
sleep(1)
if not usevnc:
call(sep(f'sudo chmod 777 {glassMem}'))
call(sep(f'sudo chmod 777 {glassSock}'))
cl = Popen(ctrl)
vm.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment