Created
May 16, 2017 19:17
-
-
Save trentonstrong/dfe71ee4f7da8b5c8a255d4646becee8 to your computer and use it in GitHub Desktop.
eh hehe
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 -ex | |
METADATA_URL_BASE="http://169.254.169.254/2012-01-12" | |
if [[ $# -eq 0 ]]; then | |
echo "Usage: mount-ephemeral.sh ROOT" | |
exit 1 | |
fi | |
ROOT=$1 | |
existing_count=$(find $ROOT -maxdepth 1 -type d | wc -l) | |
existing_count=$((existing_count - 1)) | |
if [ $existing_count -gt 0 ]; then | |
echo "Ephemeral drives already mounted, exiting." | |
exit 0 | |
fi | |
root_drive=`df -h | grep "/dev/xvda1" | awk 'NR==1{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) | |
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" | |
drives="$drives $device_path" | |
ephemeral_count=$((ephemeral_count + 1 )) | |
else | |
echo "Ephemeral disk $e, $device_path is not present. skipping" | |
fi | |
done | |
if [ "$ephemeral_count" = 0 ]; then | |
echo "FAILURE: No ephemeral disks detected!" | |
exit 1 | |
fi | |
# Unmount default ephemeral0 mount if it exists | |
umount /mnt || true | |
# Create filesystems on drives and mount | |
drive_count=0 | |
for drive in $drives; do | |
mkdir -p $ROOT/$drive_count | |
mkfs.xfs -f $drive | |
mount_point=$ROOT/$drive_count | |
mount -t xfs -o noatime $drive $mount_point | |
drive_count=$((drive_count + 1)) | |
echo "Mounted $drive to $mount_point" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment