summary refs log tree commit diff
diff options
context:
space:
mode:
authorDan Peebles <pumpkin@me.com>2017-02-22 23:47:24 +0000
committerDan Peebles <pumpkin@me.com>2017-02-22 23:49:49 +0000
commit49641e0de006fdc5edd11292b1e94410c3bb8d79 (patch)
tree180b17aa8e5df508f4cecdb8306c91cba4352887
parentdf4c0aeff864f8d3308afc1f4386895fcd3e990d (diff)
downloadnixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.gz
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.bz2
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.lz
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.xz
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.zst
nixpkgs-49641e0de006fdc5edd11292b1e94410c3bb8d79.zip
make-disk-image.nix: support additional filesystem contents
This makes make-disk-image.nix slightly more consistent with other image
builders we have. Unfortunately I duplicated some code in doing so, but
this is temporary duplication on the path to consolidating everything.
See https://github.com/NixOS/nixpkgs/issues/23052 for more details on that.

I'm also exposing the option in the amazon-image.nix maintainer module.
-rw-r--r--nixos/lib/make-disk-image.nix47
-rw-r--r--nixos/maintainers/scripts/ec2/amazon-image.nix23
2 files changed, 67 insertions, 3 deletions
diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix
index e279803f2ea..d9d76b5919a 100644
--- a/nixos/lib/make-disk-image.nix
+++ b/nixos/lib/make-disk-image.nix
@@ -7,6 +7,12 @@
 , # The size of the disk, in megabytes.
   diskSize
 
+  # The files and directories to be placed in the target file system.
+  # This is a list of attribute sets {source, target} where `source'
+  # is the file system object (regular file or directory) to be
+  # grafted in the file system at path `target'.
+, contents ? []
+
 , # Whether the disk should be partitioned (with a single partition
   # containing the root filesystem) or contain the root filesystem
   # directly.
@@ -45,7 +51,14 @@ pkgs.vmTools.runInLinuxVM (
           ${pkgs.vmTools.qemu}/bin/qemu-img create -f ${format} $diskImage "${toString diskSize}M"
           mv closure xchg/
         '';
-      buildInputs = [ pkgs.utillinux pkgs.perl pkgs.e2fsprogs pkgs.parted ];
+      buildInputs = with pkgs; [ utillinux perl e2fsprogs parted rsync ];
+
+      # I'm preserving the line below because I'm going to search for it across nixpkgs to consolidate
+      # image building logic. The comment right below this now appears in 4 different places in nixpkgs :)
+      # !!! should use XML.
+      sources = map (x: x.source) contents;
+      targets = map (x: x.target) contents;
+
       exportReferencesGraph =
         [ "closure" config.system.build.toplevel ];
       inherit postVM;
@@ -98,6 +111,38 @@ pkgs.vmTools.runInLinuxVM (
       # Remove /etc/machine-id so that each machine cloning this image will get its own id
       rm -f /mnt/etc/machine-id
 
+      # Copy arbitrary other files into the image
+      # Semi-shamelessly copied from make-etc.sh. I (@copumpkin) shall factor this stuff out as part of
+      # https://github.com/NixOS/nixpkgs/issues/23052.
+      set -f
+      sources_=($sources)
+      targets_=($targets)
+      set +f
+
+      for ((i = 0; i < ''${#targets_[@]}; i++)); do
+        source="''${sources_[$i]}"
+        target="''${targets_[$i]}"
+
+        if [[ "$source" =~ '*' ]]; then
+
+          # If the source name contains '*', perform globbing.
+          mkdir -p /mnt/$target
+          for fn in $source; do
+            rsync -a --no-o --no-g "$fn" /mnt/$target/
+          done
+
+        else
+
+          mkdir -p /mnt/$(dirname $target)
+          if ! [ -e /mnt/$target ]; then
+            rsync -a --no-o --no-g $source /mnt/$target
+          else
+            echo "duplicate entry $target -> $source"
+            exit 1
+          fi
+        fi
+      done
+
       umount /mnt
 
       # Make sure resize2fs works
diff --git a/nixos/maintainers/scripts/ec2/amazon-image.nix b/nixos/maintainers/scripts/ec2/amazon-image.nix
index bfa4f4b3ca5..b4190df8335 100644
--- a/nixos/maintainers/scripts/ec2/amazon-image.nix
+++ b/nixos/maintainers/scripts/ec2/amazon-image.nix
@@ -2,15 +2,34 @@
 
 with lib;
 
-{
+let
+  cfg = config.amazonImage;
+in {
 
   imports =
     [ ../../../modules/installer/cd-dvd/channel.nix
       ../../../modules/virtualisation/amazon-image.nix
     ];
 
-  system.build.amazonImage = import ../../../lib/make-disk-image.nix {
+  options.amazonImage = {
+    contents = mkOption {
+      example = literalExample ''
+        [ { source = pkgs.memtest86 + "/memtest.bin";
+            target = "boot/memtest.bin";
+          }
+        ]
+      '';
+      default = [];
+      description = ''
+        This option lists files to be copied to fixed locations in the
+        generated image. Glob patterns work.
+      '';
+    };
+  };
+
+  config.system.build.amazonImage = import ../../../lib/make-disk-image.nix {
     inherit lib config;
+    inherit (cfg) contents;
     pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
     partitioned = config.ec2.hvm;
     diskSize = if config.ec2.hvm then 2048 else 8192;