Update chocolatebar VMs; make USB handover optional, enable looking glass

This commit is contained in:
Benjamin Bädorf 2021-12-11 21:09:22 +01:00
parent 15c0412080
commit f53c21f3e0
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
4 changed files with 68 additions and 52 deletions

View file

@ -11,8 +11,6 @@ in
];
config = {
pub-solar.virtualisation.isolateGPU = "rx550x";
hardware.cpu.amd.updateMicrocode = true;
hardware.opengl.extraPackages = with pkgs; [

View file

@ -3,7 +3,7 @@ let
psCfg = config.pub-solar;
xdg = config.home-manager.users."${psCfg.user.name}".xdg;
varsFile = "${xdg.dataHome}/libvirt/OVMF_VARS_${vm.name}.fd";
generateXML = import ./generate-xml.nix;
generateXML = import ./guest-xml.nix;
in
{
serviceConfig = {
@ -29,33 +29,45 @@ in
NET_TMP_FILE="/tmp/network.xml"
NETUUID="$(${pkgs.libvirt}/bin/virsh net-uuid 'default' || true)"
(sed "s/UUID/$NETUUID/" '${networkXML}') > $NET_TMP_FILE
(sed "s/UUID/$NETUUID/" '${networkXML}') > "$NET_TMP_FILE"
${pkgs.libvirt}/bin/virsh net-define $NET_TMP_FILE
${pkgs.libvirt}/bin/virsh net-define "$NET_TMP_FILE"
${pkgs.libvirt}/bin/virsh net-start 'default' || true
VARS_FILE=${varsFile}
if [ ! -f "$VARS_FILE" ]; then
cp /run/libvirt/nix-ovmf/OVMF_VARS.fd $VARS_FILE
cp /run/libvirt/nix-ovmf/OVMF_VARS.fd "$VARS_FILE"
fi
# Load the template contents into a tmp file
TMP_FILE="/tmp/${vm.name}.xml"
cat "${machineXML}" > "$TMP_FILE"
# Set VM UUID
UUID="$(${pkgs.libvirt}/bin/virsh domuuid '${vm.name}' || true)"
(sed "s/UUID/$UUID/" '${machineXML}') > $TMP_FILE
sed -i "s/UUID/''${UUID}/" "$TMP_FILE"
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c52b | grep 'Bus 001' | cut -b 18)
LINE_NUMBER=$(cat $TMP_FILE | grep -n -A 1 0xc52b | tail -n 1 | cut -b 1,2,3)
sed -i "''${LINE_NUMBER}s/\(.\{33\}\)./\1''${USB_DEV}/" $TMP_FILE
${if vm.handOverUSBDevices then ''
# Hand over keyboard
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c52b | grep 'Bus 001' | cut -b 18)
LINE_NUMBER=$(cat $TMP_FILE | grep -n -A 1 0xc52b | tail -n 1 | cut -b 1,2,3)
sed -i "''${LINE_NUMBER}s/\(.\{33\}\)./\1''${USB_DEV}/" "$TMP_FILE"
USB_BUS=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 7)
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 18)
LINE_NUMBER=$(cat $TMP_FILE | grep -n -A 1 0xc328 | tail -n 1 | cut -b 1,2,3)
sed -i "''${LINE_NUMBER}s/.*/<address bus=\"''${USB_BUS}\" device=\"''${USB_DEV}\" \/>/" $TMP_FILE
# Hand over mouse
USB_BUS=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 7)
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 18)
LINE_NUMBER=$(cat $TMP_FILE | grep -n -A 1 0xc328 | tail -n 1 | cut -b 1,2,3)
sed -i "''${LINE_NUMBER}s/.*/<address bus=\"''${USB_BUS}\" device=\"''${USB_DEV}\" \/>/" "$TMP_FILE"
'' else ""}
# TODO: Set correct pci address too
# TODO: Set correct pci address for the GPU too
${pkgs.libvirt}/bin/virsh define $TMP_FILE
# Setup looking glass shm file
${pkgs.coreutils-full}/bin/truncate -s 0 /dev/shm/looking-glass
${pkgs.coreutils-full}/bin/dd if=/dev/zero of=/dev/shm/looking-glass bs=1M count=32
# Load and start the xml definition
${pkgs.libvirt}/bin/virsh define "$TMP_FILE"
${pkgs.libvirt}/bin/virsh start '${vm.name}'
'';

View file

@ -4,19 +4,17 @@ let
psCfg = config.pub-solar;
xdg = config.home-manager.users."${psCfg.user.name}".xdg;
createService = import ./create-service.nix;
isolateAnyGPU = psCfg.virtualisation.isolateGPU != null;
isolateGPU = "rx550x";
handOverUSBDevices = false;
isolateAnyGPU = isolateGPU != null;
in
{
options.pub-solar.virtualisation.isolateGPU = mkOption {
description = "Which GPU to isolate for virtualisation guests";
type = with types; nullOr (enum [ "rx5700xt" "rx550x" ]);
default = null;
};
config = mkIf psCfg.virtualisation.enable {
boot.extraModprobeConfig = mkIf isolateAnyGPU (concatStringsSep "\n" [
"softdep amdgpu pre: vfio vfio_pci"
(if psCfg.virtualisation.isolateGPU == "rx5700xt"
(if isolateGPU == "rx5700xt"
then "options vfio-pci ids=1002:731f,1002:ab38"
else "options vfio-pci ids=1002:699f,1002:aae0"
)
@ -33,6 +31,8 @@ in
id = "http://microsoft.com/win/10";
gpu = true;
mountHome = false;
isolateGPU = isolateGPU;
handOverUSBDevices = handOverUSBDevices;
};
};
vm-manjaro = createService {
@ -45,6 +45,8 @@ in
id = "https://manjaro.org/download/#i3";
gpu = true;
mountHome = true;
isolateGPU = isolateGPU;
handOverUSBDevices = handOverUSBDevices;
};
};
};

View file

@ -5,7 +5,7 @@ let
home = config.home-manager.users."${psCfg.user.name}".home;
in
''
<domain type='kvm'>
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>${vm.name}</name>
<uuid>UUID</uuid>
<metadata>
@ -166,14 +166,9 @@ in
<interface type='network'>
<mac address='52:54:00:44:cd:ac'/>
<source network='default'/>
<model type='rtl8139'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x08' slot='0x01' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
@ -190,29 +185,31 @@ in
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
</video>
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x046d'/>
<product id='0xc328'/>
<address bus='1' device='2'/>
</source>
<address type='usb' bus='0' port='4'/>
</hostdev>
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x046d'/>
<product id='0xc52b'/>
<address bus='1' device='4'/>
</source>
<address type='usb' bus='0' port='5'/>
</hostdev>
${if vm.gpu && psCfg.virtualisation.isolateGPU != null then ''
${if vm.handOverUSBDevices then ''
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x046d'/>
<product id='0xc328'/>
<address bus='1' device='2'/>
</source>
<address type='usb' bus='0' port='4'/>
</hostdev>
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x046d'/>
<product id='0xc52b'/>
<address bus='1' device='4'/>
</source>
<address type='usb' bus='0' port='5'/>
</hostdev>
'' else ""}
${if vm.gpu && vm.isolateGPU != null then ''
<hostdev mode='subsystem' type='pci' managed='yes'>
<driver name='vfio'/>
<source>
<address domain='0x0000' bus='0x0b' slot='0x00' function='0x0'/>
</source>
<rom bar='on' file='/etc/nixos/hosts/chocolatebar/virtualisation/${psCfg.virtualisation.isolateGPU}.rom'/>
<rom bar='on' file='/etc/nixos/hosts/chocolatebar/virtualisation/${vm.isolateGPU}.rom'/>
<address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0' multifunction='on'/>
</hostdev>
<hostdev mode='subsystem' type='pci' managed='yes'>
@ -232,11 +229,18 @@ in
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</memballoon>
<shmem name='scream-ivshmem'>
<shmem name='looking-glass'>
<model type='ivshmem-plain'/>
<size unit='M'>2</size>
<address type='pci' domain='0x0000' bus='0x08' slot='0x02' function='0x0'/>
<size unit='M'>32</size>
</shmem>
</devices>
<qemu:commandline>
<qemu:arg value='-device'/>
<qemu:arg value='ich9-intel-hda,bus=pcie.0,addr=0x1b'/>
<qemu:arg value='-device'/>
<qemu:arg value='hda-micro,audiodev=hda'/>
<qemu:arg value='-audiodev'/>
<qemu:arg value='pa,id=hda,server=unix:/run/user/1001/pulse/native'/>
</qemu:commandline>
</domain>
''