diff --git a/hosts/chocolatebar/chocolatebar.nix b/hosts/chocolatebar/chocolatebar.nix index 0e63561..d91b5c5 100644 --- a/hosts/chocolatebar/chocolatebar.nix +++ b/hosts/chocolatebar/chocolatebar.nix @@ -11,8 +11,6 @@ in ]; config = { - pub-solar.virtualisation.isolateGPU = "rx550x"; - hardware.cpu.amd.updateMicrocode = true; hardware.opengl.extraPackages = with pkgs; [ diff --git a/hosts/chocolatebar/virtualisation/create-service.nix b/hosts/chocolatebar/virtualisation/create-service.nix index f3eab3b..0bc3349 100644 --- a/hosts/chocolatebar/virtualisation/create-service.nix +++ b/hosts/chocolatebar/virtualisation/create-service.nix @@ -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/.*/
/" $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/.*//" "$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}' ''; diff --git a/hosts/chocolatebar/virtualisation/default.nix b/hosts/chocolatebar/virtualisation/default.nix index a024ab6..730e5a9 100644 --- a/hosts/chocolatebar/virtualisation/default.nix +++ b/hosts/chocolatebar/virtualisation/default.nix @@ -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; }; }; }; diff --git a/hosts/chocolatebar/virtualisation/generate-xml.nix b/hosts/chocolatebar/virtualisation/guest-xml.nix similarity index 87% rename from hosts/chocolatebar/virtualisation/generate-xml.nix rename to hosts/chocolatebar/virtualisation/guest-xml.nix index 4e74643..7dbeac6 100644 --- a/hosts/chocolatebar/virtualisation/generate-xml.nix +++ b/hosts/chocolatebar/virtualisation/guest-xml.nix @@ -5,7 +5,7 @@ let home = config.home-manager.users."${psCfg.user.name}".home; in '' -