Skip to content

Instantly share code, notes, and snippets.

@sgtoj
Last active September 10, 2024 18:55
Show Gist options
  • Save sgtoj/8326221411eae1aa9c1183f8235620b2 to your computer and use it in GitHub Desktop.
Save sgtoj/8326221411eae1aa9c1183f8235620b2 to your computer and use it in GitHub Desktop.
Configure Raid for AWS Instance (NVME Supported)
#!/usr/bin/env bash
# ========================================================== configurations ===
RAID_NAME=ephemeral_raid
RAID_DEVICE=/dev/md0
RAID_MOUNT_PATH=/mnt/ephemeral
# =============================================================== functions ===
function list_instance_stores {
if [[ -e /dev/nvme0n1 ]]; then
# instance with nvme
instance_stores=( $(nvme list | awk '/Instance Storage/ {print $1}') )
else
# instance without nvme
BDMURL="http://169.254.169.254/latest/meta-data/block-device-mapping/"
osdevice=$(sudo lsblk -o NAME -n | grep -v '[[:digit:]]' | sed "s/^sd/xvd/g")
instance_stores=()
for bd in $(curl -s "${BDMURL}"); do
mapdevice=$(curl -s "${BDMURL}/${bd}/" | sed "s/^sd/xvd/g");
if grep -wq "${mapdevice}" <<< "${osdevice}"; then
instance_stores+=("${mapdevice}")
fi
done
fi
echo "${instance_stores[@]}"
}
function provision_instance_stores {
devices=( $(list_instance_stores) )
count=${#devices[@]}
mkdir -p ${RAID_MOUNT_PATH}
if [[ ${count} -eq 1 ]]; then
# only 1 device so only format and mount
mkfs.ext4 "${devices[1]}"
echo "${devices[1]}" ${RAID_MOUNT_PATH} ext4 defaults,noatime 0 2 >> /etc/fstab
elif [[ ${count} -gt 1 ]]; then
# config raid 0, format, and mount
mdadm --create --verbose --level=0 ${RAID_DEVICE} --auto=yes --name=${RAID_NAME} --raid-devices="${count}" "${devices[@]}"
mdadm --wait ${RAID_DEVICE}
mkfs.ext4 ${RAID_DEVICE}
mdadm --detail --scan >> /etc/mdadm.conf
dracut -H -f "/boot/initramfs-$(uname -r).img" "$(uname -r)"
echo "${RAID_DEVICE} ${RAID_MOUNT_PATH} ext4 defaults,noatime 0 2" >> /etc/fstab
fi
mount -a
}
@sgtoj
Copy link
Author

sgtoj commented Feb 25, 2019

Simple script to RAID 0 the available ephemeral instance storage devices of a AWS EC2 instance.. It supports instances with or without NVME devices (e.g. Nitro instance types). If only one instance storage device is found, it will only format and mount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment