virt-resize now resizes partition content

I fixed a lot of bugs in virt-resize and asked people what they wanted to see, and the new version of virt-resize (patches posted upstream here) can expand partition content automatically.

$ virt-df /dev/vg_trick/Windows7x64
Filesystem     1K-blocks       Used  Available  Use%
/dev/vda1         102396      24704      77692   25%
/dev/vda2       10381308    9417676     963632   91%
$ truncate -s 12G /tmp/WindowsResized.img
$ virt-resize /dev/vg_trick/Windows7x64 /tmp/WindowsResized.img \
     --expand /dev/sda2
Summary of changes:
/dev/sda1: partition will be left alone
/dev/sda2: partition will be resized from 9.9G to 11.9G
/dev/sda2: content will be expanded using the 'ntfsresize' method
Copying /dev/sda1 ... done
Copying /dev/sda2 ... done
$ virt-df /tmp/WindowsResized.img
Filesystem        1K-blocks       Used  Available  Use%
/dev/vda1            102396      24704      77692   25%
/dev/vda2          12478332    9417740    3060592   76%

After booting with the new disk:

10 Comments

Filed under Uncategorized

10 responses to “virt-resize now resizes partition content

  1. Hey Rich, that’s awsome!🙂

    Do you have a build somewhere? Will this be included in your new stable tree? Or do you still consider it to be an experimental feature.

    Thanks!

  2. Paul

    Used this to shrink a virtual machine ext4 partition but didn’t find using it very easy.

    The first problem I encountered is that there doesn’t seem to be a way to shrink ext2/ext3/ext4 partitions from inside guestfish as the guestfish version of resize2fs only accepts one parameter (the device), so it can only resize to the underlying device. Had to resort to booting the virtual machine from an bootable CD iso and using gparted to shrink the partition. Did I miss something?

    My second problem, after having shrunk the ext4 partition, was that I couldn’t get virt-resize to work untill I had moved all the free space to the end of the “device”.

    Initially I had something that looked like:
    16G ext4 /dev/sda1
    8G free
    1G swap /dev/sda2

    I created a 17G outdisk and tried “virt-resize indisk outdisk” but it just give the error “Error: Can’t have a partition outside the disk!”

    I also tried creating an ext3 partition in the free space and tellling virt-resize to delete it but it complained the outdisk wasn’t big enough.

    e.g. I had the following:
    16G ext4 /dev/sda1
    8G ext3 /dev/sda3
    1G swap /dev/sda2

    but “virt-resize –delete /dev/sda3 lx64pac16-virt2.img outdisk” only resulted in:
    /dev/sda1: partition will be left alone
    /dev/sda3: partition will be deleted
    /dev/sda2: partition will be left alone
    virt-resize: error: there is a deficit of 8587706368 bytes (8.0G).
    You need to make the target disk larger by at least this amount,

    Finally I move the swap partition (again using gparted) so I had:
    16G ext4 /dev/sda1
    1G swap /dev/sda2
    8G free

    Then “virt-resize indisk outdisk” copied the partitions over.

    Also had a problem booting from the outdisk which I had to fix by using the bootable CD ISO and getting grub-install to rewrite thr MBR.

    Thanks for taking the time to build what looks to be a useful tool. I hope these comments can feed into making it much better.

    p.s. I used libguestfs-tools-1.3.5-1.fc14.x86_6

  3. rich

    The first problem I encountered is that there doesn’t seem to be a way to shrink ext2/ext3/ext4 partitions from inside guestfish as the guestfish version of resize2fs only accepts one parameter (the device), so it can only resize to the underlying device. Had to resort to booting the virtual machine from an bootable CD iso and using gparted to shrink the partition. Did I miss something?

    No in fact I missed something there. We need to add a variant of resize2fs which allows you to specify the (reduced) block count. (Edit: bugs added for supporting shrinking in resize2fs, pvresize and ntfsresize)

    For the rest of your comment, could you please create a BZ (or more likely, 2 or 3). I’d really like to reproduce each of these issues individually, since it seems like there are several different bugs here.

    Thanks for trying virt-resize.

    • Paul

      Unfortunately as I was pressed for disk space I deleted the orginal virtual disk image after I managed to get the shrunken file system working.

      It might be possible to reproduce some of these problems by creating a new virtual machine. Unfortunately I don’t reallh have time to try.

      I used Fedora 12 as the hypervisor when I created the orginal virtual machine disk image. The virtual disk was then partitioned and formated by booting from a bootable Fedora 12 DVD iso (as part of installing the OS on the VM). I created a conventional filing system (not lvm) with two primary partitions, /dev/sda1 an ext4 partition and /dev/sda2 a swap partition. This virtual disk I later tried to resize (as above).

      Hope that helps.

      Paul

  4. virt-resize is great – it’s not as easy to resize VMs as I thought it would be, but now I’ve got to grips with the process, it’s something I can do if I have to. I’ve been shrinking a few early VMs for which I got a little over-generous when creating them.

    I did find that virt-resize would complain about the size available if the outdisk had anything written into it already. I also found that if you ask it to write to an outdisk that’s too small, it correctly alerts you to the problem, and tells you how much you need to extend outdisk by. However, I found that unless you extend outdisk by about 50M more than virt-resize suggests, it would complain with “Error: Can’t have a partition outside the disk!”.

    In short, it seems it’ll emit some confusing error messages if you do anything slightly wrong. However, now I’ve got it sussed, it’s definitely something I can use (although growing partitions is a lot easier than shrinking them!). I’m wondering if I could script something in guestfish (or just the Unix shell) to take some of the pain out of some of the steps…

    Either way, thank you – it’s a great tool – and “ticks a box” for the advantages of VMs over real machines🙂

    • rich

      The shrinking bug is this one:
      https://bugzilla.redhat.com/show_bug.cgi?id=633766
      and the error message comes from parted (it’s essentially a bug in parted itself).

      Resizing manually is harder than it looks. I outline the basic steps here:
      https://www.redhat.com/archives/libguestfs/2010-September/msg00009.html

      • Thanks so much for the response, and for the bugzillas. I just found another (minor?) bug… My partition names are /dev/vdaX (is this because I have virtio disks?) – if I resize a VM specifying something like this:

        virt-resize –expand /dev/vda2 /vm/infile.img /vm/outfile.img

        …virt-resize says things like this:

        Summary of changes:
        /dev/sda1: partition will be left alone
        /dev/sda2: partition will be resized from 2.0G to 4.5G
        /dev/sda2: content will be expanded using the ‘pvresize’ method
        /dev/sda3: partition will be left alone
        Copying /dev/sda1 … done
        Copying /dev/sda2 … done
        Copying /dev/sda3 … done
        Expanding /dev/sda2 using the ‘pvresize’ method

        …hardly a big deal, and actually this output follows the man page, but not what I specified on the command line. Like I say, not a big deal, but thought you might like to know😉

  5. Pingback: Caseless virtualization cluster, part 5 | Richard WM Jones

  6. Pingback: Removing the cache from an LV | Richard WM Jones

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s