linux 读写方式挂载raw格式的 *.img 系统镜像

  1. 查看文件信息
➜  Wdir fdisk -l ../Downloads/openwrt-19.07.4-x86-generic-combined-ext4.img
Disk ../Downloads/openwrt-19.07.4-x86-generic-combined-ext4.img:272.5 MiB,285736960 字节,558080 个扇区
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xaebc7528

设备                                                        启动  起点   末尾   扇区  大小 Id 类型
../Downloads/openwrt-19.07.4-x86-generic-combined-ext4.img1 *      512  33279  32768   16M 83 Linux
../Downloads/openwrt-19.07.4-x86-generic-combined-ext4.img2      33792 558079 524288  256M 83 Linux

如上,本次查看的是 openwrt 镜像信息。根据查看结果可以知道,这个 img 文件由两部分组成,而我关心的只是文件系统,所有是第二部分。
如上图,第二个分区起点是 33792。

  1. 挂载分区
➜  Wdir mount -t ext4 -o loop,rw,offset=$((33792*512)) ../Downloads/openwrt-19.07.4-x86-generic-combined-ext4.img ./mnt

没有错误发生,成功挂载到当前目录的 mnt 文件夹

  1. 切换到目标文件系统
➜  Wdir chroot ./mnt 
chroot: failed to run command ‘/usr/bin/zsh’: No such file or directory
➜  Wdir SHELL=/bin/ash chroot ./mnt
BusyBox v1.30.1 () built-in shell (ash)
/ # whomai
/bin/ash: whomai: not found
/ # /bin/ls
bin         etc         lost+found  overlay     rom         sbin        tmp         var         xx
dev         lib         mnt         proc        root        sys         usr         www
/ # export PATH=$PATH:/bin/:/sbin/
/ # touch xx
/ # ls
bin         etc         lost+found  overlay     rom         sbin        tmp         var         xx
dev         lib         mnt         proc        root        sys         usr         www
➜  Wdir 

如上,可以发现,挂载后的路径是可以读写的

原文地址:https://www.cnblogs.com/sinpo828/p/14080935.html