3v-Hosting Blog

What is LVM and how to create LVM on Ubuntu

Administration

7 min read


Logical Volume Management (LVM) is a sophisticated disk management system for Linux that provides a flexible and scalable approach to storage management. It allows system administrators to dynamically allocate, resize and manage disk space on a Linux system without the need for traditional partitioning. In this short overview article, we will try to understand the basic concepts of LVM and provide a detailed step-by-step guide on how to create an LVM installation on an Ubuntu server.

 

 

LVM Basics


The main key components of LVM are:

- Physical volumes (PVs) are individual storage devices, such as hard drives or solid state drives, that serve as the building blocks for an LVM installation. PVs are initialized and added to the LVM configuration to free up storage space.

- Volume groups (VGs) are logical containers that group together one or more physical volumes. Virtual groups provide a layer of abstraction, allowing administrators to manage storage collectively rather than working with individual drives.

- Logical volumes (LVs) are virtual partitions created in volume groups. LVs are what users and applications interact with and are similar to traditional partitions but have the added benefit of dynamically resizing.

 

 

Main advantages of LVM

Dynamic Disk Management - LVM allows dynamic resizing of logical volumes, allowing administrators to adjust storage allocation without the need for downtime or complex partition resizing procedures.

Taking Snapshots - One of the notable features of LVM is the ability to take snapshots. Snapshots are point-in-time copies of a logical volume, useful for backing up, testing, and restoring data.

 

 

Preparing the system and installing LVM directly


Collecting disk information

Before configuring LVM, it is important to determine the available disks and their partitions. Use commands such as lsblk and fdisk to view and verify the current disk configuration.

$lsblk
$ sudo fdisk -l

 

 

Installing LVM tools

 

Installing LVM is extremely simple and takes minimal time. You just need to update the information about available packages, and then use the package manager to install a complete LVM build that does not require any additional modules.

$ sudo apt update
$ sudo apt install lvm2

 

After this, your system is ready for further configuration of LVM.

 

 


Creating an LVM installation

Initializing physical volumes

Start by initializing the selected disks as physical volumes using the pvcreate command. Replace /dev/sdX with the appropriate disk ID.

$ sudo pvcreate /dev/sdX

 

 

Creating a volume group

After initializing the physical volumes, create a volume group to merge them together using the vgcreate command. Replace myvg with the desired volume group name.

$ sudo vgcreate myvg /dev/sdX

 

 

Creating Logical Volumes

Once you have created a volume group, create logical volumes within it. Provide a size and name for each LV.

$ sudo lvcreate -L 20G -n mylv myvg

 

 

Formatting and mounting

Format the logical volume using the file system of your choice and mount it into a directory.

$ sudo mkfs.ext4 /dev/myvg/mylv
$ sudo mkdir /mnt/mylv
$ sudo mount /dev/myvg/mylv /mnt/mylv

 

 

Automation of mounting when server boots

Update the /etc/fstab file so that the logical volume is mounted automatically at boot.

$ echo '/dev/myvg/mylv /mnt/mylv ext4 default 0 0' | sudo tee -a /etc/fstab

 

 

LVM management

 

Resizing Logical Volumes

 

LVM allows you to easily resize logical volumes. To increase the size of the LV, use the lvextend and resize2fs commands.

$ sudo lvextend -L +10G /dev/myvg/mylv
$ sudo resize2fs /dev/myvg/mylv

 

 

Taking Snapshots

LVM snapshots can be useful for creating backups or testing. Use the lvcreate command with the --snapshot option.

$ sudo lvcreate --size 5G --snapshot --name mysnapshot /dev/myvg/mylv

 

 

Removing logical volumes

If you need to remove a logical volume, make sure it is disabled and then use the lvremove command.

$ sudo umount /mnt/mylv
$ sudo lvremove /dev/myvg/mylv

 


Conclusion

Logical Volume Management is a powerful tool for efficient disk management on Ubuntu systems. By understanding the basic concepts and following the step-by-step guide provided in this article, you can confidently create and manage LVM setups. Whether you're a seasoned system administrator or a Linux enthusiast, adding LVM to your toolbox will allow you to dynamically manage storage and adapt to changing requirements. And since in this article we only briefly described the most basic capabilities of this system, you can easily expand your knowledge of LVM for even more productive work by reading the official documentation on LVM.