0

I have XEN 4.0.x.x and Cent os 5.5

There is an .image file of each VM stored on dom0 in /var/lib/xen/images directory. Is it a some block on the hard disk which I can mount this file from dom0 and access the file system of dom0.

What I really want to do is mount the filesystem of VM, and access /etc/sysconfig/network-scripts directory. Is there a way I can o it.

I tried directly using 'mount -o loop /var/lib/xen/images/VMname.img ', but it failed asking for the filesystem type. I didn't get any file system type using 'file' command for this image. Is this image an accessible file or some kind of binary or system file that XEN creates. If it is a binary file, I think we can't do much with it. But if it is in accessible format, we can do something with it. I want to know the way with which I can do eactly that.

stillStudent
  • 323
  • 2
  • 5
  • 13

2 Answers2

2

The virtual machine image is more like a full harddrive than a single file system that you can mount, which mean it has a partition table. You can use the kpartx tool to make all of the partitions available to be mounted like so:

# kpartx -av /var/lib/xen/images/VMname.img
add map loop0p1 : 0 29333504 linear /dev/loop0 2048
add map loop0p5 : 0 1380352 linear /dev/loop0 29337600
# mount /dev/mapper/loop0p1 /mnt

When you're done and have unmounted all the partitions, you can remove them from the device mapper with this:

# kpartx -d /var/lib/xen/images/VMname.img

(Note, example cribbed from http://ppadala.net/blog/2010/09/kpartx-to-mount-vm-disk-images/)

For more extensive modification, you may wish to take a look at the guestfish tool.

Ophidian
  • 2,158
  • 13
  • 14
  • Hi, you guys are really great, it half worked for me. But there is one more problem I am facing. I am able to mount the first mapping without specifying file system. This mapping is 'boot' partition of the system. But when I try to mount second mapping, it throws error asking for filesystem type. 'file' command shows it as block. Not sure which file system to provide. But at least it has helped me to try further. Any further assistance will be of great help, as I am still not able to access /etc/sysconfig directory of VM. – stillStudent Sep 23 '11 at 07:46
  • It's possible the root partition of the domU is in an LVM partition. vgscan should pick that up if that is the case. You may also want to take a look at guestfish: http://libguestfs.org/ – Ophidian Sep 23 '11 at 12:54
1

As you can see from the above comments I was not able to mount second loop as it was again a LVM partition. I finally found a way to mount a VM.img file and do any configuration on guest machine from Dom-0.

Please follow the below steps to mount the image file:

  1. First fire vgscan command on dom-0, it will give you logical volumes that are currently active on the dom-0. Now you can safely assume that this volume number shown by the command is of your dom-0 machine and not of any guest.

  2. Now execute kpartx -av /var/lib/xen/images/$machine_name.img This command will map loops for the image file of VM and activate the VolumeGroup of guest VM.

  3. Now again fire vgscan command, you will see one more VolGroup other than what you have seen in step 1. Now you can be sure that this is the volumegroup of your guest VM.

  4. Now you should know the logical volumes in a VolumeGroup to successfully mount it. For this fire the command vgchange -ay /VolGroup00(Newly created volumegroup in step 2)/

  5. Now execute command lvs, this will give you logical volumes in VolGroup.

  6. Now create a mount point and mount any Logical volume you want to mount as follows

    mount /dev/VolGroup00/LogVol00 $mount_path

  7. Now you can access any directory of Guest VM through this way and modify any configuration you want. Once you are done you can follow reverse procedure like

    a) Umount $mount_path

    b) vgchange -an VolGroup00

    c) kpartx -d /var/lib/xen/images/$machine_name.img

And you are done

stillStudent
  • 323
  • 2
  • 5
  • 13