13

If I trying to do following on KVM host:

# kpartx -av /dev/VolGroup00/kvm101_img
add map kvm101_img1 : 0 208782 linear /dev/VolGroup00/kvm101_img 63
add map kvm101_img2 : 0 125612235 linear /dev/VolGroup00/kvm101_img 208845
# mount /dev/mapper/kvm101_img1 /mnt

then I'm getting /boot partition mounted. But I'm getting an error if I then type following:

# mount /dev/mapper/kvm101_img2 /mnt
mount: you must specify the filesystem type

Here is a fdisk layout on guest machine:

# fdisk -l
Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        7832    62806117+  8e  Linux LVM

Is it possible to find a root partition on guest and mount it on host system?

Evolver
  • 626
  • 2
  • 6
  • 14
  • I suppose your reference to kvm101_img2 is a typo. It should be _img1 instead, right? – joechip Jul 05 '11 at 23:10
  • _img1 mounts correctly with method above, but _img1 is a /boot partition, equivalent to /dev/hda1 on guest. Problem was that I couldn't mount _img2 containing /dev/hda2 Linux LVM. That partition layout is default for CentOS installer. – Evolver Jul 06 '11 at 07:12
  • Oh OK, I was thrown out by the reference to KVM. There's nothing KVM specific about this question then, it's simply one LVM structure inside another. – joechip Jul 06 '11 at 16:43

4 Answers4

15

Seems that I finally figured out how to do things I needed. Here is what I did:

# kpartx -av /dev/VolGroup00/kvm101_img
# vgscan

if VolGroup names identical in guest and host systems, then you have to rename guest VolGroup

# vgrename <uuid> VolGroupXX

uuid of VolGroups you can check in vgdisplay. So, the trick is in activating guest VolGroup:

# lvscan
# vgchange -ay VolGroupXX
# lvscan

After that it's easily mounted:

# mount /dev/VolGroupXX/LogVol00 /mnt

Finally, the backward process is:

# umount /mnt
# vgchange -an VolGroupXX
# kpartx -dv /dev/VolGroup00/kvm101_img
# pvscan

The last command cleans the LVM cache and removes the physical volume created by kpartx from LVM.

Evolver
  • 626
  • 2
  • 6
  • 14
  • You might have to edit the guest's fstab in order to use VolGroupXX instead of VolGroup00 then. – joechip Jul 06 '11 at 16:44
5

I'm glad you figured it out for your situation.

In the more general case, the guest drive could be in a variety of formats such as qcow, qcow2, etc., so you wouldn't be able to work on them directly. In this case, you might use something like on the host:

# modprobe nbd
# kvm-nbd -c /dev/nbd0 file.qcow2

Then you could access /dev/nbd0 as the raw guest drive. To stop using this device you should run:

# nbd-client -d /dev/nbd0

If, on the other hand, the guest drive were in raw format, you would use losetup:

# losetup -f file.raw

This would find and use the first available loop device (e.g., /dev/loop0). To stop using it run:

# losetup -d /dev/loop0

After this you would be able to do the kpartx / vgscan / lvscan / mount procedure you describe in your answer.

joechip
  • 668
  • 3
  • 6
2

you can do it using guestfish - http://libguestfs.org/guestfish.1.html

dyasny
  • 18,802
  • 6
  • 49
  • 64
  • Thanks for suggestion, but unfortunately I couldn't even install guestfish: I spent hours of googling and investigating - all useless, it's not works :( Maybe there is some another way to mount linux partition? I don't need all-in-one tool to mount everything, just LVM. – Evolver Jul 05 '11 at 15:38
  • 1
    try to run `qemu-img info /path/to/image`. if what you get in the Type box is not "RAW" then there is no way an OS can mount this image as a partition, because it is formatted as a qemu-specific virtual disk. This is what guestfish deals with, it's not just a simple mounting tool – dyasny Jul 05 '11 at 16:21
1

It might be easier just to use guestfish from the libguestfs package which should work out all the annoying details for you instead of trying to do it manually

TomH
  • 1,290
  • 7
  • 10