2016-07-18 00:03:23 +02:00
// Package mount implents a FUSE mounting system for rclone remotes.
// +build linux darwin freebsd
package mount
import (
2016-09-09 09:39:19 +02:00
"log"
"os"
2017-03-17 11:20:28 +01:00
"os/signal"
"syscall"
2016-10-18 15:44:16 +02:00
"time"
2016-09-09 09:39:19 +02:00
2016-07-18 00:03:23 +02:00
"bazil.org/fuse"
2017-05-02 23:35:07 +02:00
fusefs "bazil.org/fuse/fs"
2016-07-18 00:03:23 +02:00
"github.com/ncw/rclone/cmd"
2017-05-07 14:04:20 +02:00
"github.com/ncw/rclone/cmd/mountlib"
2016-07-18 00:03:23 +02:00
"github.com/ncw/rclone/fs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
2016-09-09 09:39:19 +02:00
"golang.org/x/sys/unix"
2016-07-18 00:03:23 +02:00
)
// Globals
var (
2016-10-18 15:44:16 +02:00
noModTime = false
2017-05-01 14:38:41 +02:00
noChecksum = false
2016-10-18 15:44:16 +02:00
debugFUSE = false
noSeek = false
dirCacheTime = 5 * 60 * time . Second
2016-09-09 09:39:19 +02:00
// mount options
readOnly = false
allowNonEmpty = false
allowRoot = false
allowOther = false
defaultPermissions = false
writebackCache = false
maxReadAhead fs . SizeSuffix = 128 * 1024
umask = 0
uid = uint32 ( unix . Geteuid ( ) )
gid = uint32 ( unix . Getegid ( ) )
// foreground = false
// default permissions for directories - modified by umask in Mount
dirPerms = os . FileMode ( 0777 )
filePerms = os . FileMode ( 0666 )
2016-07-18 00:03:23 +02:00
)
func init ( ) {
2016-09-09 09:39:19 +02:00
umask = unix . Umask ( 0 ) // read the umask
unix . Umask ( umask ) // set it back to what it was
2016-10-22 13:05:45 +02:00
cmd . Root . AddCommand ( commandDefintion )
2017-03-23 20:41:21 +01:00
commandDefintion . Flags ( ) . BoolVarP ( & noModTime , "no-modtime" , "" , noModTime , "Don't read/write the modification time (can speed things up)." )
2017-05-01 14:38:41 +02:00
commandDefintion . Flags ( ) . BoolVarP ( & noChecksum , "no-checksum" , "" , noChecksum , "Don't compare checksums on up/download." )
2016-10-22 13:05:45 +02:00
commandDefintion . Flags ( ) . BoolVarP ( & debugFUSE , "debug-fuse" , "" , debugFUSE , "Debug the FUSE internals - needs -v." )
commandDefintion . Flags ( ) . BoolVarP ( & noSeek , "no-seek" , "" , noSeek , "Don't allow seeking in files." )
commandDefintion . Flags ( ) . DurationVarP ( & dirCacheTime , "dir-cache-time" , "" , dirCacheTime , "Time to cache directory entries for." )
2016-09-09 09:39:19 +02:00
// mount options
2016-10-22 13:05:45 +02:00
commandDefintion . Flags ( ) . BoolVarP ( & readOnly , "read-only" , "" , readOnly , "Mount read-only." )
commandDefintion . Flags ( ) . BoolVarP ( & allowNonEmpty , "allow-non-empty" , "" , allowNonEmpty , "Allow mounting over a non-empty directory." )
commandDefintion . Flags ( ) . BoolVarP ( & allowRoot , "allow-root" , "" , allowRoot , "Allow access to root user." )
commandDefintion . Flags ( ) . BoolVarP ( & allowOther , "allow-other" , "" , allowOther , "Allow access to other users." )
commandDefintion . Flags ( ) . BoolVarP ( & defaultPermissions , "default-permissions" , "" , defaultPermissions , "Makes kernel enforce access control based on the file mode." )
commandDefintion . Flags ( ) . BoolVarP ( & writebackCache , "write-back-cache" , "" , writebackCache , "Makes kernel buffer writes before sending them to rclone. Without this, writethrough caching is used." )
commandDefintion . Flags ( ) . VarP ( & maxReadAhead , "max-read-ahead" , "" , "The number of bytes that can be prefetched for sequential reads." )
commandDefintion . Flags ( ) . IntVarP ( & umask , "umask" , "" , umask , "Override the permission bits set by the filesystem." )
commandDefintion . Flags ( ) . Uint32VarP ( & uid , "uid" , "" , uid , "Override the uid field set by the filesystem." )
commandDefintion . Flags ( ) . Uint32VarP ( & gid , "gid" , "" , gid , "Override the gid field set by the filesystem." )
//commandDefintion.Flags().BoolVarP(&foreground, "foreground", "", foreground, "Do not detach.")
2016-07-18 00:03:23 +02:00
}
2016-10-22 13:05:45 +02:00
var commandDefintion = & cobra . Command {
2016-07-18 00:03:23 +02:00
Use : "mount remote:path /path/to/mountpoint" ,
Short : ` Mount the remote as a mountpoint. **EXPERIMENTAL** ` ,
Long : `
2016-08-22 17:46:08 +02:00
rclone mount allows Linux , FreeBSD and macOS to mount any of Rclone ' s
cloud storage systems as a file system with FUSE .
2016-07-18 00:03:23 +02:00
This is * * EXPERIMENTAL * * - use with care .
First set up your remote using ` + " ` rclone config ` " + ` . Check it works with ` + " ` rclone ls ` " + ` etc .
2017-03-17 11:20:28 +01:00
Start the mount like this
2016-07-18 00:03:23 +02:00
2017-03-17 11:20:28 +01:00
rclone mount remote : path / to / files / path / to / local / mount
2016-07-18 00:03:23 +02:00
2017-03-17 11:20:28 +01:00
When the program ends , either via Ctrl + C or receiving a SIGINT or SIGTERM signal ,
the mount is automatically stopped .
2016-07-18 00:03:23 +02:00
2017-03-17 11:20:28 +01:00
The umount operation can fail , for example when the mountpoint is busy .
When that happens , it is the user ' s responsibility to stop the mount manually with
2016-07-18 00:03:23 +02:00
2017-03-17 11:20:28 +01:00
# Linux
fusermount - u / path / to / local / mount
# OS X
2017-01-30 19:16:53 +01:00
umount / path / to / local / mount
2016-07-18 00:03:23 +02:00
# # # Limitations # # #
2016-09-10 23:25:26 +02:00
This can only write files seqentially , it can only seek when reading .
2017-01-06 12:24:22 +01:00
This means that many applications won ' t work with their files on an
rclone mount .
The bucket based remotes ( eg Swift , S3 , Google Compute Storage , B2 ,
Hubic ) won ' t work from the root - you will need to specify a bucket ,
or a path within the bucket . So ` + " ` swift : ` " + ` won ' t work whereas
` + " ` swift : bucket ` " + ` will as will ` + " ` swift : bucket / path ` " + ` .
None of these support the concept of directories , so empty
directories will have a tendency to disappear once they fall out of
the directory cache .
2016-07-18 00:03:23 +02:00
2016-08-22 17:46:08 +02:00
Only supported on Linux , FreeBSD and OS X at the moment .
2016-07-18 00:03:23 +02:00
# # # rclone mount vs rclone sync / copy # #
File systems expect things to be 100 % reliable , whereas cloud storage
systems are a long way from 100 % reliable . The rclone sync / copy
commands cope with this with lots of retries . However rclone mount
can ' t use retries in the same way without making local copies of the
uploads . This might happen in the future , but for the moment rclone
mount won ' t do that , so will be less reliable than the rclone command .
2017-02-16 00:26:40 +01:00
# # # Filters # # #
Note that all the rclone filters can be used to select a subset of the
files to be visible in the mount .
2017-05-04 22:49:06 +02:00
# # # Directory Cache # # #
Using the ` + " ` -- dir - cache - time ` " + ` flag , you can set how long a
directory should be considered up to date and not refreshed from the
backend . Changes made locally in the mount may appear immediately or
invalidate the cache . However , changes done on the remote will only
be picked up once the cache expires .
Alternatively , you can send a ` + " ` SIGHUP ` " + ` signal to rclone for
it to flush all directory caches , regardless of how old they are .
2017-05-07 22:12:23 +02:00
Assuming only one rclone instance is running , you can reset the cache
2017-05-04 22:49:06 +02:00
like this :
kill - SIGHUP $ ( pidof rclone )
2016-07-18 00:03:23 +02:00
# # # Bugs # # #
* All the remotes should work for read , but some may not for write
2016-11-19 11:54:37 +01:00
* those which need to know the size in advance won ' t - eg B2
2016-07-18 00:03:23 +02:00
* maybe should pass in size as - 1 to mean work it out
2016-10-23 22:46:48 +02:00
* Or put in an an upload cache to cache the files on disk first
2016-07-18 00:03:23 +02:00
` ,
2016-09-09 09:39:19 +02:00
Run : func ( command * cobra . Command , args [ ] string ) {
2016-07-18 00:03:23 +02:00
cmd . CheckArgs ( 2 , 2 , command , args )
fdst := cmd . NewFsDst ( args )
2016-09-09 09:39:19 +02:00
err := Mount ( fdst , args [ 1 ] )
if err != nil {
log . Fatalf ( "Fatal error: %v" , err )
}
2016-07-18 00:03:23 +02:00
} ,
}
2017-05-02 23:35:07 +02:00
// mountOptions configures the options from the command line flags
func mountOptions ( device string ) ( options [ ] fuse . MountOption ) {
options = [ ] fuse . MountOption {
fuse . MaxReadahead ( uint32 ( maxReadAhead ) ) ,
fuse . Subtype ( "rclone" ) ,
fuse . FSName ( device ) , fuse . VolumeName ( device ) ,
fuse . NoAppleDouble ( ) ,
fuse . NoAppleXattr ( ) ,
// Options from benchmarking in the fuse module
//fuse.MaxReadahead(64 * 1024 * 1024),
//fuse.AsyncRead(), - FIXME this causes
// ReadFileHandle.Read error: read /home/files/ISOs/xubuntu-15.10-desktop-amd64.iso: bad file descriptor
// which is probably related to errors people are having
//fuse.WritebackCache(),
}
if allowNonEmpty {
options = append ( options , fuse . AllowNonEmptyMount ( ) )
}
if allowOther {
options = append ( options , fuse . AllowOther ( ) )
}
if allowRoot {
options = append ( options , fuse . AllowRoot ( ) )
}
if defaultPermissions {
options = append ( options , fuse . DefaultPermissions ( ) )
}
if readOnly {
options = append ( options , fuse . ReadOnly ( ) )
}
if writebackCache {
options = append ( options , fuse . WritebackCache ( ) )
}
return options
}
// mount the file system
//
// The mount point will be ready when this returns.
//
// returns an error, and an error channel for the serve process to
// report an error when fusermount is called.
2017-05-07 14:04:20 +02:00
func mount ( f fs . Fs , mountpoint string ) ( * mountlib . FS , <- chan error , func ( ) error , error ) {
2017-05-02 23:35:07 +02:00
fs . Debugf ( f , "Mounting on %q" , mountpoint )
c , err := fuse . Mount ( mountpoint , mountOptions ( f . Name ( ) + ":" + f . Root ( ) ) ... )
if err != nil {
2017-05-07 14:04:20 +02:00
return nil , nil , nil , err
2017-05-02 23:35:07 +02:00
}
filesys := NewFS ( f )
server := fusefs . New ( c , nil )
// Serve the mount point in the background returning error to errChan
errChan := make ( chan error , 1 )
go func ( ) {
err := server . Serve ( filesys )
closeErr := c . Close ( )
if err == nil {
err = closeErr
}
errChan <- err
} ( )
// check if the mount process has an error to report
<- c . Ready
if err := c . MountError ; err != nil {
2017-05-07 14:04:20 +02:00
return nil , nil , nil , err
2017-05-02 23:35:07 +02:00
}
unmount := func ( ) error {
return fuse . Unmount ( mountpoint )
}
2017-05-07 14:04:20 +02:00
return filesys . FS , errChan , unmount , nil
2017-05-02 23:35:07 +02:00
}
2016-07-18 00:03:23 +02:00
// Mount mounts the remote at mountpoint.
//
// If noModTime is set then it
func Mount ( f fs . Fs , mountpoint string ) error {
if debugFUSE {
fuse . Debug = func ( msg interface { } ) {
2017-02-09 12:01:20 +01:00
fs . Debugf ( "fuse" , "%v" , msg )
2016-07-18 00:03:23 +02:00
}
}
2016-09-09 09:39:19 +02:00
// Set permissions
dirPerms = 0777 &^ os . FileMode ( umask )
filePerms = 0666 &^ os . FileMode ( umask )
2016-12-15 18:40:17 +01:00
// Show stats if the user has specifically requested them
if cmd . ShowStats ( ) {
stopStats := cmd . StartStats ( )
defer close ( stopStats )
}
2016-12-01 09:49:47 +01:00
2016-07-18 00:03:23 +02:00
// Mount it
2017-05-07 14:04:20 +02:00
FS , errChan , unmount , err := mount ( f , mountpoint )
2016-07-18 00:03:23 +02:00
if err != nil {
return errors . Wrap ( err , "failed to mount FUSE fs" )
}
2017-05-07 14:04:20 +02:00
sigInt := make ( chan os . Signal , 1 )
signal . Notify ( sigInt , syscall . SIGINT , syscall . SIGTERM )
sigHup := make ( chan os . Signal , 1 )
signal . Notify ( sigHup , syscall . SIGHUP )
waitloop :
for {
select {
// umount triggered outside the app
case err = <- errChan :
break waitloop
// Program abort: umount
case <- sigInt :
err = unmount ( )
break waitloop
// user sent SIGHUP to clear the cache
case <- sigHup :
root , err := FS . Root ( )
if err != nil {
fs . Errorf ( f , "Error reading root: %v" , err )
} else {
root . ForgetAll ( )
}
}
2017-03-17 11:20:28 +01:00
}
2016-07-18 00:03:23 +02:00
if err != nil {
return errors . Wrap ( err , "failed to umount FUSE fs" )
}
return nil
}