Skip to content

Instantly share code, notes, and snippets.

@ajbrown
Created November 12, 2015 14:03
Show Gist options
  • Save ajbrown/d29269d76915f3347db9 to your computer and use it in GitHub Desktop.
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.
#!/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 <aj@ajbrown.org>
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