Qball's Weblog

SSHFS script.

Tags General 

A small script to easily mount remote hosts.

Now you can do:

?View Code BASH
1
smount.sh backup

It will mount backup (specified in the config file) in ~/.remote/backup/, dump you in this directory in a new shell. If you exit this shell, it automatically unmounts.
If you call it for the same directory multiple times, it will only unmount after the last one closes.

?View Code BASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# Autocomplete function:
#
#_smount.sh()
#{
#	COMPREPLY=()
#	curw=${COMP_WORDS[COMP_CWORD]}
#	COMPREPLY=($(compgen -W '$(smount.sh -l)' -- $curw))
#}
#complete -F _smount.sh smount.sh
#
 
BASH=bash
CONF_FILE=~/.smount.conf
DIRECTORY=~/.remote/
 
 
function clearlock()
{
    NAME="${1}"
    RPATH="${DIRECTORY}/${NAME}"
    if [ -f "${RPATH}.count" ];
    then
        rm "${RPATH}.count";
    fi
}
 
# create a numbered lock.
# return 0 when lock is newly created.
# returns 1 when lock existed (it increments the lock count)
function lock()
{
    NAME="${1}"
    RPATH="${DIRECTORY}/${NAME}"
    lockfile-create "${RPATH}"
    if [ -f "${RPATH}.count" ];
    then
        echo $(($(cat "${RPATH}.count")+1)) > "${RPATH}.count"
        lockfile-remove "${RPATH}"
        return 1
    else
        echo 1 >  "${RPATH}.count"
        lockfile-remove "${RPATH}"
        return 
    fi
}
 
# unlock a numbered lock.
# returns 0 if last lock is gone.
# returns 1 if something still holds a lock.
function unlock()
{
    NAME="${1}"
    RPATH="${DIRECTORY}/${NAME}"
 
    lockfile-create "${RPATH}"
    if [ -f "${RPATH}.count" ];
    then
        echo $(($(cat "${RPATH}.count")-1)) > "${RPATH}.count"
        if [ $(cat "${RPATH}.count") =  ]
        then
            # remove count file.
            rm "${RPATH}.count"
            # remove lock
            lockfile-remove "${RPATH}"
            return 
        else
            # remove lock
            lockfile-remove "${RPATH}"
            return 1
        fi
    else
        # No count file? then assume no lock.
        lockfile-remove "${RPATH}"
        return 
    fi
}
 
 
##
# mount it, and go in.
##
function smount()
{
    # SSH mount name.
    NAME="${1}"
 
 
    eval SSH_mount=\$${1}
    if [ -z "${SSH_mount}" ]
    then
        echo "Profile \"${NAME}\" does not exists";
        exit ;
    fi
 
    # Remote path
    RPATH="${DIRECTORY}/${NAME}"
 
    if [ ! -d "${RPATH}" ]
    then
        mkdir "${RPATH}";
    fi
 
    # Mount
    if lock "${NAME}"
    then
        echo "Mounting: ${NAME}"
        sshfs "${SSH_mount}" "${RPATH}"
 
        # Check if mount worked.
        if [ "$?" != "0" ]
        then
            echo "Failed to mount: ${SSH_mount}";
            # Undo set lock
            unlock "${NAME}"
            exit ;
        fi
    else
       if $(mount | awk -v val="${SSH_mount}" '($1 == val) {exit 1}')
       then
            echo "Failed to lock, stall lock file"
            clearlock "${NAME}"
            smount "${NAME}"
            exit ;
       fi
    fi
 
    # set profile
    export SP_PROFILE="SSH:${NAME}"
    pushd "${RPATH}" 2&>/dev/null
 
    # Enter interactive subshell
    ${BASH}
    popd 2&>/dev/null
 
    # Unmount it again.
    if unlock "${NAME}"
    then
        echo "Unmounting"
        fusermount -uz "${RPATH}"
    fi
}
 
 
list_profiles()
{
	# should be able todo this in one command.
	egrep -E "^(.*)=.*$" ${CONF_FILE} | awk -F'=' '{print $1}'
}
 
function check()
{
    if [ -z "$1" ]; then
        echo "Usage: smount.sh <path>";
        exit ;
    fi
 
    if [ ! -d "${DIRECTORY}" ]; then
        mkdir "${DIRECTORY}";
    fi
 
    if [ ! -z "${SP_PROFILE}" ]
    then
        echo "Already inside a mount/profile";
        exit;
    fi
 
}
##
# option parser
##
while getopts hlvrd:s: OPT; do
    case "$OPT" in
        l)
           	list_profiles
			exit 
            ;;
    esac
done
# Remove the switches we parsed above.
shift `expr $OPTIND - 1`
 
 
 
check $@
source "${CONF_FILE}"
smount $@
</path>

This script requires lockfile-progs to be installed.

Config file (~/.smount.conf) looks like:

backup=backup:/home/backups

before the = sign is the name, after the = sign the path you normally pass to sshfs.
If you copy the top into your bashrc file, you get auto completion.