Skip to content

Instantly share code, notes, and snippets.

@wallace57
Forked from sgtoj/config_aws_raid.sh
Created June 4, 2023 11:13
Show Gist options
  • Save wallace57/c63256ccfc901310a09104efd1c092b3 to your computer and use it in GitHub Desktop.
Save wallace57/c63256ccfc901310a09104efd1c092b3 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment