挂载img类型的操作系统文件镜像的方法

1. 先查看第一个空闲loop设备 
  1. sudo losetup -f  

        /dev/loop0  

2. 使用上一步得到的设备名,第一次创建loop设备 

  1. sudo losetup /dev/loop0 archlinux-2008.06-core-i686.img  


3. 查看信息 
  1. sudo fdisk -lu /dev/loop0     
  2. Disk /dev/loop0: 322 MB, 322469376 bytes  
  3. 53 heads, 12 sectors/track, 990 cylinders, total 629823 sectors  
  4. Units = sectors of 1 * 512 = 512 bytes  
  5. Disk identifier: 0x00000000  
  6.   
  7.       Device Boot      Start         End      Blocks   Id  System  
  8. /dev/loop0p1   *          63      629822      314880   83  Linux  
  9. Partition 1 has different physical/logical beginnings (non-Linux?):  
  10.      phys=(011) logical=(054)  
  11. Partition 1 has different physical/logical endings:  
  12.      phys=(395212) logical=(990153)  

我们可以看到,该镜像只有一个分区(loop0p1),从第63扇区开始(Start列),每扇区512字节(Units = sectors of 1 * 512 = 512 bytes),我们算出offset,下面mout命令会用到: 
  1. 63*512=32256  


4. mount 
  1. sudo losetup -o 32256 /dev/loop1 archlinux-2008.06-core-i686.img  
  2. sudo mount -o loop /dev/loop1 /mnt/  
  3. ls /mnt/  
  4. addons  archlive.sqfs  boot  lost+found  


事实上,fdisk可以直接查看img文件(虽然功能不全,下面会说到),mount可以自动创建loop设备,所以上面步骤可以简化为: 
I. 查看信息 
  1. sudo fdisk -lu archlinux-2008.06-core-i686.img  
  2. You must set cylinders.  
  3. You can do this from the extra functions menu.  
  4.   
  5. Disk archlinux-2008.06-core-i686.img: 0 MB, 0 bytes  
  6. 53 heads, 12 sectors/track, 0 cylinders, total 0 sectors  
  7. Units = sectors of 1 * 512 = 512 bytes  
  8. Disk identifier: 0x00000000  
  9.   
  10.                           Device Boot      Start         End      Blocks   Id  System  
  11. archlinux-2008.06-core-i686.img1   *          63      629822      314880   83  Linux  
  12. Partition 1 has different physical/logical beginnings (non-Linux?):  
  13.      phys=(011) logical=(054)  
  14. Partition 1 has different physical/logical endings:  
  15.      phys=(395212) logical=(990153)  

第一行抱怨不能得到cylinders,原因是普通文件上没有实现ioctl操作,我们可以看到0 cylinders,但这对我们不重要,关键是我们依然可以得到第一个分区(archlinux-2008.06-core-i686.img1)的偏移值 

II. 直接mount 
  1. sudo mount -o loop,offset=32256 archlinux-2008.06-core-i686.img /mnt/  
  2. ls /mnt/  
  3. addons  archlive.sqfs  boot  lost+found  

再来一个复杂点的:

$ /sbin/fdisk -lu disk.img
You must set cylinders.
You can do this from the extra functions menu.

Disk disk.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

      Device Boot      Start         End      Blocks   Id  System
disk.imgp1   *          63       96389       48163+  83  Linux
disk.imgp2           96390     2056319      979965   82  Linux swap / Solaris
disk.imgp3         2056320    78140159    38041920    5  Extended
disk.imgp5         2056383     3052349      497983+  83  Linux
disk.imgp6         3052413    10859939     3903763+  83  Linux
disk.imgp7        10860003    68372639    28756318+  83  Linux
disk.imgp8        68372703    76180229     3903763+  83  Linux
disk.imgp9        76180293    78140159      979933+  83  Linux
计算disk.imgp7的偏移,也就是文件系统的起始位置:
10860003 * 512 = 5560321536

losetup后就可以mount了:

# losetup /dev/loop0 disk.img -o $((10860003 * 512))
# file -s /dev/loop0
/dev/loop0: Linux rev 1.0 ext3 filesystem data
# mount /dev/loop0 /mnt
[...]
# umount /mnt
# losetup -d /dev/loop0

如果启动区后真的是一个文件系统的话,可以直接mount:

# mount -o loop,offset=$((10860003 * 512)) disk.img /mnt
[...]
# umount /mnt

 

转载于:https://my.oschina.net/renguijiayi/blog/176156

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值