From 45562be071bf3cda0e0dd8fea835908ef50b8554 Mon Sep 17 00:00:00 2001 From: materus Date: Mon, 3 Jul 2023 21:49:46 +0200 Subject: [PATCH] materusPC: add win10 vm --- configurations/host/materusPC/default.nix | 1 + .../host/materusPC/hardware/boot.nix | 6 +- configurations/host/materusPC/tmp.nix | 40 ++------- configurations/host/materusPC/vm/default.nix | 86 +++++++++++++++++++ .../host/materusPC/vm/win10/default.nix | 78 +++++++++++++++++ 5 files changed, 177 insertions(+), 34 deletions(-) create mode 100644 configurations/host/materusPC/vm/default.nix create mode 100644 configurations/host/materusPC/vm/win10/default.nix diff --git a/configurations/host/materusPC/default.nix b/configurations/host/materusPC/default.nix index 25c2fa4..05f891a 100644 --- a/configurations/host/materusPC/default.nix +++ b/configurations/host/materusPC/default.nix @@ -4,6 +4,7 @@ [ ./hardware + ./vm ./scripts.nix ./tmp.nix diff --git a/configurations/host/materusPC/hardware/boot.nix b/configurations/host/materusPC/hardware/boot.nix index 948560a..f8b6d37 100644 --- a/configurations/host/materusPC/hardware/boot.nix +++ b/configurations/host/materusPC/hardware/boot.nix @@ -3,7 +3,11 @@ #Kernel boot.kernelPackages = pkgs.linuxPackages_zen; boot.kernelParams = [ "nvme_core.default_ps_max_latency_us=0" "nvme_core.io_timeout=255" "nvme_core.max_retries=10" "nvme_core.shutdown_timeout=10" "amd_iommu=on" "iommu=pt" "pcie_acs_override=downstream,multifunction" ]; - boot.kernelModules = [ "i2c_dev" "kvm-amd" "vfio-pci" "v4l2loopback" "kvmfr" ]; + boot.kernelModules = [ "nbd" "i2c_dev" "kvm_amd" "vfio-pci" "v4l2loopback" "kvmfr" ]; + boot.extraModprobeConfig = '' + options kvm_amd nested=1 + options nbd max_part=16 + ''; boot.kernel.sysctl = {"vm.max_map_count" = 1000000;}; diff --git a/configurations/host/materusPC/tmp.nix b/configurations/host/materusPC/tmp.nix index 889530e..bfd00ae 100644 --- a/configurations/host/materusPC/tmp.nix +++ b/configurations/host/materusPC/tmp.nix @@ -23,7 +23,7 @@ in services.xserver.displayManager.startx.enable = true; services.teamviewer.enable = true; - + systemd.tmpfiles.rules = [ "L+ /opt/rocm/hip - - - - ${pkgs.hip}" ]; @@ -110,31 +110,7 @@ in qemu.runAsRoot = true; qemu.swtpm.enable = true; }; - virtualisation.libvirtd.qemu.package = pkgs.qemu_full; - systemd.services.libvirtd = { - path = - let - env = pkgs.buildEnv { - name = "qemu-hook-env"; - paths = with pkgs; [ - bash - libvirt - kmod - systemd - ripgrep - sd - coreutils - sudo - su - killall - procps - util-linux - bindfs - ]; - }; - in - [ env ]; - }; + users.users.materus = { isNormalUser = true; @@ -181,7 +157,10 @@ in services.pcscd.enable = true; services.samba-wsdd.enable = true; - services.samba.enable = true; + services.samba = { + enable = true; + package = pkgs.sambaFull; + }; programs.gnupg.agent = { @@ -271,10 +250,6 @@ in zip gzip - virtiofsd - config.virtualisation.libvirtd.qemu.package - looking-glass-client - tree mc lf @@ -315,8 +290,7 @@ in - virt-manager - libguestfs + bubblewrap bindfs diff --git a/configurations/host/materusPC/vm/default.nix b/configurations/host/materusPC/vm/default.nix new file mode 100644 index 0000000..800a71e --- /dev/null +++ b/configurations/host/materusPC/vm/default.nix @@ -0,0 +1,86 @@ +{ config, pkgs, lib, inputs, materusFlake, ... }: +{ + imports = [ + ./win10 + ]; + + system.activationScripts.libvirt-hooks.text = + '' + ln -Tfs /etc/libvirt/hooks /var/lib/libvirt/hooks + ''; + environment.etc."libvirt/hooks/qemu" = { + text = + '' + #!${pkgs.bash}/bin/bash + GUEST_NAME="''$1" + HOOK_NAME="''$2" + STATE_NAME="''$3" + MISC="''${@:4}" + + BASEDIR="''$(dirname ''$0)" + + HOOKPATH="''$BASEDIR/qemu.d/''$GUEST_NAME/''$HOOK_NAME/''$STATE_NAME" + + set -e # If a script exits with an error, we should as well. + + # check if it's a non-empty executable file + if [ -f "''$HOOKPATH" ] && [ -s "''$HOOKPATH"] && [ -x "''$HOOKPATH" ]; then + eval \"''$HOOKPATH\" "$@" + elif [ -d "''$HOOKPATH" ]; then + while read file; do + # check for null string + if [ ! -z "''$file" ]; then + eval \"''$file\" "''$@" + fi + done <<< "''$(find -L "''$HOOKPATH" -maxdepth 1 -type f -executable -print;)" + fi + ''; + mode = "0755"; + }; + + + virtualisation.libvirtd = { + enable = true; + onBoot = "ignore"; + onShutdown = "shutdown"; + qemu.ovmf.enable = true; + qemu.ovmf.packages = [ pkgs.OVMFFull.fd ]; + qemu.runAsRoot = true; + qemu.swtpm.enable = true; + qemu.package = pkgs.qemu_full; + }; + + environment.systemPackages = with pkgs; [ + virtiofsd + config.virtualisation.libvirtd.qemu.package + looking-glass-client + virt-manager + libguestfs-with-appliance + ]; + + systemd.services.libvirtd = { + path = + let + env = pkgs.buildEnv { + name = "qemu-hook-env"; + paths = with pkgs; [ + bash + libvirt + kmod + systemd + ripgrep + sd + coreutils + sudo + su + killall + procps + util-linux + bindfs + qemu-utils + ]; + }; + in + [ env ]; + }; +} diff --git a/configurations/host/materusPC/vm/win10/default.nix b/configurations/host/materusPC/vm/win10/default.nix new file mode 100644 index 0000000..c03da0f --- /dev/null +++ b/configurations/host/materusPC/vm/win10/default.nix @@ -0,0 +1,78 @@ +{ config, pkgs, lib, inputs, materusFlake, ... }: +{ + + + + environment.etc = { + "libvirt/hooks/kvm.conf" = { + text = + '' + VIRSH_GPU_VIDEO="0000:03:00.0" + VIRSH_GPU_AUDIO="0000:03:00.1" + ''; + mode = "0755"; + }; + + "libvirt/hooks/qemu.d/win10/prepare/begin/start.sh" = { + text = '' + #!${pkgs.bash}/bin/bash + source /etc/libvirt/hooks/kvm.conf + + systemctl stop mountWin10Share.service + + echo ''$VIRSH_GPU_VIDEO > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/driver/unbind" + echo ''$VIRSH_GPU_AUDIO > "/sys/bus/pci/devices/''${VIRSH_GPU_AUDIO}/driver/unbind" + + sleep 1s + + echo "8" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource0_resize" + echo "1" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource2_resize" + + ''; + mode = "0755"; + }; + + "libvirt/hooks/qemu.d/win10/release/end/stop.sh" = { + text = '' + #!${pkgs.bash}/bin/bash + source /etc/libvirt/hooks/kvm.conf + + + + echo ''$VIRSH_GPU_VIDEO > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/driver/unbind" + echo ''$VIRSH_GPU_AUDIO > "/sys/bus/pci/devices/''${VIRSH_GPU_AUDIO}/driver/unbind" + + sleep 1s + + echo "15" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource0_resize" + echo "8" > "/sys/bus/pci/devices/''${VIRSH_GPU_VIDEO}/resource2_resize" + + echo ''$VIRSH_GPU_VIDEO > /sys/bus/pci/drivers/amdgpu/bind + echo ''$VIRSH_GPU_AUDIO > /sys/bus/pci/drivers/snd_hda_intel/bind + + systemctl start mountWin10Share.service + + ''; + mode = "0755"; + }; + }; + + + systemd.services.mountWin10Share = { + wantedBy = [ "multi-user.target" ]; + path = [ config.virtualisation.libvirtd.qemu.package pkgs.util-linux pkgs.kmod pkgs.coreutils ]; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + script = '' + modprobe nbd max_part=16 + sleep 1 + qemu-nbd -c /dev/nbd0 /materus/data/VM/data.qcow2 --cache=unsafe --discard=unmap + sleep 1 + mount /dev/nbd0p1 /materus/data/Windows -o uid=1000,gid=100 + ''; + preStop = '' + umount /materus/data/Windows + qemu-nbd -d /dev/nbd0 + ''; + }; +}