本地挂载openwrt镜像

以下编译了一个X86-64平台的openwrt镜像,完成之后在bin/targets/x86/64目录下文件,主要由内核文件,和两个根文件系统文件,格式分别为ext4和squashfs,还有两个内核与文件系统打包在一起的镜像文件(ext4和squashfs两种格式)。

$: ~/openwrt/bin/targets/x86/64$ ls -l
total 27360
drwxr-xr-x 4 kai kai    4096 Apr  6 02:51 ./
drwxr-xr-x 3 kai kai    4096 Jan 19 14:43 ../
-rw-r--r-- 1 kai kai 7761300 Jan 20 02:47 openwrt-x86-64-combined-ext4.img.gz
-rw-r--r-- 1 kai kai 6711833 Jan 20 02:47 openwrt-x86-64-combined-squashfs.img.gz
-rw-r--r-- 1 kai kai 3311875 Jan 20 02:46 openwrt-x86-64-generic-rootfs.tar.gz
-rw-r--r-- 1 kai kai 3585977 Jan 20 02:47 openwrt-x86-64-rootfs-ext4.img.gz
-rw-r--r-- 1 kai kai 2533138 Jan 20 02:47 openwrt-x86-64-rootfs-squashfs.img.gz
-rw-r--r-- 1 kai kai 4034624 Jan 20 02:46 openwrt-x86-64-vmlinuz

以下仅以ext4格式为例,对于文件系统镜像,如openwrt-x86-64-rootfs-ext4.img.gz,在解压缩之后,可直接挂在到本地目录查看:

~/openwrt/bin/targets/x86/64$ gzip -d openwrt-x86-64-rootfs-ext4.img.gz 
~/openwrt/bin/targets/x86/64$
~/openwrt/bin/targets/x86/64$ mkdir mnt 
~/openwrt/bin/targets/x86/64$ ls
mnt  openwrt-x86-64-rootfs-ext4.img

~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ sudo mount -o loop openwrt-x86-64-rootfs-ext4.img mnt                                 
~/openwrt/bin/targets/x86/64$ ls mnt/
bin  dev  etc  lib  lib64  lost+found  mnt  overlay  proc  rom  root  sbin  sys  tmp  usr  var  www
~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ sudo umount mnt

但是对于合并之后的镜像文件,如openwrt-x86-64-combined-ext4.img.gz,在解压缩之后,不能直接使用mount命令挂载,需要绕过镜像头部字段。

~/openwrt/bin/targets/x86/64$ gzip -d openwrt-x86-64-combined-ext4.img.gz 
~/openwrt/bin/targets/x86/64$ ls
mnt openwrt-x86-64-combined-ext4.img

以下使用fdisk显示img镜像文件中包含两个部分,img1为内核所在分区,img2未文件系统所在分区。

~/openwrt/bin/targets/x86/64$ fdisk -lu openwrt-x86-64-combined-ext4.img 
Disk openwrt-x86-64-combined-ext4.img: 272.5 MiB, 285736960 bytes, 558080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbecaa1aa

Device                            Boot Start    End Sectors  Size Id Type
openwrt-x86-64-combined-ext4.img1 *      512  33279   32768   16M 83 Linux
openwrt-x86-64-combined-ext4.img2      33792 558079  524288  256M 83 Linux
~/openwrt/bin/targets/x86/64$ 

首先计算第一个分区的起始位置,其由第512个扇区开始,每个扇区的大小为512字节,起始地址为:262144。在挂载时使用offset关键字指定起始地址。以下可见此分区包含启动相关的grub配置和内核镜像vmlinuz文件。

~/openwrt/bin/targets/x86/64$ echo $((512*512)) 
262144
~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ sudo mount -o loop,offset=262144 openwrt-x86-64-combined-ext4.img mnt
~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ ls mnt
boot  lost+found
~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ cd mnt
~/openwrt/bin/targets/x86/64/mnt$ 
~/openwrt/bin/targets/x86/64/mnt$ ls boot/
grub  vmlinuz
~/openwrt/bin/targets/x86/64/mnt$ 
~/openwrt/bin/targets/x86/64/mnt$ ls boot/grub/grub.cfg 

通过加载第二个分区,可见其中为根文件系统。

~/openwrt/bin/targets/x86/64$ echo $((33792*512))
17301504

~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ sudo mount -o loop,offset=17301504 openwrt-x86-64-combined-ext4.img mnt
~/openwrt/bin/targets/x86/64$ ls mnt
bin  dev  etc  lib  lib64  lost+found  mnt  overlay  proc  rom  root  sbin  sys  tmp  usr  var  www
~/openwrt/bin/targets/x86/64$ 
~/openwrt/bin/targets/x86/64$ sudo umount mnt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章