vfio-guide-2/scripts/macos-hs.sh

142 lines
3.6 KiB
Bash
Raw Normal View History

2018-08-10 17:02:31 +00:00
#!/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
2018-08-18 12:01:18 +00:00
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
2018-08-10 17:02:31 +00:00
2018-08-10 18:32:15 +00:00
# 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
2018-08-10 17:02:31 +00:00
# Unload the Kernel Modules that use the GPU
modprobe -r nvidia_drm
2018-08-10 18:32:15 +00:00
sleep 1
2018-08-10 17:02:31 +00:00
modprobe -r nvidia_modeset
2018-08-10 18:32:15 +00:00
sleep 1
2018-08-10 17:02:31 +00:00
modprobe -r nvidia
2018-08-10 18:32:15 +00:00
sleep 1
2018-08-10 17:02:31 +00:00
modprobe -r snd_hda_intel
2018-08-10 18:32:15 +00:00
sleep 2
2018-08-10 17:02:31 +00:00
2018-08-10 18:32:15 +00:00
# Load the kernel module
2018-08-18 12:01:18 +00:00
modprobe vfio
sleep 1
modprobe vfio_iommu_type1
sleep 1
2018-08-10 18:32:15 +00:00
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
2018-08-10 17:02:31 +00:00
2018-08-18 12:01:18 +00:00
qemu-system-x86_64 -runas $USER -enable-kvm \
2018-08-10 17:02:31 +00:00
-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 \
2018-08-17 12:58:40 +00:00
-netdev user,id=net0 \
-device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
2018-08-10 17:02:31 +00:00
-device isa-applesmc,osk="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc" \
2018-08-18 12:01:18 +00:00
-drive if=pflash,format=raw,readonly,file=$OVMF \
-drive if=pflash,format=raw,file=$OVMF_VARS \
2018-08-10 17:02:31 +00:00
-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 \
2018-08-10 18:32:15 +00:00
-drive id=MacHDD,if=none,file=$IMG,format=raw > /dev/null 2>&1 &
2018-08-10 17:02:31 +00:00
# END QEMU (VM) command
# Wait for QEMU to finish before continue
wait
2018-08-10 18:32:15 +00:00
sleep 5
2018-08-10 17:02:31 +00:00
2018-08-10 18:32:15 +00:00
# Unload the vfio module. I am lazy, this leaves the GPU without drivers
modprobe -r vfio-pci
sleep 2
2018-08-18 12:01:18 +00:00
modprobe -r vfio_iommu_type1
sleep 2
modprobe -r vfio
sleep 2
2018-08-10 17:02:31 +00:00
2018-08-10 18:32:15 +00:00
# Reload the kernel modules. This loads the drivers for the GPU
2018-08-10 17:02:31 +00:00
modprobe snd_hda_intel
2018-08-10 18:32:15 +00:00
sleep 5
2018-08-10 17:02:31 +00:00
modprobe nvidia_drm
2018-08-10 18:32:15 +00:00
sleep 2
2018-08-10 17:02:31 +00:00
modprobe nvidia_modeset
2018-08-10 18:32:15 +00:00
sleep 2
2018-08-10 17:02:31 +00:00
modprobe nvidia
2018-08-10 18:32:15 +00:00
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
2018-08-10 17:02:31 +00:00
# Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
echo 1 > /sys/class/vtconsole/vtcon0/bind
2018-08-10 18:32:15 +00:00
sleep 1
2018-08-10 17:02:31 +00:00
echo 1 > tee /sys/class/vtconsole/vtcon1/bind
2018-08-10 18:32:15 +00:00
sleep 5
2018-08-10 17:02:31 +00:00
# Reload the Display Manager to access X
systemctl start lightdm
2018-08-10 18:32:15 +00:00
sleep 5
2018-08-18 12:01:18 +00:00
# Restore the Frame Buffer
2018-08-10 18:32:15 +00:00
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
sleep 1
2018-08-18 12:01:18 +00:00
# Restore ulimit
ulimit -l $ULIMIT