QEMU KVM + Libvirt
安装qemu和libvirt所需要的包:
sudo pacman -S qemu libvirt edk2-ovmf virt-manager dnsmasq ebtables iptables bridge-utils
开机启用服务
sudo systemctl enable libvirtd
sudo systemctl enable virtlogd.socket
开始Libvirt
sudo systemctl start libvirtd
sudo systemctl start virtlogd.socket
启用虚拟网络
sudo virsh net-start default
sudo virsh net-autostart default
其余设置,和windows虚拟机的设置,请参考:显卡直通
MacOS (显卡直通)
if [[ $OBJECT == "macOS-Simple-KVM" ]]; then
case "$OPERATION" in
"prepare")
systemctl start libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
/bin/vfio-startup.sh 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
;;
"release")
systemctl stop libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
/bin/vfio-teardown.sh 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
;;
esac
fi
正则表达式 与 sed
命令
sed
命令可以用于替换文本文件中的字符,可是使用正则表达式。
sed "s/CHANGEME/$USER\/qemu_VM/g" macOS-libvirt-Catalina.xml > macOS.xml
使用正则表达式的s#aaaaa#bbbbbb#g
,将aaaaa替换为bbbbb。
若出现/
可用转义字符 \/
表示。
USB直通 (PCI Passthru)
思路
与上篇显卡直通PCIE-Passthrough相同,将 USB Controler直通进虚拟机。
由于我们只有一个 USB Controler,因此要编写脚本,当启用虚拟机时,将 USB Controler的驱动改为vfio-pci
,以便直通进虚拟机;当退出虚拟机时,将 USB Controler的控制权移交回host
。
找出 USB Controler 所在的IOMMU组
使用上次显卡直通中使用的脚本:
sudo ~/文档/proj/LEDs-single-gpu-passthrough/resources/iommuamd.sh
现实结果为,USB Controler 自己在 group 2
IOMMU group 2
00:14.0 USB controller [0c03]: Intel Corporation Cannon Lake PCH USB 3.1 xHCI Host Controller [8086:a36d] (rev 10)
Driver: xhci_hcd
Usb bus:
Bus 001 Device 005: ID 1a81:203e Holtek Semiconductor, Inc. USB Keyboard
Bus 001 Device 004: ID 046d:c083 Logitech, Inc. G403 Prodigy Gaming Mouse
Bus 001 Device 003: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 002: ID 05ac:12a8 Apple, Inc. iPhone 5/5C/5S/6/SE
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Usb bus:
Bus 002 Device 003: ID 0480:0900 Toshiba America Inc MQ04UBF100
Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
00:14.2 RAM memory [0500]: Intel Corporation Cannon Lake PCH Shared SRAM [8086:a36f] (rev 10)
Driver: none
因此,我们要直通这一组。使用本次OSX-KVM
中的脚本 vfio-group.sh
和 vfio-ungroup.sh
来移交USB Controler的管理权。
将设备 8086:a36d
写入 /etc/default/grub
中
vfio-pci.ids=8086:a36d
编写qemu脚本
将脚本复制到 /etc/libvirt/hooks
cd ~/qemu_VM/OSX-KVM/scripts
sudo cp vfio-group.sh vfio-ungroup.sh /etc/libvirt/hooks/
为了执行方便,创建软链接
sudo ln -s /etc/libvirt/hooks/vfio-group.sh /bin/vfio-group.sh
sudo ln -s /etc/libvirt/hooks/vfio-ungroup.sh /bin/vfio-ungroup.sh
下面,编写用于启用和关闭虚拟机时,调用这两个脚本的qemu
脚本。
cd /etc/libvirt/hooks
sudo vim qemu
在其中添加:
#!/bin/bash
OBJECT="$1"
OPERATION="$2"
if [[ $OBJECT == "macOS-Catalina" ]]; then
case "$OPERATION" in
"prepare")
systemctl start libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
/bin/vfio-group.sh 2 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
;;
"release")
systemctl stop libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
/bin/vfio-ungroup.sh 2 2>&1 | tee -a /var/log/libvirt/custom_hooks.log
;;
esac
fi
这里面的 libvirt-nosleep@
为上篇显卡直通里,防止libvirt自动休眠的服务。
这样就可以将USB设备直通进KVM虚拟机了。