Skip to content

Instantly share code, notes, and snippets.

@cahva
Created December 8, 2024 15:01
Show Gist options
  • Save cahva/dd06a9c1c396384c4051b6ebffb24074 to your computer and use it in GitHub Desktop.
Save cahva/dd06a9c1c396384c4051b6ebffb24074 to your computer and use it in GitHub Desktop.
Mount nvme disk in aws
#!/usr/bin/env bash
MOUNT="/media"
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 -E nodiscard ${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!"
# Unmount objectivefs and remount everything to ensure correct order
echo "Unmounting /videosync-events to ensure correct mount order..."
umount /videosync-events 2>/dev/null || true
echo "Remounting all filesystems..."
mount -a
else
echo "Something went wrong with the mount - ${EPHEMERAL_DISK}"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment