I'm working a story at the moment where we need to take an AMI obtained from the AWS Marketplace and, amongst other things, amend the size of the disks attached to it. The AMI we obtain already has disks attached.
{
"builders": [
{
"type": "amazon-ebs",
"ami_name": "coop-packer-{{isotime | clean_ami_name}}",
"instance_type": "t2.medium",
"region": "eu-west-1",
"source_ami": "ami-0a1b2c3d",
"ssh_username": "ec2-user",
"launch_block_device_mappings": [
{
"device_name": "/dev/sda1",
"volume_type": "gp2",
"volume_size": "20",
"delete_on_termination": "true"
},
{
"device_name": "/dev/sdf",
"volume_type": "gp2",
"volume_size": "20",
"delete_on_termination": "false"
},
{
"device_name": "/dev/sdg",
"volume_type": "gp2",
"volume_size": "60",
"delete_on_termination": "false"
},
{
"device_name": "/dev/sdh",
"volume_type": "gp2",
"volume_size": "50",
"delete_on_termination": "false"
},
{
"device_name": "/dev/sdi",
"volume_type": "gp2",
"volume_size": "50",
"delete_on_termination": "false"
},
{
"device_name": "/dev/sdj",
"volume_type": "gp2",
"volume_size": "10",
"delete_on_termination": "false"
}
],
"ami_block_device_mappings": [
{
"device_name": "/dev/sdf",
"volume_type": "gp2",
"volume_size": "20",
"delete_on_termination": "true"
},
{
"device_name": "/dev/sdg",
"volume_type": "gp2",
"volume_size": "60",
"delete_on_termination": "true"
},
{
"device_name": "/dev/sdh",
"volume_type": "gp2",
"volume_size": "50",
"delete_on_termination": "true"
},
{
"device_name": "/dev/sdi",
"volume_type": "gp2",
"volume_size": "50",
"delete_on_termination": "true"
},
{
"device_name": "/dev/sdj",
"volume_type": "gp2",
"volume_size": "10",
"delete_on_termination": "true"
}
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "sudo {{.Vars}} sh {{.Path}}",
"script": "scripts/resize_partitions.sh"
},
{
"type": "shell",
"inline": [
"reboot",
"sleep 60"
]
},
{
"type": "shell",
"execute_command": "sudo {{.Vars}} sh {{.Path}}",
"script": "scripts/resize_filesystems.sh"
}
]
}
This works to a point - in that we get the first provisioner to at least run:
scripts/resize_partitions.sh
#!/bin/bash
set -ex
PARTITIONS=`cat /proc/partitions | awk '{ print $4 }' | awk 'length($0)==4' | grep -v 'xvda' | grep -v 'name'`
for partition in $PARTITIONS; do
sudo fdisk /dev/$partition << EEOF
d
n
p
w
EEOF
echo "Resized $partition successfully!"
sleep 30
exit 0
done
This was loosely cobbled together from various bits of experimentation and reading blog posts online, so it's not going to be perfect (iteration zero, right?!). The logic is hopefully reasonably simple: given the contents of /proc/partitions, print only the fourth column (to get rid of unnecessary stuff also included in that file), remove anything with a length less than four, and then exclude 'xvda' and 'name'. This gives me a list of partitions I want to resize. With these partitions, run fdisk on them, and provide the values inside the EEOF to fdisk.
For whatever reason, however, Packer runs this loop only once, so I get the first partition resized, but no others.
Any ideas on this one?
Thanks,
Andrew