Hello everybody!
Watch this video to find out how to work with LVM - a logical volume manager - in a Linux
operating system.
How to install it, create a logical volume, mount it, and how to recover any data you
have accidentally deleted from such a volume.
If
you
need to create one or several additional logical volumes to store your data, or combine several
drives into one volume, and do it all without having to stop applications, unmount or repartition
a hard disk, this utility is exactly what you’re looking for.
LVM - which stands for Logical Volume Management - is a system of managing volumes, which is
available for every Linux distributive and more flexible than the traditional means of
management.
What is LVM?
The volume manager provides a new level of interaction between your operating system
and disks or volumes which this system is using.
In a conventional disk management pattern, the operating system looks for available drives
first, and then checks what volumes are available on these devices.
With LVM, however, drives and volumes can be treated in such a way that a single drive
can contain several disks and volumes.
If the disk space is treated like that, the operating system will see no difference between
them and the volume manager will show the operating system only the volume groups, disks
and partitions that you have configured.
This is a very powerful system of managing volumes containing data.
It allows to create logical volumes spanned across physical partitions, and such volumes
will be shown in the operating system as conventional devices containing data (that is, as traditional
partitions).
The main benefits of this manager are that, first of all, you can create a group of logical
volumes over any number of physical volumes, and secondly, the logical volumes can be resized
easily and on the fly.
In addition, LVM supports snapshots, copying volumes on the fly, and mirroring options
similar to those in RAID 1.
How to install LVM If you are going to use LVM options for the
first time, you have to install it.
Here is the command you need for installation: sudo apt install lvm2 - and then hit “Yes”
to confirm.
In some versions of Linux, the logical volume manager is included by default.
You can check it by running the lvm command in the terminal window.
When the manager is installed, you can move on to dealing with disks.
How to partition a physical disk Here we are, with three empty hard disks 300
GB each, which are unformatted and without any partitions.
The easiest way to format a disk is by using a disk management utility.
Click on a disk - open advanced settings - then Format Partition - specify the name and file
system - Next - and click Format again to confirm your action.
Now you’ll need the password to your administrator account.
Now that this disk is formatted, repeat the operation for every other disk which is not.
Also, a disk can be formatted with the terminal, using the fdisk command.
(Sign in as superuser with a root account) - – sudo -i – enter the password.
Now run the following command: fdisk /dev/sdb – where sdb stands for the
unformatted disk.
Before you continue, make sure you have selected the right disk, because this operation erases
all data from the disk.
Type n to create a new volume, press p to set it as primary, 1 – the first partition,
then hit Enter twice.
Now let’s get the volume ready to be used with LVM.
Type t – to change the volume type, and then 8e – to assign LVM type.
Check the volume properties by pressing p - you can see everything is OK, a new formatted
volume with the name sdb1 appeared; press w to record the changes.
In the same way, format all the disks which are not formatted yet.
Now create a physical LVM volume on the partition you have just created, enter the following
command: pvcreate /dev/sdb1, and repeat it for each
of the disks: pvcreate /dev/sdс1, pvcreate /dev/sdd1
How to create a volume group Now let’s build a volume group of three
disks.
All you need is a single command: vgcreate vg1 /dev/sdb1 dev/sdc1 dev/sdd1
Where vg1 is the name of a new volume group.
Actually, you can use any name you prefer, but it’s recommended to put the vg letter
combination before the name so that you know it is a volume group.
How to create a logical volume to be used by LVM
After you’ve combined disks into a group, you can create logical volumes to be used
by LVM.
Here is the command to create a logical volume: lvcreate -L 10G -n lv1 vg1
Where the -L stands for the size of the logical volume - it is 10 GB in this case, the -n
assigns a name to the volume, and the vg1 indicates from which volume the space should
be taken.
If there is any data on the disk, the operating system will warn you about it: type “yes”
to confirm your decision to erase the information.
How to format and mount a logical volume All that we still have to do is to format
the volume in the Disk Management utility and mount it.
Now you can see a new partition appear in the Disk Management window.
Click on it to open advanced settings and then Format Partition; specify the name and
file system - Next - and click “Format” again.
Type the root account password, and click here to start mounting.
That’s all - the volume is ready to use.
If you need to format it with the terminal, type the following command:
mkfs -t ext4 /dev/vg1/lv1, file system Ext4, name of the group and volume name.
To mount it, type mkdir /mnt/lv1, - and then
mount -t ext4 /dev/vg1/lv1 /mnt/lv1, - and the volume is mounted.
How to resize a logical volume One of the advantages offered by logical volumes
is the opportunity to add a new hard disk on the fly and expand the volume group.
And on the contrary, if there is a hard disk in the volume group which is not in use, you
can remove it.
There are three main tools to make physical volumes, volume groups and logical volumes
larger or smaller.
• resize - it can shrink or expand physical volumes and logical volumes, but it doesn’t
work with volume groups; • extend - it makes volume groups and logical
volumes larger; and • reduce - it makes volume groups and logical
volumes smaller.
Add a new hard disk to a volume group To add a new hard disk to a volume group,
you should format it first, and I have described this procedure a minute before.
After that, you add a disk to a volume group with this command:
vgextend vg1 /dev/sde1 Where vg1 is the group name, and sde1 is a
new disk you have just formatted.
How to view LVM information To see detailed information on the disks included
into LVM, run the pvdisplay command.
It displays the path to a physical disk, the name of the virtual group where it belongs,
the information on the free and used disk space, disk ID and many other things.
How to remove a logical volume To remove a logical volume, make sure that
i’s disconnected (unmounted), then run the lvremove command, and the volume will be removed.
You can remove a volume group as well; for this purpose, unmount the logical volume first
umount /mnt/lv1 After that, remove it with the command lvremove
lvremove /dev/vg1/lv1, and hit “Yes” to confirm your decision.
When the removal operation is successful, you’ll see this message.
Logical volume lv1 successfully removed.
After that, you can remove the volume group with the command vgremove
sudo vgremove vg1 - that’s all, the group is removed successfully.
The last step is to remove all the disks that belong to this group - with the command pvremove.
sudo pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1 All the data on the physical volume is removed
successfully, and all the disks are back to their original state.
How to recover deleted data from an LVM volume If you accidentally removed important information
from a logical volume, you can still restore it with the help of Hetman Partition Recovery.
This utility supports data recovery from partitions created with the logical volume manager, so
you’ll have your data back without a sweat.
At the moment, there is no version of Hetman Partition Recovery for Linux, but it does
support a wide range of file systems used in Windows, MacOS, Linux and Unix; the program
will fix any errors in the logical structure of the hard disk and recover all of its contents.
You can run this tool in Linux with the help of a virtual machine, install Windows as the
second operating system on your computer, or connect the disk to a Windows computer.
As you can see, the program recognized the logical volume without effort and now it displays
all the data on this volume.
Start with running Fast scan - right-click on the disk, Open, Fast scan -
and when the scan is over, the program will display the results in the right side of the
window.
Removed elements are marked with a red cross.
If the “Fast scan” doesn’t find the data you need, then use “Full analysis.”
The next step is to select the files you want to recover, click “Recovery,” select where
you want to save them, and click “Recovery” again.
When the process is over, you will find the recovered files in the folder you have selected.
Conclusion Today we have explored how to create LVM volumes
in Linux, how to configure, resize, and mount an LVM in Linux, and how to remove it.
Now you know how powerful this technology is, and how many advantages it can give, no
matter where you use it - on a server or on a home computer.
That is all for now!
Hopefully, this video was useful.
Remember to click the Like button and subscribe to our channel.
Push the bell button to receive notifications and never miss new videos.
Leave comments to ask questions.
Thank you for watching.
Good luck.