Created
May 25, 2016 10:09
-
-
Save sneakybeaky/f1ab97336124bd40549fdf5f2f3dddc8 to your computer and use it in GitHub Desktop.
Script to prepare an EBS volume for use. Formats with ext4 if needed and updates fstab. Intended to be run as part of EC2 user data startup.
This file contains hidden or 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 | |
if [ ! $2 ]; then | |
echo "Usage: $(basename $0) DEVICE MOUNT_POINT" | |
echo | |
echo " DEVICE - The device location on the EC2 instance. (E.g. /dev/sdh)" | |
echo " MOUNT_POINT - The location you would like the EBS Volume mounted to. (E.g. /data)" | |
exit | |
fi | |
set -e | |
set -x | |
DEVICE=$1 | |
MOUNT_POINT=$2 | |
DEVICE_FS_TYPE=`file -sL $DEVICE ` | |
if [[ $DEVICE_FS_TYPE == *"ext4"* ]]; then | |
echo "Device formatted" | |
else | |
echo "Formatting $DEVICE with an Ext4 fs" | |
mkfs.ext4 -q -F $DEVICE | |
echo | |
fi | |
#Label the device | |
echo "Labelling $DEVICE" | |
e2label $DEVICE $MOUNT_POINT | |
echo | |
#Backup fstab | |
echo "Copying /etc/fstab to /etc/fstab.orig" | |
cp /etc/fstab /etc/fstab.orig | |
echo | |
if [ ! -d "$MOUNT_POINT" ]; then mkdir -p $MOUNT_POINT; fi | |
#Add new entry to fstab for the new device | |
echo "Adding new fstab entry" | |
echo "LABEL=$MOUNT_POINT $MOUNT_POINT ext4 defaults,nofail 2 2" >> /etc/fstab | |
echo | |
#Mount all devices | |
echo "Mounting all devices" | |
mount -a | |
echo | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment