Question re Packer AWS EBS builder, when specifying volume_size

2,495 views
Skip to first unread message

JDS

unread,
Mar 30, 2015, 12:18:38 PM3/30/15
to packe...@googlegroups.com
I have a Packer template that I thought would specify the volume size of the root volume on the resulting instance. It sort of does, but not quite.

What I mean is

1) user template, create AMI
2) Launch instance from that new AMI
3) Expected result: root volume on that instance is the size specfied in template

Template has EBS options like so:

            "ami_block_device_mappings": [{
                 "device_name": "/dev/sda1",
                 "volume_type": "gp2",
                 "delete_on_termination": true,
                 "volume_size": 20
            }],
            "launch_block_device_mappings": [{
                "device_name": "/dev/sda1",
                "volume_type": "gp2",
                "delete_on_termination": true,
                "volume_size": 20
            }],

The resulting instance, launched with the option "--block-device-mappings "[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"VolumeSize\":20,\"DeleteOnTermination\":true}}]"", looks like this:

[root@ip-10-78-0-43 ~]# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xeafa2de4

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1               1         652     5237158+  83  Linux
[root@ip-10-78-0-43 ~]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  20G  0 disk
└─xvda1 202:1    0   5G  0 part /

(I bolded the important bits)

In other words, I am launching the instance and specifying 20G root volume at launch. The instance does get a 20G root volume, but the extents of the root partition are only the 5G that is set in the original AMI I used to build via Packer.

I was able to use fidsk and resize2fs to resize the partition to the full extent of the volume, but only manually. I want Packer to take care of this.

I am probably a little confused. For example: what is the difference between "ami_block_device_mappings" and "launch_block_device_mappings"? From the names, I expect that the former is what is baked into the AMI; the latter is what is used when instances are launched from the AMI.

Also, I have been using "/dev/sda1" in my Packer template, but the resulting AMI gets "/dev/xvda1" as the root device. Should I use "xvda1" instead?

Finally, should Packer be the one doing this resizing? Or should some scripted process do it on my end?

Please make recommendations.

Thanks,
JDS

JDS

unread,
Apr 1, 2015, 9:55:38 AM4/1/15
to packe...@googlegroups.com
For closure, here's my solution. Maybe this will help someone.

First of all, the volume mappings in the build template do appear to work. It's just that the *partition* size is set in the base AMI I'm using, and that doesn't seem to be adjustable via Packer.

To make this work, I passed in an fdisk batch via cloud-init.

Basically, add this to the cloud-init metadata (the "Advanced" section if you are launching an EC2 instance manually/via the web UI). If you are using AWS CLI tools to launch this instance, this can be passed in as base64-encoded string to the argument "--user-data".

Here's my little script to generate the script that cloud-init gets. The script that cloud-init actually gets does this:

1) grow root partition to full size of volume, using fdisk
2) sets flag to indicate that resize2fs needs to hit the grown partition (i.e. needs to grow the FS to match the new partition size)
3) sets /etc/rc.local to look for that flag; runs resize2fs and deletes the flag

The base AMI I'm using this is a "vanilla" RHEL6 AMI from Bashton. YMMV

This script's biggest flaw is hardcoded volume/partition names.

Script, including the base64 encoding:

#!/bin/bash

cat <<EOF|base64
#!/bin/bash

# This is just a test to make sure cloud-init is picking this up
( echo -n "# Output generated on " ; date )|tee /var/tmp/cloudinit.out
echo "Running cloud-init..." |tee -a /var/tmp/cloudinit.out

echo "==> INFO: Growing root partition to size of volume"
fdisk /dev/xvda <<EEOF
d
n
p
1
1

w
EEOF

echo "==> INFO: Touching tempfile /var/tmp/do-resize2fs"
touch /var/tmp/do-resize2fs

echo "==> INFO: Preparing rc.local to resize root partition on first boot"
echo "if [ -f /var/tmp/do-resize2fs ]; then
    rm /var/tmp/do-resize2fs
    resize2fs /dev/xvda1
fi" | tee -a /etc/rc.local

echo "==> INFO: Rebooting..."
reboot

EOF

Thanks,
JDS
Reply all
Reply to author
Forward
0 new messages