HVM Disk Tricks

Disk Tricks

Well, I've just finally figured out that there are two types of "disks" that xen uses; partition images and disk images. This is irrespective of whether your container is a file, an LVM partition, or what. The "partition" is just that, a partition image, where as the disk image actually pretends it is an entire disk. The former is used mostly in software virtualized stuff, and the latter appears to be used anytime qemu comes into play (ie, hvm virtuals).

Very frustrating for me as it is not documented very many places. Anyway, on disk images, the first 63 sectors are all disk header stuff, so to do anything with the partition itself (ie, copy files, manually modify files), you must mount past that. Since it appears that most of the qemu stuff uses 512 byte sectors, you must skip past the first 63 sectors * 512 byte/sector bytes to get to the first partition. This is the qemu "raw" file system; don't know much about the other ones.

Main reason I needed to know this is that I had forgotten just how freakin' bloated Windoze was. Tried to install in a 10G partition, and it worked, but when I installed XP SP3, that was all I had, basically. So, I wanted to grow the size. Using "partition images" you simply grow the container and then run the resize routine against the container. DOES NOT WORK HERE. So, some stuff on how to make all this work.

"getting to" the disk

Mounting a disk, especially those use by QEMU, can be difficult as Linux drive mapper doesn't assume LVM partitions and/or File Backed Devices (FBD's) are full devices; it believe they are simple partitions.

When one of these is actually a device image, kpartx comes to the rescue. kpartx takes a device (or image of a device) and creates /dev/mapper entries for every partition found in it, allowing you to treat those new mappings as you would a normal partition. This is handy if you need to add/remove files, or even do a disk resize.

kpartx -av path_to_device
# do what you need to do kpartx -dv path_to_device

When the -a (for add) is executed, it will show you the partitions it found and the mappings to it (well, not exactly, but it is close; look in /dev/mapper for the actual path).


 LVM Partitions

One problem you may have is if one of the partitions from kpartx is an LVM. I have not done this, so treat it with care.. This is taken from the article http://www.tuxradar.com/answers/212

  • run command vgscan. This rescans for all available LVM partitions. IF you have them named the same (ie, if you use vg0 for your DOM0's vg's and for the client machines), you're in trouble a little. The following MAY fix it (I'll test it out when I have a machine I can play with). You'll need to do a vgchange --available n to make the other one not available, then you'll need to rename the one on the DOMU's stuff, then make it available again. Doing this on a running server which may be using lvm for other DOMU's is NOT advisable. There must be a better way.
    • vgscan
      vgchange --available n
      vgrename VolGroup00 VolGroup01

      Reconnect the slave drive and type

      vgchange --available y



"getting to" the disk - Old Way

This doesn't work, but I'm leaving it here for a while

I had trouble mounting the disk built by an hvm (actually, it was built by qemu, but more on that later). The trick is that this image is a disk image, not a partition image. To mount the only partition on one of these, you need to skip past the sectors that are used to label the disk, and the boot, and all that. On a Window image (and, I think all others), this is offset 63 512 byte sectors. So, to actually mount the image:

mount -o loop,offset=$((63*512)),rw image_file_name mount_point

That info was found at Ian Blenke's notes on moving a VMWare Image to a Xen HVM image (no longer available). If you have ntfs-3g installed, you can use it to access the partition with full read/write capabilities, by making on minor change to the above.

mount -t ntfs-3g -o loop,offset=$((63*512)),rw image_file_name mount_point

This is especially useful if you want a compressed copy of the file. In this case, the following commands are helpful:

cd mount_point
rm -fRv WINDOWS/Temp/* # common place where Windows stores temp files
rm hyberfil # hybernation file
rm pagefile.sys # This is the swap file for virtual memory, and will be recreated on boot
rm temp/* # some old programs place temporary files here
dd if=/dev/zero of=deleteme # this creates a file of all empty space filled with zeros
rm deleteme # remove it so we are not messing up disk space
cd ..
umount mount_point
bzip2 image_file_name

You now have a very compressed file system. Not as good as if you had cleaned the disk first, but pretty good.

Rapidly increasing file size

the following increases a files size without writing anything to it. It strictly allocates the additional space for image file name to increase the file size to new size

dd if=/dev/zero of=image file name count=0 bs=1 seek=new size

Resize NTFS partition for QEMU (Xen HVM) disk

A lot of this is taken from http://wiki.centos.org/EdHeron/DomU_LVM_NTFS_resize#head-b3843fdcf44a1e04487eb63bfec0aec9f2b9e88d

I had a problem. I created a 10G LVM for a client to store their QuickBooks Windows XP installation on. When we upgraded, it left the old copy of Quickbooks on it, and installed the new version. However, it removed the uninstaller for the old version so I could not safely remove it.

Quickbooks is a hog, so the XP SP4 install, QuickBooks 6 and QuickBooks 11 installations took up all but 400M of the 10G I had given to the partition.

To resolve this, I decided to extend the device for WindowsXP. Windows assumes it owns a "disk", and the LVM is treated as a disk with one partition, so it is not as simple as one might thing. You must grow the disk (the LV), then grow the partition inside of it, then grow the NTFS file system under it.

Following is a step by step procedure I had to do to accomplish this. Much thanks to the above article for the final hints on getting the ntfs file system resized. In the following example, I am talking about an LV name quickbooks in a VG named virtuals. Thus, I am working on /dev/virtuals/quickbooks. The goal is to extend it from 10G to 20G. Modify the recipe for your needs.

apt-get install ntfsprogs kpartx
xm shutdown quickbooks # make sure the virtual is shut down
# grow the LV associated with the virtual
lvextend -L 20G /dev/virtuals/quickbooks
fdisk /dev/virtuals/quickbooks

At this point, you are in fdisk. You need to perform the following actions IN ORDER. Items in italics are the commands you execute. Text in normal type are comments by me.

  1. p - print (display) the partition table. Hopefully you only have one partition. Record the partition number and starting sector someplace. You will need it later. If you have more than one partition, stop now
  2. d - Delete the partition. Since you only have one partition, it will return automatically
  3. p - print the partition table. It should be empty
  4. n - create a New partition.
  5. 1 - Choose to create partition #1 (assuming that was the partition number from the first print)
  6. 1 - Enter the first sector. This is the number you recorded in the first step. This must be the same.
  7. Enter Key -- Accept the maximum value for the ending sector.
  8. t - Choose to change the Type of the partition
  9. 1 - Change the type on partition #1 (from the first in this list)
  10. 7 - Change to partition type 7 (NTFS)
  11. b - make partition bootable (if applicable)
  12. Choose primary boot partition
  13. w - Write the changes. Note you will get an error when you do this stating that the kernel could not re-read the partition table. That is normal.

At this point, you have extended the partition, but your file system still thinks it is the old value. Thanks to the centos article mentioned above, there is a quick fix for this.

kpartx -av /dev/virtuals/quickbooks # Note the message on reply. In my case, it told me the result was virtuals-quickbooks, so I use that below
ntfsresize /dev/mapper/virtuals-quickbooks1 # the "1" at the end is for partition #1
kpartx -dv /dev/virtuals/quickbooks # remove the mapper entry since you don't need it anymore.

You can now start the virtual up. It will take a little longer since ntfsresize sets the "test me out" flag.

Last update:
2013-05-21 08:30
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.