4 # execfuse ./autosshfs ~/mnt/ssh -o fsname=autosshfs.$USER,allow_root
6 # You have to set this in ~/.ssh/config:
8 # ControlPath ~/.ssh/ctrl/%r@%n(%h):%p.sock
14 connections_subdir
=${EXECFUSE_MOUNTPOINT##*/}
15 attr_parentdir
="ino=1 mode=drwxr-xr-x nlink=2 uid=0 gid=0 rdev=0 size=0 blksize=512 blocks=2 atime=0 mtime=0 ctime=0"
16 attr_rootdir
="ino=1 mode=drwxr-xr-x nlink=2 uid=$UID gid=0 rdev=0 size=0 blksize=512 blocks=2 atime=0 mtime=0 ctime=0"
17 attr_entry
="ino=1 mode=drwx------ nlink=1 uid=$UID gid=0 rdev=0 size=0 blksize=512 blocks=2 atime=0 mtime=0 ctime=0"
18 attr_alias
="ino=1 mode=drwx------ nlink=1 uid=$UID gid=0 rdev=0 size=0 blksize=512 blocks=2 atime=0 mtime=0 ctime=0"
24 if [ ${#Conn[@]} = 0 ]
33 for file in ~
/.ssh
/ctrl
/*.sock
42 if [[ $1 =~ @
(.
+)\
( ]]
44 echo "${BASH_REMATCH[1]}"
60 local dir
="/tmp/autosshfs.$USER"
65 mkdir
-p -m 0700 "$dir"
66 flock
-x $flag "$dir/$conn" "$@"
77 lock_nb
"mount+$conn" setsid sshfs
-o Port
=$port,ControlMaster
=no
,transform_symlinks
,reconnect
,Hostname
="$hostname" "$user@$host:/" "$target" >/dev
/null
2>&1 &
78 # -o sftp_server='sudo /usr/lib/openssh/sftp-server'
82 if [ "$operation" = getattr
]
86 printf "$attr_rootdir"
90 for conn
in "${!Conn[@]}"
92 if [ "$1" = "/$conn" ]
97 hostalias
=`get_host_alias "$conn"`
98 if [ "$1" = "/$hostalias" ]
108 elif [ "$operation" = readdir
]
110 # first umount all dead connections
112 while read -r dev mp rest
114 if [[ $mp =~ ^
(.
+)/([^
/]+)$
]]
116 dir
=${BASH_REMATCH[1]}
117 subdir
=${BASH_REMATCH[2]}
118 if [ "$dir" = "$EXECFUSE_MOUNTPOINT" ]
121 if [ -z "${Conn[$subdir]}" ]
123 lock_nb
"umount+$subdir" setsid fusermount
-u -z "$mp" >/dev
/null
2>&1 &
129 # list directories to each ssh connection when listing the root directory
132 printf "$attr_rootdir .\0"
133 printf "$attr_parentdir ..\0"
136 for conn
in "${!Conn[@]}"
138 printf "$attr_entry %s\0" "$conn"
139 hostalias
=`get_host_alias "$conn"`
140 ln -snf "$connections_subdir/$conn" "$EXECFUSE_MOUNTPOINT/../$hostalias"
145 # when listing a subdirectory, mount the corresponding sshfs under it.
146 # and return async, so you need to chdir to the directory again to get the actual content.
148 printf "$attr_entry .\0"
149 printf "$attr_rootdir ..\0"
152 for conn
in "${!Conn[@]}"
154 if [ "/$conn" = "$1" ]
156 if [[ $conn =~ ^
([^@
]+)@
([^\
(]+)\
(([^\
)]+)\
):([0-9]+) ]]
158 user
=${BASH_REMATCH[1]}
159 host=${BASH_REMATCH[2]}
160 hostname
=${BASH_REMATCH[3]}
161 port
=${BASH_REMATCH[4]}
163 mount_sshfs_async
"$conn" "$port" "$user" "$host" "$hostname" "$EXECFUSE_MOUNTPOINT$1"