大网数据广告活动
服务器运维问答

How can I give access to loopback devices which are created dynamically to a Docker container?

· 52CE 我爱测

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 m…

问题原文

原帖链接 · emmerich · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · mxg · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。