From 207575129dbbaf95d8d0db45245958c82ed5d90b Mon Sep 17 00:00:00 2001
From: QaidVoid <12017109+QaidVoid@users.noreply.github.com>
Date: Wed, 2 Dec 2020 09:36:31 +0545
Subject: [PATCH] Libvirt script added
---
README.md | 223 ++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 193 insertions(+), 30 deletions(-)
diff --git a/README.md b/README.md
index 34ec771..fa93ef1 100644
--- a/README.md
+++ b/README.md
@@ -1,19 +1,19 @@
### **Enable IOMMU**
-#### ***Set the kernel paramater depending on your CPU.***
-#### For GRUB user, edit grub configuration.
+***Set the kernel paramater depending on your CPU.*** \
+For GRUB user, edit grub configuration.
| /etc/default/grub |
| ----- |
-| `GRUB_CMDLINE_LINUX_DEFAULT="intel_iommu=on iommu=pt ..."` |
+| `GRUB_CMDLINE_LINUX_DEFAULT="... intel_iommu=on iommu=pt ..."` |
| OR |
-| `GRUB_CMDLINE_LINUX_DEFAULT="amd_iommu=on iommu=pt ..."` |
-#### ***Generate grub.cfg***
+| `GRUB_CMDLINE_LINUX_DEFAULT="... amd_iommu=on iommu=pt ..."` |
+***Generate grub.cfg***
```sh
grub-mkconfig -o /boot/grub/grub.cfg
```
-#### Reboot your system for the changes to take effect.
+Reboot your system for the changes to take effect.
### **Verify IOMMU**
-#### ***If you don't see any output when running following command, IOMMU is not functioning.***
+***If you don't see any output when running following command, IOMMU is not functioning.***
```sh
dmesg | grep 'IOMMU enabled'
```
@@ -61,34 +61,47 @@ dmesg | grep 'IOMMU enabled'
```
-#### Sometimes, you might need to start default network manually.
+Sometimes, you might need to start default network manually.
```sh
virsh net-start default
virsh net-autostart default
```
### **Setup Guest OS**
-### ***NOTE: You should replace win10 with your VM's name where applicable***
-#### ***Download [virtio](https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso) driver.***
-#### Launch ***virt-manager*** and create a new virtual machine. Select ***Customize before install*** on Final Step.
-#### In ***Overview*** section, set ***Chipset*** to ***Q35***, and ***Firmware*** to ***UEFI***
-#### In ***CPUs*** section, set ***CPU model*** to ***host-passthrough***, and ***CPU Topology*** to whatever fits your system.
-#### For ***SATA*** disk of VM, set ***Disk Bus*** to ***virtio***.
-#### In ***NIC*** section, set ***Device Model*** to ***virtio***
-#### Add Hardware > CDROM: virtio-win.iso
-#### Now, ***Begin Installation***. Windows can't detect the ***virtio disk***, so you need to ***Load Driver*** and select ***virtio-iso/amd64/win10*** when prompted.
-#### After successful installation of Windows, install virtio drivers from virtio CDROM.
+***NOTE: You should replace win10 with your VM's name where applicable*** \
+Download [virtio](https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso) driver. \
+Launch ***virt-manager*** and create a new virtual machine. Select ***Customize before install*** on Final Step. \
+In ***Overview*** section, set ***Chipset*** to ***Q35***, and ***Firmware*** to ***UEFI*** \
+In ***CPUs*** section, set ***CPU model*** to ***host-passthrough***, and ***CPU Topology*** to whatever fits your system. \
+For ***SATA*** disk of VM, set ***Disk Bus*** to ***virtio***. \
+In ***NIC*** section, set ***Device Model*** to ***virtio*** \
+Add Hardware > CDROM: virtio-win.iso \
+Now, ***Begin Installation***. Windows can't detect the ***virtio disk***, so you need to ***Load Driver*** and select ***virtio-iso/amd64/win10*** when prompted. \
+After successful installation of Windows, install virtio drivers from virtio CDROM.
### **Attaching PCI devices**
-#### Remove Channel Spice, Display Spice, Video XQL, Sound ich* and other unnecessary devices.
-#### Now, click on ***Add Hardware***, select ***PCI Devices*** and add the PCI Host devices for your GPU's VGA and HDMI Audio
-#### Some GPU vBIOS needs to be patched for UEFI Support.
------ TODO: vBIOS patching ------
-#### To use patched vBIOS, edit VM's configuration
-```sh
-virsh edit win10
-```
-```xml
+Remove Channel Spice, Display Spice, Video XQL, Sound ich* and other unnecessary devices. \
+Now, click on ***Add Hardware***, select ***PCI Devices*** and add the PCI Host devices for your GPU's VGA and HDMI Audio \
+Some GPU vBIOS needs to be patched for UEFI Support. \
+----- TODO: vBIOS patching ------ \
+To use patched vBIOS, edit VM's configuration to include patched vBIOS inside ***hostdev*** block of VGA
+
+ ```sh
+ mkdir /etc/libvirt/hooks
+ touch /etc/libvirt/hooks/qemu
+ chmod +x /etc/libvirt/hooks/qemu
+ ```
+
+
+
+ virsh edit win10
+
+
+
+
+
+
+ ```xml
...
...
-```
+ ```
+
+
+
+
### Libvirt Hooks
-#### TODO
+Libvirt hooks automate the process of running specific tasks during VM state change. \
+More info at: [PassthroughPost](https://passthroughpo.st/simple-per-vm-libvirt-hooks-with-the-vfio-tools-hook-helper/)
+
+ Create Libvirt Hook
+
+ ```sh
+ mkdir /etc/libvirt/hooks
+ touch /etc/libvirt/hooks/qemu
+ chmod +x /etc/libvirt/hooks/qemu
+ ```
+
+
+
+ /etc/libvirt/hooks/qemu
+
+
+
+
+
+
+ ```sh
+ #!/bin/bash
+
+GUEST_NAME="$1"
+HOOK_NAME="$2"
+STATE_NAME="$3"
+MISC="${@:4}"
+
+BASEDIR="$(dirname $0)"
+
+HOOKPATH="$BASEDIR/qemu.d/$GUEST_NAME/$HOOK_NAME/$STATE_NAME"
+set -e # If a script exits with an error, we should as well.
+
+if [ -f "$HOOKPATH" ]; then
+ eval \""$HOOKPATH"\" "$@"
+elif [ -d "$HOOKPATH" ]; then
+ while read file; do
+ eval \""$file"\" "$@"
+ done <<< "$(find -L "$HOOKPATH" -maxdepth 1 -type f -executable -print;)"
+fi
+ ```
+
+