Skip to content

Commit

Permalink
fix(win10): problem with launch win10 vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Jun 22, 2024
1 parent d74fb72 commit 2ba64aa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
5 changes: 5 additions & 0 deletions hosts/pc/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
dev.lang.ides = [ "go" "sql" "fork" ];
hardware = {
sound.enable = true;
virtualization = {
enable = true;
enableWindowsVM = true;
};
grub = {
enable = true;
enableMultiBoot = true;
Expand All @@ -47,6 +51,7 @@
};
services = {
boinc.enable = true;
ssh.enable = true;
backup.enable = true;
rss.enable = true;
syncthing.enable = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

systemctl stop display-manager.service
systemctl stop boinc.serv
systemctl stop boinc.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ trap _revert ERR

# Stop display manager
systemctl stop display-manager.service
systemctl isolate multi-user.target

while systemctl is-active --quiet "display-manager.service"; do
sleep "1"
Expand All @@ -21,16 +20,10 @@ done
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind

# Unbind EFI-Framebuffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind

modprobe -r nvidia_uvm
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
modprobe -r i2c_nvidia_gpu
modprobe -r drm_kms_helper
modprobe -r drm

# Avoid a Race condition by waiting 2 seconds. This can be calibrated to be shorter or longer if required for your system
sleep 2
Expand All @@ -41,4 +34,3 @@ virsh nodedev-detach pci_0000_01_00_1

modprobe vfio_iommu_type1
modprobe vfio_pci
modprobe vfio_virqfd
24 changes: 13 additions & 11 deletions modules/hardware/virtualization/hooks/win10/release/end/revert.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
#!/usr/bin/env bash
set -x

function _reboot() {
reboot || systemctl reboot
}

# Reboot system cause "throw kernel panic". I think it's casues by nvidia proprietary driver
_reboot && exit 0

trap _reboot SIGINT SIGABRT SIGTERM ERR

# Disable VFIO
modprobe -r vfio_iommu_type1 vfio_pci vfio_virqfd
modprobe -r vfio_iommu_type1 vfio_pci

# Re-Bind GPU to Nvidia Driver
timeout 5s virsh nodedev-reattach pci_0000_01_00_0
timeout 5s virsh nodedev-reattach pci_0000_01_00_1

echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind

# Reload nvidia modules
modprobe drm
modprobe drm_kms_helper
modprobe i2c_nvidia_gpu
modprobe nvidia
modprobe nvidia_modeset
modprobe nvidia_drm
modprobe nvidia_uvm

nvidia-xconfig --query-gpu-info >/dev/null 2>&1
# Rebind VT consoles
echo 1 >/sys/class/vtconsole/vtcon0/bind || _reboot
echo 1 >/sys/class/vtconsole/vtcon1/bind || _reboot

# Restart Display Manager
systemctl start display-manager.service

# Rebind VT consoles
echo 1 >/sys/class/vtconsole/vtcon0/bind
echo 1 >/sys/class/vtconsole/vtcon1/bind
63 changes: 37 additions & 26 deletions modules/hardware/virutalization.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ in
};

config = mkIf cfg.enable {
virtualisation.libvirtd = {
enable = true;
onBoot = "ignore";
onShutdown = "shutdown";
qemu = {
package = unstable.qemu;
ovmf.enable = true;
runAsRoot = true;
virtualisation = {
spiceUSBRedirection.enable = true;
libvirtd = {
enable = true;
onBoot = "ignore";
onShutdown = "shutdown";
qemu = {
package = unstable.qemu;
ovmf.enable = true;
runAsRoot = true;
};
hooks.qemu.win10 = mkIf (cfg.enableWindowsVM) ./virtualization/hooks/win10;
};
hooks.qemu.win10 = mkIf (cfg.enableWindowsVM) ./virtualization/hooks/win10;
};

users.users.wittano.extraGroups = [ "libvirtd" ];
Expand All @@ -46,29 +49,37 @@ in
}];
}];

programs.virt-manager.enable = true;
programs = {
virt-manager.enable = true;
dconf.enable = true;
};

environment.systemPackages = with pkgs; [ libguestfs ];

systemd.services.libvirtd = mkIf cfg.enableWindowsVM {
path =
let
env = pkgs.buildEnv {
name = "qemu-hook-env";
paths = with pkgs; [ bash libvirt kmod systemd ripgrep sd ];
};
in
[ env ];
systemd = {
services = {
libvirtd = mkIf cfg.enableWindowsVM {
path =
let
env = pkgs.buildEnv {
name = "qemu-hook-env";
paths = with pkgs; [ bash libvirt kmod systemd ripgrep sd ];
};
in
[ env ];

preStart = mkIf (cfg.enableWindowsVM) ''
mkdir -p /var/lib/libvirt/vbios
preStart = mkIf (cfg.enableWindowsVM) ''
mkdir -p /var/lib/libvirt/vbios
ln -sf ${virutalizationDir."vibios.rom".source} /var/lib/libvirt/vbios/vibios.rom
'';
};
ln -sf ${virutalizationDir."vibios.rom".source} /var/lib/libvirt/vbios/vibios.rom
'';
};

systemd.services.pcscd.enable = mkIf cfg.enableWindowsVM false;
systemd.sockets.pcscd.enable = mkIf cfg.enableWindowsVM false;
pcscd.enable = mkIf cfg.enableWindowsVM false;
};

sockets.pcscd.enable = mkIf cfg.enableWindowsVM false;
};

boot = {
kernelParams = [ "intel_iommu=on" "iommu=pt" ];
Expand Down

0 comments on commit 2ba64aa

Please sign in to comment.