Two ways to mount a mirrored file under KVM or Xen (how to mount an image files)

Source: Internet
Author: User

In the case of KVM or Xen virtualization, it is often necessary to use the mirror files (image file), we can start the guest system, and then modify the mirrored file, but this is sometimes more cumbersome, in fact, can be the image file directly mount, There are two ways to do this.

method One: Find the starting position of the partition, and use the offset parameter of the Mount command to offset the unwanted before you can get the real partition. the specific steps are as follows:

1. Use "Fdisk-lu my.img" to query image information;
2. Calculate where the partition begins in image (calculate offset), starting with the N number sector (sector), Offset=n*m (M is a sector size, typically 512)
3. Use the Mount command to mount the loop device. (such as LVM partitions, this is more complex, see the end of this article)

Specific actions on Linux systems are demonstrated as follows:

The code is as follows Copy Code
[Root@jay-linux image]# Fdisk-lu rhel6u2.img
You must set cylinders.
You can does this from the extra functions menu.

Disk rhel6u2.img:0 MB, 0 bytes
255 heads, sectors/track, 0 cylinders, Total 0 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk identifier:0x00048b34

Device Boot Start End Blocks Id System
RHEL6U2.IMG1 * 2048 1026047 512000 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, logical=) (6527, 21, 22)
[Root@jay-linux image]# Echo $ ((2048*512))
1048576
[Root@jay-linux image]# mount-o loop,offset=1048576 rhel6u2.img/media/
[Root@jay-linux image]# cd/media/
[Root@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
[Root@jay-linux media]# Echo $ ((1026048*512))
525336576
[Root@jay-linux media]# Umount/media
[Root@jay-linux media]# cd/home/image/
[Root@jay-linux image]# Umount/media
[Root@jay-linux image]# mount-o loop,offset=525336576 rhel6u2.img/media/
Mount:unknown filesystem type ' Lvm2_member '


(The question of the LVM partition mount in the mirrored file is described in the last separate article)

The code is as follows Copy Code
[Root@jay-linux image]# Fdisk-lu sles11sp2-i386.img
You must set cylinders.
You can does this from the extra functions menu.

Disk sles11sp2-i386.img:0 MB, 0 bytes
255 heads, sectors/track, 0 cylinders, Total 0 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk Identifier:0x0002d3be

Device Boot Start End Blocks Id System
SLES11SP2-I386.IMG1 2048 4208639 2103296 Linux Swap/solaris
Partition 1 does not end on cylinder boundary.
SLES11SP2-I386.IMG2 * 4208640 41943039 18867200 Linux
Partition 2 has different physical/logical endings:
phys= (1023, 254, logical=) (2610, 212, 34)
[Root@jay-linux image]# Echo $ ((4208640*512))
2154823680
[Root@jay-linux image]# mount-o loop,offset=2154823680 Sles11sp2-i386.img/media
[Root@jay-linux image]# cd/media/
[Root@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
[Root@jay-linux image]# umount/media/



Method two: After using KPARTX to establish the partition map, then mount the mapped device,
The operation example is as follows:

The code is as follows Copy Code
[root@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
 
[Root@jay-linux image]# mount/dev/mapper/loop3p2/media/
&NBSP
[root@jay-linux image]# ls/media/
bin   dev  home  lost+found  mnt , PROC&N Bsp sbin     srv      sys  usr
boot  etc  lib    media       opt  root  selinux  success  tmp  var
[ Root@jay-linux image]# umount/media/
[root@jay-linux image]# mount/dev/mapper/loop3p1/media/
/dev/mapper/ LOOP3P1 looks like Swapspace-not mounted
Mount:you must specify the filesystem type


(The swap partition, I also do not know whether to mount; in fact, the mount swap zone is meaningless)

(after use completes, uninstall mount point, Delete mapping relation)

The code is as follows Copy Code
[Root@jay-linux image]# umount/media/
[Root@jay-linux image]# kpartx-d sles11sp2-i386.img
Loop deleted:/DEV/LOOP3

About the LVM mount, you can refer to the Vpsee in the end of the article (I also did not do a lot of success to see this article accidentally), I have the image file in the LVM partition mount operation is as follows for reference.

[Root@jay-linux image]# Fdisk-lu rhel6u2.img
You must set cylinders.
You can does this from the extra functions menu.

Disk rhel6u2.img:0 MB, 0 bytes
255 heads, sectors/track, 0 cylinders, Total 0 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk identifier:0x00048b34

Device Boot Start End Blocks Id System
RHEL6U2.IMG1 * 2048 1026047 512000 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, logical=) (6527, 21, 22)
[Root@jay-linux image]# Echo $ ((1026048*512))
525336576
[Root@jay-linux image]# losetup/dev/loop0 rhel6u2.img-o 525336576
[Root@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]
[Root@jay-linux image]# Vgchange-ay Volgroup
2 Logical Volume (s) in volume Group "Volgroup" now active
[Root@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
[Root@jay-linux image]# mount/dev/volgroup/lv_root/media/
[Root@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



(Uninstall operation after use, such as the following)

The code is as follows Copy Code
[Root@jay-linux image]# umount/media/
[Root@jay-linux image]# Vgchange-an Volgroup
0 Logical Volume (s) in volume Group "Volgroup" now active
[Root@jay-linux image]# losetup-d/dev/loop0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.