How to use LVM to extend the root file system on Linux

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Welcome to our guide to learn how to extend the root file system using LVM on Linux. This will cover ext4 and XFS file system root partition expansion. To demonstrate the complete LVM life cycle, we will perform the following operations:

  • Create LVM physical volumes, volume groups and logical volumes.
  • Create XFS and ext4 file systems on logical volumes
  • Extend LVM logical volume (root and non-root file system)

LVM allows you to create, resize or delete partitions on a running system without any reboot. Therefore, please check the following steps to extend the root file system using LVM in Linux. You can skip some steps that are not applicable.

If you are not using LVM, please check the guide below.

How to resize ext2/3/4 and XFS root partition without using LVM

Step 1: Confirm the disk partition in the distribution.

Before any expansion, we only confirm the disk layout/partition scheme.

$ lsblk 
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0            11:0    1 1024M  0 rom  
vda           252:0    0   30G  0 disk 
├─vda1        252:1    0    1G  0 part /boot
└─vda2        252:2    0   29G  0 part 
  ├─rhel-root 253:0    0 26.9G  0 lvm  /
  └─rhel-swap 253:1    0  2.1G  0 lvm  [SWAP]

As mentioned before, we have a root file system on the /dev/vda2 physical volume.

$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/vda2  rhel lvm2 a--  <29.00g    0 

Step 2: Expand the OS root disk

As shown in step 1, my root file system is on a 30GB disk. I will increase it to 40GB by expanding the virtual disk (VM disk device).

I use KVM virtualization technology, so this guide is useful to me: How to expand/increase KVM virtual machine (VM) disk size

$ lsblk  NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sr0            11:0    1 1024M  0 rom   vda           252:0    0   40G  0 disk  ├─vda1        252:1    0    1G  0 part /boot └─vda2        252:2    0   29G  0 part    ├─rhel-root 253:0    0 26.9G  0 lvm  /   └─rhel-swap 253:1    0  2.1G  0 lvm  [SWAP]

If you are using another virtualization platform, please refer to its documentation to learn how to expand the OS disk.

After adjusting the size of the OS block device, please SSH on the Linux machine and expand LVM to use the newly added disk capacity. The following command will extend the last partition (Partition 2) (As shown in 252:2) reached the maximum size provided by the disk on the disk (/dev/vda).

Install cloud tools

For those newcomers growing up, This is a Linux command line tool used to expand the partitions in the partition table to fill the available space. This command is provided by the cloud utils package.

On Ubuntu/Debian systems, run

sudo apt -y install cloud-guest-utils

For CentOS servers, run

sudo yum -y install cloud-utils-growpart

You can view the help page in the following ways -h argument

# growpart -h
growpart disk partition
   rewrite partition table so that partition takes up all the space it can
   options:
    -h | --help       print Usage and exit
         --fudge F    if part could be resized, but change would be
                      less than 'F' bytes, do not resize (default: 1048576)
    -N | --dry-run    only report what would be done, show new 'sfdisk -d'
    -v | --verbose    increase verbosity / debug
    -u | --update  R update the the kernel partition table info after growing
                      this requires kernel support and 'partx --update'
                      R is one of:
                       - 'auto'  : [default] update partition if possible
                       - 'force' : try despite sanity checks (fail on failure)
                       - 'off'   : do not attempt
                       - 'on'    : fail if sanity checks indicate no support

   Example:
    - growpart /dev/sda 1
      Resize partition 1 on /dev/sd

Now use growpart to extend the partition.

$ sudo growpart /dev/vda 2
CHANGED: partition=2 start=2099200 old: size=18872320 end=20971520 new: size=60815327,end=62914527

This will adjust the size of partition 2 on /dev/vda.

Verify the changes.

$ lsblk  NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sr0            11:0    1 1024M  0 rom   vda           252:0    0   40G  0 disk  ├─vda1        252:1    0    1G  0 part /boot └─vda2        252:2    0   39G  0 part    ├─rhel-root 253:0    0 26.9G  0 lvm  /   └─rhel-swap 253:1    0  2.1G  0 lvm  [SWAP]

Step 3: Adjust the size of the root logical volume to occupy all the space

Adjust the size of the physical volume.

$ sudo pvresize /dev/vda2  Physical volume "/dev/vda2" changed  1 physical volume(s) resized or updated / 0 physical volume(s) not resized$ sudo pvs  PV         VG   Fmt  Attr PSize   PFree   /dev/vda2  rhel lvm2 a--  <39.00g 10.00g

Check the size of the configured volume group.

$ sudo vgs   VG   #PV #LV #SN Attr   VSize   VFree    rhel   1   2   0 wz--n- <39.00g 10.00g

Then use the resized volume group to adjust the root size of the logical volume

sudo lvextend -l +100%FREE /dev/name-of-volume-group/root

In my example, this would be:

$ df -hT | grep mapper /dev/mapper/rhel-root xfs        27G  1.9G   26G   8% /$ sudo lvextend -l +100%FREE /dev/mapper/rhel-rootSize of logical volume rhel/root changed from <26.93 GiB (6893 extents) to <36.93 GiB (9453 extents).Logical volume rhel/root successfully resized.

You can replace the 100% FREE command with the preferred space in MB, for example, add 256 MB to replace it with +256M.

Step 4: Update the changes on the file system

Your root file system will still show the old size.

$ df -hT | grep mapper /dev/mapper/rhel-root xfs        27G  1.9G   26G   8% /

Let’s make the file system report the actual size, including the extended size.

For ext4 file system

sudo resize2fs /dev/name-of-volume-group/root

For xfs file system

$ sudo xfs_growfs / meta-data=/dev/mapper/rhel-root  isize=512    agcount=4, agsize=1764608 blks          =                       sectsz=512   attr=2, projid32bit=1          =                       crc=1        finobt=1, sparse=1, rmapbt=0          =                       reflink=1 data     =                       bsize=4096   blocks=7058432, imaxpct=25          =                       sunit=0      swidth=0 blks naming   =version 2              bsize=4096   ascii-ci=0, ftype=1 log      =internal log           bsize=4096   blocks=3446, version=2          =                       sectsz=512   sunit=0 blks, lazy-count=1 realtime =none                   extsz=4096   blocks=0, rtextents=0 data blocks changed from 7058432 to 9679872

in conclusion

You have learned how to use the LVM guide to extend the root file system supported by nfs and ext4. Hope it helps, and thank you for reading.

label:

  • Resize the root file system on Linux
  • Resize/use LVM on Linux
  • Resize the LVM root file system on Linux
  • An easy way to adjust the size of the LVM root file system on Linux
  • Use LVM to extend the root file system on Linux
  • Extend the LVM root file system on Linux
  • Extend LVM / on Linux

Similar articles:

How to resize ext2/3/4 and XFS root partition without using LVM

How to extend the EBS boot disk on AWS without restarting the instance

How to extend/increase KVM virtual machine (VM) disk size

How to use diskpart command to create disk partition in Windows

Use qemu-img in Linux

Understand and use the BtrFS file system in Linux

Install Arch Linux with LVM on UEFI system

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Sidebar