Data Recovery from LVM Volumes in Linux
Tags: linux, datascience, beginners, computerscience
Published: 2021-07-01 06:46:44

Would you like to know how to use the LVM technology in Linux? How to create an LVM volume, how to configure and mount it in your operating system, how to add and remove disks, and how to recover the information you have accidentally deleted? In today’s article, you will find all of that – and even more.

![Data Recovery from LVM Volumes in Linux](https://hetmanrecovery.com/pic/blog/a249/lvm-partition-in-linux-os-creation-customization-management-and-recovery-of-lost-data.png)

##What is LVM?
LVM (Logical Volume Manager) is a standard disk management feature available in every version of Linux. 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, Linux looks for available drives first, and then checks what volumes are available on these devices.

With the help of LVM, disks or volumes can be united into a single space. 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 necessary volume groups or physical disks.

YouTube: {% youtube Z6Sj-WO649c %} 

**In fact, LVM has several considerable advantages to offer:**

* It can create a logical volume based on any number of physical disks which will be represented in the operating system as a single disk space.
* The number of disks included and the size of such volume can be modified without interrupting your work.
* Also, the LVM technology lets you do many things on the fly, like copying certain volumes, or configuring the mirroring feature the way it works in RAID 1 systems.

##How to add LVM to the operating system?
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

![How to add LVM to the operating system](https://hetmanrecovery.com/pic/blog/a249/01-linux.png)

After that, type the administrator’s password, and click Yes to confirm your intention.

![administrator’s password](https://hetmanrecovery.com/pic/blog/a249/02-linux.png)

Some Linux versions have this feature installed by default.

This can be checked easily, by typing the command lvm in the terminal window.

![lvm in the terminal window](https://hetmanrecovery.com/pic/blog/a249/03-linux.png)

Now you can move on to dealing with disks.

##How to format a physical disk
Here we are, with three empty hard disks 320 GB each, which are unformatted and without any partitions.

![How to format a physical disk](https://hetmanrecovery.com/pic/blog/a249/04-linux.png)

The easiest way to format a disk is by using a disk management utility. Click on a disk – open advanced settings – then format it.

![Click on a disk – open advanced settings](https://hetmanrecovery.com/pic/blog/a249/05-linux.png)

Specify the name and file system – Next– and click Format again to confirm your action.

![Specify the name and file system](https://hetmanrecovery.com/pic/blog/a249/06-linux.png)

Now you’ll need the password to your administrator account.

![password to your administrator account](https://hetmanrecovery.com/pic/blog/a249/07-linux.png)

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 command fdisk.

Sign in with a superuser account -** sudo –i** – type the password.

![sudo –i](https://hetmanrecovery.com/pic/blog/a249/08-linux.png)

Now run the following command: **fdisk /dev/sdb** – where sdb is the unformatted disk.

![fdisk /dev/sdb](https://hetmanrecovery.com/pic/blog/a249/09-linux.png)

Before you continue, make sure you have selected the right disk, because this operation erases all data from there.

Type n – new volume, p – primary, 1 – first partition, then press Enter twice.

![new volume](https://hetmanrecovery.com/pic/blog/a249/099-linux.png)

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.

![ready to be used with LVM](https://hetmanrecovery.com/pic/blog/a249/10-linux.png)

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. Similarly, format all the disks which are not formatted yet.

![Check the volume properties](https://hetmanrecovery.com/pic/blog/a249/11-linux.png)

Now create an LVM volume on the partition you have just created, by entering the command: **pvcreate /dev/sdb1**.

![pvcreate /dev/sdb1](https://hetmanrecovery.com/pic/blog/a249/12-linux.png)

Repeat the command for every disk: **pvcreate /dev/sdс1, pvcreate /dev/sdd1**.

![pvcreate /dev/sdс1](https://hetmanrecovery.com/pic/blog/a249/13-linux.png)

##Create a volume group
Combine the three disks you have just prepared into an LVM volume. Here’s the command to use:

```
vgcreate vg1 /dev/sdb1 dev/sdc1 dev/sdd1
```

where vg1 is the name of the new 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.

![vgcreate vg1 /dev/sdb1 dev/sdc1 dev/sdd1](https://hetmanrecovery.com/pic/blog/a249/14-linux.png)

##Create a logical volume to use LVM
When the disks are combined into the group, you need to create a new logical volume, so that you can use this group. For this purpose, type the command:

```
lvcreate -L 10G -n lv1 vg1
```

Where the -L stands for the size of the logical volume – it is 10 GB in this case, and the -n assigns a name to the volume, and the vg1 indicates from which volume the space should be taken.

![lvcreate -L 10G -n lv1 vg1](https://hetmanrecovery.com/pic/blog/a249/15-linux.png)

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?
The last step you still have to take is to format the volume in the disk management utility and mount it. Click on it to open advanced settings and then format it; specify the name and file system – Next– and click Format again.

![How to format and mount a logical volume](https://hetmanrecovery.com/pic/blog/a249/16-linux.png)

Type the root (superuser) account password, and click here to start mounting. Now you can use the new volume.

![Type the root superuser account password](https://hetmanrecovery.com/pic/blog/a249/17-linux.png)

If you need to format it with the terminal, type the following command:

```
mkfs -t ext4 /dev/vg1/lv1
```

Specify the file system as Ext4, the group name, and the volume name.

![Specify the file system as Ext4](https://hetmanrecovery.com/pic/blog/a249/18-linux.png)

For mounting, type the following: 
```
mkdir /mnt/lv1
```
and then 
```
mount -t ext4 /dev/vg1/lv1 /mnt/lv1
```

![For mounting, type the following](https://hetmanrecovery.com/pic/blog/a249/19-linux.png)

The volume is mounted.

##How to change the volume size?
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 – this command shrinks or extends physical and logical volumes, but can’t handle volume groups;
* Extend – use it to increase logical volumes or volume groups;
* Reduce – it can make logical volumes or volume groups smaller.

![three main tools to make physical volumes](https://hetmanrecovery.com/pic/blog/a249/20-linux.png)

##How to add a new hard disk to a volume group
The first step when you want to add a hard disk to a group is to format it. Scroll up to find a detailed explanation of this step.

After that, run the following command: 
```
vgextend vg1 /dev/sde1
```

![vgextend vg1 /dev/sde1](https://hetmanrecovery.com/pic/blog/a249/21-linux.png)

where vg1 is the group name, and sde1 is the new (formatted) disk.

##How to display detailed information about LVM
To display detailed information on the composition of an LVM group, run the command **pvdisplay**.

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.

This command will help you to remove a volume group, but before that, you need to unmount the logical volume, so use another command first: 
```
umount /mnt/lv1
```
![umount /mnt/lv1](https://hetmanrecovery.com/pic/blog/a249/21-linux.png)

To proceed with removal, type this command: 
```
lvremove /dev/vg1/lv1
```
and type Yes to confirm this operation.

![lvremove /dev/vg1/lv1](https://hetmanrecovery.com/pic/blog/a249/22-linux.png)

When the removal operation is successful, you’ll see this message.

Now the group can be removed, with this command: 
```
sudo vgremove vg1
```

![sudo vgremove vg1](https://hetmanrecovery.com/pic/blog/a249/23-linux.png)

Now it is time to remove all the disks that used to constitute the group; this command will do it: 
```
sudo pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1
```

![sudo pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1](https://hetmanrecovery.com/pic/blog/a249/24-linux.png)

All the formatting of the physical disk is removed, and now the disk is back to its original condition.