Created
November 12, 2015 14:03
-
-
Save ajbrown/d29269d76915f3347db9 to your computer and use it in GitHub Desktop.
Add a filesystem to a drive (if it does not already have one), and mount it to a path.
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 | |
# This script will ensure a device has a filesystem before mounting it to a given path. | |
# It will only attempt to create the filesystem if one does not already exist on the device. | |
# | |
# AUTHOR: A.J. Brown <[email protected]> | |
DEV='/dev/xvdf' | |
MOUNT_TO='/data' | |
FS='ext4' | |
currentFs=`blkid -s TYPE -o value $DEV` | |
if [ -z $currentFs ]; then | |
echo "No existing filesystem, so $FS will be added." | |
mkfs.$FS $DEV | |
if [ $? -ne 0 ]; then | |
echo "Error creating $FS filesystem on $DEV. Aborting." | |
exit 1 | |
fi | |
elif [ "$currentFs" -ne "$FS" ]; then | |
echo "Warning: existing filesystem '$currentfs' doesn't match the desired filesystem '$FS'. Continuing anyway." | |
fi | |
mkdir -p $MOUNT_TO | |
mount $DEV $MOUNT_TO | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Could not mount $DEV to $MOUNT_TO. Aborting" | |
exit 1 | |
fi | |
echo "Succesfully mounted $DEV to $MOUNT_TO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment