可以将包含文件系统镜像的二进制文件进行循环挂载。我想将该二进制文件放入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 魔法?
您可以在 strace
下运行 losetup
和 mount
命令以观察它们的操作。对于 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
命令来执行。