Skip to content

Instantly share code, notes, and snippets.

@kaxing
Last active October 24, 2022 01:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaxing/f90e66a1041c8686d360a480b15be8fe to your computer and use it in GitHub Desktop.
Save kaxing/f90e66a1041c8686d360a480b15be8fe to your computer and use it in GitHub Desktop.
Mount Image file or loop device inside lxc container

A sum-up from: Can't access loop device: "mount failed: Unknown error -1" or "failed to setup loop device: Operation not permitted" · Issue #2980 · lxc/lxd

Versions that works:
  • lxd: 2.18-0ubuntu6
  • lxc --version: 2.18
  • host distro: Ubuntu 17.10 (Artful) with Kernel 4.13.0-19-generic
  • container distro: Ubuntu 16.04.3
Steps:
  1. Gain privilege and allow mount in the apparmor policy namespace:
lxc config set guest-container1 security.privileged true  
lxc config set guest-container1 raw.apparmor "mount,"
  1. Check currently used loop devices on host:
ls -ln /dev/loop*  
# eg.:
  brw-rw---- 1 0 6  7,   4 Dec 13 11:21 /dev/loop4
  brw-rw---- 1 0 6  7,   5 Dec 13 11:21 /dev/loop5
  brw-rw---- 1 0 6  7,   6 Dec 13 11:21 /dev/loop6
  brw-rw---- 1 0 6  7,   7 Dec 13 11:21 /dev/loop7
  brw-rw---- 1 0 6  7,   8 Dec 19 09:48 /dev/loop8
  brw-rw---- 1 0 6  7,   9 Dec 19 09:47 /dev/loop9
  crw-rw---- 1 0 6 10, 237 Dec 13 11:21 /dev/loop-control
  1. Added loop controller and device for container
lxc config device add guest-container1 loop-control unix-char path=/dev/loop-control  

(now add the loop{N+1} device for the guest accroding to step 2)

lxc config device add guest-container1 loop10 unix-block path=/dev/loop10  
  1. Restart and verify the changes:
lxc stop guest-container1 
lxc start guest-container1
lxc exec guest-container1 bash

(then, inside guest-container1 :)

mount the-image-file /the-mount-poiont  

Now continue to what you need to get done :)

@uvwild
Copy link

uvwild commented Mar 8, 2018

lxc restart security.privileged
is throwing an error?!?!
Shouldn't it be
lxc restart guest-container1

@nmd1
Copy link

nmd1 commented Jun 11, 2018

lxc config device add guest-container1 loop{N+1} unix-block path=/dev/loop{N+1}

Where N of course is largest numbered loop device.

This threw an error for me: error: The device path doesn't exist on the host and major/minor wasn't specified.
I used one of the existing /dev/loopN devices and the command worked just fine.

@bitinerant
Copy link

The line lxc restart security.privileged (twice) doesn't seem right. Should this be lxc restart guest-container1?

@bitinerant
Copy link

I get mount: /mnt: failed to setup loop device for ... on the last command - perhaps similar to what @nmd1 said.

@UweKrause
Copy link

worked for me. (Just wantet to let people know, since this is from 2018, last comment 2019)
Thanks for your summary!

@kaxing
Copy link
Author

kaxing commented Dec 16, 2020

Sorry, I didn't notice there are comments on this gist.

Thank you, @bitinerant. It is a mistake.
And thanks to @UweKrause let people know this is still working :)

@Mohamedemad4
Copy link

@kaxing . I am getting the same error as @nmd1. and tbh it makes sense. You can't add a loop device that doesn't exist. But I am still stuck with this and I would greatly appreciate any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment