Wren T.
Wren T.

Reputation: 606

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

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.

Upvotes: 2

Views: 1237

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54117

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.

Upvotes: 5

Related Questions