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 = { config = {
pub-solar.virtualisation.isolateGPU = "rx550x";
hardware.cpu.amd.updateMicrocode = true; hardware.cpu.amd.updateMicrocode = true;
hardware.opengl.extraPackages = with pkgs; [ hardware.opengl.extraPackages = with pkgs; [

View file

@ -3,7 +3,7 @@ let
psCfg = config.pub-solar; psCfg = config.pub-solar;
xdg = config.home-manager.users."${psCfg.user.name}".xdg; xdg = config.home-manager.users."${psCfg.user.name}".xdg;
varsFile = "${xdg.dataHome}/libvirt/OVMF_VARS_${vm.name}.fd"; varsFile = "${xdg.dataHome}/libvirt/OVMF_VARS_${vm.name}.fd";
generateXML = import ./generate-xml.nix; generateXML = import ./guest-xml.nix;
in in
{ {
serviceConfig = { serviceConfig = {
@ -29,33 +29,45 @@ in
NET_TMP_FILE="/tmp/network.xml" NET_TMP_FILE="/tmp/network.xml"
NETUUID="$(${pkgs.libvirt}/bin/virsh net-uuid 'default' || true)" 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 ${pkgs.libvirt}/bin/virsh net-start 'default' || true
VARS_FILE=${varsFile} VARS_FILE=${varsFile}
if [ ! -f "$VARS_FILE" ]; then 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 fi
# Load the template contents into a tmp file
TMP_FILE="/tmp/${vm.name}.xml" TMP_FILE="/tmp/${vm.name}.xml"
cat "${machineXML}" > "$TMP_FILE"
# Set VM UUID
UUID="$(${pkgs.libvirt}/bin/virsh domuuid '${vm.name}' || true)" UUID="$(${pkgs.libvirt}/bin/virsh domuuid '${vm.name}' || true)"
(sed "s/UUID/$UUID/" '${machineXML}') > $TMP_FILE sed -i "s/UUID/''${UUID}/" "$TMP_FILE"
${if vm.handOverUSBDevices then ''
# Hand over keyboard
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c52b | grep 'Bus 001' | cut -b 18) 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) 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 sed -i "''${LINE_NUMBER}s/\(.\{33\}\)./\1''${USB_DEV}/" "$TMP_FILE"
# Hand over mouse
USB_BUS=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 7) USB_BUS=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 7)
USB_DEV=$(${pkgs.usbutils}/bin/lsusb | grep 046d:c328 | cut -b 18) 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) 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 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}' ${pkgs.libvirt}/bin/virsh start '${vm.name}'
''; '';

View file

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

View file

@ -5,7 +5,7 @@ let
home = config.home-manager.users."${psCfg.user.name}".home; home = config.home-manager.users."${psCfg.user.name}".home;
in in
'' ''
<domain type='kvm'> <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>${vm.name}</name> <name>${vm.name}</name>
<uuid>UUID</uuid> <uuid>UUID</uuid>
<metadata> <metadata>
@ -166,14 +166,9 @@ in
<interface type='network'> <interface type='network'>
<mac address='52:54:00:44:cd:ac'/> <mac address='52:54:00:44:cd:ac'/>
<source network='default'/> <source network='default'/>
<model type='rtl8139'/> <model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x08' slot='0x01' function='0x0'/> <address type='pci' domain='0x0000' bus='0x08' slot='0x01' function='0x0'/>
</interface> </interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'> <console type='pty'>
<target type='serial' port='0'/> <target type='serial' port='0'/>
</console> </console>
@ -190,6 +185,7 @@ in
<model type='cirrus' vram='16384' heads='1' primary='yes'/> <model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
</video> </video>
${if vm.handOverUSBDevices then ''
<hostdev mode='subsystem' type='usb' managed='yes'> <hostdev mode='subsystem' type='usb' managed='yes'>
<source> <source>
<vendor id='0x046d'/> <vendor id='0x046d'/>
@ -206,13 +202,14 @@ in
</source> </source>
<address type='usb' bus='0' port='5'/> <address type='usb' bus='0' port='5'/>
</hostdev> </hostdev>
${if vm.gpu && psCfg.virtualisation.isolateGPU != null then '' '' else ""}
${if vm.gpu && vm.isolateGPU != null then ''
<hostdev mode='subsystem' type='pci' managed='yes'> <hostdev mode='subsystem' type='pci' managed='yes'>
<driver name='vfio'/> <driver name='vfio'/>
<source> <source>
<address domain='0x0000' bus='0x0b' slot='0x00' function='0x0'/> <address domain='0x0000' bus='0x0b' slot='0x00' function='0x0'/>
</source> </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'/> <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0' multifunction='on'/>
</hostdev> </hostdev>
<hostdev mode='subsystem' type='pci' managed='yes'> <hostdev mode='subsystem' type='pci' managed='yes'>
@ -232,11 +229,18 @@ in
<memballoon model='virtio'> <memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/> <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</memballoon> </memballoon>
<shmem name='scream-ivshmem'> <shmem name='looking-glass'>
<model type='ivshmem-plain'/> <model type='ivshmem-plain'/>
<size unit='M'>2</size> <size unit='M'>32</size>
<address type='pci' domain='0x0000' bus='0x08' slot='0x02' function='0x0'/>
</shmem> </shmem>
</devices> </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> </domain>
'' ''