From f33efd9f9e4f274a468e617ce3b87843b297efe1 Mon Sep 17 00:00:00 2001 From: Ganga Ram Date: Tue, 8 Oct 2024 11:36:24 +0400 Subject: [PATCH] display management in gui vm - Turn off the display when system is suspended and turn on when it resumes. Signed-off-by: Ganga Ram --- flake.lock | 8 ++--- flake.nix | 2 +- modules/givc/adminvm.nix | 2 ++ modules/givc/flake-module.nix | 4 +++ modules/givc/guivm.nix | 4 +++ modules/givc/host.nix | 3 ++ modules/hardware/common/qemu.nix | 3 -- modules/host/default.nix | 5 ++- .../microvm/virtualization/microvm/guivm.nix | 29 +++++++++++++++ .../virtualization/microvm/microvm-host.nix | 36 +++++++++++++++++++ 10 files changed, 87 insertions(+), 9 deletions(-) diff --git a/flake.lock b/flake.lock index 6a5da2334..ddfa18b4a 100644 --- a/flake.lock +++ b/flake.lock @@ -299,17 +299,17 @@ ] }, "locked": { - "lastModified": 1727957559, - "narHash": "sha256-X5yjuHZh7pu93xNaJlN0/H8KvA4oEs2lLQXc8u6J0yA=", + "lastModified": 1728912229, + "narHash": "sha256-OUZME5lZ4HkUQpZbwRA/hoVjRVck6Cq87A09oeUnCSg=", "owner": "tiiuae", "repo": "ghaf-givc", - "rev": "68079deac890d19d84389becc37805310fc0107f", + "rev": "8bf77b04420c5c70478eba27b877f5e83e6b438d", "type": "github" }, "original": { "owner": "tiiuae", "repo": "ghaf-givc", - "rev": "68079deac890d19d84389becc37805310fc0107f", + "rev": "8bf77b04420c5c70478eba27b877f5e83e6b438d", "type": "github" } }, diff --git a/flake.nix b/flake.nix index 5bdfb3460..ff53ef272 100644 --- a/flake.nix +++ b/flake.nix @@ -144,7 +144,7 @@ }; givc = { - url = "github:tiiuae/ghaf-givc/68079deac890d19d84389becc37805310fc0107f"; + url = "github:tiiuae/ghaf-givc/8bf77b04420c5c70478eba27b877f5e83e6b438d"; inputs = { nixpkgs.follows = "nixpkgs"; flake-parts.follows = "flake-parts"; diff --git a/modules/givc/adminvm.nix b/modules/givc/adminvm.nix index e501ea145..ca45067b7 100644 --- a/modules/givc/adminvm.nix +++ b/modules/givc/adminvm.nix @@ -23,6 +23,8 @@ in "givc-net-vm.service" "givc-gui-vm.service" "givc-audio-vm.service" + "display-suspend.service" + "display-resume.service" ]; tls.enable = config.ghaf.givc.enableTls; }; diff --git a/modules/givc/flake-module.nix b/modules/givc/flake-module.nix index 45969f8b5..1e9ae0e89 100644 --- a/modules/givc/flake-module.nix +++ b/modules/givc/flake-module.nix @@ -12,6 +12,10 @@ inputs.givc.nixosModules.host ./common.nix ./host.nix + { + # Include givc overlay to import app + nixpkgs.overlays = [ inputs.givc.overlays.default ]; + } ]; givc-guivm.imports = [ inputs.givc.nixosModules.sysvm diff --git a/modules/givc/guivm.nix b/modules/givc/guivm.nix index 4a7e8d737..db6ba8d0b 100644 --- a/modules/givc/guivm.nix +++ b/modules/givc/guivm.nix @@ -25,6 +25,10 @@ in port = "9000"; tls.enable = config.ghaf.givc.enableTls; admin = config.ghaf.givc.adminConfig; + services = [ + "display-suspend.service" + "display-resume.service" + ]; }; }; } diff --git a/modules/givc/host.nix b/modules/givc/host.nix index 6849accc9..183f9013a 100644 --- a/modules/givc/host.nix +++ b/modules/givc/host.nix @@ -31,8 +31,11 @@ in inherit addr; port = "9000"; services = [ + "microvm@admin-vm.service" "reboot.target" "poweroff.target" + "sleep.target" + "suspend.target" ] ++ map (vmName: "microvm@${vmName}.service") (attrNames config.microvm.vms); tls.enable = config.ghaf.givc.enableTls; admin = config.ghaf.givc.adminConfig; diff --git a/modules/hardware/common/qemu.nix b/modules/hardware/common/qemu.nix index a9ece51ac..5c627fe16 100644 --- a/modules/hardware/common/qemu.nix +++ b/modules/hardware/common/qemu.nix @@ -29,9 +29,6 @@ in ghaf.qemu.guivm = optionalAttrs (hasAttr "hardware" config.ghaf) { microvm.qemu.extraArgs = [ - # Button - "-device" - "button" # Battery "-device" "battery" diff --git a/modules/host/default.nix b/modules/host/default.nix index 5d2413b27..acfc79b79 100644 --- a/modules/host/default.nix +++ b/modules/host/default.nix @@ -3,7 +3,10 @@ # # Modules that should be only imported to host # -{ lib, ... }: +{ + lib, + ... +}: { networking.hostName = lib.mkDefault "ghaf-host"; diff --git a/modules/microvm/virtualization/microvm/guivm.nix b/modules/microvm/virtualization/microvm/guivm.nix index e7d0ee55a..ea0ca894c 100644 --- a/modules/microvm/virtualization/microvm/guivm.nix +++ b/modules/microvm/virtualization/microvm/guivm.nix @@ -89,6 +89,35 @@ let services.xdghandlers.enable = true; }; + # Adding below systemd services to save power by turning off display when system is suspended / lid close + systemd.services.display-suspend = { + enable = true; + description = "Display Suspend Service"; + serviceConfig = { + Type = "oneshot"; + ExecStart = '' + ${pkgs.wlopm}/bin/wlopm --off '*' + ''; + Environment = '' + WAYLAND_DISPLAY=/run/user/1000/wayland-0 + ''; + }; + }; + + systemd.services.display-resume = { + enable = true; + description = "Display Resume Service"; + serviceConfig = { + Type = "oneshot"; + ExecStart = '' + ${pkgs.wlopm}/bin/wlopm --on '*' + ''; + Environment = '' + WAYLAND_DISPLAY=/run/user/1000/wayland-0 + ''; + }; + }; + systemd.services."waypipe-ssh-keygen" = let keygenScript = pkgs.writeShellScriptBin "waypipe-ssh-keygen" '' diff --git a/modules/microvm/virtualization/microvm/microvm-host.nix b/modules/microvm/virtualization/microvm/microvm-host.nix index 4e8bbd1f2..8783cdfda 100644 --- a/modules/microvm/virtualization/microvm/microvm-host.nix +++ b/modules/microvm/virtualization/microvm/microvm-host.nix @@ -9,6 +9,21 @@ }: let cfg = config.ghaf.virtualization.microvm-host; + manageDisplay = + config.ghaf.givc.enable + && config.ghaf.virtualization.microvm.adminvm.enable + && config.ghaf.virtualization.microvm.guivm.enable; + + inherit (builtins) replaceStrings; + cliArgs = replaceStrings [ "\n" ] [ " " ] '' + --name ${config.ghaf.givc.adminConfig.name} + --addr ${config.ghaf.givc.adminConfig.addr} + --port ${config.ghaf.givc.adminConfig.port} + ${lib.optionalString config.ghaf.givc.enableTls "--cacert /run/givc/ca-cert.pem"} + ${lib.optionalString config.ghaf.givc.enableTls "--cert /run/givc/ghaf-host-cert.pem"} + ${lib.optionalString config.ghaf.givc.enableTls "--key /run/givc/ghaf-host-key.pem"} + ${lib.optionalString (!config.ghaf.givc.enableTls) "--notls"} + ''; in { imports = [ @@ -54,6 +69,27 @@ in withHardenedConfigs = true; }; ghaf.givc.host.enable = true; + systemd.services.display-suspend = lib.mkIf manageDisplay { + enable = true; + description = "Display Suspend Service"; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.givc-cli}/bin/givc-cli ${cliArgs} suspend"; + }; + wantedBy = [ "sleep.target" ]; + before = [ "sleep.target" ]; + }; + + systemd.services.display-resume = lib.mkIf manageDisplay { + enable = true; + description = "Display Resume Service"; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.givc-cli}/bin/givc-cli ${cliArgs} wakeup"; + }; + wantedBy = [ "suspend.target" ]; + after = [ "suspend.target" ]; + }; # TODO: remove hardcoded paths systemd.services."microvm@audio-vm".serviceConfig =