2015-07-24 09:10:49 +03:00
|
|
|
package fuse
|
2015-07-24 09:10:41 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func lineLogger(wg *sync.WaitGroup, prefix string, r io.ReadCloser) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
switch line := scanner.Text(); line {
|
|
|
|
case `fusermount: failed to open /etc/fuse.conf: Permission denied`:
|
|
|
|
// Silence this particular message, it occurs way too
|
|
|
|
// commonly and isn't very relevant to whether the mount
|
|
|
|
// succeeds or not.
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
log.Printf("%s: %s", prefix, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Printf("%s, error reading: %v", prefix, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 09:18: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) (dev *os.File, err error) {
|
|
|
|
// On linux, mounting is never delayed.
|
|
|
|
ready <- nil
|
2015-07-24 09:10:41 +03:00
|
|
|
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("Socketpair: %v", err)
|
|
|
|
return
|
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()
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
// Start fusermount, passing it pipes for stdout and stderr.
|
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}
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("StdoutPipe: %v", err)
|
|
|
|
return
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
2015-07-24 09:10:41 +03:00
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("StderrPipe: %v", err)
|
|
|
|
return
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 09:18:07 +03:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Starting fusermount: %v", err)
|
|
|
|
return
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// Log fusermount output until it closes stdout and stderr.
|
|
|
|
var wg sync.WaitGroup
|
2015-07-24 09:10:41 +03:00
|
|
|
wg.Add(2)
|
|
|
|
go lineLogger(&wg, "mount helper output", stdout)
|
|
|
|
go lineLogger(&wg, "mount helper error", stderr)
|
|
|
|
wg.Wait()
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// Wait for the command.
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("fusermount: %v", err)
|
|
|
|
return
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("FileConn: %v", err)
|
|
|
|
return
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("Expected UnixConn, got %T", c)
|
|
|
|
return
|
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 {
|
|
|
|
err = fmt.Errorf("ReadMsgUnix: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the message.
|
2015-07-24 09:10:41 +03:00
|
|
|
scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
|
|
|
|
if err != nil {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("ParseSocketControlMessage: %v", err)
|
|
|
|
return
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("expected 1 SocketControlMessage; got scms = %#v", scms)
|
|
|
|
return
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("syscall.ParseUnixRights: %v", err)
|
|
|
|
return
|
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 {
|
2015-07-24 09:18:07 +03:00
|
|
|
err = fmt.Errorf("wanted 1 fd; got %#v", gotFds)
|
|
|
|
return
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|
2015-07-24 09:18:07 +03:00
|
|
|
|
|
|
|
// Turn the FD into an os.File.
|
|
|
|
dev = os.NewFile(uintptr(gotFds[0]), "/dev/fuse")
|
|
|
|
|
|
|
|
return
|
2015-07-24 09:10:41 +03:00
|
|
|
}
|