为什么我可以在环回设备上装载一个映像,而不能在容器内的另一个映像上装载第二个映像?



我正在关注是否可以在 docker 容器中挂载 ISO? 让测试在 Docker 中运行,然后在 CI 中使用。下面的脚本是测试的一部分。我不明白的是为什么第一个图像会安装,而不是第二个图像。由于第一个安装,这不可能是权限、能力等问题。

$ dd if=/dev/zero of=testfs-1.img bs=1M count=32
32+0 records in
32+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0244791 s, 1.4 GB/s
$ dd if=/dev/zero of=testfs-2.img bs=1M count=32
32+0 records in
32+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0242179 s, 1.4 GB/s
$ mkfs -F testfs-1.img
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks:  1024/32768           done                            
Creating filesystem with 32768 1k blocks and 8192 inodes
Filesystem UUID: 7e752a1c-1f0c-4efb-8cd9-67f5922adf7b
Superblock backups stored on blocks: 
8193, 24577
Allocating group tables: 0/4   done                            
Writing inode tables: 0/4   done                            
Writing superblocks and filesystem accounting information: 0/4   done
$ mkfs -F testfs-2.img
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks:  1024/32768           done                            
Creating filesystem with 32768 1k blocks and 8192 inodes
Filesystem UUID: cdd08978-4a52-407b-81c6-98d908eadee8
Superblock backups stored on blocks: 
8193, 24577
Allocating group tables: 0/4   done                            
Writing inode tables: 0/4   done                            
Writing superblocks and filesystem accounting information: 0/4   done
$ mkdir -p src/mnt-1/hidden-1 src/mnt-2/hidden-2
$ ls -la src/
total 0
drwxr-xr-x 1 root root 20 Jun 13 23:15 .
drwxrwxrwx 1 root root 90 Jun 13 23:15 ..
drwxr-xr-x 1 root root 16 Jun 13 23:15 mnt-1
drwxr-xr-x 1 root root 16 Jun 13 23:15 mnt-2
$ losetup -f
/dev/loop15
$ mount -o loop testfs-1.img src/mnt-1
$ losetup -f
/dev/loop16
$ mount -o loop testfs-2.img src/mnt-2
mount: src/mnt-2: failed to setup loop device for /builds/krichter-sscce/docker-losetup/testfs-2.img.

测试是否来自bup,以防有人需要更多背景。

我正在使用图像ubuntu:18.04进行测试。

我可以用docker run --privileged -it ubuntu:18.04重现这一点,然后在容器内执行

#!/bin/sh
dd if=/dev/zero of=testfs-1.img bs=1M count=32
dd if=/dev/zero of=testfs-2.img bs=1M count=32
mkfs -F testfs-1.img
mkfs -F testfs-2.img
mkdir -p src/mnt-1/hidden-1 src/mnt-2/hidden-2
ls -la src/
losetup -f
mount -o loop testfs-1.img src/mnt-1
losetup -f
mount -o loop testfs-2.img src/mnt-2

bash.我的码头工人版本是

> docker version
Client: Docker Engine - Community
Version:           19.03.11
API version:       1.40
Go version:        go1.13.10
Git commit:        42e35e61f3
Built:             Mon Jun  1 09:12:34 2020
OS/Arch:           linux/amd64
Experimental:      false
Server: Docker Engine - Community
Engine:
Version:          19.03.11
API version:      1.40 (minimum version 1.12)
Go version:       go1.13.10
Git commit:       42e35e61f3
Built:            Mon Jun  1 09:11:07 2020
OS/Arch:          linux/amd64
Experimental:     false
containerd:
Version:          1.2.13
GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version:          1.0.0-rc10
GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version:          0.18.0
GitCommit:        fec3683

在 Ubuntu 20.04 主机上。

这是从 gitHub 中获取的,我把它放在这里没有任何优点,但可能会有所帮助。

看看这个链接。

Tony Fahrion 在创建循环设备时按照这些步骤修复了类似的问题。

前提条件:docker 容器必须在--privileged模式下运行。

LOOPDEV=$(losetup --find --show --partscan ${IMAGE_FILE})
# drop the first line, as this is our LOOPDEV itself, but we only want the child 
partitions
PARTITIONS=$(lsblk --raw --output "MAJ:MIN" --noheadings ${LOOPDEV} | tail -n +2)
COUNTER=1
for i in $PARTITIONS; do
MAJ=$(echo $i | cut -d: -f1)
MIN=$(echo $i | cut -d: -f2)
if [ ! -e "${LOOPDEV}p${COUNTER}" ]; then mknod ${LOOPDEV}p${COUNTER} b $MAJ $MIN; fi
COUNTER=$((COUNTER + 1))
done

这个技巧似乎与mknod功能有关。正如我之前所说,希望它有所帮助(只是太长了,无法将其放在评论中(

最新更新