Saturday, November 26, 2016

Adding Additional Filesystems in Ubuntu Linux AMI with Packer


It took me too long to find all these pieces and figure this out so I thought I would share it to make it easier for the next person.  I needed to have packer create an AMI that includes an additional filesystem that is already formatted and mounted on system boot.  At first I tried using ami_block_device_mappings, but the new ebs volumes were not re-created when an instance was launched.  Then I found launch_block_device_mappings which causes an additional snapshot to be saved with the additional volume information.

In the amazon-ebs builder, I included a launch_block_device_mappings section with the details about the additional volume:

"builders": [
  {
    "type": "amazon-ebs",
    ...
    "launch_block_device_mappings": [
      {
        "device_name": "/dev/sde",
        "delete_on_termination": true,
        "volume_type": "gp2",
        "volume_size": "8"
      }
    ]

  }
],
Then I added a shell provisioner (it was the second one in the list) to format the filesystem and add it to /etc/fstab:
"provisioners": [
  ...
  {
    "type": "shell",
    "inline": [
      "sudo mkfs.ext4 /dev/xvde",
      "sudo mkdir -p /var/www",
      "echo '/dev/xvde /var/www ext4 defaults 1 2' | sudo dd of=/etc/fstab",
      "sudo mount /var/www"
    ]

  },
  ...
],
You may notice that the device names are different between the builder and provisioner.  The builder uses the AWS name for it (/dev/sde), while the provisioner commands are run on the linux box and must user the device names that linux uses (/dev/xvde).  I haven't looked in to this much but my guess is that it is just a matter of how the linux device driver wants to name the device.

When it is finished I ended up with an AMI that has two snapshots attached to it.  One of the snapshots was the root filesystem, and the other was the additional filesystem that we specified.  When I launch a new instance from the AMI I get two volumes... again one for the root and one for our additional filesystem, and the additional filesystem is mounted where I specified at /var/www.