2015-07-24 09:10:49 +03:00
|
|
|
package fuse
|
2015-07-24 09:10:41 +03:00
|
|
|
|
|
|
|
import (
|
2016-02-29 05:47:56 +03:00
|
|
|
"bytes"
|
2018-12-26 17:44:07 +03:00
|
|
|
"errors"
|
2015-07-24 09:10:41 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
2018-12-26 17:44:07 +03:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
2015-07-24 09:10:41 +03:00
|
|
|
|
2018-12-26 17:44:07 +03:00
|
|
|
func fusermount(dir string, cfg *MountConfig) (*os.File, error) {
|
2015-07-24 09:18:07 +03:00
|
|
|
// Create a socket pair.
|
2015-07-24 09:10:41 +03:00
|
|
|
fds, err := syscall.Socketpair(syscall.AF_FILE, syscall.SOCK_STREAM, 0)
|
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("Socketpair: %v", err)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
// Wrap the sockets into os.File objects that we will pass off to fusermount.
|
2015-07-24 09:10:41 +03:00
|
|
|
writeFile := os.NewFile(uintptr(fds[0]), "fusermount-child-writes")
|
|
|
|
defer writeFile.Close()
|
|
|
|
|
|
|
|
readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads")
|
|
|
|
defer readFile.Close()
|
|
|
|
|
2016-02-29 05:47:56 +03:00
|
|
|
// Start fusermount, passing it a buffer in which to write stderr.
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2015-07-24 09:10:41 +03:00
|
|
|
cmd := exec.Command(
|
|
|
|
"fusermount",
|
2015-07-24 09:18:07 +03:00
|
|
|
"-o", cfg.toOptionsString(),
|
2015-07-24 09:10:41 +03:00
|
|
|
"--",
|
|
|
|
dir,
|
|
|
|
)
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
cmd.Env = append(os.Environ(), "_FUSE_COMMFD=3")
|
2015-07-24 09:10:41 +03:00
|
|
|
cmd.ExtraFiles = []*os.File{writeFile}
|
2016-02-29 05:47:56 +03:00
|
|
|
cmd.Stderr = &stderr
|
2015-07-24 09:10:41 +03:00
|
|
|
|
2016-02-29 05:47:56 +03:00
|
|
|
// Run the command.
|
|
|
|
err = cmd.Run()
|
2015-07-24 09:18:07 +03:00
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("running fusermount: %v\n\nstderr:\n%s", err, stderr.Bytes())
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
// Wrap the socket file in a connection.
|
2015-07-24 09:10:41 +03:00
|
|
|
c, err := net.FileConn(readFile)
|
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("FileConn: %v", err)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
// We expect to have a Unix domain socket.
|
2015-07-24 09:10:41 +03:00
|
|
|
uc, ok := c.(*net.UnixConn)
|
|
|
|
if !ok {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("Expected UnixConn, got %T", c)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
// Read a message.
|
2015-07-24 09:10:41 +03:00
|
|
|
buf := make([]byte, 32) // expect 1 byte
|
|
|
|
oob := make([]byte, 32) // expect 24 bytes
|
|
|
|
_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
|
2015-07-24 09:18:07 +03:00
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("ReadMsgUnix: %v", err)
|
2015-07-24 09:18:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the message.
|
2015-07-24 09:10:41 +03:00
|
|
|
scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
|
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("ParseSocketControlMessage: %v", err)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// We expect one message.
|
2015-07-24 09:10:41 +03:00
|
|
|
if len(scms) != 1 {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("expected 1 SocketControlMessage; got scms = %#v", scms)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
2015-07-24 09:10:41 +03:00
|
|
|
scm := scms[0]
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// Pull out the FD returned by fusermount
|
2015-07-24 09:10:41 +03:00
|
|
|
gotFds, err := syscall.ParseUnixRights(&scm)
|
|
|
|
if err != nil {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("syscall.ParseUnixRights: %v", err)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
2015-07-24 09:10:41 +03:00
|
|
|
if len(gotFds) != 1 {
|
2018-12-26 17:44:07 +03:00
|
|
|
return nil, fmt.Errorf("wanted 1 fd; got %#v", gotFds)
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// Turn the FD into an os.File.
|
2018-12-26 17:44:07 +03:00
|
|
|
return os.NewFile(uintptr(gotFds[0]), "/dev/fuse"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableFunc(flag uintptr) func(uintptr) uintptr {
|
|
|
|
return func(v uintptr) uintptr {
|
|
|
|
return v | flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func disableFunc(flag uintptr) func(uintptr) uintptr {
|
|
|
|
return func(v uintptr) uintptr {
|
|
|
|
return v &^ flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As per libfuse/fusermount.c:602: https://bit.ly/2SgtWYM#L602
|
|
|
|
var mountflagopts = map[string]func(uintptr) uintptr{
|
2020-03-23 10:51:36 +03:00
|
|
|
"rw": disableFunc(unix.MS_RDONLY),
|
|
|
|
"ro": enableFunc(unix.MS_RDONLY),
|
2018-12-26 17:44:07 +03:00
|
|
|
"suid": disableFunc(unix.MS_NOSUID),
|
|
|
|
"nosuid": enableFunc(unix.MS_NOSUID),
|
|
|
|
"dev": disableFunc(unix.MS_NODEV),
|
|
|
|
"nodev": enableFunc(unix.MS_NODEV),
|
|
|
|
"exec": disableFunc(unix.MS_NOEXEC),
|
|
|
|
"noexec": enableFunc(unix.MS_NOEXEC),
|
|
|
|
"async": disableFunc(unix.MS_SYNCHRONOUS),
|
|
|
|
"sync": enableFunc(unix.MS_SYNCHRONOUS),
|
|
|
|
"atime": disableFunc(unix.MS_NOATIME),
|
|
|
|
"noatime": enableFunc(unix.MS_NOATIME),
|
|
|
|
"dirsync": enableFunc(unix.MS_DIRSYNC),
|
|
|
|
}
|
|
|
|
|
|
|
|
var errFallback = errors.New("sentinel: fallback to fusermount(1)")
|
|
|
|
|
|
|
|
func directmount(dir string, cfg *MountConfig) (*os.File, error) {
|
2019-07-28 05:48:37 +03:00
|
|
|
// We use syscall.Open + os.NewFile instead of os.OpenFile so that the file
|
|
|
|
// is opened in blocking mode. When opened in non-blocking mode, the Go
|
|
|
|
// runtime tries to use poll(2), which does not work with /dev/fuse.
|
|
|
|
fd, err := syscall.Open("/dev/fuse", syscall.O_RDWR, 0644)
|
2018-12-26 17:44:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errFallback
|
|
|
|
}
|
2019-07-28 05:48:37 +03:00
|
|
|
dev := os.NewFile(uintptr(fd), "/dev/fuse")
|
2018-12-26 17:44:07 +03:00
|
|
|
// As per libfuse/fusermount.c:847: https://bit.ly/2SgtWYM#L847
|
|
|
|
data := fmt.Sprintf("fd=%d,rootmode=40000,user_id=%d,group_id=%d",
|
|
|
|
dev.Fd(), os.Getuid(), os.Getgid())
|
|
|
|
// As per libfuse/fusermount.c:749: https://bit.ly/2SgtWYM#L749
|
|
|
|
mountflag := uintptr(unix.MS_NODEV | unix.MS_NOSUID)
|
|
|
|
opts := cfg.toMap()
|
|
|
|
for k := range opts {
|
|
|
|
fn, ok := mountflagopts[k]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mountflag = fn(mountflag)
|
|
|
|
delete(opts, k)
|
|
|
|
}
|
|
|
|
delete(opts, "fsname") // handled via fstype mount(2) parameter
|
2020-03-11 11:51:26 +03:00
|
|
|
fstype := "fuse"
|
|
|
|
if subtype, ok := opts["subtype"]; ok {
|
|
|
|
fstype += "." + subtype
|
|
|
|
}
|
|
|
|
delete(opts, "subtype")
|
2018-12-26 17:44:07 +03:00
|
|
|
data += "," + mapToOptionsString(opts)
|
|
|
|
if err := unix.Mount(
|
|
|
|
cfg.FSName, // source
|
|
|
|
dir, // target
|
2020-03-11 11:51:26 +03:00
|
|
|
fstype, // fstype
|
2018-12-26 17:44:07 +03:00
|
|
|
mountflag, // mountflag
|
|
|
|
data, // data
|
|
|
|
); err != nil {
|
|
|
|
if err == syscall.EPERM {
|
|
|
|
return nil, errFallback
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dev, nil
|
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
2018-12-26 17:44:07 +03:00
|
|
|
// Begin the process of mounting at the given directory, returning a connection
|
|
|
|
// to the kernel. Mounting continues in the background, and is complete when an
|
|
|
|
// error is written to the supplied channel. The file system may need to
|
|
|
|
// service the connection in order for mounting to complete.
|
|
|
|
func mount(dir string, cfg *MountConfig, ready chan<- error) (*os.File, error) {
|
|
|
|
// On linux, mounting is never delayed.
|
|
|
|
ready <- nil
|
|
|
|
|
|
|
|
// Try mounting without fusermount(1) first: we might be running as root or
|
|
|
|
// have the CAP_SYS_ADMIN capability.
|
|
|
|
dev, err := directmount(dir, cfg)
|
|
|
|
if err == errFallback {
|
|
|
|
return fusermount(dir, cfg)
|
|
|
|
}
|
|
|
|
return dev, err
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|