设为首页 收藏本站
查看: 1134|回复: 0

[经验分享] 如何挂载一个镜像文件(how to mount an image file)

[复制链接]

尚未签到

发表于 2017-6-25 09:59:35 | 显示全部楼层 |阅读模式
如何挂载一个镜像文件(how to mount an image file)

08/16/2012master  4 Comments   在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(image file),我们可以将Guest系统启动起来,然后对镜像文件进行修改,不过这样有时也是比较麻烦,其实也是可以将镜像文件直接进行mount的,可以用如下两种办法。
(本文写的RAW格式镜像文件的挂载;我的另一篇文章写了“如何挂载qcow2格式的镜像文件”)
  方法一:找出分区开始的开始位置,使用mount命令的offset参数偏移掉前面不需要的,即可得到真正的分区。其具体步骤如下:
1. 用“fdisk -lu my.img”查询image信息;
2. 计算image内分区开始的地方(计算offset),用从N号sector(扇区)开始,则offset=N*M (M为一个sector的大小,一般为512)
3. 使用mount命令挂载为loop设备即可。  (如LVM分区,则会较复杂,请见本文最后的介绍)
在Linux系统上具体操作演示如下:



[iyunv@jay-linux image]# fdisk -lu rhel6u2.imgYou must set cylinders.You can do this from the extra functions menu.Disk rhel6u2.img: 0 MB, 0 bytes255 heads, 63 sectors/track, 0 cylinders, total 0 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00048b34Device Boot      Start         End      Blocks   Id  Systemrhel6u2.img1   *        2048     1026047      512000   83  LinuxPartition 1 does not end on cylinder boundary.rhel6u2.img2         1026048   104857599    51915776   8e  Linux LVMPartition 2 has different physical/logical endings:phys=(1023, 254, 63) logical=(6527, 21, 22)[iyunv@jay-linux image]# echo $((2048*512))1048576[iyunv@jay-linux image]# mount -o loop,offset=1048576 rhel6u2.img /media/[iyunv@jay-linux image]# cd /media/[iyunv@jay-linux media]# lsconfig-2.6.32-220.el6.x86_64         initramfs-3.5.0.img               System.map-2.6.32-279.el6.x86_64config-2.6.32-279.el6.x86_64         lost+found                        System.map-3.5.0efi                                  symvers-2.6.32-220.el6.x86_64.gz  vmlinuzgrub                                 symvers-2.6.32-279.el6.x86_64.gz  vmlinuz-2.6.32-220.el6.x86_64initramfs-2.6.32-220.el6.x86_64.img  System.map                        vmlinuz-2.6.32-279.el6.x86_64initramfs-2.6.32-279.el6.x86_64.img  System.map-2.6.32-220.el6.x86_64  vmlinuz-3.5.0[iyunv@jay-linux media]# echo $((1026048*512))525336576[iyunv@jay-linux media]# umount /media[iyunv@jay-linux media]# cd /home/image/[iyunv@jay-linux image]# umount /media[iyunv@jay-linux image]# mount -o loop,offset=525336576 rhel6u2.img /media/mount: unknown filesystem type 'LVM2_member'(镜像文件中的LVM分区mount的问题,本文最后单独说明)[iyunv@jay-linux image]# fdisk -lu sles11sp2-i386.imgYou must set cylinders.You can do this from the extra functions menu.Disk sles11sp2-i386.img: 0 MB, 0 bytes255 heads, 63 sectors/track, 0 cylinders, total 0 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0002d3beDevice Boot      Start         End      Blocks   Id  Systemsles11sp2-i386.img1            2048     4208639     2103296   82  Linux swap / SolarisPartition 1 does not end on cylinder boundary.sles11sp2-i386.img2   *     4208640    41943039    18867200   83  LinuxPartition 2 has different physical/logical endings:phys=(1023, 254, 63) logical=(2610, 212, 34)[iyunv@jay-linux image]# echo $((4208640*512))2154823680[iyunv@jay-linux image]# mount -o loop,offset=2154823680 sles11sp2-i386.img /media[iyunv@jay-linux image]# cd /media/[iyunv@jay-linux media]# lsbin   dev  home  lost+found  mnt  proc  sbin     srv      sys  usrboot  etc  lib   media       opt  root  selinux  success  tmp  var[iyunv@jay-linux image]# umount /media/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


[iyunv@jay-linux image]# fdisk -lu rhel6u2.img
You must set cylinders.
You can do this from the extra functions menu.

Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34

      Device Boot      Start         End      Blocks   Id  System
rhel6u2.img1   *        2048     1026047      512000   83  Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2         1026048   104857599    51915776   8e  Linux LVM
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(6527, 21, 22)
[iyunv@jay-linux image]# echo $((2048*512))
1048576
[iyunv@jay-linux image]# mount -o loop,offset=1048576 rhel6u2.img /media/
[iyunv@jay-linux image]# cd /media/
[iyunv@jay-linux media]# ls
config-2.6.32-220.el6.x86_64         initramfs-3.5.0.img               System.map-2.6.32-279.el6.x86_64
config-2.6.32-279.el6.x86_64         lost+found                        System.map-3.5.0
efi                                  symvers-2.6.32-220.el6.x86_64.gz  vmlinuz
grub                                 symvers-2.6.32-279.el6.x86_64.gz  vmlinuz-2.6.32-220.el6.x86_64
initramfs-2.6.32-220.el6.x86_64.img  System.map                        vmlinuz-2.6.32-279.el6.x86_64
initramfs-2.6.32-279.el6.x86_64.img  System.map-2.6.32-220.el6.x86_64  vmlinuz-3.5.0
[iyunv@jay-linux media]# echo $((1026048*512))
525336576
[iyunv@jay-linux media]# umount /media
[iyunv@jay-linux media]# cd /home/image/
[iyunv@jay-linux image]# umount /media
[iyunv@jay-linux image]# mount -o loop,offset=525336576 rhel6u2.img /media/
mount: unknown filesystem type 'LVM2_member'
(镜像文件中的LVM分区mount的问题,本文最后单独说明)


[iyunv@jay-linux image]# fdisk -lu sles11sp2-i386.img
You must set cylinders.
You can do this from the extra functions menu.

Disk sles11sp2-i386.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x0002d3be

             Device Boot      Start         End      Blocks   Id  System
sles11sp2-i386.img1            2048     4208639     2103296   82  Linux swap / Solaris
Partition 1 does not end on cylinder boundary.
sles11sp2-i386.img2   *     4208640    41943039    18867200   83  Linux
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(2610, 212, 34)
[iyunv@jay-linux image]# echo $((4208640*512))
2154823680
[iyunv@jay-linux image]# mount -o loop,offset=2154823680 sles11sp2-i386.img /media
[iyunv@jay-linux image]# cd /media/
[iyunv@jay-linux media]# ls
bin   dev  home  lost+found  mnt  proc  sbin     srv      sys  usr
boot  etc  lib   media       opt  root  selinux  success  tmp  var
[iyunv@jay-linux image]# umount /media/
  方法二:用kpartx建立分区映射后,再mount映射后的设备即可,操作实例如下:


[iyunv@jay-linux image]# kpartx -av sles11sp2-i386.imgadd map loop3p1 (253:2): 0 4206592 linear /dev/loop3 2048add map loop3p2 (253:3): 0 37734400 linear /dev/loop3 4208640[iyunv@jay-linux image]# mount /dev/mapper/loop3p2 /media/[iyunv@jay-linux image]# ls /media/bin   dev  home  lost+found  mnt  proc  sbin     srv      sys  usrboot  etc  lib   media       opt  root  selinux  success  tmp  var[iyunv@jay-linux image]# umount /media/[iyunv@jay-linux image]# mount /dev/mapper/loop3p1 /media//dev/mapper/loop3p1 looks like swapspace - not mountedmount: you must specify the filesystem type(其中的交换分区,我也还不知道是否可以mount;其实mount交换分区也没意义)(使用完成后,卸载挂载点、删除映射关系即可)[iyunv@jay-linux image]# umount /media/[iyunv@jay-linux image]# kpartx -d sles11sp2-i386.imgloop deleted : /dev/loop3


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


[iyunv@jay-linux image]# kpartx -av sles11sp2-i386.img
add map loop3p1 (253:2): 0 4206592 linear /dev/loop3 2048
add map loop3p2 (253:3): 0 37734400 linear /dev/loop3 4208640

[iyunv@jay-linux image]# mount /dev/mapper/loop3p2 /media/

[iyunv@jay-linux image]# ls /media/
bin   dev  home  lost+found  mnt  proc  sbin     srv      sys  usr
boot  etc  lib   media       opt  root  selinux  success  tmp  var
[iyunv@jay-linux image]# umount /media/
[iyunv@jay-linux image]# mount /dev/mapper/loop3p1 /media/
/dev/mapper/loop3p1 looks like swapspace - not mounted
mount: you must specify the filesystem type
(其中的交换分区,我也还不知道是否可以mount;其实mount交换分区也没意义)

(使用完成后,卸载挂载点、删除映射关系即可)
[iyunv@jay-linux image]# umount /media/
[iyunv@jay-linux image]# kpartx -d sles11sp2-i386.img
loop deleted : /dev/loop3
  关于LVM的mount,可以参考文末参考资料中的vpsee的文章(我也是试多次没做成功,才偶然看到这篇文章的),我对镜像文件中LVM分区的mount操作如下供参考。


[iyunv@jay-linux image]# fdisk -lu rhel6u2.imgYou must set cylinders.You can do this from the extra functions menu.Disk rhel6u2.img: 0 MB, 0 bytes255 heads, 63 sectors/track, 0 cylinders, total 0 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00048b34Device Boot      Start         End      Blocks   Id  Systemrhel6u2.img1   *        2048     1026047      512000   83  LinuxPartition 1 does not end on cylinder boundary.rhel6u2.img2         1026048   104857599    51915776   8e  Linux LVMPartition 2 has different physical/logical endings:phys=(1023, 254, 63) logical=(6527, 21, 22)[iyunv@jay-linux image]# echo $((1026048*512))525336576[iyunv@jay-linux image]# losetup /dev/loop0 rhel6u2.img -o 525336576[iyunv@jay-linux image]# pvscanPV /dev/loop0   VG VolGroup   lvm2 [49.51 GiB / 0    free]Total: 1 [49.51 GiB] / in use: 1 [49.51 GiB] / in no VG: 0 [0   ][iyunv@jay-linux image]# vgchange -ay VolGroup2 logical volume(s) in volume group "VolGroup" now active[iyunv@jay-linux image]# lvsLV      VG       Attr     LSize  Pool Origin Data%  Move Log Copy%  Convertlv_root VolGroup -wi-a--- 45.57glv_swap VolGroup -wi-a---  3.94g[iyunv@jay-linux image]# mount /dev/VolGroup/lv_root /media/[iyunv@jay-linux image]# ls /media/bin   cgroup  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  varboot  dev     home  lib64  media       mnt   opt  root  selinux  sys  usr(使用完后的卸载操作,如下)[iyunv@jay-linux image]# umount /media/[iyunv@jay-linux image]# vgchange -an VolGroup0 logical volume(s) in volume group "VolGroup" now active[iyunv@jay-linux image]# losetup -d /dev/loop0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


[iyunv@jay-linux image]# fdisk -lu rhel6u2.img
You must set cylinders.
You can do this from the extra functions menu.

Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34

      Device Boot      Start         End      Blocks   Id  System
rhel6u2.img1   *        2048     1026047      512000   83  Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2         1026048   104857599    51915776   8e  Linux LVM
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(6527, 21, 22)
[iyunv@jay-linux image]# echo $((1026048*512))
525336576
[iyunv@jay-linux image]# losetup /dev/loop0 rhel6u2.img -o 525336576
[iyunv@jay-linux image]# pvscan
  PV /dev/loop0   VG VolGroup   lvm2 [49.51 GiB / 0    free]
  Total: 1 [49.51 GiB] / in use: 1 [49.51 GiB] / in no VG: 0 [0   ]
[iyunv@jay-linux image]# vgchange -ay VolGroup
  2 logical volume(s) in volume group "VolGroup" now active
[iyunv@jay-linux image]# lvs
  LV      VG       Attr     LSize  Pool Origin Data%  Move Log Copy%  Convert
  lv_root VolGroup -wi-a--- 45.57g
  lv_swap VolGroup -wi-a---  3.94g
[iyunv@jay-linux image]# mount /dev/VolGroup/lv_root /media/
[iyunv@jay-linux image]# ls /media/
bin   cgroup  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  var
boot  dev     home  lib64  media       mnt   opt  root  selinux  sys  usr

(使用完后的卸载操作,如下)
[iyunv@jay-linux image]# umount /media/
[iyunv@jay-linux image]# vgchange -an VolGroup
  0 logical volume(s) in volume group "VolGroup" now active
[iyunv@jay-linux image]# losetup -d /dev/loop0
  mount LVM 分区的 参考资料:

Related posts:

  • 如何挂载qcow2格式的镜像文件(how to mount a qcow2 image)
  • Linux挂载NTFS分区:mount unknown filesystem type ‘ntfs’
  • Ubuntu上的android sdk: ./emulator: No such file or directory
  • “mount: could not find any free loop device”解决方案

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-387865-1-1.html 上篇帖子: FreeBSD_11-系统管理——{Part_4-内核参数定制} 下篇帖子: 我的终端
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表