c++ - 挂载文件系统映像放置在静态变量中
问题描述
可以循环挂载包含文件系统映像的二进制文件。我想将该二进制文件放入 C 静态变量中,然后挂载它。这可能吗?如果是这样,我需要什么 C API 魔法?
解决方案
我们需要采取几个步骤
- 创建文件系统映像
- 将此图像嵌入到二进制文件中
- 将嵌入的图像挂载为只读文件系统。
听起来您已经知道如何执行词干 1 和 2,但不知道如何执行第 3 步。
我准备了ab.sqfs
图像,a.out
其中包含该图像的偏移量0x3010
。以下是挂载此文件系统的命令:
# optional, look at the bytes of the filesystem from step 1
xxd -l 16 -g1 ab.sqfs
00000000: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# optional: confirm that we have the correct file offset to the start of FS image
xxd -l 16 -g1 -s 0x3010 a.out
00003010: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# create a loop device which "points" into the file:
sudo losetup -r -o 0x3010 loop0 a.out
losetup: a.out: Warning: file does not fit into a 512-byte sector; the end of the file will be ignored.
# optional: confirm that (just created) /dev/loop0 contains expected bytes
sudo xxd -l 16 -g1 /dev/loop0
00000000: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# create directory on which the FS will be mounted
mkdir /tmp/mnt
# finally mount the FS:
sudo mount -oro /dev/loop0 /tmp/mnt
# optional: verify contents of /tmp/mnt
ls -lR /tmp/mnt
... has exactly the files I've put into it.
我需要什么 C API 魔法?
您可以运行losetup
和mount
命令strace
来观察它们的作用。的关键步骤losetup
是:
openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/dev/loop0", O_RDONLY|O_CLOEXEC) = 4
ioctl(4, LOOP_SET_FD, 3) = 0
ioctl(4, LOOP_SET_STATUS64, {lo_offset=0x3010, lo_number=0, lo_flags=LO_FLAGS_READ_ONLY, lo_file_name="/tmp/a.out", ...}) = 0
对于mount
:
mount("/dev/loop0", "/tmp/mnt", "squashfs", MS_RDONLY, NULL) = 0
这些调用可以由应用程序本身执行,也可以通过“脱壳”到外部losetup
和mount
命令来执行。
推荐阅读
- python - Python Selenium send_keys ~ 如何发送高级 Unicode 字符
- python - 元组与生成器表达式。为什么性能翻转,序列越长?
- css - 为什么 google-chrome-devtools 通过 XPath 识别的元素数量少于通过 CssSelector 识别的元素数量
- c# - 在面板中生成时预制件的尺寸太小
- node.js - 如何在路由中获取完整的 url 作为参数
- r - 在 r 中使用 rvest 进行网页抓取以从网页中获取内部信息
- swift - 通过下拉刷新逐一获取行,想要在 viewdidload 上的整个数组
- javascript - 带有元音变音 (ü) 的域上的 location.href 报告不同的域
- javascript - 根据数组中的对象状态设置数组状态
- angular - 我们可以/如何通过 @ViewChild 以角度访问孙子组件吗