Skip to content

Instantly share code, notes, and snippets.

@hrzbrg
Created June 23, 2017 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrzbrg/dfeccafa1f60f46a2cb1090001737abc to your computer and use it in GitHub Desktop.
Save hrzbrg/dfeccafa1f60f46a2cb1090001737abc to your computer and use it in GitHub Desktop.
#!/bin/bash -ue
# this script will attempt to detect any ephemeral drives on an EC2 node
mount_point=${1:-"/mnt/storage"}
METADATA_URL_BASE="http://169.254.169.254/latest"
INSTANCEID=$(wget -q -O - ${METADATA_URL_BASE}/meta-data/instance-id)
# Configure Raid - take into account xvdb or sdb
root_drive=$(df -h | grep -P '^.*/$' | awk 'NF==6{print $1}')
if [ "${root_drive}" == "/dev/xvda1" ]; 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/
drives=""
ephemeral_count=0
ephemerals=$(curl --silent ${METADATA_URL_BASE}/meta-data/block-device-mapping/ | grep ephemeral)
# see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
for e in ${ephemerals}; do
echo "Probing ${e} .."
device_name=$(curl --silent ${METADATA_URL_BASE}/meta-data/block-device-mapping/${e})
if [ ! -z "${device_name}" ]; then
echo "meta-data API thinks there is a disk here: ${device_name}"
fi
# might have to convert 'sdb' -> 'xvdb'
device_name=$(echo ${device_name} | sed "s/sd/$DRIVE_SCHEME/")
device_path="/dev/${device_name}"
echo "Converting device name from ${device_name} to ${device_path}"
# 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}"
drives="$drives ${device_path}"
ephemeral_count=$((ephemeral_count + 1 ))
else
echo "Could not find the disk's device path ($device_path). Let's check if we find some NVME disks..."
declare -a DEVICES=($(find /dev -type b -name nvme[0-7]n1 | awk -F/ '{print $3}'))
for i in "${DEVICES[@]}"; do
device_name=${i}
device_path=$(readlink -f "/dev/${i}")
# test again if it
if [ -z "${device_name}" ]; then
echo "Ephemeral disk ${e}, ${device_path} is not present. skipping"
else
echo "Detected ephemeral disk: ${device_path}"
drives="$drives ${device_path}"
ephemeral_count=$((ephemeral_count + 1 ))
fi
done
fi
done
# need at least one disk
if [ "${ephemeral_count}" = 0 ]; then
echo "No ephemeral disk detected. exiting"
exit 0
fi
# umount mount point if mounted
if [[ $(cat /proc/mounts | grep ${mount_point}) ]]
then
umount ${mount_point}
sed -i '/\/mnt\/storage/d' /etc/fstab
fi
# create the new mount point if needed
if [[ ! -e ${mount_point} ]]
then
mkdir -p ${mount_point}
fi
# Configure drives
for drive in ${drives}; do
# wipe, create, tune and mount
dd if=/dev/zero of=${drive} bs=4096 count=1024
mkfs -t ext4 ${drive}
tune2fs -o journal_data_writeback ${drive}
tune2fs -O ^has_journal ${drive}
e2fsck -p -f ${drive}
mount -t ext4 -o noatime ${drive} ${mount_point}
# edit fstab
udid=$(blkid | grep ${drive} | awk -F ' ' '{print $2}' | tr -d \")
echo "${udid} ${mount_point} ext4 defaults,noatime,nofail 0 0" | tee -a /etc/fstab
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment