dongwo5940 2018-01-20 10:35
浏览 168
已采纳

使用go挂载fstab中的条目

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())
  • 写回答

1条回答 默认 最新

  • dsjhejw3232 2018-01-20 12:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!