Skip to content

Instantly share code, notes, and snippets.

@PedroHLC
Last active October 10, 2017 01:15
Show Gist options
  • Save PedroHLC/e2c5181a61b84e8972e5 to your computer and use it in GitHub Desktop.
Save PedroHLC/e2c5181a61b84e8972e5 to your computer and use it in GitHub Desktop.
QEmu: Emulating all physical HDDs + AHCI + VFIO PCI Passthrough (When available + auto binding)
#!/bin/bash
PCI_PASSTHROUGH_DEVS=('01:00.0' '01:00.1')
MOUNTED_STORAGES=($(mount | grep -oh "/dev/sd\w"))
MOUNTED_STORAGES_UNIQUE=($(echo "${MOUNTED_STORAGES[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
if dmesg | grep -i "vfio_pci: add" > /dev/null; then
export PCI_PASSTHROUGH_USING=1
vfiobind() {
dev="0000:$1"
vendor=$(cat /sys/bus/pci/devices/$dev/vendor)
device=$(cat /sys/bus/pci/devices/$dev/device)
if [ -e /sys/bus/pci/devices/$dev/driver ]; then
echo $dev > /sys/bus/pci/devices/$dev/driver/unbind
fi
echo $vendor $device > /sys/bus/pci/drivers/vfio-pci/new_id
}
for pciid in "${PCI_PASSTHROUGH_DEVS[@]}"; do
vfiobind $pciid
done
else
export PCI_PASSTHROUGH_USING=0
fi
export MOUNTCOUNT=1
export QEMU_AUDIO_DRV="pa"
rm /tmp/my_ovmfvars.fd
cp /usr/local/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd /tmp/my_ovmfvars.fd
qemu-system-x86_64 \
-drive if=pflash,format=raw,readonly,file=/usr/local/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd -drive if=pflash,format=raw,file=/tmp/my_ovmfvars.fd \
-name WBOX720 \
-enable-kvm \
-cpu host,kvm=off \
-smp sockets=1,cores=8,threads=1 \
-m 6144 \
-device ahci,id=ahci \
-drive id=disk0,file=/home/pedrohlc/vboot.img,format=raw,if=none -device ide-drive,drive=disk0,bus=ahci.0 \
$(
for hddid in "${MOUNTED_STORAGES_UNIQUE[@]}"; do
echo "-drive id=disk${MOUNTCOUNT},file=${hddid},format=raw,if=none -device ide-drive,drive=disk${MOUNTCOUNT},bus=ahci.${MOUNTCOUNT} "
((MOUNTCOUNT++))
done;) \
-boot order=c \
-serial none -parallel none \
-soundhw hda \
-net nic -net user,hostname=PedroHLC-WBOX720 \
$(
if [ $PCI_PASSTHROUGH_USING -gt 0 ]; then
echo ''
echo "-device vfio-pci,host=${PCI_PASSTHROUGH_DEVS[0]},multifunction=on,x-vga=on -device vfio-pci,host=${PCI_PASSTHROUGH_DEVS[1]} "
fi;)
#-vga none -nographic
#To fix Windows clock, either add "-rtc base=localtime" or https://wiki.archlinux.org/index.php/Time#UTC_in_Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment