How to extend filesystem on Linux (root and other)

In this tutorial, we will teach you how to resize your hard disk partitions in Linux by introducing a very useful tool. You can easily resize all partitions with this utility. Whether this is a root partition or any other partition. No need to enter a rescue mode or reboot. this will also cover both ext4 and XFS filesystem root partition extending. This tool is called growpart and we are going to do a lot of things in this tutorial using this tool.

growpart command : #

growpart, is a Linux command-line tool used to extend a partition in a partition table to fill available space. This command is provided by cloud utils package.

How Install cloud utils package on the system #

On Ubuntu / Debian system, run the commands below to install growpart tool.

sudo apt install cloud-guest-utils

For CentOS server, run:

sudo yum -y install cloud-utils-growpart

Help page can be viewed by passing -h argument

growpart04 - How to extend filesystem on Linux (root and other)

Ok , Now we can go to extend our root partitions (or other) !
In the first step, we start the tutorial according to the LVM structure on the root partition:

How to extend root filesystem using LVM on Linux #

To demonstrate a complete LVM lifecycle, we will perform the following actions:
  • Create an LVM physical volume, volume group, and logical volume.
  • Create an XFS and ext4 filesystem on the logical volumes
  • Extend LVM logical volumes ( root and non-root filesystem)
LVM allows you to create, resize or delete partitions on a running system without requiring any reboot. So check the steps below to extend the root filesystem using LVM in Linux. You can skip some steps which don’t apply to use.

1. Check the condition of the disk and its structure #

Before we can do any extension, let’s just check our disk layout/partitioning scheme. (command : lsblk)
growpart01 - How to extend filesystem on Linux (root and other)
As you see, we have a root filesystem on /dev/sda2 physical volume.
growpart02 - How to extend filesystem on Linux (root and other)

2: Extend your desired disk size (root partition or other) #

As shown in step 1, my root filesystem is on a 20GB disk. I’ll grow it to 50GB by extending the virtual disk (VM disk device).

If you did not reboot your server after resizing the partition, rescan your SCSI devices as such.
First, check the name(s) of your SCSI devices.

$ ls /sys/class/scsi_device/
0:0:0:0 1:0:0:0 2:0:0:0

Then rescan the scsi bus. Below you can replace the ‘0:0:0:0’ with the actual scsi bus name found with the previous command. Each colon is prefixed with a slash, which is what makes it look weird.

  ~$ echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan
That will rescan the current scsi bus and the disk size that has changed will show up.
growpart03 - How to extend filesystem on Linux (root and other)

Now use growpart to extend your partition. In this example, we’re extending partition 2 in disk /dev/sda. (Replace 2 and /dev/sda with your correct values.)

# growpart /dev/sda 2

CHANGED: partition=2 start=2099200 old: size=39843840 end=41943040 new: size=102758367 end=104857567

Confirm if the change was successful.

growpart05 1 - How to extend filesystem on Linux (root and other)

Step 3: Resize root logical volume to occupy all space #

Resize physical volume. (command: pvresize)

# pvresize /dev/sda2
Physical volume “/dev/sda2” changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

 

growpart06 1 - How to extend filesystem on Linux (root and other)
Check the size of the volume group configured.
growpart07 - How to extend filesystem on Linux (root and other)
Then resize logical volume used by the root file system using the extended volume group:
lvextend -r -l +100%FREE /dev/name-of-volume-group/root
Here’s an example of my setup file system extension:
growpart08 - How to extend filesystem on Linux (root and other)

This extends the logical volume to use all available capacity in the volume group. With the + sign the value is added to the actual size of the logical volume.

Command options used:

  • -l – extend or set the logical volume size in units of logical extents
  • -r – Resize underlying filesystem together with the logical volume
If you prefer setting the size to be extended manually, use command option:
-L, –size [+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]

Where size suffix are:

  • M for megabytes
  • G for gigabytes
  • T for terabytes
  • P for petabytes
  • E for exabytes

Without the + sign the value is taken as an absolute one.

# Add 20 gigabytes to the current logical volume size
$ sudo lvextend -r -L +20G /dev/name-of-volume-group/root

So we have now:

growpart09 - How to extend filesystem on Linux (root and other)

and still 30G Free:

growpart10 - How to extend filesystem on Linux (root and other)

4: Update changes on the filesystem (If you didn’t use -r option in step 3) #

Your root filesystem will still show the old size.

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

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

For ext4 filesystem

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

For xfs filesystem

$ sudo xfs_growfs /

 

 

 

Powered by BetterDocs