Skip to content

Instantly share code, notes, and snippets.

@valmat
Created January 28, 2015 15:56
Show Gist options
  • Save valmat/f5028dce9d76cc7c48a3 to your computer and use it in GitHub Desktop.
Save valmat/f5028dce9d76cc7c48a3 to your computer and use it in GitHub Desktop.
Script to auto mount external device.If for some reason you do not want to use the fstab file, this script will be a resolve
#!/bin/sh
#
#
# Script to auto mount external device
# If for some reason you do not want to use the fstab file, this script will be a resolve
#
# The path to the disk label
LABEL_PATH="/dev/disk/by-label"
# File label
LABEL_FILE="1tb"
LABEL_LINK="$LABEL_PATH/$LABEL_FILE"
# Where mounted?
MUONT_TO="/mnt/1tb"
# -h : if FILE exists and is a symbolic link
# Check whether the connected drive on the presence of a symbolic link device
if [ ! -h $LABEL_LINK ]
then
echo "Symbol link $LABEL_LINK is no exist"
echo "The device is not connected"
exit;
fi;
# Determine the location of the disk
DEVICE_PATH=$(cd $LABEL_PATH && cd $(dirname $(readlink $LABEL_LINK)) && pwd)
DEVICE_FILE=$(basename $(readlink $LABEL_LINK))
DEVICE="$DEVICE_PATH/$DEVICE_FILE"
# -b : if FILE exists and is a special block file.
if [ ! -b $DEVICE ]
then
echo "The device $DEVICE is no exist"
echo "The device is not connected"
exit;
fi
# check if device is mounted
MOUNTED=""
if grep -qs $DEVICE /proc/mounts;
then
# echo "It's mounted."
MOUNTED="1"
#else
# echo "It's not mounted."
fi
#echo $DEVICE
case "$1" in
start)
if [ ! $MOUNTED ]
then
mount -t ext4 $DEVICE $MUONT_TO
echo "mounted"
else
echo "Not mounted! The device is alredy mounted"
fi
;;
stop)
if [ $MOUNTED ]
then
umount $DEVICE
echo "umounted"
else
echo "Not unmounted! The device is alredy unmounted"
fi
;;
restart)
$0 stop
$0 start
;;
mounted)
echo -n $MOUNTED
;;
*)
echo "Please use start, stop, restart or mounted as first argument"
;;
esac
# Save it to /etc/init.d/1tb
# Don't forget run `update-rc.d 1tb defaults`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment