我正在关注 Is it possible to mount an ISO inside a docker container?在 Docker 中运行一个测试,然后在 CI 中使用。下面的脚本是测试的一部分。我不明白的是为什么第一个图像安装,而不是第二个。由于第一次挂载,这不是权限、功能等问题。afaik。
$ 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 版本是> 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 和 我没有任何功劳把它放在这里,但可能会有所帮助。
看看这个link .
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
有关。功能。正如我之前所说,希望它有所帮助(把它放在评论中太长了)
关于docker - 为什么我可以在环回设备上安装一个镜像,而不能在容器内的另一个设备上安装第二个镜像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62366670/