Last active
April 25, 2024 12:31
-
-
Save fideloper/40f7807920aa1198fa07b9e69dc82b56 to your computer and use it in GitHub Desktop.
Find, format, and mount an AWS Ephemeral NVMe disk within ec2 in user data
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
#!/usr/bin/env bash | |
### | |
## This mounts a (single) ephemral NVMe drive in an EC2 server. | |
## It's meant to be run once, within user-data | |
## For EBS drives (non-ephemeral storage), see: https://gist.github.com/jalaziz/c22c8464cb602bc2b8d0a339b013a9c4 | |
# | |
# Install the "nvme" command | |
# See: https://github.com/linux-nvme/nvme-cli | |
sudo apt-get install -y nvme-cli | |
# Create a mount point (directory) | |
sudo mkdir -p /some/mount | |
# Find ephemeral storage (assumes a single ephemeral disk) | |
# and format it (assumes this is run on first-boot in user-data, so the disk is not formatted) | |
EPHEMERAL_DISK=$(sudo nvme list | grep 'Amazon EC2 NVMe Instance Storage' | awk '{ print $1 }') | |
sudo mkfs.ext4 $EPHEMERAL_DISK | |
sudo mount -t ext4 $EPHEMERAL_DISK /some/mount | |
### For some crazy reason, add ephemeral disk mount to /etc/fstab | |
## even tho you lose data in stop/starts of ec2 (I believe you keep the data via regular reboots?) | |
# | |
# Find the mounted drive UUID so we can mount by UUID | |
EPHEMERAL_UUID=$(sudo blkid -s UUID -o value $EPHEMERAL_DISK) | |
echo "UUID=$EPHEMERAL_UUID /opt/nomad ext4 defaults 0 0" | sudo tee -a /etc/fstab | |
Author
fideloper
commented
Mar 2, 2023
via email
that’s correct, yep!
…On Thu, Mar 2, 2023 at 06:26 Rob ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Thanks, really convenient script! Although I'm just wondering where
/opt/nomad on line 32 came from?
echo "UUID=$EPHEMERAL_UUID /opt/nomad ext4 defaults 0 0" | sudo tee -a
/etc/fstab
I assumed you should use your mount point there, /some/mount?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/40f7807920aa1198fa07b9e69dc82b56#gistcomment-4489223>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADSDU6D6KSHVZHETAE3MGLW2B7VPBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANJZGQ2TENRWU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
ec2 instance will stuck if you use uuid in /etc/fstab after stop/start.
I added some checks. If I need to save data after a reboot, I will check if it's formatted
MOUNT="/opt/data"
EPHEMERAL_DISK=\$(sudo nvme list | grep 'Amazon EC2 NVMe Instance Storage' | awk '{ print \$1 }')
# Check if the mount point exists in /proc/mounts
if grep -qs \${MOUNT} /proc/mounts; then
echo "It's mounted."
else
# Check if the block device exists and is not formatted as ext4
if [ -b \${EPHEMERAL_DISK} ] && ! blkid \${EPHEMERAL_DISK} | grep -qs ext4; then
echo "It's not mounted and not formatted, formatting..."
# Format the block device as ext4
if mkfs.ext4 \${EPHEMERAL_DISK}; then
echo "Formatting \${EPHEMERAL_DISK} success!"
else
echo "Failed to format \${EPHEMERAL_DISK}."
exit 1
fi
fi
# Attempt to mount the block device to the specified mount point
echo "It's not mounted, trying to mount..."
if mkdir -p \${MOUNT} && mount -t ext4 \${EPHEMERAL_DISK} \${MOUNT}; then
echo "Mount \${MOUNT} success!"
else
echo "Something went wrong with the mount - \${EPHEMERAL_DISK}"
exit 1
fi
fi
Thanks for sharing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment