There is plenty of documentation for how to grow a VMware virtual disk file (VMDK), but there doesn’t seem to be a lot of documentation for Linux users who A) are using LVM and B) have aligned their partitions for performance reasons. I was asked how to do this and I thought I’d post it here for everybody. I basically have two suggestions. One is straightforward but you end up with two partitions in your volume group. The other is a little scarier but you’ll have only one partition in the end. This last method is what I was asked about, and what I’ll go into detail explaining.
Obligatory, seemingly obvious statement: have a backup of your system. This is also a great reason to have a test VM or two to practice on. Nothing destroys machines faster than doing bad things to a partition table, and while I intend this to be accurate you should live by the motto “trust, but verify” with stuff you find on the Internet. The nice thing is that you can take a snapshot of the VM and recover easily.
Option 1: Grow the VMDK file, then create a new partition with fdisk. Align its beginning to be on a 128 byte boundary (use fdisk’s expert mode to make the beginning sector of the new partition evenly divisible by 128). Reboot to pick up the partition table changes. Run “pvcreate” on the new partition. Use “vgextend” to grow your volume group. Easy.
Option 2: Delete and recreate your partition using fdisk. Let’s assume you’re running something like Red Hat Enterprise Linux 5, and you’ve installed it using LVM to one VMDK file, where you have two partitions: one for /boot, and one as the physical volume for the LVM. My default installations look something like this, from fdisk’s point of view:
Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200748+ 83 Linux /dev/sda2 26 3916 31254438 83 Linux
If your setup is more complex than this you’ll have to improvise.
1. Once you’ve resized the VMDK file I suggest taking a snapshot while everything is still working. That way if you completely hose your VM’s partition table you have a shot at trying again.
2. Run “/sbin/fdisk -u -l /dev/sda” to determine the size of the partitions in sectors. You need to do this if you’ve aligned your partitions. You should get something like:
11:37am phlox/plankers [~] 5$ sudo fdisk -u -l /dev/sda
Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System /dev/sda1 * 128 401624 200748+ 83 Linux /dev/sda2 401664 52420094 26009215+ 8e Linux LVM
3. Above, in bold, you’ll see the number you care about the most: the starting sector of /dev/sda2. In my case it’s 401664. It will be different for you. Copy all that output into a text file somewhere and save it.
4. Delete the partition and recreate it, with the new end point (which will be the default):
11:48am phlox/plankers [~] 8$ sudo /sbin/fdisk /dev/sda The number of cylinders for this disk is set to 3916. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200748+ 83 Linux /dev/sda2 26 3263 26009215+ 8e Linux LVM Command (m for help): d Partition number (1-4): 2 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (26-3916, default 26): (I pressed 'enter') Using default value 26 Last cylinder or +size or +sizeM or +sizeK (26-3916, default 3916): (I pressed 'enter') Using default value 3916 Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM)
5. Before you save the partition table, change the beginning sector of /dev/sda2 to match the old configuration exactly by going into expert mode. In my case it was 401664. It is different for you, use your original value:
Command (m for help): x Expert command (m for help): b Partition number (1-4): 2 New beginning of data (401625-62910539, default 401625): 401664
6. Write the new configuration:
Expert command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
7. Heed the warning about the kernel using the old partition table and reboot. If the VM comes back up, cool. If not, um, revert to your snapshot and figure out what went wrong.
8. Once the machine is back run “vgdisplay” and note the free space (Free PE / Size).
9. Run “pvresize /dev/sda2” to detect the new space.
10. Run “vgdisplay” again and note the free space. It should have grown, and you’re set.
As always, please leave a comment if something is wrong here or there’s a better way to do this (but please don’t suggest any utilities that can’t handle partition alignment).