Massive update, rewrite most of it. Check the changelog for more info.

testing
yurialek 5 years ago
parent e459a498b3
commit 652f3aa6d1

@ -0,0 +1,51 @@
# 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).
---
## 2018/12/30
### Added
- [Wiki](https://gitlab.com/YuriAlek/vfio/wikis/home)
- [Edit config and script](https://gitlab.com/YuriAlek/vfio/wikis/Edit-config-and-script)
- [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)
- [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]()
- Install.md [Moved to wiki]()
---

@ -1,49 +0,0 @@
# 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/)

@ -1,213 +1,60 @@
# Single GPU passthrough with QEMU and VFIO
<!-- AKA Passthrough VGA on first slot -->
![Windows 10 1709](/Screenshots/Windows 10 QEMU single GPU info.png)*Windows 10 1709*
## Table of Contents
1. [What this does](#what-this-does)
2. [What you need](#what-you-need)
3. [Submit your own configuration](#submit-your-own-configuration)
4. [My system](#my-system)
5. [vBIOS](#vbios)
6. [Configure](#configure)
7. [Known problems](#known-problems)
8. [TODO](#todo)
# Single GPU passthrough
## Last Update
2018/10/20
## What this does
In one command it kills X, frees the GPU from drivers and console, detaches the GPU from the host, starts the VM with the GPU, waits until the VM is off, reattaches the GPU to the host and starts lightdm.
## What you need
* An IOMMU enabled motherboard. Check your motherboard manual for an option in BIOS to enable IOMMU.
* CPU support for AMD-v/VT-x and AMD-Vi/VT-d (AMD/Intel).
* Virtualization support enabled on BIOS. Check your motherboard manual if you need help.
* One GPU that supports UEFI and its BIOS. All GPUs from 2012 and later should support this, some may have issues. If the GPU does not support UEFI you may be able to make it work, but you won't see anything in the screen until the drivers inside Windows kick in.
* QEMU, OVMF UEFI and VIRTIO drivers for Windows. [If you need to install, refer to the Install guide](Install.md)
* [Optional] An hard drive only for Windows.
* [Recommended] Another computer to login remotely with `ssh` for convenience, at least until you have everything working.
## Submit your own configuration
[Create a pull merge](https://gitlab.com/YuriAlek/vfio/merge_requests/new) with [a file explaining how you got it working](Hardware configurations/README.md).
## My system
```
[Hardware]
CPU: AMD Ryzen 5 2600
Motherboard: Gigabyte AB350M-Gaming 3 rev1.1
Motherboard BIOS: F23d
RAM: 16GB
GPU: Gigabyte Nvidia GeForce GTX 770
GPU model: GV-N770OC-2GD
GPU BIOS: 80.04.C3.00.0F
GPU codename: GK104
[Software]
Linux Distro: ArchLinux
Linux Kernel: 4.18.14
Nvidia divers: 396.51-1
QEMU version: 3.0.0-3
OVMF version: r24601
[Guests]
Windows 10 Pro 1709 x64
MacOS High Sierra 10.13.3
```
## vBIOS
I experienced some weird things when doing this on the display, like a corruption of the image, it may be my GPU. If you encounter anything, a reboot solved my problems.
### Method 1 - Linux
It did not work for me, the ROM is 59KiB and it should be around 162KiB. It may work for you.
1. Execute `scripts/iommu.sh` to get the BUS ID for the GPU. Looks like `0000:06:00.0`.
2. Edit `scripts/extract-vbios-linux.sh` to your convenience. Change `videobusid=`. [Optional] Change also the location where the vBIOS will be save `VBIOS=`.
3. Make the script executable with `chmod +x scripts/extract-vbios-linux.sh`.
4. Link the service to systemd: `ln -s scripts/qemu@.service /usr/lib/systemd/system/`.
5. Execute the systemd unit with `sudo systemctl start qemu@extract-vbios-linux.service`. You can also do it over `ssh`. The screen will turn dark for a while. The extracted ROM will be in the root directory `/root/vBIOS.rom`
2018/12/30
From here you are alone, I don't know how to proceed. Maybe you need to edit, maybe don't.
### Method 2 - nvflash in Linux
1. Download nvflash https://www.techpowerup.com/download/nvidia-nvflash/. Do not install from AUR; the package it's broken.
2. Unzip it as `/root/nvflash_linux` with `# unzip nvflash_5.414.0_linux.zip -d /root/`.
3. Execute `scripts/iommu.sh` to get the BUS ID for the GPU. Looks like `0000:06:00.0`.
4. Edit `scripts/extract-vbios-nvflash.sh`. Change the variables `videobusid` with your GPU BUS ID; `NVFLASH` if you changed the location of the executable; and `VBIOS` if you want the ROM in other path.
5. Link the service to systemd: `ln -s scripts/qemu@.service /usr/lib/systemd/system/`.
6. Execute the systemd unit with `sudo systemctl start qemu@extract-vbios-nvflash.service`. You can also do it over `ssh`. The screen will turn dark for a while. The extracted ROM will be in the root directory `/root/vBIOS.rom`
7. [Edit the vBIOS](#edit-the-vbios)
### Method 3 - Windows
[Source](https://www.youtube.com/watch?v=1IP-h9IKof0). [You can download the bios from techpowerup.com](https://www.techpowerup.com/vgabios/); if you do so, [skip to edit the vBIOS](#edit-the-vbios).
1. [Download and install GPU-Z](https://www.techpowerup.com/gpuz/).
2. [Open GPU-Z and backup the GPU BIOS](/Screenshots/GPU-Z vBIOS.png). Right next to the `Bios Version`; in my case `80.04.C3.00.0F`, there is an icon for backup. A file named `GK104.rom` will be created [Your file name may vary].
3. [Edit the vBIOS](#edit-the-vbios)
### Edit the VBIOS
1. Open the vBIOS ROM (`vBIOS.rom`) in the HEX editor.
2. [After a bunch of `00`, there is a `55`, or `U` in HEX; delete everything before the `55`](/Screenshots/Hex vBIOS.png), and save. I strongly recommend not to overwrite the original ROM. [Note that series of `FF` values have been reported on a GTX 1060](/Screenshots/Hex vBIOS 1060.png)
## Configure
1. Clone this repository
```bash
$ git clone https://gitlab.com/YuriAlek/vfio.git
```
2. [Optional] [Download virtio drivers](https://pve.proxmox.com/wiki/Windows_VirtIO_Drivers). If you do not, modify `scripts/windows.sh`.
```
$ wget -o virtio-win.iso "https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso"
```
## Table of Contents
1. [Start here](#start-here)
1. [What is this](#what-this-is)
1. [Wiki](#wiki)
1. [Branches](#branches)
1. [Known problems](#known-problems)
1. [TODO](#todo)
4. Get the iommu groups needed for the VM (GPU, GPU audio and USB controller). They look like `06:00.0`
```
$ chmod +x scripts/iommu.sh
$ scripts/iommu.sh
-------------------------
# GPU
IOMMU group 13
06:00.0 VGA compatible controller [0300]: NVIDIA Corporation GK104 [GeForce GTX 770] [10de:1184] (rev a1)
06:00.1 Audio device [0403]: NVIDIA Corporation GK104 HDMI Audio Controller [10de:0e0a] (rev a1)
# USB 3.0 Controller
IOMMU group 16
07:00.3 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD] USB 3.0 Host controller [1022:145f]
# SATA Controller
IOMMU group 18
08:00.2 SATA controller [0106]: Advanced Micro Devices, Inc. [AMD] FCH SATA Controller [AHCI mode] [1022:7901] (rev 51)
```
## Start here
- [How to use the script for Windows](https://gitlab.com/YuriAlek/vfio/wikis/Use/#windows).
- [How to use the script for MacOs](https://gitlab.com/YuriAlek/vfio/wikis/Use/#macos).
- [How to use the script for Linux](https://gitlab.com/YuriAlek/vfio/wikis/Use/#linux).
5. [Optional] Create the image for the VM. Only if not using a physical hard drive. You can edit the path, the size and the format. Check `man qemu-img` for more information.
```bash
$ qemu-img create -f raw /path/to/image/windows.raw 60G
```
## What this is
A series of scripts that allows you to do GPU passthrough with only one GPU in the system by detaching the GPU from the host and passing it to the guest, usually a Windows VM.
6. Edit the config in `scripts/config.sh` to convenience. If you use systemd to start the VM you have to edit `EnvironmentFile` in `qemu@.service` to point to your config file. Variables you may have to edit:
1. PCI devices. `IOMMU_GPU`; `IOMMU_USB`.
2. User.
3. Location of HDD/IMG, ISO, vBIOS and OVMF image.
4. The Desktop Environment, Display Manager, Window Manager, etc. `lightdm`/`i3`.
5. QEMU options like RAM and CPU cores.
6. Kernel modules.
7. Other things like add a command to kill PulseAudio `pulseaudio -k` and another, at the end of the script, to start it again `pulseaudio --start`.
8. The network options `-device virtio-net-pci,netdev=n1 -netdev user,id=n1`.
9. Swap virtio for sata as the HDD interface.
However, you may not use this method as it is tedious and having two GPUs allows you to do things like dual monitors (one running Linux and the other running Windows) or [LookingGlass][8].
7. Start the VM
```
# scripts/windows-install.sh
```
## Wiki
[Check the wiki for more information and guides on how to make it work](https://gitlab.com/YuriAlek/vfio/wikis/Home).
8. When installing Windows, in the section `Where do you want to install Windows?` there will be no hard drives to install to; to fix it:
1. Load driver
2. Browse
3. CD Drive (E:) virtio-win-0.1.1
4. vioscsi
5. w10
6. amd64
7. ok
8. Load driver `Red Hat VirtIO SCSI pass-through controller (E:\vioscsi\w10\amd64\vioscsi.inf)`
9. Next
10. Select the `Unallocated Space`
11. Proceed as normal.
12. Let Windows find the drivers for the GPU (if Windows has network) or [download the updated ones from NVIDIA](https://www.nvidia.com/Download/index.aspx?lang=en-us).
## Branches
I keep more than one branch for multiple purposes. [There is a personal branch][1] where I have my actual configuration; [a testing branch][2] where I push changes while I am testing new things, it is **NOT stable**; and [a testing-auto branch][3] where I **try** to make everything work automatically.
9. Once installed Windows, run the VM with:
```
# scripts/windows.sh
```
## Known problems
Audio is not supported, yet. I use an external USB DAC.
### For the sake of convenience
```
# ln -s scripts/qemu@.service /usr/lib/systemd/system/
# ln -s scripts/qemu-mac@.service /usr/lib/systemd/system/
```
```bash
$ alias windows="sudo systemctl start qemu@windows.service"
$ alias macos="sudo systemctl start qemu-mac@macos-hs.service"
```
To start the Windows VM
```
$ windows
```
To start the MacOS VM
```
$ macos
```
Performance is not important to me at this moment. There is a lot of things that you can do to get better performance.
### For MacOS
[Check this amazing guide for creating the MacOS install image and Clover](https://github.com/kholia/OSX-KVM).
MacOS High Sierra does not like USB hubs, therefore anything connected to a hub won't work. Mojave works fine with hubs.
## Known problems
### MacOS does not like USB hubs, therefore anything connected to a hub will be ignored.
### Windows version
Windows 10 Pro 1709 works for me, but 1803 does not (may be the UEFI). [I have heard that the 1803 version comes with a Spectre patch and the performance is bad](https://www.reddit.com/r/VFIO/comments/97unx4/passmark_lousy_2d_graphics_performance_on_windows/). The Spectre patch can be disabled.
Windows 10 Pro 1709 works for me, but 1803 does not (may be the UEFI). [I have read that the 1803 version comes with a Spectre patch and the performance is bad][4]. The Spectre patch can be disabled.
## TODO
- [x] Unbind GPU without `virsh`.
- [x] Run QEMU as user.
- [x] Try if is necessary to edit `/etc/mkinitcpio.conf`. No need to load the kernel modules at boot.
- [x] Extract the vBIOS in Linux.
- [x] Install guide.
- [x] Fix the race condition.
- [ ] Network. [I am too lazy, check this](https://yurialek.gitlab.io/gitbook/docs/qemu/network.html). I will add it to the scripts at some point.
- [ ] Update macos script.
- [ ] IOMMU guide.
- [ ] Audio.
- [ ] Troubleshooting guide.
- [ ] How to edit the `windows.sh` script.
- [ ] Create scripts for install and use (Without DVD images).
- [ ] Improve the script with multiple options for HDD/IMG; network; PCI devices, etc.
- [ ] ACS Patch (Does not work for me).
- [ ] CPU pinning and RAM HugePages.
- [ ] Not kill X, [`shifter` & `xpra` may be the solution](https://www.linuxquestions.org/questions/linux-desktop-74/move-application-between-desktops-736982/#post4161705). Also [uswsusp (userspace software suspend)](https://wiki.archlinux.org/index.php/Uswsusp). [Source](https://www.reddit.com/r/linux_gaming/comments/98376e/i_am_creating_a_guide_for_gpu_passthrough_with/e4ebaoj/)
<!--
And you must supply QEMU with the Full GPU's ROM extracted extracted using a tool called "nvagetbios" , which you can find in a package called "envytools"
-->
- [ ] CPU pinning.
- [ ] Performance tunning for QEMU.
- [ ] [Automation][3]
- [ ] Don't kill X server, [shifter and xpra may be the solution][5]. Also [uswsusp (userspace software suspend)][6]. [Source][7]
<!-- Links -->
[1]: /tree/personal "Personal branch"
[2]: /tree/testing "Testing branch"
[3]: /tree/testing-auto "Testing-auto branch"
[4]: https://www.reddit.com/r/VFIO/comments/97unx4/passmark_lousy_2d_graphics_performance_on_windows/
[5]: https://www.linuxquestions.org/questions/linux-desktop-74/move-application-between-desktops-736982/#post4161705
[6]: https://wiki.archlinux.org/index.php/Uswsusp "uswsusp"
[7]: https://www.reddit.com/r/linux_gaming/comments/98376e/i_am_creating_a_guide_for_gpu_passthrough_with/e4ebaoj/
[8]: https://github.com/gnif/LookingGlass "LookingGlass"
[pull merge]: https://gitlab.com/YuriAlek/vfio/merge_requests/new "Create a pull merge"
[Xen Wiki]: https://wiki.xen.org/wiki/VTd_HowTo
[IOMMU Hardware]:https://en.wikipedia.org/wiki/List_of_IOMMU-supporting_hardware
[IOMMU Hardware]: https://en.wikipedia.org/wiki/List_of_IOMMU-supporting_hardware
[archwiki-url]: https://wiki.archlinux.org/index.php/Main_page
[qemu_archwiki]: https://wiki.archlinux.org/index.php/QEMU
[kvm-archwiki]: https://wiki.archlinux.org/index.php/KVM

Before

Width:  |  Height:  |  Size: 194 KiB

After

Width:  |  Height:  |  Size: 194 KiB

Before

Width:  |  Height:  |  Size: 5.9 MiB

After

Width:  |  Height:  |  Size: 5.9 MiB

Before

Width:  |  Height:  |  Size: 4.1 MiB

After

Width:  |  Height:  |  Size: 4.1 MiB

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

@ -1,43 +1,59 @@
# 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
VIRTIO=$IMAGES/virtio.iso
ISO=$IMAGES/win10.iso
HDD=/dev/sdc
## User
VM_USER=
## For extracting the VBIOS
VBIOS_EXTRACT_PATH=/root/VBIOS.rom
NVFLASH=/root/nvflash_linux
extract_videobusid="0000:"
## IOMMU groups
IOMMU_GPU=""
IOMMU_GPU_AUDIO=""
## 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/
## Windows and MacOS
VBIOS=$IMAGES/VBIOS.rom
OVMF=/usr/share/ovmf/x64/OVMF_CODE.fd
OVMF_VARS=/usr/share/ovmf/x64/OVMF_VARS.fd
# QEMU options
RAM=12G
CORES=12
## Windows
VIRTIO=$IMAGES/virtio.iso
WINDOWS_ISO=$IMAGES/win10.iso
WINDOWS_IMG=$IMAGES/windows.raw
# 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}')
## MacOS
MACOS_ISO=$IMAGES/mojave.iso
MACOS_IMG=$IMAGES/mojave.raw
MACOS_CLOVER=$IMAGES/clover.qcow2
MACOS_OPTIONS="+pcid,+ssse3,+sse4.2,+popcnt,+aes,+xsave,+avx,+xsaveopt,avx2,+smep,check"
## Network stuff
TAP_INTERFACE=tap0
DNSMASQ_CONF=dnsmasq.conf
SMB_CONF=smb.conf
## Ulimit
ULIMIT=$(ulimit -a | grep "max locked memory" | awk '{print $6}')
ULIMIT_TARGET=$(( $(echo $RAM | tr -d 'G')*1048576+100000 ))

@ -1,47 +0,0 @@
# User.
USER=yu
# Path to VBIOS, IMG, Mac OS 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
IMG_MOJAVE=/mnt/wd/vm/hdd/macos-mojave.raw
CLOVER=$IMAGES/Clover-1080.qcow2
CLOVER_MOJAVE=/mnt/wd/vm/macos/clover-mojave.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"
MOJAVE_OPTIONS="+pcid,+ssse3,+sse4.2,+popcnt,+aes,+xsave,+avx,+xsaveopt,avx2,+smep,check"
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}')
## Network stuff
TAP_INTERFACE=tap0
DNSMASQ_CONF=dnsmasq.conf
SMB_CONF=smb.conf

@ -1,79 +1,39 @@
#!/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

@ -1,78 +1,37 @@
#!/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

@ -0,0 +1,89 @@
#!/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"
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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 the kernel module
modprobe vfio
modprobe vfio_iommu_type1
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
# 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=$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=$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=raw,if=none >> $LOG 2>&1 &
# Wait for QEMU
wait
## Unload vfio
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
# Reload 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

@ -1,145 +0,0 @@
#!/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

@ -1,141 +0,0 @@
#!/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

@ -1,141 +1,74 @@
#!/bin/bash
## Edit this comand before using it
## Required software
## DNSmasq
## samba
## iproute2
## qemu
## ovmf
## Maybe I am missing something else
## For fucking Windows to be able to install stuff on a network drive
## https://community.spiceworks.com/topic/366976-trying-to-install-a-program-to-a-network-drive
## All you need to do is run Registry Editor (regedit.exe), locate the key
## HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System, and create a new DWORD entry with the name
## EnableLinkedConnections and value 1:
## Check if the script is being run as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Load the config file.
source config-macos
## Memory lock limit.
## By default, at least in Arch, a user can not lock so much memory, so you have to allow it plus 10 just to be sure.
## The `+100000` is just because it ay use a little shade over 12G and that is 100M
## If you get an error that QEMU can't allocate memory just do `ulimit -l unlimited` as root. Revert it after you shutdown the VM.
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != $(( $(echo $RAM | tr -d 'G')*1048576+100000 )) ]; then
ulimit -l $(( $(echo $RAM | tr -d 'G')*1048576+100000 ))
fi
## Kill X and related. You can change `lightdm` and `i3` for whatever you use.
## Make shure you don't have anything important open because this will kill the X session.
## You can also just do "killall xorg" or "killall xinit"
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
# killall xorg
# killall xinit
sleep 2
## START ## Network stuff
## THIS REQUIRES A FIREWALL BACKEND LIKE IPTABLES. If there is nothing routing the traffic this will be useless.
## Also open ports 53 tcp/udp 67,68 tcp/udp and icmp in ther firewall rules for the $TAP_INTERFACE
## Tap interface
tap_interface(){
tap_stop(){
if [[ $(ip tuntap list | grep $1) ]]
then
sudo ip link set dev $1 down
sudo ip tuntap del mode tap name tap0
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_start(){
if [[ ! $(ip tuntap list | grep $1) ]]
then
#sudo tunctl -u $VM_USER
sudo ip tuntap add mode tap user $USER name tap0
sudo ip addr add dev $1 10.10.10.1/24
sudo ip link set dev $1 up
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
if [ $1 == start ]; then tap_start $TAP_INTERFACE; elif [ $1 == stop ]; then tap_stop $TAP_INTERFACE; fi
}
## DHCP Server (DNSmasq)
dhcp_server(){
dhcp_stop(){
[[ -f /var/run/dnsmasq.pid ]] && sudo kill -15 $(cat /var/run/dnsmasq.pid) && sudo rm /var/run/dnsmasq.pid
}
dhcp_start(){
[[ -f /var/run/dnsmasq.pid ]] || dnsmasq --conf-file=$DNSMASQ_CONF
}
if [ $1 == start ]
then
dhcp_start
elif [ $1 == stop ]
then
dhcp_stop
fi
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 controls
## Samba server
samba_server(){
samba_stop(){
[[ -f /var/run/smbd.pid ]] && echo "Stopping samba" && sudo kill -15 $(cat /var/run/smbd.pid) || echo "Samba was already stopped"
}
samba_start(){
[[ -f /var/run/smbd.pid ]] && echo "Samba was already started" || echo "Starting samba" && sudo smbd --configfile=$SMB_CONF
[[ -f /var/run/smbd.pid ]] || sudo smbd --configfile=$SMB_CONF
}
if [ $1 == start ]
then
samba_start
elif [ $1 == stop ]
then
samba_stop
fi
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
}
## Start the network
tap_interface start
dhcp_server start
samba_server start
## For samba to work you must run as root `smbpasswd -a <USER>`
## Load the config file
source "${BASH_SOURCE%/*}/config"
## END ## Network stuff
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## Kill the console to free the GPU.
## The console, by default, is attached to the boot GPU, in this case there is only one and we need to left it unused to remove it from the system.
## 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
## You may need to unload more or other kernel modules (drivers) for your specific GPU.
## This command allows you to see which modules depend uppon others. Change nvidia for noveau if you are using the OSS drivers or for the AMD equivalent.
# lsmod | grep nvidia
## The left column are the modules and the right are the modules that are using that module. The modules should be unloaded only if there is no other modules using it; if there are, then unload them first.
# nvidia_drm 49152 7
# nvidia_modeset 1044480 23 nvidia_drm
# nvidia 16605184 1038 nvidia_modeset
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
## Also unload the audio modules, may be more than one, because they use the audio device in the GPU
modprobe -r snd_hda_intel
## Load the kernel module related to vfio
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
## Detach the GPU from drivers and attach to vfio. Also the usb.
## This may not be the right way, but it works for me and there is no symptoms of anything not working.
## 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
@ -146,44 +79,19 @@ 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 \
# -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 \
# -device virtio-net-pci,netdev=net0 \
# -netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no,vhost=on \
# -device #isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerI#nc" \
# -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 &
qemu-system-x86_64 -runas $USER -enable-kvm \
## 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 $RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MOJAVE_OPTIONS\
-m $MACOS_RAM \
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
-machine pc-q35-2.11 \
-smp $CORES,sockets=1,cores=$(($CORES/2)),threads=2 \
-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 \
-device vfio-pci,host=$IOMMU_USB \
-usb -device usb-kbd -device usb-tablet \
-device nec-usb-xhci,id=xhci \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no \
@ -193,60 +101,38 @@ qemu-system-x86_64 -runas $USER -enable-kvm \
-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_MOJAVE \
-device ide-drive,bus=ide.1,drive=MacHDD \
-drive id=MacHDD,if=none,file=$IMG_MOJAVE,format=raw,id=disk > /dev/null 2>&1 &
-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=raw,if=none >> $LOG 2>&1 &
## Network
## virtio-net-pci does not work, obviously
## e1000 does not work
## rtl8139 does not work
## e1000-82545em
## Wait for QEMU to finish before continuing
## Wait for QEMU
wait
sleep 1
## Stop the network
tap_interface stop
dhcp_server stop
samba_server stop
## Unload the vfio module. This leaves the GPU without drivers.
## Unload vfio module
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Reload the kernel modules that previously were unloaded. Load them inversed as you unloaded. The las unloaded should be the first to be loaded and viceversa. THIS MAY NOT BE THE CASE FOR YOU. This SHOULD load the drivers for the GPU.
## Load the kernel modules
modprobe snd_hda_intel
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia
## Bind the USB. If you passed the USB controller to the VM.
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/new_id
## This does not exist, so...
#echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/remove_id
#ls -la /sys/bus/pci/devices/$usbbusid/
## Rebind the EFI-Framebuffer and Rebind 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
## This just creates a file called `tee`
#echo 1 > tee /sys/class/vtconsole/vtcon1/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. You can also run startx, though I don't know how to do it and `startx` will probably not work.
# Reload the Display Manager to access X
systemctl start lightdm
sleep 2
## Restore the Frame Buffer.
## I tried MANY different ways and timings of doing this, and this is the only one that works, FOR ME.
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
## Restore ulimit to the previous state.
## Restore ulimit
ulimit -l $ULIMIT

@ -0,0 +1,80 @@
#!/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 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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
virsh nodedev-detach $VIRSH_GPU_AUDIO
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
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=$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=$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=raw,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
virsh nodedev-reattach $VIRSH_GPU
## Reload the framebuffer
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
echo 1 > /sys/class/vtconsole/vtcon0/bind
echo 1 > /sys/class/vtconsole/vtcon1/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

@ -0,0 +1,130 @@
#!/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 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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
virsh nodedev-detach $VIRSH_GPU_AUDIO
## 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=$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=$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=raw,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
virsh nodedev-reattach $VIRSH_GPU
## Reload the framebuffer and console
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
echo 1 > /sys/class/vtconsole/vtcon0/bind
echo 1 > /sys/class/vtconsole/vtcon1/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

@ -1,66 +0,0 @@
#!/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

@ -1,12 +0,0 @@
[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

@ -1,12 +0,0 @@
[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

@ -23,7 +23,7 @@
#============================ Share Definitions ==============================
[shared]
comment = Games
path = /mnt/wd/games/
path = /path/to/shared/folder/
guest ok = yes
browseable = yes
writable = yes

@ -1,159 +0,0 @@
#!/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
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
#ls -la /sys/bus/pci/devices/$usbbusid/
sleep 1
}
_stop()
{
# 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 $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]
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
# 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

@ -0,0 +1,86 @@
#!/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"
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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 the kernel module
modprobe vfio
modprobe vfio_iommu_type1
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
# QEMU (VM) command
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=raw,if=none >> $LOG 2>&1 &
# Wait for QEMU
wait
## Unload vfio
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
# Reload 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

@ -1,115 +0,0 @@
#!/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
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 the kernel module
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
# Detach the GPU from drivers and attach to vfio. Also the usb.
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
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
wait
sleep 1
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
# Reload the kernel modules. This loads the drivers for the GPU
modprobe snd_hda_intel
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia
# 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
#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]
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 1
# Reload the Display Manager to access X
systemctl start lightdm
sleep 2
# Restore the Frame Buffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
# Restore ulimit
ulimit -l $ULIMIT

@ -1,141 +1,74 @@
#!/bin/bash
## Edit this comand before using it
## Required software
## DNSmasq
## samba
## iproute2
## qemu
## ovmf
## Maybe I am missing something else
## For fucking Windows to be able to install stuff on a network drive
## https://community.spiceworks.com/topic/366976-trying-to-install-a-program-to-a-network-drive
## All you need to do is run Registry Editor (regedit.exe), locate the key
## HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System, and create a new DWORD entry with the name
## EnableLinkedConnections and value 1:
## Check if the script is being run as root
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
## Load the config file.
source config
## Memory lock limit.
## By default, at least in Arch, a user can not lock so much memory, so you have to allow it plus 10 just to be sure.
## The `+100000` is just because it ay use a little shade over 12G and that is 100M
## If you get an error that QEMU can't allocate memory just do `ulimit -l unlimited` as root. Revert it after you shutdown the VM.
if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != $(( $(echo $RAM | tr -d 'G')*1048576+100000 )) ]; then
ulimit -l $(( $(echo $RAM | tr -d 'G')*1048576+100000 ))
fi
## Kill X and related. You can change `lightdm` and `i3` for whatever you use.
## Make shure you don't have anything important open because this will kill the X session.
## You can also just do "killall xorg" or "killall xinit"
systemctl stop lightdm > /dev/null 2>&1
killall i3 > /dev/null 2>&1
# killall xorg
# killall xinit
sleep 2
## START ## Network stuff
## THIS REQUIRES A FIREWALL BACKEND LIKE IPTABLES. If there is nothing routing the traffic this will be useless.
## Also open ports 53 tcp/udp 67,68 tcp/udp and icmp in ther firewall rules for the $TAP_INTERFACE
## Tap interface
tap_interface(){
tap_stop(){
if [[ $(ip tuntap list | grep $1) ]]
then
sudo ip link set dev $1 down
sudo ip tuntap del mode tap name tap0
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_start(){
if [[ ! $(ip tuntap list | grep $1) ]]
then
#sudo tunctl -u $VM_USER
sudo ip tuntap add mode tap user $USER name tap0
sudo ip addr add dev $1 10.10.10.1/24
sudo ip link set dev $1 up
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
if [ $1 == start ]; then tap_start $TAP_INTERFACE; elif [ $1 == stop ]; then tap_stop $TAP_INTERFACE; fi
}
## DHCP Server (DNSmasq)
dhcp_server(){
dhcp_stop(){
[[ -f /var/run/dnsmasq.pid ]] && sudo kill -15 $(cat /var/run/dnsmasq.pid) && sudo rm /var/run/dnsmasq.pid
}
dhcp_start(){
[[ -f /var/run/dnsmasq.pid ]] || dnsmasq --conf-file=$DNSMASQ_CONF
}
if [ $1 == start ]
then
dhcp_start
elif [ $1 == stop ]
then
dhcp_stop
fi
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 controls
## Samba server
samba_server(){
samba_stop(){
[[ -f /var/run/smbd.pid ]] && echo "Stopping samba" && sudo kill -15 $(cat /var/run/smbd.pid) || echo "Samba was already stopped"
}
samba_start(){
[[ -f /var/run/smbd.pid ]] && echo "Samba was already started" || echo "Starting samba" && sudo smbd --configfile=$SMB_CONF
[[ -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
if [ $1 == start ]; then samba_start; elif [ $1 == stop ]; then samba_stop; fi
}
## Start the network
tap_interface start
dhcp_server start
samba_server start
## Load the config file
source "${BASH_SOURCE%/*}/config"
## For samba to work you must run as root `smbpasswd -a <USER>`
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## END ## Network stuff
## Kill the Display Manager
systemctl stop lightdm
## Kill the console to free the GPU.
## The console, by default, is attached to the boot GPU, in this case there is only one and we need to left it unused to remove it from the system.
## 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
## You may need to unload more or other kernel modules (drivers) for your specific GPU.
## This command allows you to see which modules depend uppon others. Change nvidia for noveau if you are using the OSS drivers or for the AMD equivalent.
# lsmod | grep nvidia
## The left column are the modules and the right are the modules that are using that module. The modules should be unloaded only if there is no other modules using it; if there are, then unload them first.
# nvidia_drm 49152 7
# nvidia_modeset 1044480 23 nvidia_drm
# nvidia 16605184 1038 nvidia_modeset
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
## Also unload the audio modules, may be more than one, because they use the audio device in the GPU
modprobe -r snd_hda_intel
## Load the kernel module related to vfio
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
## Detach the GPU from drivers and attach to vfio. Also the usb.
## This may not be the right way, but it works for me and there is no symptoms of anything not working.
## 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
@ -146,72 +79,57 @@ 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
## Start the network
tap_interface start
dhcp_server start
samba_server start
## QEMU (VM) command
qemu-system-x86_64 -runas $USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-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 vfio-pci,host=$IOMMU_USB \
-device virtio-net-pci,netdev=net0 \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no,vhost=on \
-drive if=pflash,format=raw,readonly,file=$OVMF \
-device virtio-scsi-pci,id=scsi0 \
-device scsi-hd,bus=scsi0.0,drive=rootfs \
-drive id=rootfs,file=$IMG,media=disk,format=raw,if=none > /dev/null 2>&1 &
## Wait for QEMU to finish before continuing
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
-nographic -vga none -parallel none -serial none \
-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=net0 \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no,vhost=on \
-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=raw,if=none >> $LOG 2>&1 &
## Wait for QEMU
wait
sleep 1
## Stop the network
tap_interface stop
dhcp_server stop
samba_server stop
## Unload the vfio module. This leaves the GPU without drivers.
## Unload vfio module
modprobe -r vfio-pci
modprobe -r vfio_iommu_type1
modprobe -r vfio
## Reload the kernel modules that previously were unloaded. Load them inversed as you unloaded. The las unloaded should be the first to be loaded and viceversa. THIS MAY NOT BE THE CASE FOR YOU. This SHOULD load the drivers for the GPU.
## Load the kernel modules
modprobe snd_hda_intel
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia
## Bind the USB. If you passed the USB controller to the VM.
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/new_id
## This does not exist, so...
#echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind
echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind
#echo $usbid > /sys/bus/pci/drivers/xhci_hcd/remove_id
#ls -la /sys/bus/pci/devices/$usbbusid/
## Rebind the EFI-Framebuffer and Rebind 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
## This just creates a file called `tee`
#echo 1 > tee /sys/class/vtconsole/vtcon1/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. You can also run startx, though I don't know how to do it and `startx` will probably not work.
# Reload the Display Manager to access X
systemctl start lightdm
sleep 2
## Restore the Frame Buffer.
## I tried MANY different ways and timings of doing this, and this is the only one that works, FOR ME.
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
## Restore ulimit to the previous state.
## Restore ulimit
ulimit -l $ULIMIT

@ -0,0 +1,77 @@
#!/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 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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
virsh nodedev-detach $VIRSH_GPU_AUDIO
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
## QEMU (VM) command
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=raw,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
virsh nodedev-reattach $VIRSH_GPU
## Reload the framebuffer
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
echo 1 > /sys/class/vtconsole/vtcon0/bind
echo 1 > /sys/class/vtconsole/vtcon1/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

@ -1,111 +1,126 @@
#!/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 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 $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 && LIBVIRTD=STOPPED
## Memory lock limit
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
## Kill the Display Manager
systemctl stop lightdm
## 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
## Detach the GPU
virsh nodedev-detach $VIRSH_GPU
virsh nodedev-detach $VIRSH_GPU_AUDIO
# Load the kernel module
## Load vfio
modprobe vfio
modprobe vfio_iommu_type1
modprobe vfio-pci
# Detach the GPU from drivers and attach to vfio. Also the usb.
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
## Start the network
tap_interface start
dhcp_server start
samba_server start
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
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 (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 \
-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
-nographic -vga none -parallel none -serial none \
-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=net0 \
-netdev tap,id=net0,ifname=$TAP_INTERFACE,script=no,downscript=no,vhost=on \
-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=raw,if=none >> $LOG 2>&1 &
## Wait for QEMU
wait
sleep 1
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
## 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
# Reload the kernel modules. This loads the drivers for the GPU
modprobe snd_hda_intel
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia
# 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
#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]
## Reattach the GPU
virsh nodedev-reattach $VIRSH_GPU_AUDIO
virsh nodedev-reattach $VIRSH_GPU
## Reload the framebuffer and console
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
echo 1 > /sys/class/vtconsole/vtcon0/bind
sleep 1
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
sleep 1
echo 1 > /sys/class/vtconsole/vtcon1/bind
# Reload the Display Manager to access X
## Reload the Display Manager
systemctl start lightdm
sleep 2
# Restore the Frame Buffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
## If libvirtd was stopped then stop it
[[ $LIBVIRTD == "STOPPED" ]] && systemctl stop libvirtd
# Restore ulimit
## Restore ulimit
ulimit -l $ULIMIT

@ -1,16 +0,0 @@
# Troubleshooting
## QEMU can not allocate memory
```
qemu-system-x86_64: VFIO_MAP_DMA: -12
qemu-system-x86_64: vfio_dma_map(0x7fae44695a00, 0xc0000000, 0x40000, 0x7fab29e00000) = -12 (Cannot allocate memory)
qemu: hardware error: vfio: DMA mapping failed, unable to continue
```
Solution:
```
ulimit -l $(( $(echo $RAM | tr -d 'G')*1048576+10 ))
```
Note: This is already addressed in the `windows.sh` script.
Loading…
Cancel
Save