-
-
Save nklatt/41f04a4d6a60f60a1dc9 to your computer and use it in GitHub Desktop.
mountsshfs with addition of MOUNT_AS variable
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/sh | |
## http://ubuntuforums.org/showthread.php?t=430312 | |
## The script will attempt to mount any fstab entry with an option | |
## "...,comment=$SELECTED_STRING,..." | |
## Use this to select specific sshfs mounts rather than all of them. | |
SELECTED_STRING="sshfs" | |
## mount as user, default = "root" | |
MOUNT_AS="root" | |
# Not for loopback | |
[ "$IFACE" != "lo" ] || exit 0 | |
## define a number of useful functions | |
## returns true if input contains nothing but the digits 0-9, false otherwise | |
## so realy, more like isa_positive_integer | |
isa_number () { | |
! echo $1 | egrep -q '[^0-9]' | |
return $? | |
} | |
## returns true if the given uid or username is that of the current user | |
am_i () { | |
[ "$1" = "`id -u`" ] || [ "$1" = "`id -un`" ] | |
} | |
## takes a username or uid and finds it in /etc/passwd | |
## echoes the name and returns true on success | |
## echoes nothing and returns false on failure | |
user_from_uid () { | |
if isa_number "$1" | |
then | |
# look for the corresponding name in /etc/passwd | |
local IFS=":" | |
while read name x uid the_rest | |
do | |
if [ "$1" = "$uid" ] | |
then | |
echo "$name" | |
return 0 | |
fi | |
done </etc/passwd | |
else | |
# look for the username in /etc/passwd | |
if grep -q "^${1}:" /etc/passwd | |
then | |
echo "$1" | |
return 0 | |
fi | |
fi | |
# if nothing was found, return false | |
return 1 | |
} | |
## Parses a string of comma-separated fstab options and finds out the | |
## username/uid assigned within them. | |
## echoes the found username/uid and returns true if found | |
## echoes "root" and returns false if none found | |
uid_from_fs_opts () { | |
local uid=`echo $1 | egrep -o 'uid=[^,]+'` | |
if [ -z "$uid" ]; then | |
# no uid was specified, so default is root | |
echo "root" | |
return 1 | |
else | |
# delete the "uid=" at the beginning | |
uid_length=`expr length $uid - 3` | |
uid=`expr substr $uid 5 $uid_length` | |
echo $uid | |
return 0 | |
fi | |
} | |
# unmount all shares first | |
sh "/etc/network/if-down.d/umountsshfs" | |
while read fs mp type opts dump pass extra | |
do | |
# check validity of line | |
if [ -z "$pass" -o -n "$extra" -o "`expr substr ${fs}x 1 1`" = "#" ]; | |
then | |
# line is invalid or a comment, so skip it | |
continue | |
# check if the line is a selected line | |
elif echo $opts | grep -q "comment=$SELECTED_STRING"; then | |
sudo -u $MOUNT_AS sh -c "mount $mp" && | |
echo "$mp mounted as $MOUNT_AS" || | |
echo "$mp failed to mount as $MOUNT_AS"; | |
fi | |
# if not an sshfs line, do nothing | |
done </etc/fstab | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment