How to grow LVM and underlying partition?

I was messing around with a VM running Ubuntu 10.04 Server (64-bit) with LVM on a single 4GB disk. I just used the installer's automatic LVM setup and I landed with (ignore the disk size - that was after I resized the disk to be 8GB):

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b903

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32         523     3942401    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5              32         523     3942400   8e  Linux LVM

Ubuntu also made the LVM house just a root FS and a 512M swap.

At this point, I grew the underlying disk to 8GB using the VM management software, then booted into the VM and tried to figure out how to grow my swap and root FS. Reading man pvresize:

...
EXAMPLES
Expand the PV on /dev/sda1 after enlarging the partition with fdisk
pvresize /dev/sda1
...

This made me figure I had to first enlarge the partition with fdisk. So I tried deleting the extended partition 2 (which also deleted the logical partition 5 housing LVM), then recreating both partitions but this time using all cylinders (with the same partition numbers and also setting the logical partition's type to 8e for Linux LVM). Alas, that failed - I rebooted at that point, but the VM would no longer boot.

So is what I'm trying to do possible? What's the right way to go about it? Thanks in advance.


What you did should work, though I've not done it this way previously myself. As you are using a virtualisation solution you could do what I do to expand LVM based VMs: add a new virtual disk and add that to your LVM configuration - this way you don't have to mess about with partitioning at all.

Once you add the drive and restart the VM you'll see a new harddrive listed under /dev (such as /dev/sdb). Mark this as a physical volume (pvcreate /dev/sdb) and add it to your volume group (vgextend VolGroupName /dev/sdb).

Now you have a larger volume group you can expand the logical volumes into the new space with lvresize and expand the filesystems into the extra romm created in the volumes (with resize2fs for ext2/3/4, other tools for other formats).