Skip to content

Instantly share code, notes, and snippets.

@maxkandler
Created August 20, 2013 00:23

Revisions

  1. Max Kleucker created this gist Aug 20, 2013.
    75 changes: 75 additions & 0 deletions automounter.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash

    host="island1.local"
    mountPoint="/media/KRETA"
    drive="/dev/sda1"
    device="/dev/sda"

    hostIsUp=-1

    checkHostIsUp()
    {

    ping -q -c 1 $host >> /dev/null
    if [ "$?" -eq 0 ]; then
    hostIsUp=1
    else
    hostIsUp=0
    fi
    }

    driveIsMounted=-1
    checkDriveIsMounted()
    {
    if grep -qs $mountPoint /proc/mounts >> /dev/null; then
    driveIsMounted=1
    else
    driveIsMounted=0
    fi
    }

    doMountDrive()
    {
    sh -c 'AUTHFILE="/sys/bus/usb/devices/1-1.2/authorized" ; echo 0 > "$AUTHFILE" ; sleep 1 ; echo 1 > "$AUTHFILE"'
    sleep 10
    mount -t vfat $drive $mountPoint
    }

    doUnmountDrive()
    {
    udisks --unmount $drive
    }

    doDisconnectDrive()
    {
    udisks --detach $device >> /dev/null
    }

    checkHostIsUp
    checkDriveIsMounted

    if [ "$hostIsUp" == 1 ]
    then
    echo "host is up"
    if [ "$driveIsMounted" == 0 ]
    then
    doMountDrive
    echo "mounted drive"
    else
    echo "host is available. drive already mounted."
    fi
    else
    echo $hostIsUp
    echo "host not up"
    if [ "$driveIsMounted" == 1 ]
    then
    doUnmountDrive
    doDisconnectDrive
    echo "unmounted drive successfully"

    else
    echo "host not available. drive is not mounted."
    fi
    fi

    exit 0