LVM (Logical Volume Manager) is a software that allows you to manage multiple hard disk space by creating logical volumes. Using LVM you can create, grow and shrink partitions as per your needs. LVM is made up of three components: Physical Volume, Volume Group and Logical Volume. Each volume group is made up of physical volumes. You can extend or reduce a volume group by adding or removing physical volumes.
Benefits of LVM
- If you have multiple hard drives, through LVM you can group all the hard drives into one large drive.
- You can create and resize new partitions as needed.
- You can add an extra hard disk to a current volume group to increase space.
Note: This tutorial assumes you are using Ubuntu and have three hard disks (/dev/sda, /dev/sdb and /dev/sdc).
Create Partition on /dev/sdb and /dev/sdc
First you need two unpartitioned hard disks “/dev/sdb” and “/dev/sdc.” You need to create physical volume on this hard disk and need to create a partition on each hard disk for this.
To create a partition on “/dev/sdb”, run:
Answer all the questions shown below.
Repeat the same process for /dev/sdc.
To re-read the partition table without rebooting, run the following commands:
Create the Physical Volumes
A physical hard disk is divided into one or more physical volumes. Here we will create a single physical volume on each hard disk.
To create the physical volume on “/dev/sdb1” and “/dev/sdc1”, run
It will output something like this.
Create the Volume Group
A combination of one or more physical volumes is called a Volume Group. You can use the vgcreate command to create a volume group from one or more physical volumes.
To create the volume group “VG1” on a physical volume (/dev/sdb1 and /dev/sdc1), run the following command:
The output is something like this.
To verify the volume group, run
and its output.
Now we have a single 19.52 GB size volume group (VG1).
Creating the Logical volume
The volume groups can be divided into logical volumes and assigned mount points. When the size of logical volumes are full, you can add required free space from the volume group.
Now create the logical volume (LV1 and LV2) of 5 GB size.
Change “VG1” to the volume group name. The -L flag specifies the size of the partition, while the -n flag specifies the name of the logical volume.
To verify the logical volume, run
Finally, we have successfully created an LVM partition of 5GB.
Format and Mount the LVM Partition (LV1, LV2)
To use the logical volumes, we need to format them first. We can format the LVM partition using the ext4 file-system with the following command:
Create a directory in “/mnt” for a mounting partition.
Mount the LVM partition by editing the “/etc/fstab” file.
Add the following line:
For the changes to take effect without rebooting the system, run
To verify the mount:
Resize Logical Volumes
One of the benefits of LVM is to extend or reduce the size of the partitions. We can resize the logical volumes on the fly without rebooting the server. We can easily extend/reduce the logical volumes using lvextend/resize2fs commands.
Extend Logical Volume
In some situations, we need to expand the size of a low space partition. We can easily expand any partition using the lvextend command. We can extend logical volumes only if the volume group has enough free space.
To extend the logical volume LV1 from 5GB to 6GB, run
The logical volume is 6 GB, but the file system on that volume is still only 5 GB.
To make the file system use the entire 6 GB available, run
Reduce Logical Volume
Reducing logical volume is a more interesting part than any other part in logical volume management. Before reducing logical volume, it is good to back up the data.
You need to perform the following steps to reduce logical volumes.
First, to unmount the logical volume LV2, run
Then check for the file-system error using the following command:
Next, reduce the logical volume LV2 from 5GB to 4GB by running
Mount the file-system back to the mount point.
Check the new size of logical volume by running
Delete Logical Volume
To delete a logical volume, you need to unmount then volume before you can delete it.
To delete a logical volume LV1, run
Conclusion
I have tried to explain each topic briefly. You can set up a virtual lab environment and test what you can do with LVM. If you have any questions, feel free to comment below.
Our latest tutorials delivered straight to your inbox