How to Mount the VM Disk

Mount the VM Disk which built On image file,
-------------------------------------------------------------------------

losetup /dev/loop0 /root/mailserver.img

-Attach the image file to loop device.

kpartx -a -v /dev/loop0

-Map the vm partitions,

Output:
------------
add map loop0p1 (253:4): 0 1024000 linear /dev/loop0 2048
add map loop0p2 (253:5): 0 7362560 linear /dev/loop0 1026048

Then mount the VM partitions,

mount /dev/mapper/loop0p1 /mnt/boot
mount /dev/mapper/loop0p2 /mnt/root -->if it is not LVM.

if vm root partition is an LVM,
You will receive the below error,
mount: unknown filesystem type 'LVM2_member'

Which means LVM not identify that vm lvm partition.

  pvscan
vgscan && lvscan

Output:
-------------
inactive          '/dev/VolGroup/lv_root' [17.54 GiB] inherit
  inactive          '/dev/VolGroup/lv_swap' [1.97 GiB] inherit
  ACTIVE            '/dev/vg0/ubuntu' [10.00 GiB] inherit
  ACTIVE            '/dev/vg0/centos' [10.00 GiB] inherit
  ACTIVE            '/dev/vg0/ubuntuhvm' [10.00 GiB] inherit
  ACTIVE            '/dev/vg0/images' [20.00 GiB] inherit

-need to activate that new volumegroup,

 vgchange -ay VolGroup

 mount /dev/VolGroup/lv_root /mnt/centos

That's All.


Finally, the backward process is:
# umount /mnt
# vgchange -an VolGroup
# kpartx -dv /dev/loop0

 Reference:
------------------






Mount virtual machine’s LVM partition[Inside VM] on KVM host


1. Scan volume Group
  #vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg     1  10   0 wz--n- 500G  300G
2. Scan lvm group.
Notice all lvm are active and belong to vg volume group.
In server lvm our VM is installed.
  #lvscan
  ACTIVE            '/dev/vg/tmp' [1.00 GiB] inherit
  ACTIVE            '/dev/vg/home' [2.00 GiB] inherit
  ACTIVE            '/dev/vg/swap' [4.00 GiB] inherit
  ACTIVE            '/dev/vg/opt' [10.00 GiB] inherit
  ACTIVE            '/dev/vg/server' [20.00 GiB] inherit
3.
 #kpartx -av /dev/vg/yumserver
4. Scan lvm group again, notice their are now two inactive lvm volumes, these are of server lvm where our VM is installed
  #lvscan
  inactive          '/dev/VolGroup/lv_root' [17.54 GiB] inherit
  inactive          '/dev/VolGroup/lv_swap' [1.97 GiB] inherit
  ACTIVE            '/dev/vg/tmp' [1.00 GiB] inherit
  ACTIVE            '/dev/vg/home' [2.00 GiB] inherit
  ACTIVE            '/dev/vg/swap' [4.00 GiB] inherit
  ACTIVE            '/dev/vg/opt' [10.00 GiB] inherit
  ACTIVE            '/dev/vg/server' [20.00 GiB] inherit
5. Scan volume group again.
Now notice that their are two volume groups. One is of KVM HOST and other is of Guest VM.
  #vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   2   0 wz--n- 19.51g    0
  vg         1  10   0 wz--n- 500G  300G
6. Perform this step only if GuestVM volume group is same as of our KVMHost.
  #vgrename GuestVMvolumegroup  newvolgroup
  In our case both are different so we skip this step
7. activating the VolGroup LVM (GUEST VM)
  #vgchange -ay VolGroup   
  2 logical volume(s) in volume group "VolGroup" now active
8 scan lvm again now all lvms are active
  #lvscan
  ACTIVE            '/dev/VolGroup/lv_root' [17.54 GiB] inherit
  ACTIVE            '/dev/VolGroup/lv_swap' [1.97 GiB] inherit
  ACTIVE            '/dev/vg/tmp' [1.00 GiB] inherit
  ACTIVE            '/dev/vg/home' [2.00 GiB] inherit
  ACTIVE            '/dev/vg/swap' [4.00 GiB] inherit
  ACTIVE            '/dev/vg/opt' [10.00 GiB] inherit
  ACTIVE            '/dev/vg/server' [20.00 GiB] inherit
9. Mount the VM lvm volume.
  #mount /dev/VolGroup/lv_root /mnt/
10. Do whatever

11. Unmount the lvm.
   #umount /mnt/
12. Deactivating the VolGroup LVM (GUEST VM)
  #vgchange -an VolGroup   
13. List LVM again
  #lvscan
  inactive          '/dev/VolGroup/lv_root' [17.54 GiB] inherit
  inactive          '/dev/VolGroup/lv_swap' [1.97 GiB] inherit
  ACTIVE            '/dev/vg/tmp' [1.00 GiB] inherit
  ACTIVE            '/dev/vg/home' [2.00 GiB] inherit
  ACTIVE            '/dev/vg/swap' [4.00 GiB] inherit
  ACTIVE            '/dev/vg/opt' [10.00 GiB] inherit
  ACTIVE            '/dev/vg/server' [20.00 GiB] inherit
14. Perform this step only if you performed Step 6
  #vgrename  newvolgroup GuestVMvolumegroup 
In our case both are different so we skip this step

15.
#kpartx  -dv /dev/vg/yumserver
16 List VolumeGroup
  #vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg     1  10   0 wz--n- 500G  300G



Mount the VM Disk which built on LVM:
-----------------------------------------------------------------------

[root@xen3 xen]# kpartx -av /dev/mapper/vg0-centos
add map vg0-centos1 (253:4): 0 409600 linear /dev/mapper/vg0-centos 2048
add map vg0-centos2 (253:5): 0 16384000 linear /dev/mapper/vg0-centos 411648
add map vg0-centos3 (253:6): 0 4096000 linear /dev/mapper/vg0-centos 16795648

Then it is very simple,

vg0-centos1 - boot partition

mount /dev/mapper/vg0-centos1 /mnt/boot

vg0-centos2 - root partition

mount /dev/mapper/vg0-centos2 /mnt/root

Other one is swap partition.

Reverse:

umount /dev/mapper/vg0-centos1

umount /dev/mapper/vg0-centos2

 kpartx -dv /dev/mapper/vg0-centos

Crosscheck with,

ls -all /dev/mapper/


Comments

Popular posts from this blog

Using a Linux server to route packets between two private networks

PHP Fatal error: Class 'JFactory' not found

KVM & Qemu