英文:
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库
如果你真的不想从你的代码中调用losetup(在我看来这是最明智的做法),那么我会将losetup.c源代码复制到我的go项目中,重命名main()函数,然后使用cgo调用重命名的main函数或相关的内部函数。
英文:
I don't think there is an losetup library
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论