Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘usb’

Automated Harddrive Mounting Script

Posted by oxaric on November 15, 2008

I have 2 big 500 GB harddrives in USB cases with 3 partitions per drive. When they are connected to my machine the system automatically mounts them to /media and this seems great in theory. I only noticed something annoying after the drives laid idle for a while. My machine would quietly lose touch the drives and their files would no longer be accessible. However, the partition names would still remain in /media becoming ghost partition points.


If I accessed either drives after they had gone to sleep they would be mounted again but because the first mount points were still in /media my system seemed to like to randomly append an ‘_’ to the names of the mount points… sometimes… when it felt like it. The problem with this is that if anything running on my machine was looking for ‘/media/my_usb_drive’ it might find this mount point but because it is has become a ghost mount point the program will find nothing inside. The real drive would now be mounted to ‘/my_media/usb_drive_’.


So I created a script to deal with automatically finding the drives attached to my computer and mounting them to permanent mount points I created in /mnt. The mount points are simply directories I created in /mnt for the sole purpose of being a permanent home for my drive partitions. Now if the drives go to sleep I can simply run this script file and it will wake up the drives that are sleeping and mount them in their proper place in /mnt.


The benefit of this is that I no longer have to worry about having ghost mount points and I can use absolute file paths with no problems. Also, if I unplug and plug back in one of the drives I run this script and it automatically finds the correct /dev location of the drive and mounts it for me.


The script is fairly simple and contains enough comments that it should be fairly easy to edit it for your own needs. It is currently setup for my own system. Also, notice this program does not need to be used just with USB drives. Any drive type will work.


An extra tip


If you really hate to have your drives fall asleep you can directly edit your cron job process by using “crontab -e” and adding the line for each partition or drive:
“0 1 * * * ls /path_to_drive/my_mount_point”


The cron job process executes a command at a specific time like 1 AM for a system backup or at set time interval like every hour. Adding the above will tell the cron job to poke your drives every hour to keep them awake. Although if you don’t have a serious reason for using this you’ll save energy and have happier drives by letting them fall asleep. :) You could also setup cron to execute this script every so often just to make sure you’ll definitely keep your drives awake and mounted properly.


Click to directly download mount-usb-drives.sh

#!/bin/bash
# file name 'mount-usb-drives.sh'
# coded by: Louis Casillas, e-mail: oxaric@gmail.com

# Below are the physical IDs of the drives
# To see this run: $ sudo fdisk -l
# look at Disk identifier:

# ID: USB DRIVE: S_DRIVE
S_ID=0x44fdfe06

# ID: USB DRIVE: P_DRIVE
P_ID=0x9a4c35e6

# My USB drives are FAT32 so they use the vfat format to be mounted
MY_FILE_FORMAT="vfat"

# this is my user ID
# To see this run: $ id
MY_USER_ID=1000

total_max_drives_to_find=2 # number of USB drives I have
number_of_drives_found=0

echo
echo "Attempting to locate connected drives..."

# grabs a list of all the drives connected to the machine
sudo fdisk -l | column > /tmp/current_drives_connected.txt

# setup the input so the while loop will read every
# line from the file 'current_drives_connected.txt'
exec< "/tmp/current_drives_connected.txt"

# remove the temp file
rm /tmp/current_drives_connected.txt

while read line
do
    # takes the current line and looks for the physical ID
    # of the S_DRIVE
    test="$(echo "$line" | grep "$S_ID")"

    # if the ID was found then attempt to mount the drives
    if [[ "$test" != "" ]]; then

        number_of_drives_found=$(( $number_of_drives_found + 1 ))

        echo
        echo "S_DRIVE FOUND!"
        echo "Attempting to mount S_DRIVE..."

        # burn a line
        # the line after the one that contains the physical drive ID
        # has nothing we want
        read

        # read in the line that will tell us where linux has mounted
        # the drive. usually /dev/s__
        read line

        # grab the basic location i.e. /dev/sdb
        dev_location="$(echo $line | head -c8)"

        # my USB drives have 3 partitions and I have created a permanent location
        # for them in my /mnt drive.
        # this will attempt to mount the partitions. If they are already mounted
        # then mount will display this
        sudo mount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""1" /mnt/s-one
        sudo mount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""2" /mnt/s-two
        sudo mount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""3" /mnt/s-three
    fi

    # takes the current line and looks for the physical ID
    # of the P_DRIVE
    test="$(echo "$line" | grep "$P_ID")"

    # if the ID was found then attempt to mount the drives
    if [[ "$test" != "" ]]; then

        number_of_drives_found=$(( $number_of_drives_found + 1 ))

        echo
        echo "P_DRIVE FOUND!"
        echo "Attempting to mount P_DRIVE..."

        # burn a line
        # the line after the one that contains the physical drive ID
        # has nothing we want
        read

        # read in the line that will tell us where linux has mounted
        # the drive. usually /dev/s__
        read line

        # grab the basic location i.e. /dev/sdb
        dev_location="$(echo $line | head -c8)"

        # my USB drives have 3 partitions and I have created a permanent location
        # for them in my /mnt drive.
        # this will attempt to mount the partitions. If they are already mounted
        # then mount will display this
        sudo mount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""1" /mnt/p-one
        sudo mount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""2" /mnt/p-two
        sudomount -t "$MY_FILE_FORMAT" -o uid="$MY_USER_ID" "$dev_location""3" /mnt/p-three
    fi
       
    # if all the drives were found then exit the while loop
    if [ $number_of_drives_found -eq $total_max_drives_to_find ]; then
        break
    fi
done

echo

Posted in BASH, Linux, Programming | Tagged: , , , , , , , , , | Leave a Comment »