在KVM或Xen下挂载一个镜像文件(how to mount an image file)的两种方
2019/10/10/17:46:44 阅读:1875 来源:谷歌SEO算法 标签:
发外链
在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(image file),我们可以将Guest系统启动起来,然后对镜像文件进行修改,不过这样有时也是比较麻烦,其实也是可以将镜像文件直接进行mount的,可以用如下两种办法.
方法一:找出分区开始的开始位置,使用mount命令的offset参数偏移掉前面不需要的,即可得到真正的分区,其具体步骤如下.
1.用“fdisk -lu my.img”查询image信息;
2.计算image内分区开始的地方(计算offset),用从N号sector(扇区)开始,则offset=N*M (M为一个sector的大小,一般为512).
3.使用mount命令挂载为loop设备即可,如LVM分区,则会较复杂,请见本文最后的介绍).
在Linux系统上具体操作演示如下:
- [root@jay-linuximage]#fdisk-lurhel6u2.img
- Youmustsetcylinders.
- Youcandothisfromtheextrafunctionsmenu.
- Diskrhel6u2.img:0MB,0bytes
- 255heads,63sectors/track,0cylinders,total0sectors
- Units=sectorsof1*512=512bytes
- Sectorsize(logical/physical):512bytes/512bytes
- I/Osize(minimum/optimal):512bytes/512bytes
- Diskidentifier:0x00048b34
- DeviceBootStartEndBlocksIdSystem
- rhel6u2.img1*2048102604751200083Linux
- Partition1doesnotendoncylinderboundary.
- rhel6u2.img21026048104857599519157768eLinuxLVM
- Partition2hasdifferentphysical/logicalendings:
- phys=(1023,254,63)logical=(6527,21,22)
- [root@jay-linuximage]#echo$((2048*512))
- 1048576
- [root@jay-linuximage]#mount-oloop,offset=1048576rhel6u2.img/media/
- [root@jay-linuximage]#cd/media/
- [root@jay-linuxmedia]#ls
- config-2.6.32-220.el6.x86_64initramfs-3.5.0.imgSystem.map-2.6.32-279.el6.x86_64
- config-2.6.32-279.el6.x86_64lost+foundSystem.map-3.5.0
- efisymvers-2.6.32-220.el6.x86_64.gzvmlinuz
- grubsymvers-2.6.32-279.el6.x86_64.gzvmlinuz-2.6.32-220.el6.x86_64
- initramfs-2.6.32-220.el6.x86_64.imgSystem.mapvmlinuz-2.6.32-279.el6.x86_64
- initramfs-2.6.32-279.el6.x86_64.imgSystem.map-2.6.32-220.el6.x86_64vmlinuz-3.5.0--phpfensi.com
- [root@jay-linuxmedia]#echo$((1026048*512))
- 525336576
- [root@jay-linuxmedia]#umount/media
- [root@jay-linuxmedia]#cd/home/image/
- [root@jay-linuximage]#umount/media
- [root@jay-linuximage]#mount-oloop,offset=525336576rhel6u2.img/media/
- mount:unknownfilesystemtype'LVM2_member'
镜像文件中的LVM分区mount的问题,本文最后单独说明,代码如下:
- [root@jay-linuximage]#fdisk-lusles11sp2-i386.img
- Youmustsetcylinders.
- Youcandothisfromtheextrafunctionsmenu.
- Disksles11sp2-i386.img:0MB,0bytes
- 255heads,63sectors/track,0cylinders,total0sectors
- Units=sectorsof1*512=512bytes
- Sectorsize(logical/physical):512bytes/512bytes
- I/Osize(minimum/optimal):512bytes/512bytes
- Diskidentifier:0x0002d3be
- DeviceBootStartEndBlocksIdSystem
- sles11sp2-i386.img120484208639210329682Linuxswap/Solaris
- Partition1doesnotendoncylinderboundary.
- sles11sp2-i386.img2*4208640419430391886720083Linux
- Partition2hasdifferentphysical/logicalendings:
- phys=(1023,254,63)logical=(2610,212,34)
- [root@jay-linuximage]#echo$((4208640*512))
- 2154823680
- [root@jay-linuximage]#mount-oloop,offset=2154823680sles11sp2-i386.img/media
- [root@jay-linuximage]#cd/media/
- [root@jay-linuxmedia]#ls
- bindevhomelost+foundmntprocsbinsrvsysusr
- bootetclibmediaoptrootselinuxsuccesstmpvar
- [root@jay-linuximage]#umount/media/
方法二:用kpartx建立分区映射后,再mount映射后的设备即可,操作实例如下:
- [root@jay-linuximage]#kpartx-avsles11sp2-i386.img
- addmaploop3p1(253:2):04206592linear/dev/loop32048
- addmaploop3p2(253:3):037734400linear/dev/loop34208640
- [root@jay-linuximage]#mount/dev/mapper/loop3p2/media/
- [root@jay-linuximage]#ls/media/
- bindevhomelost+foundmntprocsbinsrvsysusr
- bootetclibmediaoptrootselinuxsuccesstmpvar
- [root@jay-linuximage]#umount/media/
- [root@jay-linuximage]#mount/dev/mapper/loop3p1/media/
- /dev/mapper/loop3p1lookslikeswapspace-notmounted
- mount:youmustspecifythefilesystemtype
其中的交换分区,我也还不知道是否可以mount,其实mount交换分区也没意义,使用完成后,卸载挂载点、删除映射关系即可,代码如下:
- [root@jay-linuximage]#umount/media/
- [root@jay-linuximage]#kpartx-dsles11sp2-i386.img
- loopdeleted:/dev/loop3
关于LVM的mount,可以参考文末参考资料中的vpsee的文章,我也是试多次没做成功,才偶然看到这篇文章的,我对镜像文件中LVM分区的mount操作如下供参考.
- [root@jay-linuximage]#fdisk-lurhel6u2.img
- Youmustsetcylinders.
- Youcandothisfromtheextrafunctionsmenu.
- Diskrhel6u2.img:0MB,0bytes
- 255heads,63sectors/track,0cylinders,total0sectors
- Units=sectorsof1*512=512bytes
- Sectorsize(logical/physical):512bytes/512bytes
- I/Osize(minimum/optimal):512bytes/512bytes
- Diskidentifier:0x00048b34
- DeviceBootStartEndBlocksIdSystem
- rhel6u2.img1*2048102604751200083Linux
- Partition1doesnotendoncylinderboundary.
- rhel6u2.img21026048104857599519157768eLinuxLVM
- Partition2hasdifferentphysical/logicalendings:
- phys=(1023,254,63)logical=(6527,21,22)
- [root@jay-linuximage]#echo$((1026048*512))
- 525336576
- [root@jay-linuximage]#losetup/dev/loop0rhel6u2.img-o525336576
- [root@jay-linuximage]#pvscan
- PV/dev/loop0VGVolGrouplvm2[49.51GiB/0free]
- Total:1[49.51GiB]/inuse:1[49.51GiB]/innoVG:0[0]
- [root@jay-linuximage]#vgchange-ayVolGroup
- 2logicalvolume(s)involumegroup"VolGroup"nowactive
- [root@jay-linuximage]#lvs
- LVVGAttrLSizePoolOriginData%MoveLogCopy%Convert--phpfensi.com
- lv_rootVolGroup-wi-a---45.57g
- lv_swapVolGroup-wi-a---3.94g
- [root@jay-linuximage]#mount/dev/VolGroup/lv_root/media/
- [root@jay-linuximage]#ls/media/
- bincgroupetcliblost+foundmiscnetprocsbinsrvtmpvar
- bootdevhomelib64mediamntoptrootselinuxsysusr
使用完后的卸载操作,代码如下:
- [root@jay-linuximage]#umount/media/
- [root@jay-linuximage]#vgchange-anVolGroup
- 0logicalvolume(s)involumegroup"VolGroup"nowactive
- [root@jay-linuximage]#losetup-d/dev/loop0
热门评论