如何在没有超级用户权限的情况下创建 ext2 图像?

Equ*_*oid 6 filesystems ext2 disk-image

我需要生成几个 ext2 图像。最明显的方法是创建一个图像,安装它并复制内容。但它需要两次 root 权限(以 chown 文件和挂载映像)。我还发现了两个用于生成图像的工具:e2fsimage 和 genext2fs。

  • genext2fs 在生成时将图像放置在 RAM 中,但我的其中一张图像的大小为 ~30GiB。

  • e2fsimage 因图像大小的某些值而崩溃。

那么如何生成我的图像呢?如果该工具能够自行计算图像大小,那就太好了。

Cir*_*郝海东 5

mke2fs -d最小的可运行示例,无需sudo

mke2fs是 e2fsprogs 包的一部分。它由著名的 Linux 内核文件系统开发人员 Theodore Ts'o 编写,他于 2018 年在 Google 工作,上游源代码位于 kernel.org 下:https: //git.kernel.org/pub/scm/fs/ext2 /e2fsprogs因此,该存储库可以被视为 ext 文件系统操作的参考用户态实现:

#!/usr/bin/env bash
set -eu

root_dir=root
img_file=img.ext2

# Create a test directory to convert to ext2.
mkdir -p "$root_dir"
echo asdf > "${root_dir}/qwer"

# Create a 32M ext2 without sudo.
# If 32M is not enough for the contents of the directory,
# it will fail.
rm -f "$img_file"
mke2fs \
  -L '' \
  -N 0 \
  -O ^64bit \
  -d "$root_dir" \
  -m 5 \
  -r 1 \
  -t ext2 \
  "$img_file" \
  32M \
;

# Test the ext2 by mounting it with sudo.
# sudo is only used for testing.
mountpoint=mnt
mkdir -p "$mountpoint"
sudo mount "$img_file" "$mountpoint"
sudo ls -l "$mountpoint"
sudo cmp "${mountpoint}/qwer" "${root_dir}/qwer"
sudo umount "$mountpoint"
Run Code Online (Sandbox Code Playgroud)

GitHub 上游.

关键选项是-d,它选择用于图像的目录,它是提交0d4deba22e2aa95ad958b44972dc933fd0ebbc59中 v1.43 的一个相对较新的补充。

因此,它可以在 Ubuntu 18.04 上直接运行,其中 e2fsprogs 1.44.1-1,但不能在 Ubuntu 16.04 上运行,后者的版本为 1.42.13。

然而,我们可以像 Buildroot 一样,在 Ubuntu 16.04 上轻松地从源代码编译它:

git clone git://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git
cd e2fsprogs
git checkout v1.44.4
./configure
make -j`nproc`
./misc/mke2fs -h
Run Code Online (Sandbox Code Playgroud)

如果mke2fs失败:

__populate_fs: Operation not supported while setting xattrs for "qwer"
mke2fs: Operation not supported while populating file system
Run Code Online (Sandbox Code Playgroud)

添加选项时:

-E no_copy_xattrs
Run Code Online (Sandbox Code Playgroud)

例如,当根目录位于 NFS 中或tmpfs而不是 extX 中时,这是必需的,因为这些文件系统似乎没有扩展属性

mke2fs通常符号链接到mkfs.extX,并man mke2fs表示如果您将 call if 与此类符号链接一起使用,则-t隐含了 then 。

我是如何发现这一点以及如何解决未来的问题:Buildroot生成 ext2 映像而不需要 sudo ,如此处所示,因此我只是运行构建V=1并从最后出现的映像生成部分中提取命令。好的旧复制粘贴从来没有让我失望过。

TODO:描述如何解决以下问题:

一个镜像文件中的多个分区

请参阅: https: //stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-image-without-root-privileges/52850819#52850819


Equ*_*oid 3

找出e2fsimage崩溃的原因。这是当图片大小大于4GiB时int32溢出引起的。因此,解决方案是计算所需的块和索引节点,创建循环文件(truncate& mke2fs),然后e2fsimage-n参数一起使用(因此它不会创建循环文件,而是使用已经创建的循环文件)