4

I have a small Docker container from which I can access /dev/loop0. For this I've added to docker run:

--device=/dev/loop-control:/dev/loop-control \
--device=/dev/loop0:/dev/loop0 \

On this loopback device I'm creating two or more partitions with parted/fdisk.

Now, I need to mount both partitions but unfortunately I cannot access either /dev/loop0p1 or /dev/loop0p2 or any other partition which was dynamically created.

Is there any way I could access them without having to spawn the container --privileged?

Thank you.

emmerich
  • 53
  • 5

1 Answers1

3

Although the question is old this is still an issue.

The Linux kernel does not propagate device registration events to containers which is why /dev/* files do not appear within the container for devices that are added while the container is running.

As a workaround within the container you can look for missing device files and create them like the following shell script does:

FILTER='^loop'
lsblk --raw -a --output "NAME,MAJ:MIN" --noheadings | grep -E "$FILTER" | while read LINE; do
    DEV=/dev/$(echo $LINE | cut -d' ' -f1)
    MAJMIN=$(echo $LINE | cut -d' ' -f2)
    MAJ=$(echo $MAJMIN | cut -d: -f1)
    MIN=$(echo $MAJMIN | cut -d: -f2)
    [ -b "$DEV" ] || mknod "$DEV" b $MAJ $MIN
done

(I used FILTER='^(r|n)bd' to match /dev/rbdX and /dev/nbdX devices ceph creates dynamically in my case.)

Please be aware that the script above does not unregister devices that have been removed on the host. In case you also need to do this you can run the following as well (FILTER needs to be specified):

find /dev -mindepth 1 -maxdepth 1 -type b | cut -d/ -f3 | grep -E "$FILTER" | sort > /tmp/devs-created
lsblk --raw -a --output "NAME" --noheadings | grep -E "$FILTER" | sort > /tmp/devs-available
for ORPHAN in $(comm -23 /tmp/devs-created /tmp/devs-available); do
    rm /dev/$ORPHAN
done

If your devices are created dynamically you may want to run both scripts periodically within the container.

You still need to run your container --privileged in order to work with devices.

Also see this moby issue comment my script is based on.

mxg
  • 46
  • 4