Skip to content

Instantly share code, notes, and snippets.

@rickalm
Last active April 15, 2016 17:36
Show Gist options
  • Save rickalm/8e6bd76a90055871219404cd1c7e170b to your computer and use it in GitHub Desktop.
Save rickalm/8e6bd76a90055871219404cd1c7e170b to your computer and use it in GitHub Desktop.
Transform the /dev/xvd[b-z] devices on AWS to /dev/ephemeral[0-9]
#!/bin/bash
#
# this script will attempt to detect any ephemeral drives and create /dev/ephemeral[0-9] device names
#
# Beware, This script is NOT fully idempotent.
#
METADATA_URL_BASE="http://169.254.169.254/2012-01-12"
# figure out the nameing convention
root_drive=`df -h / | grep -v grep | awk 'NR==2{print $1}' | sed -re 's/[a-z][0-9]$//' `
if [ "$root_drive" == "/dev/xvd" ]; then
echo "Detected 'xvd' drive naming scheme (root: $root_drive)"
DRIVE_SCHEME='xvd'
else
echo "Detected 'sd' drive naming scheme (root: $root_drive)"
DRIVE_SCHEME='sd'
fi
# figure out how many ephemerals we have by querying the metadata API, and then:
# - convert the drive name returned from the API to the hosts DRIVE_SCHEME, if necessary
# - verify a matching device is available in /dev/
# - create the /dev/ephemeral[0-9] names
ephemerals=$(curl --silent $METADATA_URL_BASE/meta-data/block-device-mapping/ | grep ephemeral)
for e in $ephemerals; do
echo "Probing $e .."
device_name=$(curl --silent $METADATA_URL_BASE/meta-data/block-device-mapping/$e)
# might have to convert 'sdb' -> 'xvdb'
device_name=$(echo $device_name | sed "s/sd/$DRIVE_SCHEME/")
device_path="/dev/$device_name"
# test that the device actually exists since you can request more ephemeral drives than are available
# for an instance type and the meta-data API will happily tell you it exists when it really does not.
if [ -b $device_path ]; then
echo "Detected ephemeral disk: $device_path"
mkfs.ext4 -F $device_path
ln -s $device_path /dev/$e
else
echo "Ephemeral disk $e, $device_path is not present. skipping"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment