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.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?