整理 kvm host

This commit is contained in:
2023-07-22 18:03:09 +08:00
parent 04e19884fe
commit 91e866b14d
5 changed files with 112 additions and 82 deletions

View File

@@ -222,6 +222,7 @@
{
waydroid.enable = true;
docker.enable = true;
kvmHost = { enable = true; gui = true; autoSuspend = [ "win10" "hardconnect" ]; };
};
};}
)
@@ -247,7 +248,6 @@
[ ./modules/users/root.nix {} ]
[ ./modules/users/chn.nix {} ]
./modules/virtualisation/kvm_guest.nix
./modules/virtualisation/kvm_host.nix
./modules/home/root.nix
./modules/home/chn.nix
]

View File

@@ -2,11 +2,6 @@ inputs:
{
config =
{
# modules auto loaded in stage2
boot.kernelModules = [ "kvm-intel" "br_netfilter" ];
# modules install but not auto loaded
# boot.extraModulePackages = [ yourmodulename ];
boot.extraModprobeConfig = "options kvm_intel nested=1";
# initrd, luks
boot.initrd.systemd.services."systemd-cryptsetup@swap" =
{

View File

@@ -17,6 +17,7 @@ inputs:
{
boot =
{
kernelModules = [ "br_netfilter" ];
initrd.availableKernelModules =
[
"ahci" "bfq" "nls_cp437" "nls_iso8859-1" "nvme" "sr_mod" "usbhid" "usb_storage" "virtio_blk" "virtio_pci"

View File

@@ -1,71 +0,0 @@
# TODO: disable auto usb redirection
inputs:
{
config =
{
virtualisation =
{
libvirtd = { enable = true; qemu.runAsRoot = false; onBoot = "ignore"; onShutdown = "shutdown"; };
spiceUSBRedirection.enable = true;
};
environment.systemPackages = with inputs.pkgs; [ qemu_full virt-manager win-spice ];
systemd.services =
let
virsh = "${inputs.pkgs.libvirt}/bin/virsh";
hibernate = inputs.pkgs.writeShellScript "libvirt-hibernate"
''
if [ "$(LANG=C ${virsh} domstate $1)" = 'running' ]
then
if ${virsh} dompmsuspend "$1" disk
then
echo "Waiting for $1 to suspend"
while ! [ "$(LANG=C ${virsh} domstate $1)" = 'shut off' ]
do
sleep 1
done
echo "$1 suspended"
touch "/tmp/libvirt.$1.suspended"
else
echo "Failed to suspend $1"
fi
fi
'';
resume = inputs.pkgs.writeShellScript "libvirt-resume"
''
if [ "$(LANG=C ${virsh} domstate $1)" = 'shut off' ] && [ -f "/tmp/libvirt.$1.suspended" ]
then
if ${virsh} start "$1"
then
echo "Waiting for $1 to resume"
while ! [ "$(LANG=C ${virsh} domstate $1)" = 'running' ]
do
sleep 1
done
echo "$1 resumed"
rm "/tmp/libvirt.$1.suspended"
else
echo "Failed to resume $1"
fi
fi
'';
makeServices = machine:
{
"libvirt-hibernate-${machine}" =
{
description = "libvirt hibernate ${machine}";
wantedBy = [ "systemd-hibernate.service" "systemd-suspend.service" ];
before = [ "systemd-hibernate.service" "systemd-suspend.service" ];
serviceConfig = { Type = "oneshot"; ExecStart = "${hibernate} ${machine}"; };
};
"libvirt-resume-${machine}" =
{
description = "libvirt resume ${machine}";
wantedBy = [ "systemd-hibernate.service" "systemd-suspend.service" ];
after = [ "systemd-hibernate.service" "systemd-suspend.service" ];
serviceConfig = { Type = "oneshot"; ExecStart = "${resume} ${machine}"; };
};
};
in
(makeServices "win10") // (makeServices "hardconnect");
};
}

View File

@@ -4,6 +4,12 @@ inputs:
{
waydroid.enable = mkOption { default = false; type = types.bool; };
docker.enable = mkOption { default = false; type = types.bool; };
kvmHost =
{
enable = mkOption { default = false; type = types.bool; };
gui = mkOption { default = false; type = types.bool; };
autoSuspend = mkOption { type = types.listOf types.string; };
};
};
config = let inherit (inputs.lib) mkMerge mkIf; in mkMerge
[
@@ -12,12 +18,111 @@ inputs:
# docker
(
mkIf inputs.config.nixos.virtualization.docker.enable { virtualisation.docker =
{
enable = true;
rootless = { enable = true; setSocketVariable = true; };
enableNvidia = true;
storageDriver = "overlay2";
};}
)
# kvmHost
(
mkIf inputs.config.nixos.virtualization.kvmHost.enable
{
boot =
{
enable = true;
rootless = { enable = true; setSocketVariable = true; };
enableNvidia = true;
storageDriver = "overlay2";
}; }
kernelModules =
let
modules =
{
intel = [ "kvm-intel" ];
amd = [];
};
in
builtins.concatLists (builtins.map (cpu: modules.${cpu}) inputs.config.nixos.hardware.cpu);
extraModprobeConfig =
let
configs =
{
intel = "options kvm_intel nested=1";
amd = "";
};
in
builtins.concatStringsSep "\n" (builtins.map (cpu: configs.${cpu}) inputs.config.nixos.hardware.cpu);
};
virtualisation =
{
libvirtd = { enable = true; qemu.runAsRoot = false; onBoot = "ignore"; onShutdown = "shutdown"; };
spiceUSBRedirection.enable = true;
};
environment.systemPackages = with inputs.pkgs; [ qemu_full win-spice ] ++
(if (inputs.config.nixos.virtualization.kvmHost.gui) then [ virt-manager ] else []);
systemd.services =
let
virsh = "${inputs.pkgs.libvirt}/bin/virsh";
hibernate = inputs.pkgs.writeShellScript "libvirt-hibernate" (inputs.localLib.stripeTabs
''
if [ "$(LANG=C ${virsh} domstate $1)" = 'running' ]
then
if ${virsh} dompmsuspend "$1" disk
then
echo "Waiting for $1 to suspend"
while ! [ "$(LANG=C ${virsh} domstate $1)" = 'shut off' ]
do
sleep 1
done
echo "$1 suspended"
touch "/tmp/libvirt.$1.suspended"
else
echo "Failed to suspend $1"
fi
fi
'');
resume = inputs.pkgs.writeShellScript "libvirt-resume" (inputs.localLib.stripeTabs
''
if [ "$(LANG=C ${virsh} domstate $1)" = 'shut off' ] && [ -f "/tmp/libvirt.$1.suspended" ]
then
if ${virsh} start "$1"
then
echo "Waiting for $1 to resume"
while ! [ "$(LANG=C ${virsh} domstate $1)" = 'running' ]
do
sleep 1
done
echo "$1 resumed"
rm "/tmp/libvirt.$1.suspended"
else
echo "Failed to resume $1"
fi
fi
'');
makeHibernate = machine:
{
name = "libvirt-hibernate-${machine}";
value =
{
description = "libvirt hibernate ${machine}";
wantedBy = [ "systemd-hibernate.service" "systemd-suspend.service" ];
before = [ "systemd-hibernate.service" "systemd-suspend.service" ];
serviceConfig = { Type = "oneshot"; ExecStart = "${hibernate} ${machine}"; };
};
};
makeResume = machine:
{
name = "libvirt-resume-${machine}";
value =
{
description = "libvirt resume ${machine}";
wantedBy = [ "systemd-hibernate.service" "systemd-suspend.service" ];
after = [ "systemd-hibernate.service" "systemd-suspend.service" ];
serviceConfig = { Type = "oneshot"; ExecStart = "${resume} ${machine}"; };
};
};
makeServices = serviceFunction: builtins.map serviceFunction
inputs.config.nixos.virtualization.kvmHost.autoSuspend;
in
builtins.listToAttrs (makeServices makeHibernate ++ makeServices makeResume);
}
)
];
}