Add option ec2.hvm, to set some boot configuration specific for EC2 HVM instances.

(cherry picked from commit 35c76d917307b7ac405486855cfe63021810dba5)

Conflicts:
	nixos/modules/virtualisation/amazon-image.nix
This commit is contained in:
Rob Vermaas 2014-05-21 10:55:34 +02:00
parent d05e832b14
commit 3da94435c0

View File

@ -1,164 +1,183 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib; with lib;
let
cfg = config.ec2;
in
{ {
imports = [ ../profiles/headless.nix ./ec2-data.nix ]; imports = [ ../profiles/headless.nix ./ec2-data.nix ];
system.build.amazonImage = options = {
pkgs.vmTools.runInLinuxVM ( ec2 = {
pkgs.runCommand "amazon-image" hvm = mkOption {
{ preVM = default = false;
'' description = ''
mkdir $out Whether the EC2 instance is a HVM instance.
diskImage=$out/nixos.img '';
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G" };
mv closure xchg/ };
''; };
buildInputs = [ pkgs.utillinux pkgs.perl ];
exportReferencesGraph =
[ "closure" config.system.build.toplevel ];
}
''
# Create an empty filesystem and mount it.
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
mkdir /mnt
mount /dev/vda /mnt
# The initrd expects these directories to exist. config = {
mkdir /mnt/dev /mnt/proc /mnt/sys system.build.amazonImage =
pkgs.vmTools.runInLinuxVM (
pkgs.runCommand "amazon-image"
{ preVM =
''
mkdir $out
diskImage=$out/nixos.img
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
mv closure xchg/
'';
buildInputs = [ pkgs.utillinux pkgs.perl ];
exportReferencesGraph =
[ "closure" config.system.build.toplevel ];
}
''
# Create an empty filesystem and mount it.
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
mkdir /mnt
mount /dev/vda /mnt
mount -o bind /proc /mnt/proc # The initrd expects these directories to exist.
mkdir /mnt/dev /mnt/proc /mnt/sys
# Copy all paths in the closure to the filesystem. mount -o bind /proc /mnt/proc
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
mkdir -p /mnt/nix/store # Copy all paths in the closure to the filesystem.
echo "copying everything (will take a while)..." storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
cp -prd $storePaths /mnt/nix/store/
# Register the paths in the Nix database. mkdir -p /mnt/nix/store
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \ echo "copying everything (will take a while)..."
chroot /mnt ${config.nix.package}/bin/nix-store --load-db cp -prd $storePaths /mnt/nix/store/
# Create the system profile to allow nixos-rebuild to work. # Register the paths in the Nix database.
chroot /mnt ${config.nix.package}/bin/nix-env \ printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel} chroot /mnt ${config.nix.package}/bin/nix-store --load-db
# `nixos-rebuild' requires an /etc/NIXOS. # Create the system profile to allow nixos-rebuild to work.
mkdir -p /mnt/etc chroot /mnt ${config.nix.package}/bin/nix-env \
touch /mnt/etc/NIXOS -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
# `switch-to-configuration' requires a /bin/sh # `nixos-rebuild' requires an /etc/NIXOS.
mkdir -p /mnt/bin mkdir -p /mnt/etc
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh touch /mnt/etc/NIXOS
# Install a configuration.nix. # `switch-to-configuration' requires a /bin/sh
mkdir -p /mnt/etc/nixos mkdir -p /mnt/bin
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
# Generate the GRUB menu. # Install a configuration.nix.
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot mkdir -p /mnt/etc/nixos
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix
umount /mnt/proc # Generate the GRUB menu.
umount /mnt chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
''
);
fileSystems."/".device = "/dev/disk/by-label/nixos"; umount /mnt/proc
umount /mnt
''
);
boot.initrd.kernelModules = [ "xen-blkfront" ]; fileSystems."/".device = "/dev/disk/by-label/nixos";
boot.kernelModules = [ "xen-netfront" ];
boot.kernelParams = [ "console=ttyS0" ];
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd. boot.initrd.kernelModules = [ "xen-blkfront" ];
boot.loader.grub.version = 1; boot.kernelModules = [ "xen-netfront" ];
boot.loader.grub.device = "nodev";
boot.loader.grub.timeout = 0;
boot.loader.grub.extraPerEntryConfig = "root (hd0)";
boot.initrd.postDeviceCommands = # Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
'' boot.loader.grub.version = 1;
# Force udev to exit to prevent random "Device or resource busy boot.loader.grub.device = if cfg.hvm then "/dev/xvda" else "nodev";
# while trying to open /dev/xvda" errors from fsck. boot.loader.grub.timeout = 0;
udevadm control --exit || true boot.loader.grub.extraPerEntryConfig = "root (hd0${lib.optionalString cfg.hvm ",0"})";
kill -9 -1
'';
# Mount all formatted ephemeral disks and activate all swap devices. boot.initrd.postDeviceCommands =
# We cannot do this with the fileSystems and swapDevices options ''
# because the set of devices is dependent on the instance type # Force udev to exit to prevent random "Device or resource busy
# (e.g. "m1.large" has one ephemeral filesystem and one swap device, # while trying to open /dev/xvda" errors from fsck.
# while "m1.large" has two ephemeral filesystems and no swap udevadm control --exit || true
# devices). Also, put /tmp and /var on /disk0, since it has a lot kill -9 -1
# more space than the root device. Similarly, "move" /nix to /disk0 '';
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
# Nix operations.
boot.initrd.postMountCommands =
''
diskNr=0
diskForUnionfs=
for device in /dev/xvd[abcde]*; do
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
fsType=$(blkid -o value -s TYPE "$device" || true)
if [ "$fsType" = swap ]; then
echo "activating swap device $device..."
swapon "$device" || true
elif [ "$fsType" = ext3 ]; then
mp="/disk$diskNr"
diskNr=$((diskNr + 1))
echo "mounting $device on $mp..."
if mountFS "$device" "$mp" "" ext3; then
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
fi
else
echo "skipping unknown device type $device"
fi
done
if [ -n "$diskForUnionfs" ]; then # Mount all formatted ephemeral disks and activate all swap devices.
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root # We cannot do this with the fileSystems and swapDevices options
# because the set of devices is dependent on the instance type
# (e.g. "m1.large" has one ephemeral filesystem and one swap device,
# while "m1.large" has two ephemeral filesystems and no swap
# devices). Also, put /tmp and /var on /disk0, since it has a lot
# more space than the root device. Similarly, "move" /nix to /disk0
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
# Nix operations.
boot.initrd.postMountCommands =
''
diskNr=0
diskForUnionfs=
for device in /dev/xvd[abcde]*; do
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
fsType=$(blkid -o value -s TYPE "$device" || true)
if [ "$fsType" = swap ]; then
echo "activating swap device $device..."
swapon "$device" || true
elif [ "$fsType" = ext3 ]; then
mp="/disk$diskNr"
diskNr=$((diskNr + 1))
echo "mounting $device on $mp..."
if mountFS "$device" "$mp" "" ext3; then
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
fi
else
echo "skipping unknown device type $device"
fi
done
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp if [ -n "$diskForUnionfs" ]; then
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
if [ ! -e $targetRoot/.ebs ]; then mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var
mkdir -p /unionfs-chroot/ro-nix if [ ! -e $targetRoot/.ebs ]; then
mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var
mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix mkdir -p /unionfs-chroot/ro-nix
mkdir -p /unionfs-chroot/rw-nix mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix
fi mkdir -p /unionfs-chroot/rw-nix
fi mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
'';
boot.initrd.extraUtilsCommands = unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
'' fi
# We need swapon in the initrd. fi
cp ${pkgs.utillinux}/sbin/swapon $out/bin '';
'';
# Don't put old configurations in the GRUB menu. The user has no boot.initrd.extraUtilsCommands =
# way to select them anyway. ''
boot.loader.grub.configurationLimit = 0; # We need swapon in the initrd.
cp ${pkgs.utillinux}/sbin/swapon $out/bin
'';
# Allow root logins only using the SSH key that the user specified # Don't put old configurations in the GRUB menu. The user has no
# at instance creation time. # way to select them anyway.
services.openssh.enable = true; boot.loader.grub.configurationLimit = 0;
services.openssh.permitRootLogin = "without-password";
# Force getting the hostname from EC2. # Allow root logins only using the SSH key that the user specified
networking.hostName = mkDefault ""; # at instance creation time.
services.openssh.enable = true;
services.openssh.permitRootLogin = "without-password";
# Always include cryptsetup so that Charon can use it. # Force getting the hostname from EC2.
environment.systemPackages = [ pkgs.cryptsetup ]; networking.hostName = mkDefault "";
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ]; # Always include cryptsetup so that Charon can use it.
environment.systemPackages = [ pkgs.cryptsetup ];
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
# Prevent logging in as root without a password. This doesn't really matter,
# since the only PAM services that allow logging in with a null
# password are local ones that are inaccessible on EC2 machines.
security.initialRootPassword = mkDefault "!";
};
} }