Skip to content

Expanding a Linux Partition

Overview

Instructions for how to expand partition with LVM on Linux. These have only been validated on CentOS 7. These instructions also assume a virtual system such as VMWare guest or cloud compute units as greater than 99% of Nanitor systems are running in this kind of environment.

For additional reading on this check out the following blog https://www.tecmint.com/extend-and-reduce-lvms-in-linux/

Prep work and analysis

sudo fdisk -l
Disk /dev/sda: 322.1 GB
This shows you the size of each physical disk along with the name of each disk, you'll need the disk name ( /dev/sda in this example) for the actual expanding

sudo pvdisplay
--- Physical volume ---
 PV Name /dev/sda2
 VG Name centos
 PV Size <299.12 GiB / not usable 2.00 MiB
 Allocatable yes
This shows you the details about the physical volume in LVM. PV Name is important in all PV operations ( pvs shows summary).

sudo vgdisplay
--- Volume group ---
 VG Name centos
 System ID 
 Format lvm2
 Metadata Areas 1
 Metadata Sequence No 22
 VG Access read/write
 VG Status resizable
 MAX LV 0
 Cur LV 8
 Open LV 8
 Max PV 0
 Cur PV 1
 Act PV 1
 VG Size <299.12 GiB
 PE Size 4.00 MiB
 Total PE 76574
 Alloc PE / Size 61875 / <241.70 GiB
 Free PE / Size 14699 / <57.42 GiB
This command shows you the volume Group info. The most important information here is that last line with information on if there is free space to grow into. PE stands for Physical Extents, which is an allocation block in LVM. For the summary, output use the sudo vgs command.

sudo lvdisplay
--- Logical volume ---
 LV Path /dev/centos/var_log
 LV Name var_log
 VG Name centos
 LV UUID SsGEDQ-NQ5K-DCyD-JBpb-Effk-JEV1-lbXxWw
 LV Write Access read/write
 LV Creation host, time localhost, 2015-03-13 16:24:46 +0000
 LV Status available
 # open 1
 LV Size 1000.00 MiB
 Current LE 250
 Segments 1
 Allocation inherit
 Read ahead sectors auto
 - currently set to 8192
 Block device 253:4

 --- Logical volume ---
 LV Path /dev/centos/var
 LV Name var
 VG Name centos
 LV UUID cbVTAe-a1wO-QAmi-XifB-ukMX-ctdx-JUjapu
 LV Write Access read/write
 LV Creation host, time localhost, 2015-03-13 16:24:46 +0000
 LV Status available
 # open 1
 LV Size 4.88 GiB
 Current LE 1250
 Segments 1
 Allocation inherit
 Read ahead sectors auto
 - currently set to 8192
 Block device 253:5

 --- Logical volume ---
 LV Path /dev/centos/home
 LV Name home
 VG Name centos
 LV UUID NBHOM4-2nav-UXcB-fBBy-ysp8-QYnx-kHFPhu
 LV Write Access read/write
 LV Creation host, time localhost, 2015-03-13 16:24:46 +0000
 LV Status available
 # open 1
 LV Size 1000.00 MiB
 Current LE 250
 Segments 1
 Allocation inherit
 Read ahead sectors auto
 - currently set to 8192
 Block device 253:6
This command shows you all the logical volumes with all sorts of details. To get a summary listing use sudo lvs. LV Path and LV Size are most important here. The resize command takes the LV for the logical volume that you need to expand.

Process for expanding

Assuming there is no space available to grow into, you need to start by adding space in the virtualization control panel. Then reboot the server so it sees the additional space provided. To see if you have space to grow into or not, reference the vgdisplay command from above. If you already have enough space to grow into according to vgdisplay command, there is no need to make any changes in your virtualization control panel and you can jump straight to step 4.

This process involves changing the partition table, which is a scary step for anyone used to Windows OS (see the below quote to alleviate your fears), loading the new partition table into memory followed by change to the logical volume manager. Here are the detailed steps

About Changing Partition Tables: The partition information for all the LUNs on a physical device is 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. -- https://www.thegeekdiary.com/centos-rhel-how-to-extend-physical-volume-in-lvm-by-extending-the-disk-partition-used/

  1. sudo fdisk /dev/sda
    1. This takes you into the partition utility, which is menu driven system where you give single-letter commands. Give the following:
      1. p, Print the current partition table so that you know what it was to begin with
      2. d then 2, to delete the second partition (adjust number per your scenario)
      3. n, for a new partition. If you want a new partition to fill up the disk, accepting all the defaults are good
      4. p, print out the state after changes
      5. w, to write the changes to disk and exit fdisk
  2. sudo partx -u /dev/sda
    • forces the kernel to refresh the in-memory partition table
  3. sudo pvresize /dev/sda2
    • resizes the physical volume to match the new physical disk partition
  4. sudo lvextend -l +2000 /dev/centos/usr
    • Adds 2000 PEs to LV Path /dev/centos/usr
    • vgdisplay gives you the number of free PEs and how many GB that is. See man lvextends for a detail explanation
  5. sudo xfs_growfs /dev/centos/usr
    • Grows the actual filesystem to match the logical volume.