Created
August 20, 2013 00:23
-
-
Save maxkandler/6275736 to your computer and use it in GitHub Desktop.
Mounts and unmounts a USB drive based on the presence of another machine in the network. A little more explanation on what it does: http://justcurious.is/2013/raspberry-pi-and-external-drives/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment