summaryrefslogtreecommitdiffstats
path: root/nixos/maintainers/scripts/openstack/openstack-image-zfs.nix
blob: 580b67889d99ee94a29c395e9eebd3677ec7f8f0 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# nix-build '<nixpkgs/nixos>' -A config.system.build.openstackImage --arg configuration "{ imports = [ ./nixos/maintainers/scripts/openstack/openstack-image.nix ]; }"

{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib) mkOption types;
  copyChannel = true;
  cfg = config.openstackImage;
  imageBootMode = if config.openstack.efi then "uefi" else "legacy-bios";
in
{
  imports = [
    ../../../modules/virtualisation/openstack-config.nix
    ../../../modules/virtualisation/disk-size-option.nix
    ../../../modules/image/file-options.nix
    (lib.mkRenamedOptionModuleWith {
      sinceRelease = 2411;
      from = [
        "openstackImage"
        "sizeMB"
      ];
      to = [
        "virtualisation"
        "diskSize"
      ];
    })
    (lib.mkRenamedOptionModuleWith {
      sinceRelease = 2505;
      from = [
        "openstackImage"
        "name"
      ];
      to = [
        "image"
        "baseName"
      ];
    })

  ]
  ++ (lib.optional copyChannel ../../../modules/installer/cd-dvd/channel.nix);

  options.openstackImage = {
    ramMB = mkOption {
      type = types.int;
      default = (3 * 1024);
      description = "RAM allocation for build VM";
    };

    format = mkOption {
      type = types.enum [
        "raw"
        "qcow2"
      ];
      default = "qcow2";
      description = "The image format to output";
    };
  };

  config = {
    documentation.enable = copyChannel;
    openstack = {
      efi = true;
      zfs = {
        enable = true;
        datasets = {
          "tank/system/root".mount = "/";
          "tank/system/var".mount = "/var";
          "tank/local/nix".mount = "/nix";
          "tank/user/home".mount = "/home";
        };
      };
    };

    # Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
    # to avoid breaking existing configs using that.
    virtualisation.diskSize = lib.mkOverride 1490 (8 * 1024);
    virtualisation.diskSizeAutoSupported = false;

    image.extension = cfg.format;
    system.nixos.tags = [
      "openstack"
      "zfs"
    ];
    system.build.image = config.system.build.openstackImage;
    system.build.openstackImage = import ../../../lib/make-single-disk-zfs-image.nix {
      inherit lib config;
      inherit (cfg) contents format;
      name = config.image.baseName;
      pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package

      configFile = pkgs.writeText "configuration.nix" ''
        { modulesPath, ... }: {
          imports = [ "''${modulesPath}/virtualisation/openstack-config.nix" ];
          openstack.zfs.enable = true;
        }
      '';

      includeChannel = copyChannel;

      bootSize = 1000;
      memSize = cfg.ramMB;
      rootSize = config.virtualisation.diskSize;
      rootPoolProperties = {
        ashift = 12;
        autoexpand = "on";
      };

      datasets = config.openstack.zfs.datasets;

      postVM = ''
         extension=''${rootDiskImage##*.}
         friendlyName=$out/${config.image.baseName}
         rootDisk="$friendlyName.$extension"
         mv "$rootDiskImage" "$rootDisk"

         mkdir -p $out/nix-support
         echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products

        ${pkgs.jq}/bin/jq -n \
          --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
          --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
          --arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
          --arg boot_mode "${imageBootMode}" \
          --arg root "$rootDisk" \
         '{}
           | .label = $system_label
           | .boot_mode = $boot_mode
           | .system = $system
           | .disks.root.logical_bytes = $root_logical_bytes
           | .disks.root.file = $root
           ' > $out/nix-support/image-info.json
      '';
    };
  };
}