Install Alpine on Amazon EC2

From Alpine Linux

The goal here is to have a "1GB" (the smallest possible) EBS 'virtual usb stick' that can boot and run Alpine Linux.

Create an EBS backed Alpine Linux AMI

Note: You need to do this process at least once in each availability region. EBS can't be shared between Ireland and California, for instance.
  • Create an Amazon instance in the desired availability region. A micro instance is fine - we will need it only long enough to create our EBS usb stick.
  • Create a new 1GB EBS volume
  • Attach the new volume to the running instance
  • The new volume will have a name like /dev/xvdf or such
  • Format the volume as ext4

    mke2fs -t ext4 /dev/xvdf

    Do not partition it - just format the whole volume
  • wget a x86_64 iso and extract it to the new volume. 32bit will not work.
wget http://dl-4.alpinelinux.org/alpine/v3.6/releases/x86_64/alpine-virt-3.6.2-x86_64.iso
mkdir target
mkdir source
mount /dev/xvdf target
mount -o loop alpine-virt-3.6.2-x86_64.iso source
cp -av source/boot target
cp -av source/apks target
umount source
  • Create a grub.conf on the new partition.
mkdir -p target/boot/grub
cat - >target/boot/grub/grub.conf <<EOF 
default=0
timeout=3
hiddenmenu

title Alpine Linux
root (hd0)
kernel /boot/vmlinuz-virthardened alpine_dev=xvda1:ext4 modules=loop,squashfs,sd-mod,ext4 console=hvc0 pax_nouderef BOOT_IMAGE=/boot/vmlinuz-virthardened
initrd /boot/initramfs-virthardened
EOF
  • Syslinux automatically adds BOOT_IMAGE to the kernel command line; grub does not, so make sure you specify it in the grub.conf
  • You do not need any other grub files.
  • symlink the grub.conf to menu.lst
ln -sf ./grub.conf target/boot/grub/menu.lst
  • Create an amazon.apkovl.tar.gz file to put on the target
    • This is probably easiest on a local alpine linux instance. Make sure the following are configured:
      • eth0 uses dhcp
      • networking is set to autostart
      • sshd is installed and set to autostart
      • Your ssh public key is in /root/.ssh/authorized_keys
      • The root password is set to something
      • lbu include root/.ssh
      • (optional) - Delete the /etc/ssh/*key* files, so they are created on the new box
    • lbu package amazon.apkovl.tar.gz

      Warning: If you are packaging on a 32bit box, manually delete etc/apk/arch from the apkovl.tar.gz file

    • Copy amazon.apkovl.tar.gz to target/
  • Unmount target
  • Do the following from the Amazon web interface
    • Detach the new volume
    • Make note of the volume ID
    • Launch NEW instance. Use defaults, amazon linux, micro; we are going to canibalize it in a bit, so defaults are fine here.
    • Once the instance starts, stop but do not terminate the instance.
    • Under EBS, detach the existing volume, and attach the alpine linux volume as /dev/sda1 (note the 1 at the end)
    • Restart the instance
  • Log in and make sure it works
  • Do any final cleanups necessary, and if necessary lbu ci
    • Only make configs that are appropriate for an AMI, we are going to snapshot this instance and create an AMI out of it
  • Again from the Amazon web interface
    • Delete the 8GB volume that is no longer needed
    • Stop but do not terminate the instance
    • Right click the stopped instance and choose 'Create Image (EBS AMI)'
      • Image name should be unique for the image - example AlpineLinux-2.4.5
      • Description can be anything - example 'Base AlpineLinux Installation - no services'
  • Done.