运行无根 Podman 时如何在容器中挂载 squashfs 文件?
Posted
技术标签:
【中文标题】运行无根 Podman 时如何在容器中挂载 squashfs 文件?【英文标题】:How to mount a squashfs file in a container when running rootless Podman? 【发布时间】:2021-12-15 13:19:50 【问题描述】:我有一个使用 mksquashfs 创建的 squashfs 文件。如何从容器内挂载存档?我正在使用 Podman 3.4.1 版并且在计算机系统上有一个普通用户(所以我正在运行无根 Podman)。
【问题讨论】:
【参考方案1】:squashfuse 可用于在容器内挂载 squashfs 存档。
Fedora 34 计算机上的示例
从这个 Dockerfile 构建一个 squashfuse 容器镜像
FROM docker.io/library/fedora
RUN dnf install -y squashfuse fuse
使用 podman 构建
[testuser@laptop ~]$ mkdir squashfuse_container
[testuser@laptop ~]$ emacs -nw squashfuse_container/Dockerfile
[testuser@laptop ~]$ cat squashfuse_container/Dockerfile
FROM docker.io/library/fedora
RUN dnf install -y squashfuse fuse
[testuser@laptop ~]$ podman --version
podman version 3.4.1
[testuser@laptop ~]$ podman build -q -t squashfuse squashfuse_container/
1c2a97ccd71698978973ddc534b96f955a96aad89aef447456d3d327957faeb6
[testuser@laptop ~]$
使用 mksquashfs
创建一个 squashfs 文件 (data.squash)[testuser@laptop ~]$ mkdir data
[testuser@laptop ~]$ echo foo > data/file1.txt
[testuser@laptop ~]$ echo bar > data/file2.txt
[testuser@laptop ~]$ mkdir res
[testuser@laptop ~]$ mksquashfs data res/data.squash
Parallel mksquashfs: Using 8 processors
Creating 4.0 filesystem on res/data.squash, block size 131072.
[================================================================|] 2/2 100%
Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 131072
compressed data, compressed metadata, compressed fragments,
compressed xattrs, compressed ids
duplicates are removed
Filesystem size 0.35 Kbytes (0.00 Mbytes)
76.28% of uncompressed filesystem size (0.46 Kbytes)
Inode table size 55 bytes (0.05 Kbytes)
35.71% of uncompressed inode table size (154 bytes)
Directory table size 36 bytes (0.04 Kbytes)
75.00% of uncompressed directory table size (48 bytes)
Xattr table size 54 bytes (0.05 Kbytes)
100.00% of uncompressed xattr table size (54 bytes)
Number of duplicate files found 0
Number of inodes 3
Number of files 2
Number of fragments 1
Number of symbolic links 0
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 1
Number of ids (unique uids + gids) 1
Number of uids 1
testuser (1007)
Number of gids 1
testuser (1007)
[testuser@laptop ~]$ ls -l res/
total 4
-rw-r--r--. 1 testuser testuser 4096 Oct 31 11:06 data.squash
[testuser@laptop ~]$
运行 podman run,然后使用 squashfuse 挂载 squashfs 存档
[testuser@laptop ~]$ podman run --rm \
--cap-add=sys_admin \
--device /dev/fuse \
-v ./res/data.squash:/a:Z \
-ti localhost/squashfuse
[root@1e6860530222 /]# mkdir /mnt/dir
[root@1e6860530222 /]# squashfuse /a /mnt/dir
[root@1e6860530222 /]# ls -l /mnt/dir
total 0
-rw-r--r--. 1 1007 1007 4 Oct 31 09:31 file1.txt
-rw-r--r--. 1 1007 1007 4 Oct 31 09:31 file2.txt
[root@1e6860530222 /]# cat /mnt/dir/file1.txt
foo
[root@1e6860530222 /]# cat /mnt/dir/file2.txt
bar
[root@1e6860530222 /]# exit
exit
[testuser@laptop ~]$
同一行的命令。 (删除此非交互版本的-ti
):
podman run --rm --cap-add=sys_admin --device /dev/fuse -v ./res/data.squash:/a:Z localhost/squashfuse /bin/bash -c "mkdir /mnt/dir ; squashfuse /a /mnt/dir; ls -l /mnt/dir; cat /mnt/dir/file1.txt; cat /mnt/dir/file2.txt"
【讨论】:
以上是关于运行无根 Podman 时如何在容器中挂载 squashfs 文件?的主要内容,如果未能解决你的问题,请参考以下文章