How to lvm in linux

Create LVM Logical Volumes in Linux

In this tutorial, we are going to learn about how to create LVM logical volumes in Linux. LVM is a standard feature in most modern Linux distributions that allows you to create and manage logical volumes. In this tutorial, we will show you how to create and manage LVM logical volumes in Linux, using command-line tools. We will cover the basic concepts of LVM, including physical volumes, volume groups, and logical volumes, and demonstrate how to create filesystem on logical volumes in Linux.

Table of Contents

How to Create LVM Logical Volumes in Linux

Before we dive deeper, let us have a look at LVM architecture.

LVM Architecture Overview

The structure of an LVM disk environment is simply illustrated in the figure below.

Create LVM Logical Volumes in Linux

LVM Logical Volumes Components

The underlying structure of an LVM environment is a block device such as partition or a whole disk which is initialized a Physical Volume (PV) . A single or multiple block devices can be grouped together to form a PV.

The physical volumes can be combined into a Volume Group (VG) . This creates a pool of disk space out of which LVM logical volumes (LVs) can be allocated. A top logical volumes, regular file systems, such as ext4, xfs, etc. can then be created. This process is comparable to the how disks are divided into partitions.

  • Physical Volume
  • Volume Group
  • Logical Volume

As depicted in the architecture above.

How to Create LVM Logical Volumes in Linux

In order to create LVM logical volumes, here is a basic four step procedure:

In order to learn how to create LVM logical volumes in Linux, we will be following the above outlined steps.

Create Disk Partitions to be Used

So before you can create any logical volume, attach extra drives yo your system. In our case, we have two uninitialized disks, /dev/sdb1 and /dev/sdc1 and we will be using both of them to create LVM logical volumes.

Читайте также:  Настройка общих папок samba linux

Initialize Partitions as Physical Volumes

First, let us initialize our disks as physical volume using the command, pvcreate.

Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created.

To confirm this, you can run the command, pvs, to display information about physical volumes.

 PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a--  

As you can see, our two physical volumes have no volume group assigned to them yet.

Create Volume Group

Now, create a volume group that consists of the physical volumes created above. To create a volume group, run the command below;

vgcreate test_vol_group /dev/sdb1 /dev/sdc1
Volume group "test_vol_group" successfully created

where test_vol_group is the name of our volume group and /dev/sdb1, /dev/sdc1 are our physical volumes created above.

You can use the vgs command to display the attributes of the new volume group.

VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- 

Create Logical Volume

Now that we have the volume group, let us create a logical volume, test_log_volume, that will occupy 1G of the volume group space.

lvcreate -L 1G -n test_log_volume test_vol_group
Logical volume "test_log_group" created.

where –L specifies the size and –n specifies the name of the logical volume. test_vol_group is our volume group created above.
You can use the lvs command to display the attributes of the new volume group.

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- 

Create Logical Volume Filesystem

So we now have a 1G logical volume. Let us make it usable by creating a file system on it. The following command creates a xfs file system on the logical volume. We will use mkfs.xfs command in its simplest form.

mkfs.xfs /dev/test_vol_group/test_log_group

Next,create a mount point and mount the logical volume.

mount /dev/test_vol_group/test_log_group /tmp/mnt/

Let us check the logical volume filesystem and disk space usage.

Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 8.0G 1.1G 7.0G 14% / . /dev/mapper/test_vol_group-test_log_group xfs 1014M 33M 982M 4% /tmp/mnt

Upto there, we have created a usable LVM logical volume.
Stay tunned.

Other Tutorials

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Источник

How to Create LVM Partition in Linux.

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 # fdisk -l Image description

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

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

Image description

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

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

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

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

Image description

4)Create filesystem for the logical volumes.

Lets create a filesystem, so the logical volume will be ready to use.

Before mounting , we need to format it using mkfs command

Image description

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

Image description

Note
To mount it permanently use that UUID number and paste it inside etc/fstab path

Image description

Here as shown below a lvm of 20GB successfully mounted on a folder called vol-1

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

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

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

Image description

Check the file system for any Errors using e2fsck -f commands

Image description

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

Mount the file system and check the reduced size

Image description

Here our lvm size reduce to 12G.

Источник

Оцените статью
Adblock
detector