Last active
February 15, 2021 15:52
-
-
Save tcprescott/31fe86f82b3d966db6e78e0c4026c2da to your computer and use it in GitHub Desktop.
ec2 format and mount ephemeral instance store disks
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 | |
#set -e | |
#set -o pipefail | |
### | |
# This script will strip together all disk to create a raid device at /dev/md0 | |
# EC2 instances with a single instance store disk will instead use the raw nvme device | |
# | |
# Requires these packages: lvm2 jq mdadm nvme-cli | |
### | |
# Configure stuff here | |
VGNAME=EphemeralData | |
VOL1_MOUNTPOINT=/sastmp/saswork | |
VOL1_LVNAME=saswork | |
VOL2_MOUNTPOINT=/sastmp/sascache | |
VOL2_LVNAME=sascache | |
### No need to modify anything after this line | |
# Get a list of ephemeral storage devices using the nvme cli | |
DEVLIST=$(nvme list --output-format=json | jq -r '.Devices | to_entries[] | .value | select(.ModelNumber == "Amazon EC2 NVMe Instance Storage") | .DevicePath') | |
# Get a count of those devices | |
NUMDEV=$(echo $DEVLIST | wc -w) | |
if [ $NUMDEV -eq 0 ] | |
then | |
# No ephermal storage devices found. We're going to have the script exit as there is nothing to do here. | |
echo "no ephemeral devices found" | |
mkdir -p $VOL1_MOUNTPOINT | |
mkdir -p $VOL2_MOUNTPOINT | |
exit | |
elif [ $NUMDEV -eq 1 ] | |
then | |
# Simply use the single device we have. Don't bother making a RAID out of it | |
DEVICE=$DEVLIST | |
else | |
# Create a raid device using the disks we were provided. | |
if [ -b /dev/md0 ] | |
then | |
print "Raid already exists, skipping..." | |
else | |
yes | mdadm --create /dev/md0 --level=stripe --raid-devices=$NUMDEV $DEVLIST | |
fi | |
DEVICE=/dev/md0 | |
fi | |
WARNING='This filesystem is ephemeral. It WILL be deleted when this EC2 instance is stopped or restarted. DO NOT STORE IMPORTANT INFORMATION ON THIS VOLUME!' | |
# Create a tempdata volume group | |
if vgdisplay | grep -q $VGNAME; then | |
echo "Volume Group $VGNAME exists, skipping...." | |
else | |
vgcreate $VGNAME $DEVICE | |
fi | |
if [ -b /dev/$VGNAME/$VOL1_LVNAME ] | |
then | |
echo "/dev/$VGNAME/$VOL1_LVNAME exists, skipping...." | |
else | |
lvcreate -L 100G -n $VOL1_LVNAME $VGNAME | |
if [ $? -ne 0 ] | |
then | |
echo "failed to create $VGNAME" | |
exit 10 | |
fi | |
mkfs.xfs /dev/$VGNAME/$VOL1_LVNAME | |
mkdir -p $VOL1_MOUNTPOINT | |
fi | |
if mountpoint -q $VOL1_MOUNTPOINT | |
then | |
echo "$VOL1_MOUNTPOINT already mounted, skipping..." | |
else | |
mount /dev/$VGNAME/$VOL1_LVNAME $VOL1_MOUNTPOINT | |
echo $WARNING > $VOL1_MOUNTPOINT/WARNING_README.txt | |
fi | |
if [ -b /dev/$VGNAME/$VOL2_LVNAME ] | |
then | |
echo "/dev/$VGNAME/$VOL2_LVNAME exists, skipping...." | |
else | |
lvcreate -l 100%FREE -n $VOL2_LVNAME $VGNAME | |
if [ $? -ne 0 ] | |
then | |
echo "failed to create $VGNAME" | |
exit 10 | |
fi | |
mkfs.xfs /dev/$VGNAME/$VOL2_LVNAME | |
mkdir -p $VOL2_MOUNTPOINT | |
fi | |
if mountpoint -q $VOL2_MOUNTPOINT | |
then | |
echo "$VOL2_MOUNTPOINT already mounted, skipping..." | |
else | |
mount /dev/$VGNAME/$VOL2_LVNAME $VOL2_MOUNTPOINT | |
echo $WARNING > $VOL2_MOUNTPOINT/WARNING_README.txt | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment