golang在syscall.Mount中没有这样的设备。

huangapple go评论92阅读模式
英文:

golang no such device in syscall.Mount

问题

我正在尝试使用syscall.Mount函数将USB闪存驱动器挂载到某个文件夹,并自动检测文件系统。我从内核的netlink套接字中获取设备路径,并尝试将其挂载到/tmp/+devicename,在我的实例中,/dev/sdd1应该挂载到/tmp/sdd1

我在Go程序中有以下代码行:

if err := syscall.Mount(src, target, "auto", 0, "ro"); err != nil {
    log.Printf("Mount(\"%s\", \"%s\", \"auto\", 0, \"ro\")\n", src, target)
    log.Fatal(err)
}

输出:

main.go:47: Mount("/dev/sdd1", "/tmp/sdd1", "auto", 0, "ro")
main.go:48: no such device

我使用"sudo"以root权限运行应用程序,但似乎无法使用syscall包进行挂载。然而,如果我在终端中键入sudo mount /dev/sdd1 /tmp/sdd1,那么可以正常工作。

问题出在哪里?在使用系统调用时,设备路径是否有所不同?

感谢任何帮助。祝好!

英文:

I'm trying to use the syscall.Mount function to mount a usb pendrive and autodetect the filesystem to some folder. I fetch the device path from the kernel's netlink socket and try to mount it to /tmp/+devicename, in my instance /dev/sdd1 should be mounted to /tmp/sdd1

I have the following lines of code in a go program

if err := syscall.Mount(src, target, "auto", 0, "ro"); err != nil {
	log.Printf("Mount(\"%s\", \"%s\", \"auto\", 0, \"ro\")\n",src,target)
	log.Fatal(err)
}

Output:

main.go:47: Mount("/dev/sdd1", "/tmp/sdd1", "auto", 0, "ro")
main.go:48: no such device

I'm running the application with root privileges with "sudo", however it seems unable to mount using the syscall package. If i however in the terminal type sudo mount /dev/sdd1 /tmp/sdd1 then that works fine.

What is the issue here? Is the device path somehow different when using the system call?

Any help is appreciated.
Cheers

答案1

得分: 6

你没有指定你的操作系统,但我认为这个问题在许多实现上都是相同的。

在Linux的syscall.Mount中,它只是包装了mount(2),而mount(2)本身并不支持"auto"文件系统类型的概念。

mount(8)命令之所以能够使用"auto",是因为它有自己的魔法:

如果没有给出-t选项,或者指定了auto类型,mount将尝试猜测所需的类型。mount使用blkid库来猜测文件系统类型;如果找不到熟悉的类型,mount将尝试读取文件/etc/filesystems,如果该文件不存在,则读取/proc/filesystems。

英文:

You didn't specify your OS but I think the problem is the same on many implementations.

On Linux syscall.Mount just wraps mount(2) which doesn't itself support the concept of an "auto" fstype.

The reason the mount(8) command works with "auto" is because it does its own magic:

> If no -t option is given, or if the auto type is specified,
> mount will try to guess the desired type. Mount uses the
> blkid library for guessing the filesystem type
; if that does
> not turn up anything that looks familiar, mount will try to
> read the file /etc/filesystems, or, if that does not exist,
> /proc/filesystems.

huangapple
  • 本文由 发表于 2016年2月18日 21:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/35482778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定