Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
#!/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
You can’t perform that action at this time.