Saturday, October 22, 2011

A script to automount external USB HDD with the executable bit set

Recent releases of Ubuntu mounts external USB HDD in userspace and hence it does not set the executable bit on the drive. As a result if I try to run softwares or scripts installed in the external HDD I have to pmount it manually. So I wrote a small script which would - 1. verify that drive is mounted. 2. it is not empty(from a previous manual mount unmount cycle) 3. then unmount the drive and re-mount it in userspace with the executable bit set.

I wanted this script to be minimal. So I did not make it complicated which accepts a drive LABEL from the user and uses it. Just replace your drives LABEL in place of "passport-2" in the second line of the script. So if your external drive is called Sandisk replace passport-2 with Sandisk. If it turns out that the script fails due the use of $LABEL, then replace all $LABEL in the entire script with your drive label manually.

cd /media
# check if $LABEL exists
if [ ! -d "$LABEL" ]; then
echo "ERROR : " $LABEL" not found in /media"
echo "Plugin drive or mount" $LABEL "...."
exit 0
# even if $LABEL exist it might be empty because of manual mount and unmount. then prompt user to manually remove that folder and remount $LABEL. if no. of files inside $LABEL is 2 it only has . and .. So it is basically empty.
cd /media/$LABEL
filecount=`ls -l | wc -l`
if [[ $filecount -lt 3 ]]; then
echo "ERROR : Empty /media/"$LABEL " found."
echo -e "Remove empty " $LABEL " as root ... remount correctly ... not attempting to do myself because it can cause potential loss of data."
loca=`df | grep $LABEL | cut -f1 -d" " `
echo $LABEL " is at device node : " $loca
sudo umount -l /media/$LABEL
if [ ! -d "$LABEL" ]; then
echo "Remounting passport-2 in RW mode with executable bit set."
pmount --fmask 077 -d -e $loca $LABEL

No comments:

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...