Figuring out how to use lvextend command on Linux helps you manage the storage partitions easily. We’re here to help you guide through the process.
Linux uses LVM (Logical Volume Manager) to manage logical partitions of physical volume. Logical partitions logically divide the physical hard drive, SSD, or any other storage media for better performance. A single large partition of physical volume may become too clumsy to manage over time.
There are many logical volume managers out there on Linux, but here we mainly focus on the lvextend command. The LVM2 (Logical Volume Manager 2) command provides this command. The lvextend command or program facilitates different options to manage i.e., increase the size of the different partitions easily.
How to Use the Ivextend Command on Linux
These partitions can be used by different file systems to store documents, media, etc. If you are a Linux admin or Linux enthusiast, learning how to resize file systems is a great skill.
Prerequisite
Some obvious and not apparent prerequisites are:
- A Linux machine
- Sudo privilege of that machine
- Any external storage or internal storage you want to extent
- lvm2 package
Install lvm2 package
To get started, you must have any LVM tools you like. Install LVM package with the following command like this:
$ sudo apt update
$ sudo apt-get install lvm2
It’s a pretty easy step. Once done, you can verify it with the following command:
$ lvm version
If you see something like this, you are good to go.
Check for Logical and Physical Volume
Now that you have successfully installed the lvm package. To extend a logical volume, you must have a physical volume first that you can use here. Run the following sets of commands to verify it.
$ sudo pvs $sudo vgs
You will see like above if your physical media is mounted to the Linux system. Now, run the following command to check for free available space that can be extended.
For this tutorial, we will text the lv01
volume, which is currently 100 MB.
Extending the Logical Volume using lvextend
You have several methods in the lvextend command that can be followed to extend logical volume. Let’s follow them one by one.
Method 1: Extending volume by Given Amount
To follow along with this tutorial, we will be extending our volume (logical) by 100 MB. To be more precise, this command extends to already free logical storage.
For example, we have 100MB already free. Using this command, we will extend by 100MB more total of 200MB. The syntax for that command is given below:
$ lvextend -L +<amount> <path-of-volume>
For our example, as said earlier, the amount will be 100MB, and the path is “dev/vg01/lv01
”. So, the command will look like this:
$ lvextend -L +100M /dev/vg01/lv01
As you can see, we had 100MB earlier, and now, after following this step, we successfully increased by 100MB to a total of 200MB. Now, as earlier, you can lvs
command to check the volume of given physical storage:
Method 2: Increase to Given Amount
Unlike method 1, volume is increased to a given amount rather than extended. For example, if your volume is 100 MB and you increase it to 330 MB. The final size will be 330 MB.
So, instead of adding a given amount to the current volume, it replaces it with a given amount.
The syntax for the command is,
$ lvextend -L <amount> <directory>
In our case, the command will be:
$ lvextend -L 330M /dev/vg01/lv01
We have successfully upgraded to 330 MB. To confirm this, run the lvs
command just like above, and the output will look like this.
Method 3: Extend by given Percentage
This method provides the specific percentage by which the current logical volume will be increased. It adds to the current space. The existing size of our logical volume is 332 MB. Let’s increase it by 5%.
Syntax is:
$ lvextend -l +<percentage>VG <directory>
For our example:
$ lvextend -l +5%VG /dev/vg01/lv01
You will see a message saying successfully resized. Let’s verify it using the lvs
command.
That’s it! We extended our size by 5% i.e., to 360 MB.
Method 4: Extend by Remaining Free Space
Until this point, we have only extended fractions of the total space. In this method, we will expand based on the free space available.
Syntax:
$ lvextend -l +<percentage>FREE <path>
For this example, we will extend by 50% of the free space like this:
$ lvextend -l +50%FREE /dev/vg01/lv01
Let’s verify and find out the remaining free space with the lvs
command.
As you can see, our new logical volume is 436 MB. We have successfully used the lvextend command and its different methods to extend logical volume.
Using the resize2fs command to resize the file system
Earlier, we used the lvextend to extend the logical volume successfully. But that logical volume is useless because our file system has not recognized or allocated it.
To use that logical volume, we have to increase our file system. We will use the resize2fs
command to extend the file system.
Syntax:
$ resize2fs <path>
This simple command will increase the file system by 100% of the logical partition.
In our case, the command will be:
$ resize2fs /dev/vg01/lv01
Now verify it by running the df -h
command, and the output will be like this depending on your system:
In this tutorial, we learned to use the lvextend command. Using this command, we successfully increased our logical volume and used the resize2fs command to make use of that logical volume.
If this guide helped you, please share it.