情景
有一个硬盘的全盘映像文件,里面有三个分区。而你需要挂载其中的文件系统并修改文件。
方法
通过分区表信息计算出需要挂载的文件系统的偏移地址值,然后将这个映像文件的偏移地址开始的数据和 loop 设备映射起来,就可以使用 mount 挂载了。
操作
fdisk -lu hd.img
You must set cylinders.
You can do this from the extra functions menu.
Disk hd.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x9ed48710
Device Boot Start End Blocks Id System
hd.img1 63 2008124 1004031 83 Linux
hd.img2 2008125 4016249 1004062+ 83 Linux
hd.img3 4016250 8385929 2184840 83 Linux
通过上面的信息我们得知 Units = 512 bytes,分区的开始扇区数。下面计算偏移地址:分区偏移地址 = Units * 开始地址。例如 hd.img2 的偏移地址 = 512 * 2008125 = 1028160000 bytes
设置 loop 设备
sudo losetup -fo 偏移地址值 映像文件
挂载 loop 设备
sudo mount /dev/loop0 /mnt
到这里就挂载成功了。
卸载
sudo umount /dev/loop0
sudo losetup -d /dev/loop0
Over!