How to Create LVM Partition in Linux.
Published: 2022-07-19 18:18:58

**Logical Volume Manager (LVM) plays an important role in the Linux operating system by improving the availability, disk I/O, performance and capability of disk management.**

**LVM is a widely used technique that is extremely flexible for disk management.**

**This adds an extra layer between the physical disks and the file system, allowing you to create a logical volume instead of a physical disk.**
**LVM allows you to easily resize, extend and decrease the logical volume when you need it.**

**We need to identify the correct disk which are to be used in the LVM using the fdisk command or any other disk management command [lsblk].** 

**For example, Lets say we have three 8GB disk /dev/sdc , /dev/sdd and /dev/sde are available.**

`# lsblk`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzdcy8xnfsyj0ed0wx4e.png)

`# fdisk -l`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhax0mhogrhfi0ha9xog.png)

## How to create a LVM in Linux, these are the below steps to be followed.

1. Create a Physical Volumes(PV) on the disk.
2. Create the Volume Group(VG) on the Physical Volumes
3. Create Logical Volumes(LV) on the Volume Group
4. Create a filesystem for the logical volumes**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/376o8vujoy2x30ldqpt4.jpg)


## **1)How to Create LVM Physical Volumes**

> We can create the physical volumes using pvcreate command as shown below which initialize a disk or partition for use by LVM. Physical volumes (PV) are the partitions on hard disk, or hard disk itself. PV are the base of LVM structure and referred as physical volumes.

```
pvcreate [Physical Volume Name]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9lgry8fvq37ej4gtpa3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pv2qz1e06q9sigkaztn.png)

```
Make a note:

The above command erases any data on the given disks /dev/sde, /dev/sdc and /dev/sdd.
Physical disk can be added directly into the LVM PV instead of the disk partition.
```

Use pvs command to list out your physical volumes

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiaxff3ogc7ros3e2dbs.png)

## **2)How to Create a Volume Group**

> Volume groups are nothing but a pool of storage that consists of one or more physical volumes. Once you create the physical volume, you can create the volume group (VG) from these physical volumes (PV).

> Use the vgcreate command to create a Volumegroup on the physical volumes and vgs to show volume group.
vgcreate [Volume Group Name] [Physical Volume Name]

```
vgcreate vcontainer /dev/sdc /dev/sdd
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rsf9wt9iflvrk89o4c8.png)
here vcontainer is the name of volume group.

Now if you want to extend the volume again, Ist check if you have any physical volume or not..
so here we have a extra physical volume [ dev/sde ]
vgextend [Existing Volume Group Name] [Physical Volume Name]

```
vgextend vcontainer /dev/sde
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbs2xbf1akum0d5g7kal.png)
Use the vgs and vgdisplay commands to display information about the VG you created.

## **3)How to Create Logical Volume Using**

After creating the volumegroup, use “lvcreate” command to create the logical volumes (LV) within the Volume group (VG).

Lets create a Logical volume of 20GB “mylv”(logical vol name) using the command “lvcreate” as shown in the below.

lvcreate –L [Logical Volume Size] –n [Logical Volume Name] [Name of the Volume Group where the LV to be created]

```
lvcreate -L 20GB -n mylv vcontainer
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/298ul62fa4dpoa146w3d.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8jx7e60kxkfsjlcss8n.png)

## **4)Create filesystem for the logical volumes.**

Lets create a filesystem, so the logical volume will be ready to use.
- mkdir /vol-1
Here vol-1 is a directory name where we want to mount our lvm.)

Before mounting , we need to format it using mkfs command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsiwfn0qli25ue4k0suw.png)

Now mount that lvm on the vol-1 directory using mount command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xip8gp2hxxk0fuxqsusk.png)

Note
To mount it permanently use that UUID number and paste it inside etc/fstab path
- vi /etc/fstab
/dev/vg01/lv001 /lvmtest xfs defaults 0 0

Here as shown below a lvm of 20GB successfully mounted on a folder called vol-1
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34ssqy76vwrd4wvcztw3.png)

**How to Extend/Increase LVM’s (Logical Volume Resize)** 

Expanding the logical volume is extremely easy, it takes very few steps and can be done online without unmounting a certain logical volume.

The main purpose of LVM is flexible disk management, which allows you to easily resize, extend and reduce the logical volume size when you need it.

Use the following command to increase the existing logical volume.
lvextend [Additional space to be added] [Existing Logical Volume Name]

```
lvextend -L +2G /dev/mapper/vcontainer-mylv

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlvc3ofcpeotiia49dnw.png)

Now, the logical volume is extended and you need to resize the file system to extend the space inside the logical volume.

```
resize2fs /dev/mapper/vcontainer-mylv
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/91myp3keojhtbk739z0x.png)
as you can see that the volume increased by 2 GB.

**How to Reduce/Shrink LVM’s (Logical Volume Resize)**

Reducing/Shrinking the logical volume is the highest risk of data corruption.

So try to avoid this kind of situation if possible, but go ahead if you have no other options.

It is always recommended to make a backup before shrinking an LVM.

When you are running out of disk space in LVM, you can make some free space on the volume group by reducing the exsisting LVM that no longer uses the full size, instead of adding a new physical disk.

Use the umount command to unmount the file system

- umount -v [file system]

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5j8a5zm9r9hhqy57yfy.png)
Check the file system for any Errors using e2fsck -f commands

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq6f9s31d2vejfexjjol.png)
Shrink the file system.
resize2fs [Existing Logical Volume Name] [New Size of File System]

```
resize2fs /dev/mapper/vcontainer-mylv 10G

```
Reduce the Logical Volume (LVM)
lvreduce [New Size of LVM] [Existing Logical Volume Name]

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnt45w13j1n67wb0zfok.png)

Mount the file system and check the reduced size

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klxip4aqbshx3zu6w6yp.png)
Here our lvm size reduce to 12G.