How can I resize LVM partition ie physical volume?

In this lesson, we’ll take a look at how to extend your Linux LVM volumes. This isn’t very difficult to do but before we start, let me explain some terminology about LVM:

How can I resize LVM partition ie physical volume?

On the left side we see our PV (Physical Volume). These are partitions on your physical harddisks that are available to LVM. In the middle is the VG (Volume Group) which is a group of physical volumes. The LV on the right side is our logical volume.  This is a “piece” of the volume group that we can use as a partition for our operating system.  Now you know about the terminology, let’s look at an actual example where we expand the size of our logical volume.

In short, here are the steps to extend the size of your logical volume:

  1. Create new partition on harddisk.
  2. Add the partition you just created as a physical volume.
  3. Add the new physical volume to the volume group.
  4. Assign space from the volume group to the logical volume.
  5. Resize the filesystem.

Before you start making changes to your partitions and/or LVM configuration, backup everything. This process is relatively straightforward but cannot be undone if things go wrong.

I’m using a virtual machine that has a 30GB harddisk at the moment. We can verify this with the fdisk command:

# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x000d78cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        3917    30944256   8e  Linux LVM

Disk /dev/mapper/vg_vmware-lv_root: 29.6 GB, 29569843200 bytes
255 heads, 63 sectors/track, 3594 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: 0x00000000

Disk /dev/mapper/vg_vmware-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

As you can see above there’s a single volume group called “vmware” which has two logical volumes (lv_root and lv_swap). The idea is to extend the size of lv_root so that we have more space to use. If you want to take a closer look at the partitions then you need to use the df command:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot

Here you can see that the lv_root logical volume is 28G and has 12G available. To increase the “physical” size of our disk I will power off this machine and extend the size of the harddisk in vmware to 60GB:

 

How can I resize LVM partition ie physical volume?

After starting the virtual machine here’s what fdisk looks like:

# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000d78cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        3917    30944256   8e  Linux LVM

Disk /dev/mapper/vg_vmware-lv_root: 29.6 GB, 29569843200 bytes
255 heads, 63 sectors/track, 3594 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: 0x00000000

Disk /dev/mapper/vg_vmware-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

Great! Our physical disk is now 60GB (64GB actually…) but no changes were made to our physical volume and logical volumes. That’s something we have to do ourselves. The first thing to do is to create a new partition on the harddisk and tell LVM that it can be used as a physical volume:

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3917-7832, default 3917): 
Using default value 3917
Last cylinder, +cylinders or +size{K,M,G} (3917-7832, default 7832): 
Using default value 7832

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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

I just created a new primary partition on the harddisk and assigned all available free space to it. Reboot your computer and take a look at fdisk:

# fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000d78cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        3917    30944256   8e  Linux LVM
/dev/sda3            3917        7832    31453260   83  Linux

Above you see the newly created /dev/sda3 Linux partition.

In my example I created an extra partition on the current harddisk. Of course you can also use a new harddisk, partition it and add it to your volume group. The only difference will be the partition letter, for example: /dev/sdb1 .

Now we can add it to our physical volume, here’s what it looks like right now:

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_vmware
  PV Size               29.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7554
  Free PE               0
  Allocated PE          7554
  PV UUID               rxHFrq-a1p1-8woW-1Vr2-4C6w-zLQt-IdTgFk

Use the pvdisplay command to see the current state of the physical volume. Right now only the /dev/sda2 partition is a physical volume so we’ll tell LVM that our /dev/sda3 partition is a PV as well with the pvcreate command:

# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

Run the pvdisplay command again so see the results:

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_vmware
  PV Size               29.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7554
  Free PE               0
  Allocated PE          7554
  PV UUID               rxHFrq-a1p1-8woW-1Vr2-4C6w-zLQt-IdTgFk

  "/dev/sda3" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JRdnMd-4bm1-ahFI-6FtR-Opzl-4SBL-OXmKFQ

Our /dev/sda3 partition has been added as a physical volume. Now we can add it to the volume group. Use the vgdisplay command to check for the name of your volume group:

# vgdisplay | grep Name
  VG Name               vg_vmware

In my example it’s called “vg_vmware”. Let’s add the /dev/sda3 partition to this volume group:

# vgextend vg_vmware /dev/sda3
  Volume group "vg_vmware" successfully extended

The vgextend command adds it to the volume group, let’s look at the volume group again to see our changes:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
0

Excellent…our volume group is now close to 60GB. We still have to assign this space to our logical volume…let’s use lvdisplay to see the name of our logical volumes:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
1

I want to use all the free space from my volume group and add it to the lv_root logical volume. We can use the lvextend command for this:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
2

There we go, lv_root is now almost 60GB. Let’s verify our work:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
3

We are almost done but we still have to resize the actual filesystem, otherwise your operating system can still only use 30GB. I’ll use resize2fs for this:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
4

This process can be done without rebooting. If everything went OK you now have more free space to use:

[root@vmware ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G   15G   12G  57% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
5

Great! Our operating system has more free space to use now. Hopefully this lesson has been useful for you to expand your logical volume(s) and perhaps to understand Linux LVM a little bit more. If you have any questions feel free to leave a comment!

What tool should you use to resize a physical volume in LVM?

The lvextend command allows you to extend the size of the Logical Volume from the Volume Group.

Can we extend physical volume?

To resize or extend a Volume group you can either add a new Physical Volume or extend an existing Physical volume. In the latter case, VG automatically recognizes the new increase size whereas in the former case you have to extend the VG using vgextend. Let's see an example of resizing the existing PV to grow VG size.

Should you use to replace a physical volume in LVM?

To remove any physical volume in a volume group vgreduce command can be used. The vgreduce command shrinks the volume group by removing one or more PVs. We can then either use these free PVS in another VG or remove them from the LVM configuration.

What command would you use to resize an LVM volume group to include an additional physical volume?

How to resize volumes with LVM.
To see the details of logical volume group use: vgdisplay..
To see the details of logical volumes use: lvdisplay..
To create new volumes, use: lvcreate..
To resize existing volumes, use: lvresize..
Use "--help" see quick help for the command: lvresize --help..