Friday, June 23, 2006

Reading xen guest disk image from the outside (FC5)

On Fedora's Xen wiki, there's a tutorial on how to read a xen guest OS's disk from the outside. The instruction works for a physical LVM, but when the disk is actually an image file,
the instruction there does not really work. Here I share my experience, step by step, in the hope of saving somebody else's time. The problem that the original instruction doesn't work is because many people (including myself) simply use Fedora's default
Volume Group name, VolGroup00 in both dom0 and guest systems. Therefore, vgscan will be confused by two volume groups with the same name and gathers only the info of the one currently working, i.e. the one used in dom0. Since LVM basically
provides a unique name for volume accessing, there is no way that we can assign a particular logical volume within a particular volume group, without having a unique name for it. The solution is to change the name of one of them. Since we usually deal
with many guest systems on the same dom0, I choose to change the name for dom0. (It's also possible to change the guest system instead by steps similar to the below, but note that it can only be done from the inside.)
Use Rescue Mode to access the system's root file system
  1. Reboot into the rescue mode (insert FC5 DVD, CD1, or Rescue CD, and at the boot prompt, type linux rescue).
  2. Choose language and keyboard layout.
  3. Choose No when asked about bringing up the network.
  4. When prompted about mounting the original file system, press F2 to bring up a shell.
  5. Wait for a while (for the background lvm process to find necessary info).
Change the volume group's name
  1. At the # prompt, type lvm vgrename VolGroup00 VGHost00
Change original references to the volume group's name
  1. # cd /mnt
  2. # mkdir root boot
  3. # mount /dev/mapper/VGHost00-LogVol00 /mnt/root
  4. # vi root/etc/fstab (replace VolGroup00 with VGHost00, you can use :%s/VolGroup00/VGHost00/g to do so and use ZZ to quit vi)
  5. # umount root; rm -rf root
  6. # mount /dev/hda1 /mnt/boot
  7. # vi boot/grub/grub.config (do the same substitution)
  8. # umount boot; rm -rf boot
Now we need to build a new init ramdisk that knows about the new volume group (reference from this blog)
  1. Press F1 to go back to the rescue menu where it asks about mounting the original system,select Continue (you can also manually mount it -- see the blog reference above)
  2. # chroot /mnt/sysimage
  3. # cp /boot/initrd-2.6.16-1.2133_FC5xen0.img /boot/initrd-2.6.16-1.2133_FC5xen0.old.img
  4. # mkinitrd -v -f /boot/initrd-2.6.16-1.2133_FC5xen0.img 2.6.16-1.2133_FC5xen0
Reboot
  1. # exit
  2. # exit
  3. Remember to remove the Fedora DVD/CD1/RescueCD to boot from your HD
Read the disk image of the guest OS
  1. Use losetup -f to find a free loopback device. Here we assume it's /dev/loop0
  2. # losetup /dev/loop0 your_disk_image_file
  3. # kpartx -av /dev/loop0 (this should give us loop0p1 loop0p2 under /dev/mapper)
  4. # vgscan (this should report both VolGroup00 and VGHost00)
  5. # vgchange -ay VolGroup00
  6. # mkdir /mnt/guest_root
  7. # mount /dev/mapper/VolGroup00-LogVol00 /mnt/guest_root
Voila!

To unmount it, basically we just reverse the steps:
  1. umount /mnt/guest_root
  2. rm -rf /mnt/guest_root
  3. vgchange -an VolGroup00
  4. kpartx -d /dev/loop0
  5. losetup -d /dev/loop0

No comments:

Post a Comment