In a recent upgrade to the new generation of instances we faced an interesting conundrum. Previous generations came with quite the amount of disk spaces. Usually instance stores are mounted on /mnt. And it is all good and working. The best part, one can leave the default settings for the first instance store and do anything with the second. And “anything” translated to enabling swap on the second instance store. With the new instance types, however the number (and the size) of the instance stores is reduced. It is SSD, but m2.4xlarge comes with 2 x 840 GB, while the equivalent in the last generation, r3.2xlarge, comes with only one 160 GB instance store partition.
Not a problem, just a challenge!
We prefer to use UserData for automatic server setup. After some attempts it became clear that partitioning disks from a shell script is not exactly trivial tasks under Linux in AWS. BSD-based operating systems come with disklabel and fdisk and those will do the job. Linux comes with fdisk by default and that tool is somewhat limited …
Luckily, fdisk reads data from stdin so quick-and-dirty solution quickly emerged!
The following UserData is used to modify the instance store of a m3.large instance, creating 8GB swap partition and re-mounting the rest as /mnt:
# Mark execution start
echo "STARTING" > /root/user_data_run
# Unmount /dev/xvdb if already mounted
umount -f /dev/xvdb
# Partition the disk (8GB for SWAP / Rest for /mnt)
(echo n; echo p; echo 1; echo 2048; echo +8G; echo t; echo 82; echo n; echo p; echo 2; echo; echo; echo w) | fdisk /dev/xvdb
# Make and enable swap
# Make /mnt partition and mount it
mount /dev/xvdb2 /mnt
sed -i s/xvdb/xvdb2/g /etc/fstab
# Mark execution end
echo "DONE" > /root/user_data_run
Execute it with AWS CLI (Using stock Ubuntu 14.04 HVM AMI):
aws ec2 run-instances --image-id ami-1d8c9574 --count 1 --instance-type m3.large --key-name test-key --security-groups test-sg --user-data file://userdata.sh
:~> ssh email@example.com "df -h"
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 7.8G 765M 6.6G 11% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 3.7G 12K 3.7G 1% /dev
tmpfs 749M 336K 748M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 3.7G 0 3.7G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/xvdb2 22G 44M 21G 1% /mnt
:~> ssh firstname.lastname@example.org "free -h"
total used free shared buffers cached
Mem: 7.3G 276M 7.0G 352K 8.6M 177M
-/+ buffers/cache: 90M 7.2G
Swap: 8.0G 0B 8.0G
There it is, 8GB swap partition (/dev/xvdb1) and the rest (/dev/xvdb2) mounted as /mnt. Note that /etc/fstab is also updated to account for the device name change!
- Small Tip: How to use –block-device-mappings to manage instance volumes with AWS CLI
- Small Tip: Use AWS CLI to create instances with bigger root partitions
- Small Tip: How to use AWS CLI ‘–filter’ parameter
- Small Tip: How to use AWS CLI to start Spot instances with UserData
- Small Tip: AWS announces T2 instance types