How to create a basic LVM volume on any Linux distribution?

LVM Linux

LVM (also known as Logical Volume Management), is a logical volume manager that allows Linux users to extend, reduce and modify partitions on hard drives in real time, without the need to unmount the filesystem.

In a simplified way we could say that LVM is an abstraction layer between a storage device and a file system.

In this tutorial we will see how to create a basic LVM configuration on Linux.

To be able to create an LVM volume implies deleting data, so before starting, you must create a system backup or preferably you can perform this exercise on a virtual machine so as not to compromise your data.

This is not a simple warning, as all information on a hard drive will be completely removed, which is why you need to perform this process on a LIVE system or on another hard drive.

Install LVM2

Now that your data is completely safe and that you are aware of the possibility of losing your data (hence the need for data backup).

We can proceed to install LVM in our system, for this we are going to open a terminal and in it we are going to execute the following command according to the distribution we are using.

On Debian, Ubuntu and derivatives:

sudo apt-get install lvm2*

On Arch Linux, Manjaro, Antergos and derivatives:

sudo pacman -S lvm2

For Fedora and its derivatives

sudo dnf install lvm2*

In OpenSUSE

sudo zypper instalar lvm2

Preparing the media

Now having lvm in our system, let's proceed to get root access with:

sudo -s

Once this is done, we must create the volume that we will use with LVM in our system, for this we are going to use the cfdisk tool:

cfdisk /dev/sdX

Here we are going to remove all partitions from the disk (that's why bakcup was warned).

When all the partitions have disappeared, now we are going to select the option of "new" to create a new partition on the disk, use the entire hard disk.

Now we are going to select "primary" and then just hit enter. Then select the option «Type» and here you must find and select the option "Linux LVM"

When they finish this process, they must click on "write" and finally click on exit. Before we can create our LVM logical volumes and format them, we must create an LVM physical volume.

Creating LVM volumes on Linux is done with the pvcreate tool. In the terminal, we are going to execute the pvcreate command together with the partition that we recently created, it should look something like this:

pvcreate /dev/sda1

Now we can confirm the creation of the LVM volume with the command:

lsblk

Create LVM Volume Group

Now that we have an LVM physical volume configuration, the next step is to create an LVM volume group.

To be able to create a new one, just run the vgcreate command together with the partition path, in this case we are going to name it “volume-lvm” but you can replace this in the command:

vgcreate -s 16M volumen-lvm /dev/sda1

Configure LVM logical volumes

Logical volumes are where all the data is stored in an LVM. To create a new logical volume in LVM, we use the following command, for example, to create a logical volume of 80 GB, it would be as follows:

lvcreate -L 80G -n lvm1 volumen-lvm

The basic syntax for creating logical volumes is:

lvcreate -L espacioengigasG -n logicvolumename logicvolumegroup

Finally we must proceed to give a format to the logical volume created, for this we must place ourselves in the following path:

cd /dev/mapper

And here we can execute an ls, to check that our volume is here:

ls

Once confirmed, we proceed to format the volume with:

mkfs.ext4 /dev/mapper/volumen-lvm-lvm1

Once this is done, we can exit root by typing exit, and now we are simply going to proceed to mount the volume with the following commands:

mkdir /mnt/vfs/
sudo mount /dev/mapper/volumen-lvm-lvm1 /mnt/vfs/
cd /mnt/vfs/


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Sera said

    Hello,

    If you are going to install an operating system in LVM, the PV must always be a partition, but if you are going to use an entire disk only for data, it is best to create a PV with the entire disk, this way if you increase the size of the disk (if it is a virtual machine for example) you can increase your PV, VG and LV hot.

    Greetings.