summaryrefslogtreecommitdiffstats
path: root/nixos/modules/system/boot/kexec.nix
blob: d5272ff4da953ce5467eb745e8cd0f96a4d62f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.boot.kexec;
in
{
  options.boot.kexec = {
    enable = lib.mkEnableOption "kexec" // {
      default = lib.meta.availableOn pkgs.stdenv.hostPlatform pkgs.kexec-tools;
      defaultText = lib.literalExpression "lib.meta.availableOn pkgs.stdenv.hostPlatform pkgs.kexec-tools";
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ pkgs.kexec-tools ];

    systemd.services.prepare-kexec = {
      description = "Preparation for kexec";
      wantedBy = [ "kexec.target" ];
      before = [ "systemd-kexec.service" ];
      unitConfig.DefaultDependencies = false;
      serviceConfig.Type = "oneshot";
      path = [ pkgs.kexec-tools ];
      script = ''
        # Don't load the current system profile if we already have a kernel loaded
        if [[ 1 = "$(</sys/kernel/kexec_loaded)" ]] ; then
          echo "kexec kernel has already been loaded, prepare-kexec skipped"
          exit 0
        fi

        p=$(readlink -f /nix/var/nix/profiles/system)
        if ! [[ -d $p ]]; then
          echo "Could not find system profile for prepare-kexec"
          exit 1
        fi
        echo "Loading NixOS system via kexec."
        exec kexec --load "$p/kernel" --initrd="$p/initrd" --append="$(cat "$p/kernel-params") init=$p/init"
      '';
    };
  };
}