LVM: Extending Physical Volume (under VMWare)

11,528

Solution 1

I just built a test virtual machine with a 15GB disk and LVM on /dev/sda2. Then I grew the disk to 20GB and made LVM see the extra 5GB without adding another physical volume.

Here are the steps I followed:

  • apt-get install gnu-fdisk (or yum install gnu-fdisk)

I had to use gfdisk to make the whole thing work. No luck with the "standard" fdisk.

  • gfdisk /dev/sda

Switch to "sectors" as the unit (this is critical!!!) and print the partition table:

Command (m for help): u                                                   
Changing display/entry units to sectors
Command (m for help): p                                                   

Disk /dev/sda: 21 GB, 21089617920 bytes
255 heads, 63 sectors/track, 2564 cylinders, total 41190660 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System 
/dev/sda1   *        2048      499711      257008   83  Linux
Warning: Partition 1 does not end on cylinder boundary.                   
/dev/sda2          501758    29798632    14643247   8e  Linux LVM
Warning: Partition 2 does not end on cylinder boundary.                   
Command (m for help):                                       

Write down the "start" sector of the "Linux LVM" partition (/dev/vda2). Delete the partition and recreate it using the same "start" sector (501758) and the same partition type (8e):

Command (m for help): d                                                   
Partition number (1-2): 2                                                 
Command (m for help): n                                                   
Partition type                                                            
   e   extended
   p   primary partition (1-4)
p
First sector  (default 63s): 501758                                       
Last sector or +size or +sizeMB or +sizeKB  (default 41190659s):          
Command (m for help): p                                                   

Disk /dev/sda: 21 GB, 21089617920 bytes
255 heads, 63 sectors/track, 2564 cylinders, total 41190660 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System 
/dev/sda1   *        2048      499711      257008   83  Linux
Warning: Partition 1 does not end on cylinder boundary.                   
/dev/sda2          501758    41190659    20338290   83  Linux
Command (m for help): t                                                   
Partition number (1-2): 2                                                 
Hex code (type L to list codes): 8e                                       
Changed type of partition 2 to 8e (Linux LVM)
Command (m for help):                                                     

WARNING: please note that I didn't accept the default start sector for the partition, I entered it manually so that it matches the original value! I did accept the default value for the "last sector" though, because I want this partition to be as large as the disk.

Verify that you did everything correctly with "p" and write the new partition table to disk:

Command (m for help): w                                                   

Reboot the virtual machine. Now log-in the virtual machine and run:

root@git:~# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Done! Now run vgdisplay and you will see the extra 5GB available as free extents.

CAVEATS:

  1. If LVM is not the last (or only) partition on the virtual machine disk, you might have an hard time extending the size of the partition. It might even be impossible.
  2. If LVM is on a logical partition (which is where Debian puts it by default at install time) or in other words if LVM is on /dev/sda5 instead of /dev/sda2, you must write down the starting sector of both the extended partition (let's call it /dev/sda2) and the logical partition (let's call it /dev/sda5), then delete both partitions, then recreate them with the same starting sector. The last sector of both /dev/sda2 and /dev/sda5 should be the last sector of the disk.
  3. Since this is a risky procedure I recommend doing a backup of the virtual machine before attempting it.

Solution 2

Here are the instructions I always use when I'm doing the same thing...

Do “fdisk –l /dev/sda” I’m assuming /dev/sda2 is your LVM partition and you don’t have a /dev/sda3. Adjust the instructions to match your config.

1) Make sure you have expanded the VM disk.

2) do “fdisk /dev/sda”

  • p (to see existing partitions)
  • n (new partition)
  • p (primary partition) -- give it the next available partition number, probably 3
  • accept the defaults. It will use the rest of the disk
  • w (write changes)

3) REBOOT

4) run ‘pvcreate /dev/sda3’

5) run ‘vgdisplay’ to get the name of the volume group. In this example, it’s “VolGroup”, as in: enter step5

6) run vgextend VolGroup /dev/sda3

7) run “df” to see the name of the root volume group.
step7

8) run lvextend /dev/mapper/VolGroup-lv_root -r -l+100%FREE ( the (-r) option causes it to format it, too)

9 run “df –h” to see your new disk space

Solution 3

Personally I followed the instruction in this blog.

To summarize the steps:

  • Increase the virtual disk size using the vm-ware tools vmware-vdiskmanager
  • Boot on another system (GParted live CD or mount an Iso CD-ROM to boot on)
  • Use the Gparted tools (easy and graphic interface) to increase the recently expended drive
  • remove the CD-ROM (or the ISO) and boot on the original partition.

For expanding my Linux VM (RedHat EL 6) I did boot on the GParted ISO image (from here) and I was able to extend the partition I wanted. Note I had to move the partition I wanted unchanged at the end.

It was safe with one of my colleague as well, but I would recommand to backup the VM before running Gparted Linux.

Solution 4

This is one of the reasons I prefer to have one big physical volume on the VMs that is separate from the /boot and other initial parts. This allows you to create a whole disk as an LVM physical disk (pvcreate /dev/sdb). Then if you need to do a resize, none of the file system changes need to happen because it is directly on disk.

In that scenario, you simply run pvresize /dev/sdb and move on with your vgextend, lvextend and resize2fs. Much simpler to maintain.

Share:
11,528
stolsvik
Author by

stolsvik

Merge KEEP! Java coder. Check LinkedIn page for more info.

Updated on June 18, 2022

Comments

  • stolsvik
    stolsvik almost 2 years

    I have a virtual server under VMware, where I got 10 more GB harddisk from the sysadm.

    Inside Linux (running Ubuntu 11.04 server), things are set up with LVM. In [c]fdisk, I do find these extra 10 gigs - they are present directly after the primary LVM partition.

    I understand that I could make a new LVM partition out of this extra space, and then add this to the volume group, and then to the logical volume, and then resize the filesystem.

    However, is it possible to assimilate these extra gigs into the existing LVM partition? So that the LVM's view of the physical volume increased instead of adding another physical volume into the volume group (preferably automatically percolating up to the volume group)?

  • freshop
    freshop about 3 years
    This still works in 2021, thank you! Was able to resize LVM partition with parted, reboot, pvrsize, and lvextend after a VMware disk increase.
  • nd34567s32e
    nd34567s32e about 2 years
    this one actually helped me. only really needed step 5 and beyond as i had used gparted live to extend partition