Created
July 8, 2013 05:33
-
-
Save kppullin/5946427 to your computer and use it in GitHub Desktop.
EC2 Ephemeral Swap Helper The intent here is to initialize and active a swap partition from the local ephemeral disk, if any.
Generally using an ephemeral disk is faster than EBS and you don't incur EBS IOPS overhead (read: cost) for swappy systems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Create a swap partition equal to the system RAM | |
# on the first ephemeral disk. | |
# | |
function activate_swap_on_device { | |
swap_on=`swapon -s | grep 'dev' | wc -l` | |
if [[ ${swap_on} -gt 0 ]]; then | |
echo "At least one swap partition is already active. Doing nothing (we assume zero or one only)" | |
return 3 | |
fi | |
swap_list=`sfdisk -l $1` | |
swap_check_status=$? | |
swap_dev=`echo "${swap_list}" | grep -i "linux swap" | awk '{print $1}'` | |
swap_count=`echo "${swap_dev}" | wc -l` | |
# note - it'd be nice to check if the swap partition is already active | |
if [[ ${swap_check_status} -eq 0 ]] && [[ ${swap_count} == 1 ]] | |
then | |
echo "Initializing swap on '${swap_dev}'" | |
mkswap ${swap_dev} | |
echo "Activating swap on '${swap_dev}'" | |
swapon ${swap_dev} | |
return 0 | |
elif [[ ${swap_check_status} -ne 0 ]]; then | |
echo "Error checking for swap partitions on '$1'" | |
return 2 | |
else | |
echo "Found ${swap_count} swap paritions on '$1'. Expected one - doing nothing" | |
return 1 | |
fi | |
} | |
device=`wget -q -O - http://169.254.169.254/latest/meta-data/block-device-mapping/ephemeral0` | |
device="/dev/${device}" | |
echo "Looking for block device '${device}'" | |
if [[ ! -b "${device}" ]]; then | |
echo "Block device '${device}' not found. Looking for xvdX named device" | |
device_id=`echo "${device}" | rev | cut -c 1` | |
device_id_ord=`printf '%d' "'${device_id}"` | |
device_id_ord=$(( ${device_id_ord} + 4 )) # add four to map A->E, B->F, etc | |
device_id_oct=`printf '%03o' ${device_id_ord}` | |
device_id=`printf \\\\${device_id_oct}` | |
device="/dev/xvd$device_id" | |
echo "Looking for block device '${device}'" | |
if [[ ! -b "${device}" ]]; then | |
echo "Unable to find device. Bailing out..." | |
exit | |
fi | |
fi | |
echo "Found block device '${device}'" | |
sfdisk=`sfdisk -l ${device}` | |
sfdisk_empty_partitions=`echo "${sfdisk}" | grep -i "empty" | wc -l` | |
if [[ ${sfdisk} =~ "#blocks" ]] && [[ ${sfdisk_empty_partitions} -ne 4 ]] | |
then | |
echo "Existing partitions found on '${device}'. Attempting to active existing swap (if any) " | |
activate_swap_on_device ${device} | |
echo "Done" | |
exit | |
fi | |
swap_size=`grep MemTotal /proc/meminfo | awk '{print $2}'` | |
echo "Creating a swap parition of size ${swap_size}KB" | |
parted --align optimal --script -- ${device} mklabel msdos | |
parted --align optimal --script -- ${device} mkpart primary linux-swap 0 ${swap_size}KB | |
echo "Activating swap on '${device}'" | |
activate_swap_on_device ${device} | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment