2

Is there a way to mount a ssh device with Go using an entry in fstab where the mount options are defined. I have been trying syscall.Mount without success.

func main() {

    src := "jeanluc@<remote IP>:/home/jeanluc"
    target := "/home/jeanluc/my-mnt/ursule_jeanluc"
    fs := "fuse.sshfs"

    err := syscall.Mount(src, target, fs, 0, "rw")

    if err != nil {
        log.Fatal(err)
    }
}

2018/01/20 11:31:07 operation not permitted exit status 1

A user mount using the fstab entry works fine.

sshfs#jeanluc@<remote IP>:/home/jeanluc /home/jeanluc/my-mnt/ursule_jeanluc fuse user,noauto,uid=1000,gid=1000,follow_symlinks,defaults 0 0

Edit:

Following Marc's advise below here is what worked for me:

cmd := exec.Command("mount /home/jeanluc/my-mnt/ursule_jeanluc")

// capture STDOUT
var out bytes.Buffer
cmd.Stdout = &out

// run cmd
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}

// print STDOUT
fmt.Printf("%s", out.String())
ripat
  • 3,076
  • 6
  • 26
  • 38
  • I run it as suid. When I run it as root i get `invalid argument`. Running it as root is not an option if I can avoid it as I want to have the file permissions for the user. My attempt to use `os/exec` and `syscall.Exec` failed also. – ripat Jan 20 '18 at 12:19
  • Can you show those attempts as well, including code and output. And the output of a normal `mount` command you want to run. – Marc Jan 20 '18 at 12:22

1 Answers1

1

You need to run your binary either as root, or owned by root and with the setuid bit.

This is because /etc/fstab is used by mount (8) (the command), not mount (2), so the user attribute in your fstab entry does nothing (nor does the rest of the entry).

Indeed, the mount (2) man page clearly states:

Appropriate privilege (Linux: the CAP_SYS_ADMIN capability) is required to mount file systems.

If you cannot run as a privileged user or want to make use of the /etc/fstab entry, you could always exec the actual mount command.

Marc
  • 19,394
  • 6
  • 47
  • 51
  • As mentioned in my comment above, I also failed to use os/exec for some reason. Most probably because of me. I'll keep on trying. Thanks for your input. – ripat Jan 20 '18 at 12:23
  • I think there are oddities when using `mount (2)` with fuse. I suggest trying `strace mount ....` and seeing how it invokes `mount`. But yes, getting the `mount (8)` command to work first would be good. – Marc Jan 20 '18 at 12:35
  • Working fine now with `os/exec` calling the mount(8) command. – ripat Jan 20 '18 at 20:48