Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

NixOS/amazonImageZfs: init

Introduce an AWS EC2 AMI which supports aarch64 and x86_64 with a ZFS
root.

This uses `make-zfs-image` which implies two EBS volumes are needed
inside EC2, one for boot, one for root. It should not matter which
is identified `xvda` and which is `xvdb`, though I have always
uploaded `boot` as `xvda`.

+165 -28
+12
nixos/maintainers/scripts/ec2/amazon-image-zfs.nix
··· 1 + { 2 + imports = [ ./amazon-image.nix ]; 3 + ec2.zfs = { 4 + enable = true; 5 + datasets = { 6 + "tank/system/root".mount = "/"; 7 + "tank/system/var".mount = "/var"; 8 + "tank/local/nix".mount = "/nix"; 9 + "tank/user/home".mount = "/home"; 10 + }; 11 + }; 12 + }
+78 -25
nixos/maintainers/scripts/ec2/amazon-image.nix
··· 4 4 5 5 let 6 6 cfg = config.amazonImage; 7 + 7 8 in { 8 9 9 10 imports = [ ../../../modules/virtualisation/amazon-image.nix ]; ··· 53 54 }; 54 55 }; 55 56 56 - config.system.build.amazonImage = import ../../../lib/make-disk-image.nix { 57 - inherit lib config; 58 - inherit (cfg) contents format name; 59 - pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package 60 - partitionTableType = if config.ec2.efi then "efi" 61 - else if config.ec2.hvm then "legacy+gpt" 62 - else "none"; 63 - diskSize = cfg.sizeMB; 64 - fsType = "ext4"; 57 + config.system.build.amazonImage = let 65 58 configFile = pkgs.writeText "configuration.nix" 66 59 '' 67 60 { modulesPath, ... }: { ··· 72 65 ${optionalString config.ec2.efi '' 73 66 ec2.efi = true; 74 67 ''} 68 + ${optionalString config.ec2.zfs.enable '' 69 + ec2.zfs.enable = true; 70 + networking.hostId = "${config.networking.hostId}"; 71 + ''} 75 72 } 76 73 ''; 77 - postVM = '' 78 - extension=''${diskImage##*.} 79 - friendlyName=$out/${cfg.name}.$extension 80 - mv "$diskImage" "$friendlyName" 81 - diskImage=$friendlyName 74 + 75 + zfsBuilder = import ../../../lib/make-zfs-image.nix { 76 + inherit lib config configFile; 77 + inherit (cfg) contents format name; 78 + pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package 79 + 80 + includeChannel = true; 81 + 82 + bootSize = 1000; # 1G is the minimum EBS volume 83 + 84 + rootSize = cfg.sizeMB; 85 + rootPoolProperties = { 86 + ashift = 12; 87 + autoexpand = "on"; 88 + }; 89 + 90 + datasets = config.ec2.zfs.datasets; 91 + 92 + postVM = '' 93 + extension=''${rootDiskImage##*.} 94 + friendlyName=$out/${cfg.name} 95 + rootDisk="$friendlyName.root.$extension" 96 + bootDisk="$friendlyName.boot.$extension" 97 + mv "$rootDiskImage" "$rootDisk" 98 + mv "$bootDiskImage" "$bootDisk" 99 + 100 + mkdir -p $out/nix-support 101 + echo "file ${cfg.format} $bootDisk" >> $out/nix-support/hydra-build-products 102 + echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products 103 + 104 + ${pkgs.jq}/bin/jq -n \ 105 + --arg label ${lib.escapeShellArg config.system.nixos.label} \ 106 + --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \ 107 + --arg root_logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$bootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \ 108 + --arg boot_logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \ 109 + --arg root "$rootDisk" \ 110 + --arg boot "$bootDisk" \ 111 + '$ARGS.named' \ 112 + > $out/nix-support/image-info.json 113 + ''; 114 + }; 115 + 116 + extBuilder = import ../../../lib/make-disk-image.nix { 117 + inherit lib config configFile; 118 + 119 + inherit (cfg) contents format name; 120 + pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package 121 + 122 + fsType = "ext4"; 123 + partitionTableType = if config.ec2.efi then "efi" 124 + else if config.ec2.hvm then "legacy+gpt" 125 + else "none"; 126 + 127 + diskSize = cfg.sizeMB; 128 + 129 + postVM = '' 130 + extension=''${diskImage##*.} 131 + friendlyName=$out/${cfg.name}.$extension 132 + mv "$diskImage" "$friendlyName" 133 + diskImage=$friendlyName 82 134 83 - mkdir -p $out/nix-support 84 - echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products 135 + mkdir -p $out/nix-support 136 + echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products 85 137 86 - ${pkgs.jq}/bin/jq -n \ 87 - --arg label ${lib.escapeShellArg config.system.nixos.label} \ 88 - --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \ 89 - --arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \ 90 - --arg file "$diskImage" \ 91 - '$ARGS.named' \ 92 - > $out/nix-support/image-info.json 93 - ''; 94 - }; 138 + ${pkgs.jq}/bin/jq -n \ 139 + --arg label ${lib.escapeShellArg config.system.nixos.label} \ 140 + --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \ 141 + --arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \ 142 + --arg file "$diskImage" \ 143 + '$ARGS.named' \ 144 + > $out/nix-support/image-info.json 145 + ''; 146 + }; 147 + in if config.ec2.zfs.enable then zfsBuilder else extBuilder; 95 148 }
+8 -2
nixos/modules/virtualisation/amazon-image.nix
··· 41 41 42 42 boot.growPartition = cfg.hvm; 43 43 44 - fileSystems."/" = { 44 + fileSystems."/" = mkIf (!cfg.zfs.enable) { 45 45 device = "/dev/disk/by-label/nixos"; 46 46 fsType = "ext4"; 47 47 autoResize = true; 48 48 }; 49 49 50 - fileSystems."/boot" = mkIf cfg.efi { 50 + fileSystems."/boot" = mkIf (cfg.efi || cfg.zfs.enable) { 51 + # The ZFS image uses a partition labeled ESP whether or not we're 52 + # booting with EFI. 51 53 device = "/dev/disk/by-label/ESP"; 52 54 fsType = "vfat"; 53 55 }; 56 + 57 + services.zfs.expandOnBoot = mkIf cfg.zfs.enable "all"; 58 + 59 + boot.zfs.devNodes = mkIf cfg.zfs.enable "/dev/"; 54 60 55 61 boot.extraModulePackages = [ 56 62 config.boot.kernelPackages.ena
+53 -1
nixos/modules/virtualisation/amazon-options.nix
··· 1 1 { config, lib, pkgs, ... }: 2 - { 2 + let 3 + inherit (lib) types; 4 + in { 3 5 options = { 4 6 ec2 = { 7 + zfs = { 8 + enable = lib.mkOption { 9 + default = false; 10 + internal = true; 11 + description = '' 12 + Whether the EC2 instance uses a ZFS root. 13 + ''; 14 + }; 15 + 16 + datasets = lib.mkOption { 17 + description = '' 18 + Datasets to create under the `tank` and `boot` zpools. 19 + 20 + **NOTE:** This option is used only at image creation time, and 21 + does not attempt to declaratively create or manage datasets 22 + on an existing system. 23 + ''; 24 + 25 + default = {}; 26 + 27 + type = types.attrsOf (types.submodule { 28 + options = { 29 + mount = lib.mkOption { 30 + description = "Where to mount this dataset."; 31 + type = types.nullOr types.string; 32 + default = null; 33 + }; 34 + 35 + properties = lib.mkOption { 36 + description = "Properties to set on this dataset."; 37 + type = types.attrsOf types.string; 38 + default = {}; 39 + }; 40 + }; 41 + }); 42 + }; 43 + }; 5 44 hvm = lib.mkOption { 6 45 default = lib.versionAtLeast config.system.stateVersion "17.03"; 7 46 internal = true; ··· 17 56 ''; 18 57 }; 19 58 }; 59 + }; 60 + 61 + config = lib.mkIf config.ec2.zfs.enable { 62 + networking.hostId = lib.mkDefault "00000000"; 63 + 64 + fileSystems = let 65 + mountable = lib.filterAttrs (_: value: ((value.mount or null) != null)) config.ec2.zfs.datasets; 66 + in lib.mapAttrs' 67 + (dataset: opts: lib.nameValuePair opts.mount { 68 + device = dataset; 69 + fsType = "zfs"; 70 + }) 71 + mountable; 20 72 }; 21 73 }
+14
nixos/release.nix
··· 217 217 }).config.system.build.amazonImage) 218 218 219 219 ); 220 + amazonImageZfs = forMatchingSystems [ "x86_64-linux" "aarch64-linux" ] (system: 221 + 222 + with import ./.. { inherit system; }; 223 + 224 + hydraJob ((import lib/eval-config.nix { 225 + inherit system; 226 + modules = 227 + [ configuration 228 + versionModule 229 + ./maintainers/scripts/ec2/amazon-image-zfs.nix 230 + ]; 231 + }).config.system.build.amazonImage) 232 + 233 + ); 220 234 221 235 222 236 # Test job for https://github.com/NixOS/nixpkgs/issues/121354 to test