Site icon System Zone

LVM Management in CentOS/Red Hat 7 Linux

LVM (Logical Volume Manager) is the recommended partition method for CentOS/Red Hat 7 Linux. Specially, if we work on Mail Server, File Server, FTP Server, Web Proxy Server or any application where disk space changes frequently, the LVM partition is mandatory in this case. In my previous article, I discussed how to install CentOS 7.5 with LVM partitioning. If you are new here or CentOS 7 is not yet installed, I will suggest you to follow my previous article and install CentOS 7 with LVM partitioning and then continue this article. In this article, I will discuss how to manage LVM partitioning in CentOS/ Red Hat 7 Linux with the command line utility.

Basic Components and Architecture of LVM

Before going to start LVM management with command line tool, we should have basic knowledge on the basic LVM terms and architecture. The following three terms are the basic components of the LVM partitioning.


In LVM technology, one or more disks or disk partitions are initialized as physical volumes (PV). These physical volumes are then combined into a volume group (VG) which works as a pool of disk space. From volume groups, one or more logical volumes (LV) according to the system requirements can be created. This process is analogous to the way in which disks are divided into partitions. A logical volume is used by the file systems and applications by mounting to a mount point. The following image is showing an overview of the LVM architecture.
LVM Basic Architecture

Linux LVM Management with CMD

According to the LVM architecture, the first task is to manage Physical Volume (PV). If CentOS/Red Hat 7 Linux is installed with LVM partition, there will have at least a physical volume initialized. To view the available physical volume in CentOS/Red Hat 7 Linux, issue the following command from you command prompt.

[root@localhost ~]# pvdisplay

 

— Physical volume —

PV Name               /dev/sda2

VG Name               centos

PV Size               19.51 GiB / not usable 3.00 MiB

Allocatable           yes (but full)

PE Size               4.00 MiB

Total PE              4994

Free PE               0

Allocated PE          4994

PV UUID               1zXhSz-m6PA-GITi-CbNU-zJDl-fAfj-GiWHcI

From the above output we can see that there is a physical volume (PV) named /dev/sda2 and the PV is a partition of the first disk drive. We can also see that there is a volume group (VG) named centos on this physical volume.

Now we want to add another Hard Disk Drive to increase our volume group. The new HDD will be the second Disk Drive and the device location will be /dev/sdb. To find your newly added drive location, issue the following command.

[root@localhost ~]# fdisk -l

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000d6751

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     1026047      512000   83  Linux

/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-root: 19.9 GB, 19943915520 bytes, 38952960 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 1002 MB, 1002438656 bytes, 1957888 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

From the above output we can see that a raw hard disk drive is added and its location is /dev/sdb. We will now create a physical volume with this raw hard disk drive.

Creating Physical Volume (PV)

The pvcreate command is used to create a PV. To create a PV with the above raw disk drivc, issue the pvcreate command according to the following format.

[root@localhost ~]# pvcreate /dev/sdb

 

Physical volume “/dev/sdb” successfully created.

A new PV has now been created. With the pvdisplay or pvs command, you can see the status of the new PV.

[root@localhost ~]# pvdisplay /dev/sdb

 

— NEW Physical volume —

PV Name               /dev/sdb

VG Name

PV Size               5.00 GiB

Allocatable           NO

PE Size               0

Total PE              0

Free PE               0

Allocated PE          0

PV UUID               g3l7kl-db10-T0GA-tgGI-PHb0-0UQD-Vhsx2s

From the above output we can see that the new PV has no Volume Group (VG). We can now create a new volume group or can extend the previous volume group with this PV.

Creating Volume Group (VG)

The vgcreate command is used to create a new VG. So, if you wish to create a new volume group, issue the vgcreate command according to the following format.

[root@localhost ~]# vgcreate vg2 /dev/sdb

 

Volume group “vg2” successfully created

The vgcreate command uses first option as volume group name and other option as PV name. One or more PV name can be supplied but the name will be space separated.

Extending Volume Group

As we have an existing volume group, we do not want to create another one but want to extend it. Issue the following command to view the volume group summery.

[root@localhost ~]# vgs

 

VG     #PV #LV #SN Attr   VSize  VFree

centos   2   3   0 wz–n- 19.50g  0

Our existing VG name is centos and its size is 19.5 GB. Now we want to extend this VG as we have a new Physical Volume.  Issue the following command to extend the current VG.

[root@localhost ~]# vgextend centos /dev/sdb

 

Volume group “centos” successfully extended

Our current VG(centos) has been extended. With the vgs command, you can ensure your extended VG size.

[root@localhost ~]# vgs

 

VG     #PV #LV #SN Attr   VSize  VFree

centos   2   3   0 wz–n- 24.50g <5.00g

Creating Logical Volume (LV)

After creating or extending volume group, it is time to create Logical Volumes (LVs). We will now create a Logical Volume that will be used to keep backup data. So, issue the following command to create a new LV named backup_volume.

[root@localhost ~]# lvcreate -n backup_volume -L 2GB centos

 

Logical volume “backup_volume” created.

A new logical volume has been created with the lvcreate command where n option represent LV name and L option represent the size of the new LV. With the lvdisplay or lvs command, you can view your new LV information.

[root@localhost ~]# lvdisplay

 

— Logical volume —

LV Path                /dev/centos/backup_volume

LV Name                backup_volume

VG Name                centos

LV UUID                2orZcM-1PW0-m56v-v7k1-8MD3-XAgB-wsLbsc

LV Write Access        read/write

LV Creation host, time localhost.localdomain, 2019-03-13 12:03:36 +0600

LV Status              available

# open                 0

LV Size                2.00 GiB

Current LE             512

Segments               1

Allocation             inherit

Read ahead sectors     auto

– currently set to     8192

Block device           253:2

After creating a logical volume, we have to format this volume with a file system and then mount the volume to a mount point. Issue the following command to format the new LV with the xfs file system.

[root@localhost ~]# mkfs.xfs /dev/centos/backup_volume

Now create a mount point directory named backup and mount the LV to this mount point with the following commands.

[root@localhost ~]# mkdir /backup

 

[root@localhost ~]# mount /dev/centos/backup_volume /backup

[root@localhost ~]# df –HT

Filesystem                       Type      Size  Used Avail Use% Mounted on

/dev/mapper/centos-root          xfs        20G  4.2G   16G  21% /

devtmpfs                         devtmpfs  494M     0  494M   0% /dev

tmpfs                            tmpfs     511M     0  511M   0% /dev/shm

tmpfs                            tmpfs     511M  8.8M  503M   2% /run

tmpfs                            tmpfs     511M     0  511M   0% /sys/fs/cgroup

/dev/sda1                        xfs       521M  157M  364M  31% /boot

tmpfs                            tmpfs     103M   13k  103M   1% /run/user/42

tmpfs                            tmpfs     103M     0  103M   0% /run/user/0

/dev/mapper/centos-backup_volume xfs       2.2G   34M  2.2G   2% /backup

The new volume is now mounted to the backup directory and we are now ready to keep data into it. But this is temporary mounting that means if the system is rebooted, the mount point will be removed. To make it permanent, put an fstab entry and issue the mount command to mount all file system in fstab file.

[root@localhost ~]# vim /etc/fstab

 

/dev/mapper/centos-root /                       xfs     defaults        0 0

UUID=5ab285dc-013e-401d-99a9-c37569d2a499 /boot                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

/dev/mapper/centos-backup_volume        /backup   xfs   defaults        0 0

[root@localhost ~]# mount -a

Now your mount point is permanent and you will get this mount point although your operating system gets rebooted.

Extending Logical Volume (LV)

The beauty of the LVM partitioning is that you can extend your Logical Volume without unmounting the mounting point. For example, we want to extend our backup_volume from 2 GB to 4 GB. Before extending a LV make sure that your VG has enough free space.  To view VG summery, issue the vgs command.

[root@localhost ~]# vgs

 

VG     #PV #LV #SN Attr   VSize  VFree

centos   2   3   0 wz–n- 24.50g <3.00g

As we have 3GB free space, we are able to extend our backup_volume to 4GB. Issue the following command to extend the backup_volume.

[root@localhost ~]# lvextend -L +2GB /dev/centos/backup_volume

 

Size of logical volume centos/backup_volume changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).

Logical volume centos/backup_volume successfully resized.

The backup_volume has been extended. With the lvs command, you can ensure your new LV size.

[root@localhost ~]# lvs

 

LV            VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

backup_volume centos -wi-ao—-   4.00g

root          centos -wi-ao—-  18.57g

swap          centos -wi-ao—- 956.00m

Although LV has been extended, the file system (xfs) has not been updated yet. With the following df command, you will find that the backup size is yet the previous size (2GB).

[root@localhost ~]# df -HT | grep backup

 

/dev/mapper/centos-backup_volume xfs       2.2G   34M  2.2G   2% /backup

To update the file system, issue the following command from your command prompt.

[root@localhost ~]# xfs_growfs /backup

Now you will find that your file system has been updated and the backup size is extended to 4GB.

[root@localhost ~]# df -HT | grep backup

 

/dev/mapper/centos-backup_volume xfs       4.3G   34M  4.3G   1% /backup

Note: According to the Red Hat 7 documentation, the xfs file system cannot be reduced or shrink although Logical Volume can be shrink with the lvreduce command. To reduce the xfs file system (if require), we have to apply some strategies. In the next article, I will discuss how to shrink or reduce the xfs file system if required.

Removing Logical Volume (LV)

Sometimes you may need to remove logical volume. Before removing a LV, you must remove the fstab entry that you put before and unmount the mounting point.

For example, we will now remove our backup_volume logical volume. So, first make ensure that there is no entry in the fastab file for backup_volume and then issue the following command to unmount the backup mount point.

[root@localhost ~]# umount /backup

The backup_volume is now unmounted. Issue the following command to remove the logical volume.

[root@localhost ~]# lvremove /dev/centos/backup_volume

 

Do you really want to remove active logical volume centos/backup_volume? [y/n]: y

Logical volume “backup_volume” successfully removed

Now you will find that the Logical Volume has been removed and the VG has got free space.

[root@localhost ~]# lvs

 

LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

root centos -wi-ao—-  18.57g

swap centos -wi-ao—- 956.00m

[root@localhost ~]# vgs

VG     #PV #LV #SN Attr   VSize  VFree

centos   2   2   0 wz–n- 24.50g <5.00g

Removing Volume Group (VG)

If your root directory and swap are not in on a Volume Group (VG), you may remove that volume group. The vgremove command is used to remove a volume group. So, issue the vgremove command according to the following format.

[root@localhost ~]# vgremove  volume_group_name

If you have logical volume on the volume group and yet you have not removed them, you will be prompted to confirm for LV removal. If you don’t want confirmation, just use  -ff option to remove all LVs forcefully .

Removing Physical Volume (PV)

If you don’t have LV and VG on a Physical Volume (PV), you will be able to remove a PV. To check whether a PV contains LV or not, issue the following command.

[root@localhost ~]# pvdisplay -m /dev/sdb

 

— Physical volume —

PV Name               /dev/sdb

VG Name               centos

PV Size               5.00 GiB / not usable 4.00 MiB

Allocatable           yes

PE Size               4.00 MiB

Total PE              1279

Free PE               767

Allocated PE          512

PV UUID               S0YmJ3-Us5J-uDg3-rLDY-km0B-icQd-xuH8H7

 

— Physical Segments —

Physical extent 0 to 511:

    Logical volume      /dev/centos/backup_volume

Logical extents     0 to 511

Physical extent 512 to 1278:

FREE

From the above output we can see that a LV is present on the PV. So, first remove the LV with the lvremove command.

[root@localhost ~]# lvremove /dev/centos/backup_volume

 

Do you really want to remove active logical volume centos/backup_volume? [y/n]: y

Logical volume “backup_volume” successfully removed

After removing LV, we need to remove PV from the VG. The vgreduce command is used to remove a PV from VG. So, issue the vgremove command to remove the PV(/dev/sdb) from the VG(centos)

[root@localhost ~]# vgreduce centos /dev/sdb

 

Removed “/dev/sdb” from volume group “centos”

As there is no LV and VG on the PV, we are now able to remove the PV. The pvremove command is used to remove a PV from a VG. The pvremove command wipes the label on a device so that LVM will no longer recognise it as a PV. To remove the PV named /dev/sdb, issue the following command.

[root@localhost ~]# pvremove /dev/sdb

 

Labels on physical volume “/dev/sdb” successfully wiped.

Note: If the PV is used by any VG, it will ask to either use the vgreduce command to free the PV or use the –ff option to remove PV forcefully. If you use the pvremove command with –ff option to remove PV forcibly, issue the following command to reduce VG otherwise VG will show wrong information about the free space.

[root@localhost ~]# vgreduce –removemissing VG

If you face any confusion to follow the above steps properly, watch the following video about Linux LVM Management. I hope it will reduce your any confusion.

How to manage LVM in CentOS/Red Hat 7 Linux has been discussed in this article. I hope you now be able to manage your LVM partitions following the above steps properly. However, if you face any confusion to manage your LVM partition, feel free to discuss in comment or contact with me from Contact Page. I will try my best to stay with you.

Exit mobile version