I'm attempting to use a loop device inside a container, to mount some image file:
> sudo losetup /dev/loop0 test.img
losetup: /dev/loop0: failed to set up loop device: No such file or directory
/dev/loop0
indeed doesn't exist, and
> sudo mknod /dev/loop0 b 7 0
mknod: ‘/dev/loop0’: Operation not permitted
How can I make this work? Does the container need some cgroup permission that it might not have?
If you're using systemd-nspawn, start up your container with the
--capability=CAP_MKNOD
command line switch. This will allow you to create device nodes inside your container. Then create a loop device like this:Remember that this loop device is shared with the host and is called
/dev/loop0
there as well. And that it is now possible to access host devices if you know the major and minor numbers. There could also be other consequences that I haven't thought about. Be warned.Loop devices are provided by a kernel module. Therefore, you need special privileges to access them. You also need them to be exposed into your container, or you need to manually create the device files.
The quick answer
An alternative
This almost works
However I get this error:
See this link for more information.
A note on systemd-nspawn man page: