2018-11-22 21:48:36 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
## Check if the script is being run as root
|
|
|
|
[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit 1
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Tap interface
|
2018-11-22 21:48:36 +00:00
|
|
|
tap_interface(){
|
2018-12-30 21:51:17 +00:00
|
|
|
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
|
2018-11-22 21:48:36 +00:00
|
|
|
fi
|
|
|
|
}
|
2018-12-30 21:51:17 +00:00
|
|
|
tap_stop(){
|
|
|
|
if [[ $(ip tuntap list | grep $1) ]]; then
|
|
|
|
ip link set dev $1 down
|
|
|
|
ip tuntap del mode tap name $TAP_INTERFACE
|
2018-11-22 21:48:36 +00:00
|
|
|
fi
|
|
|
|
}
|
2018-12-30 21:51:17 +00:00
|
|
|
if [ $1 == start ]; then tap_start $TAP_INTERFACE; elif [ $1 == stop ]; then tap_stop $TAP_INTERFACE; fi
|
2018-11-22 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## DHCP Server (DNSmasq)
|
2018-11-22 21:48:36 +00:00
|
|
|
dhcp_server(){
|
|
|
|
dhcp_start(){
|
|
|
|
[[ -f /var/run/dnsmasq.pid ]] || dnsmasq --conf-file=$DNSMASQ_CONF
|
|
|
|
}
|
2018-12-30 21:51:17 +00:00
|
|
|
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
|
2018-11-22 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Samba server
|
2018-11-22 21:48:36 +00:00
|
|
|
samba_server(){
|
|
|
|
samba_start(){
|
2018-12-30 21:51:17 +00:00
|
|
|
[[ -f /var/run/smbd.pid ]] || sudo smbd --configfile=$SMB_CONF
|
2018-11-22 21:48:36 +00:00
|
|
|
}
|
2018-12-30 21:51:17 +00:00
|
|
|
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
|
2018-11-22 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Load the config file
|
|
|
|
source "${BASH_SOURCE%/*}/config"
|
2018-11-22 21:48:36 +00:00
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Memory lock limit
|
|
|
|
[[ $ULIMIT != $ULIMIT_TARGET ]] && ulimit -l $ULIMIT_TARGET
|
2018-11-22 21:48:36 +00:00
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Kill the Display Manager
|
|
|
|
systemctl stop lightdm
|
2019-01-02 18:26:23 +00:00
|
|
|
sleep 1
|
2018-11-22 21:48:36 +00:00
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Remove the framebuffer and console
|
2018-11-22 21:48:36 +00:00
|
|
|
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
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Load vfio
|
2018-11-22 21:48:36 +00:00
|
|
|
modprobe vfio-pci
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Detach the GPU
|
2018-11-22 21:48:36 +00:00
|
|
|
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
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Start the network
|
|
|
|
tap_interface start
|
|
|
|
dhcp_server start
|
|
|
|
samba_server start
|
|
|
|
|
|
|
|
qemu-system-x86_64 -runas $VM_USER -enable-kvm \
|
2018-11-22 21:48:36 +00:00
|
|
|
-nographic -vga none -parallel none -serial none \
|
2018-12-30 21:51:17 +00:00
|
|
|
-m $MACOS_RAM \
|
|
|
|
-cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MACOS_OPTIONS\
|
2018-11-22 21:48:36 +00:00
|
|
|
-machine pc-q35-2.11 \
|
2018-12-30 21:51:17 +00:00
|
|
|
-smp $MACOS_CORES,sockets=1,cores=$(( $MACOS_CORES / 2 )),threads=2 \
|
2018-11-22 21:48:36 +00:00
|
|
|
-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" \
|
2019-01-02 18:26:23 +00:00
|
|
|
-drive if=pflash,format=raw,readonly,file=$MACOS_OVMF \
|
|
|
|
-drive if=pflash,format=raw,file=$MACOS_OVMF_VARS \
|
2018-11-22 21:48:36 +00:00
|
|
|
-smbios type=2 \
|
|
|
|
-device ide-drive,bus=ide.2,drive=Clover \
|
2018-12-30 21:51:17 +00:00
|
|
|
-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 &
|
2018-11-22 21:48:36 +00:00
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Wait for QEMU
|
2018-11-22 21:48:36 +00:00
|
|
|
wait
|
|
|
|
|
|
|
|
## Stop the network
|
|
|
|
tap_interface stop
|
|
|
|
dhcp_server stop
|
|
|
|
samba_server stop
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Unload vfio module
|
2018-11-22 21:48:36 +00:00
|
|
|
modprobe -r vfio-pci
|
|
|
|
modprobe -r vfio_iommu_type1
|
|
|
|
modprobe -r vfio
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Load the kernel modules
|
2018-11-22 21:48:36 +00:00
|
|
|
modprobe snd_hda_intel
|
|
|
|
modprobe nvidia_drm
|
|
|
|
modprobe nvidia_modeset
|
|
|
|
modprobe nvidia
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Reload the framebuffer and console
|
2018-11-22 21:48:36 +00:00
|
|
|
echo 1 > /sys/class/vtconsole/vtcon0/bind
|
2018-12-30 21:51:17 +00:00
|
|
|
nvidia-xconfig --query-gpu-info > /dev/null 2>&1
|
|
|
|
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
|
2018-11-22 21:48:36 +00:00
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
# Reload the Display Manager to access X
|
2018-11-22 21:48:36 +00:00
|
|
|
systemctl start lightdm
|
|
|
|
|
2018-12-30 21:51:17 +00:00
|
|
|
## Restore ulimit
|
2018-11-22 21:48:36 +00:00
|
|
|
ulimit -l $ULIMIT
|