How to: qcow2 files mount in yours linux system , with lvm


Sometime we need mount image of virtual machine for change something in system . Command mount and losetup not working in qcow2 files. We must have necessary a NBD protocol is a device node whose content is provided by a remote machine.

 

First you need the module NBD loaded:


root@main ~ # >>> modprobe nbd max_part=8

Then you cant use NBD protocol to create device


root@main ~ # >>> qemu-nbd --connect=/dev/nbd0 /var/kvm/images/UNIFI-clone.img
root@main ~ # >>> fdisk /dev/nbd0 -l

Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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: 0x5cdb6749

Device Boot Start End Blocks Id System
/dev/nbd0p1 * 2048 499711 248832 83 Linux
/dev/nbd0p2 501758 20969471 10233857 5 Extended
/dev/nbd0p5 501760 20969471 10233856 8e Linux LVM

we can see 3 disk device nbd0p1 nbd0p2 nbd0p5

We can try mount disk nr 5

root@main ~ # >>> ls  /dev/nbd0*
/dev/nbd0  /dev/nbd0p1  /dev/nbd0p2  /dev/nbd0p5
root@main ~ # >>> mount /dev/nbd0p5 tel
mount: unknown filesystem type 'LVM2_member'

We have trouble with LVM 'LVM2_member'

Lest start with LVM

root@main ~ # >>> vgscan                                                                                                                                              ⏎
  Reading all physical volumes.  This may take a while...
  Found volume group "unifek-vg" using metadata type lvm2
  Found volume group "r2d3" using metadata type lvm2

we found 2 volume group,nex we try finding volume with lvscan

  root@main ~ # >>> lvscan
  inactive          '/dev/unifek-vg/root' [9.31 GiB] inherit
  inactive          '/dev/unifek-vg/swap_1' [460.00 MiB] inherit
  ACTIVE            '/dev/r2d3/root' [17.40 GiB] inherit
  ACTIVE            '/dev/r2d3/usr' [8.38 GiB] inherit
  ACTIVE            '/dev/r2d3/var' [225.45 GiB] inherit
  ACTIVE            '/dev/r2d3/swap_1' [7.98 GiB] inherit
  ACTIVE            '/dev/r2d3/tmp' [380.00 MiB] inherit
  ACTIVE            '/dev/r2d3/home' [281.00 GiB] inherit

We have 2 types inactive volume
Volumes are inactive , vgchange this 😉

root@main ~ # >>> vgchange -a y
  2 logical volume(s) in volume group "unifek-vg" now active
  6 logical volume(s) in volume group "r2d3" now active
root@main ~ # >>> ls /dev/unifek-vg/
root  swap_1

mount disk

root@main ~ # >>> mkdir subroot
root@main ~ # >>> mount /dev/unifek-vg/root subroot
root@main ~ # >>> ls subroot
bin  boot  dev  etc  home  lib  lib64  logs  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  initrd.img.old  vmlinuz.old

Important : Remeber when you finish umount lvm , disable partitions and disconnect device ndb

root@main ~ # >>> umount subroot   ⏎                                                                                                                                   
root@main ~ # >>> vgchange -a n unifek-vg
  0 logical volume(s) in volume group "unifek-vg" now active
root@main ~ # >>> qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
Advertisements

2 Comments Add yours

  1. Nice instruction, thanks!

    One note – on my machine with Ubuntu Xenial, vgscan did not automatically found the volume groups from nbd-mounted qcow2. So before that, I had to
    $ sudo pvscan –cache /dev/nbd0p5
    after that, vgscan finds volume groups.

    1. dzaku says:

      Thanks for info 🙂

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s