linuxgomountfstab

Mount entry in fstab with go


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())

Solution

  • 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.