LVM What it is, how it is installed and managed (Ubuntu)

 

We are going to see what LVM ( Logical Volume Manager ) is, how to install it on an Ubuntu Server or any other Debian-based distribution.

What is LVM?
LVM is an implementation of a logical volume manager for the Linux kernel , it is used to manage storage space. It was originally written in 1998 by Heinz Mauelshagen , which was based on the Veritas volume manager used on HP-UX systems.

LVM establishes a logical layer between the file system and the data storage partitions used. This allows you to create a file system that spans multiple partitions and/or disks. In this way, for example, the storage space of several partitions or data carriers can be combined.

Advantages of using LVM
1.- We can create, expand or reduce the logical volumes ( LV ), on the fly, without the need to format the storage media.
2.- We can also resize the logical groups ( VG ).
2.- We will have the data distributed by several supports, increasing the performance of the system.
3.- The data can be reorganized while we are working .
4.- Data replication is simple .
5.- It is possible to carry outSnapshots .
6.- We have the option of RAID0 of logical volumes ( LV ).

To implement another type, such as RAID1 or RAID5 , which are the most used, it is recommended to use specific software and have the logical volumes above RAID .

Let's define the concepts...
Physical Volumes (PV) – The PV represents the lowest level of an LVM . They are physical disks and the partitions (block device) of the hard disk with LVM file system .
Logical Volumes (LV) – This is the equivalent of a partition in a traditional system. The LV is visible as a standard block device (loop), so it can contain a file system (eg /home). They can be used as anormal partition , formatted with any file system , and can be mounted .
Volume Groups (VG) – This is the top of the LVM . It is the container in which we have our logical volumes ( LV ) and our physical volumes ( PV ). It can be seen as an administrative unit in which our resources are included. It should be noted that as long as a PV is not added to the GV , we cannot start using it.
With LVM , classic partitions are initialized as PVs , which are mapped to a VG . Within this volume group , LVs can be created and formatted with any file system .

Why use LVM on a small system?
When we install a GNU/Linux operating system , one of the first decisions is how to partition the disk . Although some install it on a disk, without making partitions , it is advisable to separate them . At least for the directories / (Root), /boot , /swap (swap memory) and /home (user directory), if we work with databases , it would also be convenient to separate /var , which is the directory that will contain. To do this, we will have to decide how much space to allocate to these directories.

With LVM , we can assign the physical hard drive to a logical group and from there create several logical volumes for the different directories. This will allow us to resize them later. If, for example, we run out of space in /home , we can reduce the size of an oversized LV and allocate it to /home . (Not all filesystems support resizing up and down) . We can leave unallocated space and thus we will have room to increase where we need.

Why use LVM in a large system?
Working with many disks is an arduous and sometimes complex task , since we work with media that contain important data . With LVM the work is simplified and done with more security .

By working with user groups , it allows us to assign logical volumes to each one, and manage the allocated space according to needs.

For example, if with the traditional partitions ( MBR and GPT ), we have created 4 contiguous partitions on a disk, and we need to increase some of the first 3, we cannot do it without deleting the following ones, which is a problem and will require stopping the service almost certain.

With LVM this problem is solved, in addition to making the system scalable very easily, being able to add new physical disks .

After adding a physical disk to the system , there is no need to migrate user data . We add this to the logical group ( VG ) and resize the logical volumes ( VL ) by adding space to them.

Let's see it with a practical example.
In a virtual machine with Ubuntu Server OS , we will see:

1.- Installation of Logical Volume Manager ( LVM ).
2.- We create the structure we want.
3.- We add a new hard drive.
4.- We remove a hard drive.

Installing Logical Volume Manager (LVM)
We expanded the machine with 4 2Gb hard drives. We check it with the following command ,

fdisk -l
We install the lvm2 package on our machine ( Ubuntu Server )
sudo apt-get install lvm2
We create partitions on each hd (fdisk /dev/sdb). We will create the partitioning of the hard disk (a single primary partition that will occupy the entire hard disk) of type LVM ( type 8e ).

fdisk /dev/sdb
We create the partitions for LVM
pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
We delete the created partitions
pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
To check that it works we recreate it
pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
We check
pvdisplay


We create the logical volume , on the physical system ( fileserver is the group name)

vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
We check


On the created logical volume , we will make the following partitions :

lvcreate –name system –size 1Gb fileserver
lvcreate –name backup –size 1Gb fileserver
lvcreate –name save –size 1Gb fileserver
These are logical volumes . We check
lvdisplay

lvscan

To enlarge a partition on the logical volume

lvextend -L2.5G /dev/fileserver/save 
or to reduce it...

lvreduce -L1G /dev/fileserver/save
We format each of the logical volumes : system ext3, backup xfs and save reiserfs .
mkfs.ext3 /dev/fileserver/system
mkfs.xfs /dev/fileserver/backup
mkfs.reiserfs /dev/fileserver/save
We create mount point.

What if we wanted to add them to fstab?
This will be absolutely necessary if the installation is for production. If not, the system would not mount it at startup.

To increase the size of the "system" partition by 0.5 Gb . Now that they are mounted, we will need to unmount them first in order to use the "lvextend" command.

umount /dev/fileserver//system
lvextend -L1, 5 /dev/fileserver/system
resize2fs /dev/fileserver/system
The « resize » command extends the format to the enlargement

and we ride it

mount /dev/fileserver/system
We add a new hard drive
Now we add a new hard drive. To do this, we will first create the primary partition of type linux LVM ( 8e ) and add the new physical volume (PV, 1o)

We will therefore extend the size of the VG group "fileserver" (eg, 2o). We add it to the logical volume.
vgextend lvm /dev/sdf1

We check the new size of the VG "fileserver" (eg, 2o)

vgdisplay

We remove a hard drive
Now we are going to remove the hard drive /dev/sdb1 belonging to VG “fileserver” . Since it contains data , we will first copy the data from the sdb1 partition to the newly added /dev/sdf1 partition .

pvmove /dev/sdb1 /dev/sdf1
We delete the partition of the VG «fileserver»
vgreduce fileserver /dev/sdb1
We check the new size of the VG "fileserver "

We delete the membership of the hard drive of the PV

pvremove /dev/sdb1
We check that the PV has decreased in size

You can now remove the sdb hard drive

pvremove /dev/sdb1

No comments

Powered by Blogger.