Mounting a KVM/Qemu VM root partition in Fedora 14

Since My development now needs to target F14, not F13, I figured I start using a F14 virtual machine, but leave my F13 VM alone, just in case I needed something off of it. Well, it turns out I do need something off of it. But why should I have to wait for it to boot in order to see it? I can mount it loop back, right…

It was a little trickier than I thought at first.  I’ll just give the final solution, and not all the trial and error I went through to get there.

Here’s the ultrashort version. I’m sure there is the start of a script here:

fdisk -u -l  /var/lib/libvirt/images/F13-IPA.img
echo 1026048*512 | bc
losetup /dev/loop1 -o525336576 /var/lib/libvirt/images/F13-IPA.img
vgchange -a y vg_f13prototype
mount  /dev/vg_f13prototype/lv_root /mnt/ipa/root/

Here’s the explanation.

The image file was saved in /var/lib/libvirt/images/F13-IPA.img.

file /var/lib/libvirt/images/F13-IPA.img
/var/lib/libvirt/images/F13-IPA.img: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x84a42, GRUB version 0.94; partition 1: ID=0x83, active, starthead 32, startsector 2048, 1024000 sectors; partition 2: ID=0x8e, starthead 221, startsector 1026048, 11556864 sectors, code offset 0x48

This is not a disk that can be directly mounted loopback, as it has two (really 3) partitions on it, beyond the boot sector.

Find the offset of the root partition:

[root@ayoung ~]# fdisk -u -l  /var/lib/libvirt/images/F13-IPA.img 

Disk /var/lib/libvirt/images/F13-IPA.img: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders, total 12582912 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: 0x0008b902

                              Device Boot      Start         End      Blocks   Id  System
/var/lib/libvirt/images/F13-IPA.img1   *        2048     1026047      512000   83  Linux
/var/lib/libvirt/images/F13-IPA.img2         1026048    12582911     5778432   8e  Linux LVM
[root@ayoung ~]#

The first one is the boot secttor, or /boot in the directory. To mount this, first find the offset in bytes.

[root@ayoung ~]# echo 2048*512 | bc
1048576

And then use that as part of the mount command

mount  -o loop,offset=1048576 /var/lib/libvirt/images/F13-IPA.img /mnt/ipa/boot

The Root partition is a little trickier, as it is a Logical volume.

David Hilley’ blog post on mounting LVM helped alot. The one thing it missed was activating the Logical Volume Group.

I had to calculate the offset of the second partition and use that to set up the loop device.

[root@ayoung ~]# echo 1026048*512 | bc
525336576
[root@ayoung ~]# losetup /dev/loop1 -o525336576 /var/lib/libvirt/images/F13-IPA.img

Now scan for logical volumes.

[root@ayoung ~]# lvm pvscan
  PV /dev/sda2    VG vg_ayoung         lvm2 [148.85 GiB / 0    free]
  PV /dev/loop1   VG vg_f13prototype   lvm2 [5.50 GiB / 0    free]
  Total: 2 [154.35 GiB] / in use: 2 [154.35 GiB] / in no VG: 0 [0   ]

My vm’s Logical volume is vg_f13prototype

[root@ayoung ~]# lvm lvs
  LV      VG              Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv_root vg_ayoung       -wi-ao 145.04g
  lv_swap vg_ayoung       -wi-ao   3.81g
  lv_root vg_f13prototype -wi---   4.50g
  lv_swap vg_f13prototype -wi---   1.00g 

[root@ayoung ~]# lvdisplay /dev/vg_f13prototype
  --- Logical volume ---
  LV Name                /dev/vg_f13prototype/lv_root
  VG Name                vg_f13prototype
  LV UUID                NkLdOw-UVcH-ZsoE-6J0P-ZSdl-49iU-yqOZFm
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                4.50 GiB
  Current LE             144
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  --- Logical volume ---
  LV Name                /dev/vg_f13prototype/lv_swap
  VG Name                vg_f13prototype
  LV UUID                rm6ZBP-72Qv-L1dv-FB2L-rRNY-6R83-herxaf
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                1.00 GiB
  Current LE             32
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

NOte the line ” LV Status NOT available”. To make it avaialble:

[root@ayoung ~]# vgchange -a y vg_f13prototype
  2 logical volume(s) in volume group "vg_f13prototype" now active

And now mount it:

[root@ayoung ~]# mount  /dev/vg_f13prototype/lv_root /mnt/ipa/root/
[root@ayoung ~]# ls /mnt/ipa/root
bin   dev  home  lib64       media  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  mnt    proc  sbin  srv      tmp  var

To clean up afterwards:

[root@ayoung ~]#  umount  /mnt/ipa/root
[root@ayoung ~]# vgchange -a n vg_f13prototype
  0 logical volume(s) in volume group "vg_f13prototype" now active
[root@ayoung ~]# losetup -d /dev/loop1

4 thoughts on “Mounting a KVM/Qemu VM root partition in Fedora 14

  1. [… whoops]

    I imagine that this method would be more robust and simpler than messing with offsets in losetup.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.