...
 
Commits (10)
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 2019/06/13
Improved libvirt hooks.
### Added
- [libvirt](/libvirt)
- [virt1.xml](/libvirt/virt1.xml)
### Changed
- [qemu](/libvirt/qemu)
## 2019/03/22
### Added
- [scritps](/scripts)
- [virsh-network.xml](/scripts/virsh-network.xml)
- [windows.xml](/scripts/windows.xml)
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [libvirt](https://gitlab.com/YuriAlek/vfio/wikis/Libvirt)
### Changed
- [README.md](/README.md)
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [Home](https://gitlab.com/YuriAlek/vfio/wikis/Home)
- [Install](https://gitlab.com/YuriAlek/vfio/wikis/Install)
- [Troubleshoot](https://gitlab.com/YuriAlek/vfio/wikis/Troubleshoot)
- [Use](https://gitlab.com/YuriAlek/vfio/wikis/Use)
## 2019/01/29
Minor changes in the scripts and new pages in the wiki.
### Added
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [Edit config and config](https://gitlab.com/YuriAlek/vfio/wikis/Edit-the-config)
- [Edit config and script](https://gitlab.com/YuriAlek/vfio/wikis/Edit-the-scripts)
- [Troubleshoot](https://gitlab.com/YuriAlek/vfio/wikis/Troubleshoot)
- [Windows](https://gitlab.com/YuriAlek/vfio/wikis/Windows)
### Changed
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [Home](https://gitlab.com/YuriAlek/vfio/wikis/Home)
- [Use](https://gitlab.com/YuriAlek/vfio/wikis/Use)
- [scritps](/scripts)
- [config](/scripts/config)
- [macos.sh](/scripts/macos.sh)
- [macos-basic.sh](/scripts/macos-basic.sh)
- [macos-network.sh](/scripts/macos-network.sh)
- [macos-virsh.sh](/scripts/macos-virsh.sh)
- [windows.sh](/scripts/windows.sh)
- [windows-basic.sh](/scripts/windows-basic.sh)
- [windows-network.sh](/scripts/windows-network.sh)
- [windows-virsh.sh](/scripts/windows-virsh.sh)
## 2019/01/02
### Changed
- [scritps](/scripts)
- [config](/scripts/config)
- [macos.sh](/scripts/macos.sh)
- [macos-basic.sh](/scripts/macos-basic.sh)
- [macos-network.sh](/scripts/macos-network.sh)
- [macos-virsh.sh](/scripts/macos-virsh.sh)
- [windows.sh](/scripts/windows.sh)
- [windows-basic.sh](/scripts/windows-basic.sh)
- [windows-network.sh](/scripts/windows-network.sh)
- [windows-virsh.sh](/scripts/windows-virsh.sh)
- [README.md](/README.md)
## 2018/12/30
### Added
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [Home](https://gitlab.com/YuriAlek/vfio/wikis/Home)
- [Install](https://gitlab.com/YuriAlek/vfio/wikis/Install)
- [Use](https://gitlab.com/YuriAlek/vfio/wikis/Use)
- [vbios](https://gitlab.com/YuriAlek/vfio/wikis/vbios)
- [scritps](/scripts)
- [dnsmasq.conf](/scripts/dnsmasq.conf)
- [macos.sh](/scripts/macos.sh)
- [macos-basic.sh](/scripts/macos-basic.sh)
- [macos-network.sh](/scripts/macos-network.sh)
- [macos-virsh.sh](/scripts/macos-virsh.sh)
- [smb.conf](/scripts/smb.conf)
- [windows-basic.sh](/scripts/windows-basic.sh)
- [windows-network.sh](/scripts/windows-network.sh)
- [windows-virsh.sh](/scripts/windows-virsh.sh)
- [Changelog](/CHANGELOG.md)
### Changed
- [scripts](/scripts)
- [config](/scripts/config)
- [extract-vbios-linux.sh](/scripts/extract-vbios-linux.sh)
- [extract-vbios-nvflash.sh](/scripts/extract-vbios-nvflash.sh)
- [windows.sh](/scripts/windows.sh)
- [README.md](/README.md)
### Removed
- scripts
- config-macos
- macos-hs-install.md
- macos-hs.sh
- network.sh
- qemu-mac@.service
- qemu@.service
- test-qemu.sh
- windows-install.sh
- Troubleshoot.md [Moved to wiki](https://gitlab.com/YuriAlek/vfio/wikis/Troubleshoot)
- Install.md [Moved to wiki](https://gitlab.com/YuriAlek/vfio/wikis/Install)
# Install guide for QEMU and VFIO
## ArchLinux
### Install the necessary packages
```
# pacman -S qemu ovmf
```
### Enabling IOMMU support on boot
#### For `systemd-boot` edit `/boot/loader/entries/arch.conf` and add `intel_iommu=on` **OR** `amd_iommu=on` and `iommu=pt`.
```
# AMD
options root=/dev/sda2 amd_iommu=on iommu=pt
---------------------------------------------
# Intel
options root=/dev/sda2 intel_iommu=on iommu=pt
```
Reboot.
#### For `GRUB` edit `/etc/default/grub` and append your kernel options, `intel_iommu=on` **OR** `amd_iommu=on` and `iommu=pt`, to the `GRUB_CMDLINE_LINUX_DEFAULT`.
```
# AMD
GRUB_CMDLINE_LINUX_DEFAULT="amd_iommu=on iommu=pt"
---------------------------------------------------
# Intel
GRUB_CMDLINE_LINUX_DEFAULT="intel_iommu=on iommu=pt"
```
And then automatically re-generate the grub.cfg file with:
```
# grub-mkconfig -o /boot/grub/grub.cfg
```
Reboot.
After reboot IOMMU should be working:
```
$ dmesg | grep -e DMAR -e IOMMU
--------------------------------------------
[ 0.492684] AMD-Vi: IOMMU performance counters supported
[ 0.494370] AMD-Vi: Found IOMMU at 0000:00:00.2 cap 0x40
[ 0.494644] perf/amd_iommu: Detected AMD IOMMU #0 (2 banks, 4 counters/bank).
```
That's everything; unlike two GPU passthrough you don't need to load kernel modules on boot.
## For more information refer to this guides
[ArchWiki QEMU](https://wiki.archlinux.org/index.php/QEMU)
[ArchWiki PCI passthrough via OVMF](https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF)
[How to setup a gaming virtual machine with gpu passthrough qemu kvm libvirt and vfio](https://www.se7ensins.com/forums/threads/how-to-setup-a-gaming-virtual-machine-with-gpu-passthrough-qemu-kvm-libvirt-and-vfio.1371980/)
This diff is collapsed.
#!/usr/bin/env bash
## QEMU hooks. How to use.
# chmod +x qemu
# sudo ln -sf "$(pwd)/qemu" /etc/libvirt/hooks/qemu
## Virsh devices.
VIRSH_GPU="pci_0000_06_00_0"
VIRSH_GPU_AUDIO="pci_0000_06_00_1"
## Log file. Only tells if the GPU was un/binded correctly.
LOG=/tmp/qemu_log.txt
if [[ "$1" == "GPU"* ]]; then
if [ "$2" == "prepare" ]; then
## Kill the Window Manager. Yours may vary.
killall i3 > /dev/null 2>&1
sleep 5 ## Just in case
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
## Detach the GPU and USB
virsh nodedev-detach "$VIRSH_GPU" >> "$LOG" 2>&1
virsh nodedev-detach "$VIRSH_GPU_AUDIO" >> "$LOG" 2>&1
## Load vfio.
modprobe vfio-pci
## Start the network.
if [[ $(virsh net-list --all | grep "virt1" | awk '{print $2}') != "active" ]]; then
virsh net-start virt1
fi
fi
if [ "$2" == "release" ]; then
## Stop the network.
if [ "$(virsh net-list --all | grep "virt1" | awk '{print $2}')" == "active" ]; then
## "destroy" actually stops it.
virsh net-destroy virt1
fi
## Unload vfio module.
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Reattach the GPU and USB.
virsh nodedev-reattach "$VIRSH_GPU_AUDIO" >> "$LOG" 2>&1
virsh nodedev-reattach "$VIRSH_GPU" >> "$LOG" 2>&1
## Reload the framebuffer and console.
echo 1 > /sys/class/vtconsole/vtcon0/bind
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
fi
fi
<network>
<name>virt1</name>
<uuid>c387430a-d3f9-4859-a9bd-3a25987de672</uuid>
<forward dev='enp3s0' mode='nat'>
<interface dev='enp3s0'/>
</forward>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:c8:36:d2'/>
<domain name='virt1'/>
<ip address='192.168.100.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.100.128' end='192.168.100.254'/>
</dhcp>
</ip>
</network>
# User.
USER=yu
# Path to VBIOS, IMG, Windows ISO, Virtio iso, ...
IMAGES=/home/$USER/vm
# IOMMU groups for passed devices.
IOMMU_GPU=06:00.0
IOMMU_GPU_AUDIO=06:00.1
IOMMU_USB=07:00.3
# Virsh devices, only needed if you use virsh.
VIRSH_GPU=pci_0000_06_00_0
VIRSH_GPU_AUDIO=pci_0000_06_00_1
VIRSH_USB=pci_0000_07_00_3
# PCI BUS ID for binding/unbinding devices.
videoid="10de 1184"
audioid="10de 0e0a"
usbid="1022 145f"
videobusid="0000:06:00.0"
audiobusid="0000:06:00.1"
usbbusid="0000:07:00.3"
# Images needed for QEMU.
VBIOS=$IMAGES/GK104_80.04.C3.00.0F-MODED.rom
IMG=$IMAGES/windows.raw
## User
VM_USER=
## IOMMU groups
IOMMU_GPU=""
IOMMU_GPU_AUDIO=""
## For extracting the VBIOS
VBIOS_EXTRACT_PATH=/root/VBIOS.rom
NVFLASH=/root/nvflash_linux
extract_videobusid="0000:$IOMMU_GPU"
## Virsh devices
VIRSH_GPU=pci_0000_
VIRSH_GPU_AUDIO=pci_0000_
# PCI BUS ID
videoid=""
audioid=""
videobusid="0000:$IOMMU_GPU"
audiobusid="0000:$IOMMU_GPU_AUDIO"
## QEMU options
RAM=8G
CORES=8
MACOS_RAM=8G
MACOS_CORES=4
## Network
TAP_INTERFACE=tap0
TAP_IP="10.10.10.1/24"
DNSMASQ_CONF="${BASH_SOURCE%/*}/dnsmasq.conf"
SMB_CONF="${BASH_SOURCE%/*}/smb.conf"
## Log file
LOG=/home/$VM_USER/qemu_log.txt
## Local path for storage
IMAGES=/home/$VM_USER/
## VBIOS
VBIOS=$IMAGES/VBIOS.rom
## Windows
VIRTIO=$IMAGES/virtio.iso
ISO=$IMAGES/win10.iso
HDD=/dev/sdc
WINDOWS_ISO=$IMAGES/win10.iso
WINDOWS_IMG=$IMAGES/windows.qcow2
OVMF=/usr/share/ovmf/x64/OVMF_CODE.fd
OVMF_VARS=/usr/share/ovmf/x64/OVMF_VARS.fd
# QEMU options
RAM=12G
CORES=12
## MacOS
MACOS_ISO=$IMAGES/mojave.iso
MACOS_IMG=$IMAGES/mojave.qcow2
MACOS_CLOVER=$IMAGES/clover.qcow2
MACOS_OVMF=$IMAGES/MACOS_OVMF_CODE.fd
MACOS_OVMF_VARS=$IMAGES/MACOS_OVMF_VARS.fd
MACOS_OPTIONS="+pcid,+ssse3,+sse4.2,+popcnt,+aes,+xsave,+avx,+xsaveopt,avx2,+smep,check"
# To run QEMU as user you need to allow more RAM to be locked by an user.
## Ulimit
ULIMIT=$(ulimit -a | grep "max locked memory" | awk '{print $6}')
# Variable used to change the Frame Buffer resolution. Not needed.
RES="1920 1080"
ULIMIT_TARGET=$(( $(echo $RAM | tr -d 'G')*1048576+100000 ))
# User.
USER=yu
# Path to VBIOS, IMG, Windows ISO, Virtio iso, Clover, OVMF, ...
IMAGES=/home/$USER/vm
# IOMMU groups for passed devices.
IOMMU_GPU=06:00.0
IOMMU_GPU_AUDIO=06:00.1
IOMMU_USB=07:00.3
# Virsh devices, only needed if you use virsh.
VIRSH_GPU=pci_0000_06_00_0
VIRSH_GPU_AUDIO=pci_0000_06_00_1
VIRSH_USB=pci_0000_07_00_3
# PCI BUS ID for binding/unbinding devices.
videoid="10de 1184"
audioid="10de 0e0a"
usbid="1022 145f"
videobusid="0000:06:00.0"
audiobusid="0000:06:00.1"
usbbusid="0000:07:00.3"
# Images needed for QEMU.
VBIOS=$IMAGES/GK104_80.04.C3.00.0F-MODED.rom
IMG=$IMAGES/mac-hs.raw,id=disk,format=raw,if=none
CLOVER=$IMAGES/Clover-1080.qcow2
ISO=$IMAGES/HighSierra-10.13.6-qemu.iso
OVMF=$IMAGES/OSX-KVM/OVMF_CODE.fd
OVMF_VARS=$IMAGES/OSX-KVM/OVMF_VARS.fd
# QEMU options
MY_OPTIONS="+aes,+xsave,+avx,+xsaveopt,avx2,+smep"
RAM=12G
CORES=4
# To run QEMU as user you need to allow more RAM to be locked by an user.
ULIMIT=$(ulimit -a | grep "max locked memory" | awk '{print $6}')
# Variable used to change the Frame Buffer resolution. Not needed.
RES="1920 1080"
# Disable DNS
port=0
# Address to listen on
listen-address=10.10.10.1
# Interface to listen on
interface=tap0
# Except interfaces
except-interface=lo,enp3s0
# Set default gateway
dhcp-option=3,10.10.10.1
# Set DNS servers to announce
dhcp-option=6,1.1.1.1,1.0.0.1
# Dynamic range of IPs, and lease time.
dhcp-range=10.10.10.10,10.10.10.240,12h
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
## Check if the script was executed as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
# Variables
VBIOS=/root/vBIOS.rom
videobusid="0000:06:00.0"
# END Variables
## Load the config file
source "${BASH_SOURCE%/*}/config"
_start()
{
# Memory lock limit
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
## Kill the Display Manager
systemctl stop lightdm
# Kill the console to free the GPU
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 0 > /sys/class/vtconsole/vtcon1/bind
sleep 1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
sleep 1
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
sleep 1
modprobe -r nvidia_modeset
sleep 1
modprobe -r nvidia
sleep 1
modprobe -r snd_hda_intel
sleep 2
}
_stop()
{
# Reload the kernel modules. This loads the drivers for the GPU
## Extract the VBIOS
echo 1 > /sys/bus/pci/devices/$videobusid/rom
cat /sys/bus/pci/devices/$videobusid/rom > $VBIOS_EXTRACT_PATH
echo 0 > /sys/bus/pci/devices/$videobusid/rom
# Reload the kernel modules
modprobe snd_hda_intel
sleep 5
modprobe nvidia_drm
sleep 2
modprobe nvidia_modeset
sleep 2
modprobe nvidia
sleep 5
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
# [Source] [https://github.com/joeknock90/Single-GPU-Passthrough/blob/master/README.md#vm-stop-script]
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 5
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
# Reload the Display Manager to access X
systemctl start lightdm
sleep 5
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
}
extract_vbios()
{
echo 1 > /sys/bus/pci/devices/$videobusid/rom
cat /sys/bus/pci/devices/$videobusid/rom > $VBIOS
echo 0 > /sys/bus/pci/devices/$videobusid/rom
}
_start
extract_vbios
_stop
exit
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
## Check if the script was executed as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
# Variables
VBIOS=/root/vBIOS.rom
NVFLASH=/root/nvflash_linux
videobusid="0000:06:00.0"
# END Variables
## Load the config file
source "${BASH_SOURCE%/*}/config"
_start()
{
# Memory lock limit
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
## Kill the Display Manager
systemctl stop lightdm
# Kill the console to free the GPU
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 0 > /sys/class/vtconsole/vtcon1/bind
sleep 1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
sleep 1
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
sleep 1
modprobe -r nvidia_modeset
sleep 1
modprobe -r nvidia
sleep 1
modprobe -r snd_hda_intel
sleep 2
}
_stop()
{
# Reload the kernel modules. This loads the drivers for the GPU
## Extract the VBIOS
$NVFLASH --save $VBIOS_EXTRACT_PATH
# Reload the kernel modules
modprobe snd_hda_intel
sleep 5
modprobe nvidia_drm
sleep 2
modprobe nvidia_modeset
sleep 2
modprobe nvidia
sleep 5
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
# [Source] [https://github.com/joeknock90/Single-GPU-Passthrough/blob/master/README.md#vm-stop-script]
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 5
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
# Reload the Display Manager to access X
systemctl start lightdm
sleep 5
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
}
extract_vbios()
{
$NVFLASH --save $VBIOS
}
_start
extract_vbios
_stop
exit
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
source config.sh
_start()
{
# Memory lock limit
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != 12884900 ]; then
ulimit -l 12884900
fi
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
# Kill the console to free the GPU
echo 0 > /sys/class/vtconsole/vtcon0/bind
## Check if the script was executed as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Load the config file
source "${BASH_SOURCE%/*}/config"
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
sleep 1
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
sleep 1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
sleep 1
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
sleep 1
modprobe -r nvidia_modeset
sleep 1
modprobe -r nvidia
sleep 1
modprobe -r snd_hda_intel
sleep 2
# Load the kernel module
modprobe vfio
sleep 1
modprobe vfio_iommu_type1
sleep 1
modprobe vfio-pci
sleep 1
# Detach the GPU from drivers and attach to vfio. Also the usb.
## Detach the GPU
echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind
sleep 1
echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $videoid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind
sleep 1
echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
sleep 1
echo $usbbusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/remove_id
#ls -la /sys/bus/pci/devices/$usbbusid/
sleep 1
}
_stop()
{
# Wait for QEMU to finish before continue
# QEMU (VM) command
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $MACOS_RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
-machine pc-q35-2.11 \
-smp $MACOS_CORES,sockets=1,cores=$(( $MACOS_CORES / 2 )),threads=2 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev user,id=net0 \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$MACOS_OVMF \
-drive if=pflash,format=raw,file=$MACOS_OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$MACOS_CLOVER \
-device ide-drive,bus=ide.0,drive=ISO \
-drive id=ISO,if=none,snapshot=on,media=cdrom,file=$MACOS_ISO \
-device ide-drive,bus=ide.1,drive=HDD \
-drive id=HDD,file=$MACOS_IMG,media=disk,format=qcow2,if=none >> $LOG 2>&1 &
# Wait for QEMU
wait
sleep 5
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
## Unload vfio
modprobe -r vfio-pci
sleep 2
modprobe -r vfio_iommu_type1
sleep 2
modprobe -r vfio
sleep 2
# Reload the kernel modules. This loads the drivers for the GPU
# Reload the kernel modules
modprobe snd_hda_intel
sleep 5
modprobe nvidia_drm
sleep 2
modprobe nvidia_modeset
sleep 2
modprobe nvidia
sleep 5
# Bind the usb
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/new_id
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind
sleep 10
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/remove_id
#ls -la /sys/bus/pci/devices/$usbbusid/
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
# [Source] [https://github.com/joeknock90/Single-GPU-Passthrough/blob/master/README.md#vm-stop-script]
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 5
#echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
#sleep 1
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
# Reload the Display Manager to access X
systemctl start lightdm
sleep 5
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
# Restore ulimit
ulimit -l $ULIMIT
}
_help()
{
echo "Usage: test-qemu.sh [OPTIONS]"
echo " start"
echo " stop"
}
_do()
{
if [ "$1" = "start" ]; then
_start
exit
elif [ "$1" = "stop" ]; then
_stop
exit
else
_help
exit 1
fi
}
if [[ $1 ]]; then
_do $1
else
_help
exit 1
fi
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
source config-macos
# Memory lock limit
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != 12884900 ]; then
ulimit -l 12884900
fi
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
# Kill the console to free the GPU
echo 0 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 0 > /sys/class/vtconsole/vtcon1/bind
sleep 1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
sleep 1
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
sleep 1
modprobe -r nvidia_modeset
sleep 1
modprobe -r nvidia
sleep 1
modprobe -r snd_hda_intel
sleep 2
# Load the kernel module
modprobe vfio
sleep 1
modprobe vfio_iommu_type1
sleep 1
modprobe vfio-pci
sleep 1
# Detach the GPU from drivers and attach to vfio. Also the usb.
echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind
sleep 1
echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $videoid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind
sleep 1
echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
sleep 1
echo $usbbusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
# QEMU (VM) command
qemu-system-x86_64 -runas $USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MY_OPTIONS\
-machine pc-q35-2.9 \
-smp $CORES,cores=$CORES \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-device vfio-pci,host=$IOMMU_USB \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev user,id=net0 \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$OVMF \
-drive if=pflash,format=raw,file=$OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$CLOVER \
-device ide-drive,bus=ide.1,drive=MacHDD \
-drive id=MacHDD,if=none,file=$IMG,format=raw \
-device ide-drive,bus=ide.0,drive=MacDVD \
-drive id=MacDVD,if=none,snapshot=on,media=cdrom,file=$ISO > /dev/null 2>&1 &
# END QEMU (VM) command
# Wait for QEMU to finish before continue
wait
sleep 5
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
modprobe -r vfio-pci
sleep 2
modprobe -r vfio_iommu_type1
sleep 2
modprobe -r vfio
sleep 2
# Reload the kernel modules. This loads the drivers for the GPU
modprobe snd_hda_intel
sleep 5
modprobe nvidia_drm
sleep 2
modprobe nvidia_modeset
sleep 2
modprobe nvidia
sleep 5
# Bind the usb
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind
sleep 10
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 5
# Reload the Display Manager to access X
systemctl start lightdm
sleep 5
# Restore the Frame Buffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
# Restore ulimit
ulimit -l $ULIMIT
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
source config-macos
# Memory lock limit
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != 12884900 ]; then
ulimit -l 12884900
fi
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
# Kill the console to free the GPU
echo 0 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 0 > /sys/class/vtconsole/vtcon1/bind
sleep 1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
sleep 1
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
sleep 1
modprobe -r nvidia_modeset
sleep 1
modprobe -r nvidia
sleep 1
modprobe -r snd_hda_intel
sleep 2
# Load the kernel module
modprobe vfio
sleep 1
modprobe vfio_iommu_type1
sleep 1
modprobe vfio-pci
sleep 1
# Detach the GPU from drivers and attach to vfio. Also the usb.
echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind
sleep 1
echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $videoid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind
sleep 1
echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/new_id
sleep 1
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
sleep 1
echo $usbbusid > /sys/bus/pci/drivers/vfio-pci/bind
sleep 1
echo $usbid > /sys/bus/pci/drivers/vfio-pci/remove_id
sleep 1
qemu-system-x86_64 -runas $USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MY_OPTIONS\
-machine pc-q35-2.9 \
-smp $CORES,cores=$CORES \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-device vfio-pci,host=$IOMMU_USB \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev user,id=net0 \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$OVMF \
-drive if=pflash,format=raw,file=$OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$CLOVER \
-device ide-drive,bus=ide.1,drive=MacHDD \
-drive id=MacHDD,if=none,file=$IMG,format=raw > /dev/null 2>&1 &
# END QEMU (VM) command
# Wait for QEMU to finish before continue
wait
sleep 5
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
modprobe -r vfio-pci
sleep 2
modprobe -r vfio_iommu_type1
sleep 2
modprobe -r vfio
sleep 2
# Reload the kernel modules. This loads the drivers for the GPU
modprobe snd_hda_intel
sleep 5
modprobe nvidia_drm
sleep 2
modprobe nvidia_modeset
sleep 2
modprobe nvidia
sleep 5
# Bind the usb
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind
sleep 10
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 5
# Reload the Display Manager to access X
systemctl start lightdm
sleep 5
# Restore the Frame Buffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
# Restore ulimit
ulimit -l $ULIMIT
#!/bin/bash
## Check if the script is being run as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Tap interface
tap_interface(){
tap_start(){
if [[ ! $(ip tuntap list | grep $1) ]]; then
ip tuntap add mode tap user $VM_USER name $TAP_INTERFACE
ip addr add dev $1 $TAP_IP
ip link set dev $1 up
fi
}
tap_stop(){
if [[ $(ip tuntap list | grep $1) ]]; then
ip link set dev $1 down
ip tuntap del mode tap name $TAP_INTERFACE
fi
}
if [ $1 == start ]; then tap_start $TAP_INTERFACE; elif [ $1 == stop ]; then tap_stop $TAP_INTERFACE; fi
}
## DHCP Server (DNSmasq)
dhcp_server(){
dhcp_start(){
[[ -f /var/run/dnsmasq.pid ]] || dnsmasq --conf-file=$DNSMASQ_CONF
}
dhcp_stop(){
[[ -f /var/run/dnsmasq.pid ]] && sudo kill -15 $(cat /var/run/dnsmasq.pid) && sudo rm /var/run/dnsmasq.pid
}
if [ $1 == start ]; then dhcp_start; elif [ $1 == stop ]; then dhcp_stop; fi
}
## Samba server
samba_server(){
samba_start(){
[[ -f /var/run/smbd.pid ]] || sudo smbd --configfile=$SMB_CONF
}
samba_stop(){
[[ -f /var/run/smbd.pid ]] && sudo kill -15 $(cat /var/run/smbd.pid)
}
if [ $1 == start ]; then samba_start; elif [ $1 == stop ]; then samba_stop; fi
}
## Load the config file
source "${BASH_SOURCE%/*}/config"
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
sleep 1
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
## Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
modprobe -r snd_hda_intel
## Load vfio
modprobe vfio-pci
## Detach the GPU
echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id
echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind
echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind
echo $videoid > /sys/bus/pci/drivers/vfio-pci/remove_id
echo $audioid > /sys/bus/pci/drivers/vfio-pci/new_id
echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind
echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind
echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id
## Start the network
tap_interface start
dhcp_server start
samba_server start
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $MACOS_RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
-machine pc-q35-2.11 \
-smp $MACOS_CORES,sockets=1,cores=$(( $MACOS_CORES / 2 )),threads=2 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$MACOS_OVMF \
-drive if=pflash,format=raw,file=$MACOS_OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$MACOS_CLOVER \
-device ide-drive,bus=ide.0,drive=ISO \
-drive id=ISO,if=none,snapshot=on,media=cdrom,file=$MACOS_ISO \
-device ide-drive,bus=ide.1,drive=HDD \
-drive id=HDD,file=$MACOS_IMG,media=disk,format=qcow2,if=none >> $LOG 2>&1 &
## Wait for QEMU
wait
## Stop the network
tap_interface stop
dhcp_server stop
samba_server stop
## Unload vfio module
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Load the kernel modules
modprobe snd_hda_intel
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
# Reload the Display Manager to access X
systemctl start lightdm
## Restore ulimit
ulimit -l $ULIMIT
#!/bin/bash
## Check if the script was executed as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Load the config file
source "${BASH_SOURCE%/*}/config"
## Check libvirtd
[[ $(systemctl status libvirtd | grep running) ]] || systemctl start libvirtd && sleep 1 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
sleep 1
## Kill the console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
## Detach the GPU
virsh nodedev-detach $VIRSH_GPU > /dev/null 2>&1
virsh nodedev-detach $VIRSH_GPU_AUDIO > /dev/null 2>&1
## Load vfio
modprobe vfio-pci
## QEMU (VM) command
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $MACOS_RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
-machine pc-q35-2.11 \
-smp $MACOS_CORES,sockets=1,cores=$(( $MACOS_CORES / 2 )),threads=2 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev user,id=net0 \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$MACOS_OVMF \
-drive if=pflash,format=raw,file=$MACOS_OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$MACOS_CLOVER \
-device ide-drive,bus=ide.0,drive=ISO \
-drive id=ISO,if=none,snapshot=on,media=cdrom,file=$MACOS_ISO \
-device ide-drive,bus=ide.1,drive=HDD \
-drive id=HDD,file=$MACOS_IMG,media=disk,format=qcow2,if=none >> $LOG 2>&1 &
## Wait for QEMU
wait
## Unload vfio
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Reattach the GPU
virsh nodedev-reattach $VIRSH_GPU_AUDIO > /dev/null 2>&1
virsh nodedev-reattach $VIRSH_GPU > /dev/null 2>&1
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
## Reload the Display Manager
systemctl start lightdm
## If libvirtd was stopped then stop it
[[ $LIBVIRTD == "STOPPED" ]] && systemctl stop libvirtd
## Restore ulimit
ulimit -l $ULIMIT
#!/bin/bash
## Check if the script is being run as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Tap interface
tap_interface(){
tap_start(){
if [[ ! $(ip tuntap list | grep $1) ]]; then
ip tuntap add mode tap user $VM_USER name $TAP_INTERFACE
ip addr add dev $1 $TAP_IP
ip link set dev $1 up
fi
}
tap_stop(){
if [[ $(ip tuntap list | grep $1) ]]; then
ip link set dev $1 down
ip tuntap del mode tap name $TAP_INTERFACE
fi
}
if [ $1 == start ]; then tap_start $TAP_INTERFACE; elif [ $1 == stop ]; then tap_stop $TAP_INTERFACE; fi
}
## DHCP Server (DNSmasq)
dhcp_server(){
dhcp_start(){
[[ -f /var/run/dnsmasq.pid ]] || dnsmasq --conf-file=$DNSMASQ_CONF
}
dhcp_stop(){
[[ -f /var/run/dnsmasq.pid ]] && sudo kill -15 $(cat /var/run/dnsmasq.pid) && sudo rm /var/run/dnsmasq.pid
}
if [ $1 == start ]; then dhcp_start; elif [ $1 == stop ]; then dhcp_stop; fi
}
## Samba server
samba_server(){
samba_start(){
[[ -f /var/run/smbd.pid ]] || sudo smbd --configfile=$SMB_CONF
}
samba_stop(){
[[ -f /var/run/smbd.pid ]] && sudo kill -15 $(cat /var/run/smbd.pid)
}
if [ $1 == start ]; then samba_start; elif [ $1 == stop ]; then samba_stop; fi
}
## Load the config file
source "${BASH_SOURCE%/*}/config"
## Check libvirtd
[[ $(systemctl status libvirtd | grep running) ]] || systemctl start libvirtd && sleep 1 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
sleep 1
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
## Detach the GPU
virsh nodedev-detach $VIRSH_GPU > /dev/null 2>&1
virsh nodedev-detach $VIRSH_GPU_AUDIO > /dev/null 2>&1
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
## Start the network
tap_interface start
dhcp_server start
samba_server start
## QEMU (VM) command
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-m $MACOS_RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
-machine pc-q35-2.11 \
-smp $MACOS_CORES,sockets=1,cores=$(( $MACOS_CORES / 2 )),threads=2 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
-drive if=pflash,format=raw,readonly,file=$MACOS_OVMF \
-drive if=pflash,format=raw,file=$MACOS_OVMF_VARS \
-smbios type=2 \
-device ide-drive,bus=ide.2,drive=Clover \
-drive id=Clover,if=none,snapshot=on,format=qcow2,file=$MACOS_CLOVER \
-device ide-drive,bus=ide.0,drive=ISO \
-drive id=ISO,if=none,snapshot=on,media=cdrom,file=$MACOS_ISO \
-device ide-drive,bus=ide.1,drive=HDD \
-drive id=HDD,file=$MACOS_IMG,media=disk,format=qcow2,if=none >> $LOG 2>&1 &
## Wait for QEMU
wait
## Stop the network
tap_interface stop
dhcp_server stop
samba_server stop
## Unload vfio module
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Reattach the GPU
virsh nodedev-reattach $VIRSH_GPU_AUDIO > /dev/null 2>&1
virsh nodedev-reattach $VIRSH_GPU > /dev/null 2>&1
## Reload the framebuffer and console
echo 1 > /sys/class/vtconsole/vtcon0/bind
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
## Reload the Display Manager
systemctl start lightdm
## If libvirtd was stopped then stop it
[[ $LIBVIRTD == "STOPPED" ]] && systemctl stop libvirtd
## Restore ulimit
ulimit -l $ULIMIT
#!/bin/bash
# Requires iproute2, dnsmasq, and firewall [iptables, shorewall, ...]
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
# Variables
NIC=qemubr0
IP=10.10.10.1/24
start_network()
{
ip link add name $NIC type bridge
ip link set $NIC up
ip address add $IP dev $NIC
}
stop_network()
{
ip link set $NIC down
ip link delete $NIC type bridge
}
start_dnsmasq()
{
systemctl start dnsmasq
}
stop_dnsmasq()
{
systemctl stop dnsmasq
}
_help()
{
echo "Usage: network.sh [OPTIONS]"
echo " start - start the network"
echo " stop - stop the network"
}
_start()
{
if [ "$1" = "start" ]; then
start_network
start_dnsmasq
exit
elif [ "$1" = "stop" ]; then
stop_dnsmasq
stop_network
exit
else
_help
exit 1
fi
}
if [[ $1 ]]; then
_start $1
else
_help
exit 1
fi
[Unit]
Description=QEMU virtual machine (%i)
[Service]
#Type=forking
#PIDFile=/run/qemu_%i.pid
EnvironmentFile=/home/yu/scripts/config-macos
ExecStart=/home/yu/scripts/%i.sh
#TimeoutStopSec=1m
[Install]
WantedBy=multi-user.target
[Unit]
Description=QEMU virtual machine (%i)
[Service]
#Type=forking
#PIDFile=/run/qemu_%i.pid
EnvironmentFile=/home/yu/scripts/config
ExecStart=/home/yu/scripts/%i.sh
#TimeoutStopSec=1m
[Install]
WantedBy=multi-user.target
## Samba config for VMs
## Source https://git.samba.org/samba.git/?p=samba.git;a=blob_plain;f=examples/smb.conf.default;hb=HEAD
## You must run # smbpasswd -a <USER>
[global]
# Group of windows
workgroup = WORKGROUP
# The shared name of the server
server string = Samba Server
# The role of the server
server role = standalone server
# Allowed IPs [10.10.10. means 10.10.10.0/24]
hosts allow = 10.10.10.
# Location of the logs
log file = /var/log/samba/%m.log
# In kb
max log size = 50
# Interface for samba to use
interfaces = 10.10.10.1/24
# Do not resolve names via dns
dns proxy = no
#============================ Share Definitions ==============================
[shared]
comment = Games
path = /path/to/shared/folder/
guest ok = yes
browseable = yes
writable = yes
public = yes
create mask = 0765
#!/bin/bash
# Check if the script is executed as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit 1
fi
# END Check if you are sudo
source config
# Memory lock limit
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != $(( $(echo $RAM | tr -d 'G')*1048576+10 )) ]; then
ulimit -l $(( $(echo $RAM | tr -d 'G')*1048576+10 ))
fi
## Kill X and related
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
sleep 2
# Kill the console to free the GPU
## Check if the script was executed as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Load the config file
source "${BASH_SOURCE%/*}/config"
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
sleep 1
## Remove the framebuffer and console
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
# Unload the Kernel Modules that use the GPU
## Unload the nvidia drivers
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
modprobe -r snd_hda_intel
# Load the kernel module
modprobe vfio
modprobe vfio_iommu_type1
## Load vfio
modprobe vfio-pci
# Detach the GPU from drivers and attach to vfio. Also the usb.
## Detach the GPU
echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id
echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind
echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind
......@@ -46,70 +38,48 @@ echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind
echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind
echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id
echo $usbid > /sys/bus/pci/drivers/vfio-pci/new_id
echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/vfio-pci/bind
echo $usbid > /sys/bus/pci/drivers/vfio-pci/remove_id
# QEMU (VM) command
qemu-system-x86_64 -runas $USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-enable-kvm \
-m $RAM \
-cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv_vapic,hv_vendor_id=0xDEADBEEFFF \
-rtc clock=host,base=localtime \
-smp $CORES,sockets=1,cores=$CORES,threads=0 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-device vfio-pci,host=$IOMMU_USB \
-device virtio-net-pci,netdev=n1 \
-netdev user,id=n1 \
-drive if=pflash,format=raw,readonly,file=$OVMF \
-drive media=cdrom,file=$ISO,id=virtiocd1,if=none \
-device ide-cd,bus=ide.1,drive=virtiocd1 \
-drive media=cdrom,file=$VIRTIO,id=virtiocd2,if=none \
-device ide-cd,bus=ide.1,drive=virtiocd2 \
-device virtio-scsi-pci,id=scsi0 \
-device scsi-hd,bus=scsi0.0,drive=rootfs \
-drive id=rootfs,file=$HDD,media=disk,format=raw,if=none > /dev/null 2>&1 &
# END QEMU (VM) command
# Wait for QEMU to finish before continue
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-enable-kvm \
-m $RAM \
-cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv_vapic,hv_vendor_id=0xDEADBEEFFF \
-rtc clock=host,base=localtime \
-smp $CORES,sockets=1,cores=$(( $CORES / 2 )),threads=2 \
-device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
-device vfio-pci,host=$IOMMU_GPU_AUDIO \
-device virtio-net-pci,netdev=n1 \
-netdev user,id=n1 \
-drive if=pflash,format=raw,readonly,file=$OVMF \
-drive media=cdrom,file=$WINDOWS_ISO,id=cd1,if=none \
-device ide-cd,bus=ide.1,drive=cd1 \
-drive media=cdrom,file=$VIRTIO,id=cd2,if=none \
-device ide-cd,bus=ide.1,drive=cd2 \
-device virtio-scsi-pci,id=scsi0 \
-device scsi-hd,bus=scsi0.0,drive=rootfs \
-drive id=rootfs,file=$WINDOWS_IMG,media=disk,format=qcow2,if=none >> $LOG 2>&1 &
# Wait for QEMU
wait
sleep 1
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
## Unload vfio
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio