Skip to content

Instantly share code, notes, and snippets.

@wleepang
Last active January 11, 2020 01:45
Show Gist options
  • Save wleepang/528ceb82681028a7d02758cb8b9f115f to your computer and use it in GitHub Desktop.
Save wleepang/528ceb82681028a7d02758cb8b9f115f to your computer and use it in GitHub Desktop.
Detect and mount ephemeral storage
#!/bin/bash
set -e
function get_instance_storage_nvme() {
for nvme_device_name in `lsblk | cut --delimiter=" " --fields=1 | grep ^nvme | sort -n`; do
local nvme_device="/dev/${nvme_device_name}"
if ( nvme id-ctrl "${nvme_device}" | grep --quiet "^mn\\s*:\\s*Amazon EC2 NVMe Instance Storage\\s*$" ); then
echo "${nvme_device}"
fi
done
}
MOUNTPOINT=${1:-/ephemeral}
DEVICE=($(get_instance_storage_nvme))
echo "mountpoint for instance storage: $MOUNTPOINT"
echo "instance storage devices found: ${DEVICE[@]}"
if [ "${#DEVICE[@]}" -eq "0" ]; then
echo "no instance storage devices available"
exit 0
elif [ "${#DEVICE[@]}" -gt "1" ]; then
echo "${#DEVICE[@]} storage devices available, creating RAID0"
mdadm --create --level=0 /dev/md0 --verbose --raid-devices=${#DEVICE[@]} ${DEVICE[@]}
DEVICE=/dev/md0
fi
mkfs -t ext4 -V $DEVICE
mkdir -p $MOUNTPOINT
mount $DEVICE $MOUNTPOINT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment