Skip to content

Instantly share code, notes, and snippets.

@valtoni
Last active February 25, 2021 20:50
Show Gist options
  • Save valtoni/5e806b11e288ed1cd38c42c5561b8741 to your computer and use it in GitHub Desktop.
Save valtoni/5e806b11e288ed1cd38c42c5561b8741 to your computer and use it in GitHub Desktop.
This script automatically mount ephemeral disks in EC2 nitro instances.
#!/usr/bin/env bash
# Mandatory: apt install nvme-cli
# Mount point (directory)
TARGET_MOUNT=/data/tmp
# Raid info
TARGET_RAID_DEVICE=/dev/md0
TARGET_RAID_LABEL=RAID0
storages=()
echo "*** Ephemeral mount has been started"
# Find ephemeral storages
for EPHEMERAL_DISK in $(sudo nvme list | grep 'Amazon EC2 NVMe Instance Storage' | awk '{ print $1 }'); do
storages+=($EPHEMERAL_DISK)
echo "Found ephemeral disk: $EPHEMERAL_DISK"
done
umount $TARGET_MOUNT
DEVICES="${#storages[@]}"
if [ "$DEVICES" == "" ] || [ "$DEVICES" == "0" ]; then
echo "This machine doesn't have any EPHEMERAL disk"
elif [ "$DEVICES" == "1" ]; then
echo "Only one EPHEMERAL DISK are found: create simple xfs (${storages[@]})"
mkfs.xfs -f -L DISK1 ${storages[@]}
mount ${storages[0]} $TARGET_MOUNT
else
echo "More than one EPHEMERAL DISK are found: creating RAID 0 between ${storages[@]}"
mdadm --stop $TARGET_RAID_DEVICE
mdadm --create --verbose $TARGET_RAID_DEVICE --level=0 --name=$TARGET_RAID_LABEL --raid-devices=${DEVICES} ${storages[@]}
mkfs.xfs -f -L $TARGET_RAID_LABEL $TARGET_RAID_DEVICE
#echo "UUID=$(/usr/sbin/blkid -s UUID -o value $TARGET_RAID_DEVICE) $TARGET_MOUNT  xfs  defaults,nofail  0  2" | sudo tee -a /etc/fstab
mount LABEL=$TARGET_RAID_LABEL $TARGET_MOUNT
fi
echo "*** Ephemeral mount ended"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment