CentOS / RHEL : How to extend Physical Volume in LVM by extending the Disk Partition used

The post discusses on how to expand the space available in an LVM volume by extending the physical disk partition using fdisk. This example shows how to resize the physical volume /dev/sdc1 from 200MB to 400MB.

Potential Data Loss Warning: This step will delete the existing partition structure and create a new partition in its place. It is essential that the new partition be created at exactly the same origin as the deleted partition, else severe data corruption and loss will occur.

Check the current configuration

1. Check the current size of the physical volume on the disk.

# pvdisplay /dev/sdc1
  "/dev/sdc1" is a new physical volume of "200.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               200.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               EkD95A-wb04-5GHn-OH7N-suVt-vak2-AKSsyv

2. Get the information about the current LVM setup. The logical volume which we want to extend is ‘testlv’.

# lvscan
  ACTIVE            '/dev/testvg/testlv' [100.00 MiB] inherit

Deactivate the existing logical volume

1. Before we proceed with the actual PV and LV expansion, we must deactivate the logical volume first. Use the below command to deactivate the LV.

# lvchange -a n /dev/testvg/testlv 

2. Verify if the volume has been deactivated.

# lvscan
  inactive          '/dev/testvg/testlv' [100.00 MiB] inherit

Now we know the LVM is not being used, so we can start the expansion process on it.

Extend the partition “/dev/sda1” with fdisk utility

1. In order to extend the physical device partition information, we first delete the old partition and then create a new one. Note that we first print out the current partition information so that we know precisely where the partition starts. We must overlay the new, larger partition beginning at the same block as the old partition.

# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x085579f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048      411647      204800   83  Linux

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):          ### we will press ENTER here to select default first sector 2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +400M
Partition 1 of type Linux and of size 400 MiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

This step can be aborted at any time prior to writing out the altered partition table with the “w” command. Until then, only an in-memory copy of the partition table is altered.

About Changing Partition Tables: The partition information for all the LUNs on a physical device are kept in a partition table. The fdisk command only manipulates this partition table. The remainder of the disk is always left unaltered. This means the partition table information can be changed but the content of the partitions is never touched, always left intact.

Load the new partition table to kernel

The currently-running kernel is not aware of the new partition information, since fdisk alters the partition table by writing directly to the device. Normally, the kernel only reads partition information at system boot or when a hot-plug event (such as inserting a USB device) occurs.

However, we can avoid a needless reboot by causing the kernel to re-read its partition information, and then to update its LVM information.

# partprobe

Now that the kernel is aware of the updated partition table, inform the LVM subsystem about this change to the physical device. By default the resize will use the entire partition size as obtained from the partition table:

# pvresize /dev/sdc1 
  Physical volume "/dev/sdc1" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
# pvdisplay /dev/sdc1
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               testvg
  PV Size               399.00 MiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              99
  Free PE               74
  Allocated PE          25
  PV UUID               EkD95A-wb04-5GHn-OH7N-suVt-vak2-AKSsyv

Active the existing logical volume

The final step is to bring the modified LVM back on-line. As always, we check our work first:

# lvscan
  ACTIVE            '/dev/testvg/testlv' [100.00 MiB] inherit

As seen from the output above, the logical volume is inactive. So lets activate it first.

# lvchange -a y /dev/testvg/testlv
# /usr/sbin/lvscan
ACTIVE '/dev/vgtest/lvtest' [100.00 MB] inherit

Resizing the Logical Volume (optional)

1. You can now also extend the logical volume as you have more space in the underlying physical volume now. Use the command below to resize the LV.

# lvresize -L +200M /dev/testvg/testlv 
  Size of logical volume testvg/testlv changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
  Logical volume testvg/testlv successfully resized.

2. Verify the new sise of the logical volume.

# lvscan
  ACTIVE            '/dev/testvg/testlv' [300.00 MiB] inherit
Related Post