在Linux上是否有一个用于创建回环设备的Golang库?

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

Is there a golang library for creating loopback devices on Linux?

问题

我有一个文件。我们称之为“x”。我想在Linux中将“x”与回环设备关联起来。从bash中,我会输入:

losetup -f x

这将创建类似于/dev/loop0的东西,我可以将其挂载到任何我想要的地方。当它没有被挂载时,我可以使用类似于以下命令销毁回环文件:

losetup -d /dev/loop0

我想在golang中能够做同样的事情,而不是从go程序中调用losetup。是否有一个库实现了这个功能?我查看了losetup的源代码,它看起来有点棘手,特别是创建部分。

英文:

I have a file. Let's call it "x". I'd like to associate "x" with a loopback device in linux. From bash, I'd type:

losetup -f x

That will create something like /dev/loop0 that I can mount wherever I'd like. When it's not mounted, I can destroy the loopback file with something like:

losetup -d /dev/loop0

I'd like to be able to do the same in golang without calling losetup from the go program. Is there a library somewhere to that implements this as I looked through the losetup source, and it looks somewhat tricky, especially the create part.

答案1

得分: 5

我不认为有一个losetup库 在Linux上是否有一个用于创建回环设备的Golang库?

如果你真的不想从你的代码中调用losetup(在我看来这是最明智的做法),那么我会将losetup.c源代码复制到我的go项目中,重命名main()函数,然后使用cgo调用重命名的main函数或相关的内部函数。

英文:

I don't think there is an losetup library 在Linux上是否有一个用于创建回环设备的Golang库?

If you really don't want to call losetup from your code (which is the most sensible thing to do IMHO) then I'd copy the losetup.c source code into my go project, rename the main() function and then use cgo to call the renamed main function or the relevant internals directly.

huangapple
  • 本文由 发表于 2013年3月19日 05:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/15487643.html
匿名

发表评论

匿名网友

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

确定