materusPC: add win10 vm

This commit is contained in:
Mateusz Słodkowicz 2023-07-03 21:49:46 +02:00
parent 9e7f2a36b3
commit 45562be071
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
5 changed files with 177 additions and 34 deletions

View File

@ -4,6 +4,7 @@
[
./hardware
./vm
./scripts.nix
./tmp.nix

View File

@ -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;};

View File

@ -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

View File

@ -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 ];
};
}

View File

@ -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
'';
};
}