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 

如上,可以發現,掛載后的路徑是可以讀寫的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM