summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2016-02-02 16:17:20 +0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2016-02-02 19:59:27 +0100
commit06731dfcae15287aa42a06fc5fdd0224c01ca08d (patch)
tree2cfb464a531c4741d61f3c35c493c5c8ae819ea7 /nixos
parent9725e067ffff0e738e72dde462a7435591698ff8 (diff)
downloadnixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar.gz
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar.bz2
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar.lz
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar.xz
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.tar.zst
nixlib-06731dfcae15287aa42a06fc5fdd0224c01ca08d.zip
ec2: Don't use ephemeral disks for /nix unionfs
This is a regression introduced by merging the EBS and S3 images. The
EBS images had a special marker /.ebs to prevent the initrd from using
ephemeral storage for the unionfs, but this marker was missing in the
consolidated image.

The fix is to check the file ami-manifest-path on the metadata server
to see if we're an S3-based instance. This does require networking in
the initrd.

Issue #12613.
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/virtualisation/amazon-image.nix43
1 files changed, 39 insertions, 4 deletions
diff --git a/nixos/modules/virtualisation/amazon-image.nix b/nixos/modules/virtualisation/amazon-image.nix
index 7cb37bbc4a76..dd2cdd43f31c 100644
--- a/nixos/modules/virtualisation/amazon-image.nix
+++ b/nixos/modules/virtualisation/amazon-image.nix
@@ -8,7 +8,28 @@
 
 with lib;
 
-let cfg = config.ec2; in
+let
+
+  cfg = config.ec2;
+
+  udhcpcScript = pkgs.writeScript "udhcp-script"
+    ''
+      #! /bin/sh
+      if [ "$1" = bound ]; then
+        ip address add "$ip/$mask" dev "$interface"
+        if [ -n "$router" ]; then
+          ip route add default via "$router" dev "$interface"
+        fi
+        if [ -n "$dns" ]; then
+          rm -f /etc/resolv.conf
+          for i in $dns; do
+            echo "nameserver $dns" >> /etc/resolv.conf
+          done
+        fi
+      fi
+    '';
+
+in
 
 {
   imports = [ ../profiles/headless.nix ./ec2-data.nix ./amazon-grow-partition.nix ./amazon-init.nix ];
@@ -20,8 +41,10 @@ let cfg = config.ec2; in
       autoResize = true;
     };
 
-    boot.initrd.kernelModules = [ "xen-blkfront" ];
-    boot.kernelModules = [ "xen-netfront" ];
+    boot.initrd.kernelModules =
+      [ "xen-blkfront" "xen-netfront"
+        "af_packet" # <- required by udhcpc
+      ];
     boot.kernelParams = mkIf cfg.hvm [ "console=ttyS0" ];
 
     # Prevent the nouveau kernel module from being loaded, as it
@@ -55,6 +78,18 @@ let cfg = config.ec2; in
     # Nix operations.
     boot.initrd.postMountCommands =
       ''
+        metaDir=$targetRoot/etc/ec2-metadata
+        mkdir -m 0755 $targetRoot/etc
+        mkdir -m 0700 -p "$metaDir"
+
+        echo "getting EC2 instance metadata..."
+        ip link set eth0 up
+        udhcpc --interface eth0 --quit --now --script ${udhcpcScript}
+
+        if ! [ -e "$metaDir/ami-manifest-path" ]; then
+          wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
+        fi
+
         diskNr=0
         diskForUnionfs=
         for device in /dev/xvd[abcde]*; do
@@ -85,7 +120,7 @@ let cfg = config.ec2; in
             mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
             mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
 
-            if [ ! -e $targetRoot/.ebs ]; then
+            if [ "$(cat "$metaDir/ami-manifest-path")" != "(unknown)" ]; then
                 mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var
                 mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var