What LVM Is and How to Set It Up in Ubuntu

Spread the love

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:

sudo fdisk /dev/sdb

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:

sudo partprobe /dev/sdb
sudo partprobe /dev/sdc

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

sudo pvcreate /dev/sdb1 /dev/sdc1

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:

sudo vgcreate VG1 /dev/sdb1 /dev/sdc1

The output is something like this.

To verify the volume group, run

sudo vgdisplay /dev/VG1

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.

sudo lvcreate VG1 -L +5G -n LV1
sudo lvcreate VG1 -L +5G -n LV2

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

sudo lvdisplay

and its output.

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:

sudo mkfs.ext4 /dev/VG1/LV1
sudo mkfs.ext4 /dev/VG1/LV2

Create a directory in “/mnt” for a mounting partition.

sudo mkdir /mnt/LV1-mount  /mnt/LV2-mount

Mount the LVM partition by editing the “/etc/fstab” file.

sudo nano /etc/fstab

Add the following line:

/dev/VG1/LV1 /mnt/LV1-mount ext4 defaults 0 0 
/dev/VG1/LV2 /mnt/LV2-mount ext4 defaults 0 0

For the changes to take effect without rebooting the system, run

sudo mount -a

To verify the mount:

sudo df -h

It will output something like this.

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

sudo lvextend -L+1G /dev/VG1/LV1

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

sudo resize2fs /dev/VG1/LV1

The output is something like this.

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

sudo umount /dev/VG1/LV2

Then check for the file-system error using the following command:

sudo e2fsck -f /dev/VG1/LV2

Next, reduce the logical volume LV2 from 5GB to 4GB by running

sudo resize2fs -p /dev/VG1/LV2 4G
sudo e2fsck -f /dev/VG1/LV2

Mount the file-system back to the mount point.

sudo mount /dev/VG1/LV2 /mnt/LV2-mount

Check the new size of logical volume by running

sudo df -h

The output is something like this.

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

sudo umount /dev/VG1/LV1
sudo lvremove /dev/VG1/LV1

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Hitesh Jethva

Over 5 years of experience as IT system administrator for IT company in India. My skills include a deep knowledge of Rehat/Centos, Ubuntu nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, squied proxy, NFS, FTP, DNS, Samba, ldap, Openvpn, Haproxy, Amazon web services, WHMCS, Openstack Cloud, Postfix Mail Server, Security etc.

Comments are closed