Resize an LVM-partitioned volume in RHEL 7, CentOS 7 and Oracle Linux 7 virtual machines

By default, all ProComputers LVM-partitioned RHEL 7, CentOS 7 and Oracle Linux 7 images are configured with a default 16 GiB disk volume, that is usually split like this:

$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   16G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 15.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var

The above command output tells the following:

  • There is a 16 GiB disk volume nvme0n1 attached to the virtual machine (nvme0n1 259:0 0 16G 0 disk)
  • There are 3 partitions on this disk volume, like below:
  1. nvme0n1p1 - the first partition that is 1 MiB in size (nvme0n1p1 259:1 0 1M 0 part).
  2. nvme0n1p2 - the boot partition (or the partition number 2) that is 512 MiB in size.(nvme0n1p2 259:2 0 512M 0 part /boot).
  3. nvme0n1p3 - the 3rd partition that is 15.5 GiB in size (nvme0n1p3 259:3 0 15.5G 0 part).
  • The partition number 3 (nvme0n1p3) is further split in 6 filesystems, using Logical Volume Manager or LVM.
  • The LVM volume group is called rootvg.
  • The LVM logical volume groups and corresponding filesystems are the following:
  1. rootlv or the root (/) filesystem that is 4 GiB in size;
  2. tmplv or the tmp (/tmp) filesystem that is 2 GiB in size;
  3. usrlv or the usr (/usr) filsystem that is 2 GiB in size;
  4. optlv or the opt (/opt) filesystem that is 2 GiB in size;
  5. homelv or the home (/home) filesystem that is 2 GiB in size;
  6. varlv or the var (/var) filesystem that us 3.5 GiB in size.

The above filesystem sizes are not enough in all situations, therefore this article describes the steps required to increase their size.

To effectively use this documentation article, it’s essential to have a solid working knowledge of CentOS Linux. This includes familiarity with the command line, file system structure, basic file operations, LVM (Local Volume Manager) and system configuration.

IMPORTANT : As with any partition / filesystem / LVM change, it is very important to prepare well. Make sure you test in advance this procedure using a test virtual machine. Also, make sure you have backups and/or snapshots of your virtual machine before performing the actual change. PROCOMPUTERS SHALL NOT BE HELD LIABLE FOR ANY LOSS, DAMAGE, OR DISRUPTION RESULTING FROM THE UPGRADE PROCESS. THIS INCLUDES, BUT IS NOT LIMITED TO, DATA LOSS, SERVICE INTERRUPTIONS, OR ANY OTHER ADVERSE EFFECTS THAT MAY ARISE.

To change the size of your filesystems, please follow the steps below.

As an example, the /usr and /var filesystem sizes will be increase with 2 GiB each.

  1. First, we will need to increase the disk volume attached to the virtual machine with 4 GiB (2 Gib for the /usr filesystem and 2 GiB for the /var filesystem).
  • In case your VM is running in AWS, use this AWS document to change the root volume size from 16Gib to 20 GiB.
  1. After the disk volume size is changed to 20 GiB, you should be able to see this line nvme0n1 259:0 0 20G 0 disk within the lsblk command output like below:
]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   20G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 15.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
  1. Extent the 3rd partition nvme0n1p3 with 4 GiB using the maximum free available space we have just added.
  • Relocate backup GPT data structures at the end of the nvme0n1 disk
$ sudo sgdisk --move-second-header /dev/nvme0n1
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
  • Extend the 3rd partition on the nvme0n1 disk to maximum available space
sudo parted --script /dev/nvme0n1 "resizepart 3 100%"
  • Inform the kernel about the changes on the 3rd partiton
$ sudo partx --update --nr 3 /dev/nvme0n1
  • To validate the above changes (nvme0n1p3 partition size increased with 4 GiB from 15.5 GiB to 19.5 GiB), you should be able to see this line nvme0n1p3 259:3 0 19.5G 0 part within the lsblk command output like below:
$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   20G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 19.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
  1. Extend the LVM physical volume size (with 4 GiB from 15.5 GiB to 19.5 GiB) to the maximum available space on the nvme0n1p3 partition:
$ sudo pvresize /dev/nvme0n1p3
  Physical volume "/dev/nvme0n1p3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

$ sudo pvdisplay /dev/nvme0n1p3
  --- Physical volume ---
  PV Name               /dev/nvme0n1p3
  VG Name               rootvg
  PV Size               <19.50 GiB / not usable 16.50 KiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              4991
  Free PE               1024
  Allocated PE          3967
  PV UUID               MFrfyd-UoXH-a72E-aUtE-UbdH-ziRR-hvrZSS
  1. Extend the usrlv LVM logical volume group and corresponding filesystem size with 2 GiB, from 2 GiB to 4 GiB:
$ sudo lvextend --resizefs --size +2G /dev/rootvg/usrlv
  Size of logical volume rootvg/usrlv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume rootvg/usrlv successfully resized.
meta-data=/dev/mapper/rootvg-usrlv isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524288 to 1048576
  1. Extend the varlv LVM logical volume group and corresponding filesystem size with maximum free available space, from 3.5 GiB to 5.5 GiB:
$ sudo lvextend --resizefs --extents +100%FREE /dev/rootvg/varlv
  Size of logical volume rootvg/varlv changed from <3.50 GiB (895 extents) to <5.50 GiB (1407 extents).
  Logical volume rootvg/varlv successfully resized.
meta-data=/dev/mapper/rootvg-varlv isize=512    agcount=4, agsize=229120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=916480, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 916480 to 1440768
  1. To validate the above changes, you should be able to see this line rootvg-usrlv 253:2 0 4G 0 lvm /usr and this line rootvg-varlv 253:5 0 5.5G 0 lvm /var within the lsblk command output like below:
$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   20G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 19.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    4G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  5.5G  0 lvm  /var
  1. You may reboot to make sure you have a clean start of your virtual machine when using the modified disk volume, however this is not required.
$ sudo reboot

References

For any questions related to this article, please contact ProComputers Support.

Why choose ProComputers?

With over 10 years of experience working with Microsoft Azure, ProComputers provides open-source software bundled together into solutions ready to be launched on-demand in the cloud.

ProComputers is a proud sponsor of the AlmaLinux OS Foundation and the Rocky Enterprise Software Foundation.

Red Hat and CentOS are trademarks or registered trademarks of Red Hat, Inc. or its subsidiaries in the United States and other countries. We are not affiliated with, endorsed by or sponsored by Red Hat or the CentOS Project.

All other trademarks are the property of their respective owners.