Learn LVM in Linux

Tram Ho

About LVM

Logical Volume Management (LVM) is used to manage storage devices. LVM is a utility that divides hard disk space into Logical Volumes to make resizing easy.

1. Create and Manage LVM in Linux

Steps to manage and create LVM with commands vgcreatelvcreate and lvextend

Preparation: add 3 more physical hard disks sdbsdcsdd Capacity 10G

2. Create Physical Volume

Run the following command to create a physical volume (PV) on /dev/sdb/dev/sdc, and /dev/sdd

List the newly created physical volumes (PVs), run as follows:

Meaning of fields of pvs:

  • PV: Disk used
  • PFree: Physical Disk Size (PV Size)

To get detailed information about each physical volume (PV), use the following command: pvdisplay

View detailed information about the physical volume(PV) /dev/sdb. We do the following:

Similar to /dev/sdc and /dev/sdd:

Note: If we have 2 drives or more drives to create a volume and one disk in the volume is lost, the volume will be lost forever, so we must run LVM on RAID or use LVM’s RAID feature to be able to do this. fault tolerance.

3.Create Volume Group

To create a volume group with the name vg0 using /dev/sdb and /dev/sdc. We do the following:

View information about the newly created volume group:

Since vg0 contains two 10GB disks, VG Size = 19.99GB

Meaning of Volume group information when running vgdisplay command:

  • VG Name: Name of the Volume Group.
  • Format: The LVM architecture used.
  • VG Access: Volume Group can be read and written and ready to use.
  • VG Status: Volume Group can be resized, we can expand it further if we need more space.
  • PE Size: Physical Expansion, Size for disk can be specified by PE or GB size, 4MB is the default PE size of LVM
  • Total PE: The capacity Volume Group has
  • Alloc PE: Total PE used
  • Free PE: Total unused PE

Check the amount of physical volume (PV) used to create the volume group as follows:

Inside:

  • VG: Volume Group name
  • PV: Physical Volume used in Volume Group
  • VFree: Show available free space in Volume Group
  • VSize: Total size of the Volume Group
  • LV: Logical Volume is in volume group
  • SN: Number of Snapshots of the volume group
  • Attr: The state of the Volume group is writable, readable, mutable,

4. Create Logical Volume

We will create 2 logical volumes with the name projects of 10GB and backups using the entire remaining capacity of the volume group. We run the following command:

Logical volume “backups” created. Inside:

-n: Use specifies the name of the logical volume to be created.

-L: Use only one fixed size.

-l: Uses only a percentage of the remaining space in the volume group.

See the list of logical volumes that have just been created:

Meaning of lvs fields:

  • LV: Logical volume name
  • %Data: Percentage of logical volume used
  • Lsize: Size of logical volume

Display detailed information of logical volumes:

We will use the ext4 file system because it allows us to increase and decrease the size of each logical volume (with the xfs file system only allowing to increase the size). We do the following:

Expand the Volume Group and resize the Logical Volumes

In the example below we will add a physical volume named /dev/sdd with size 10GB to volume group vg0, then we will increase the size of logical volume /projects to 10GB doing the following:

Run the following commands to create the mount archive:

Run the following command to mount:

Check filesystem disk space usage:

Use the following command to add /dev/sdd to the volume group vg0:

Volume group “vg0” successfully extended

We run the vgdisplay vg0 command before and after we execute the vgextend vg0 /dev/sdd command, you will see an increase in the size of the volume group(VG):

Before running the command vgextend vg0 /dev/sdd

After running the command vgextend vg0 /dev/sdd

Through the above test command, we see that our added capacity is 10GB, we can increase the size of the logical volume /projects to 10GB by doing the following:

After running the above command we need to resize the file system, so we have to run the following command to resize:

  • For file systems (ext2, ext3, ext 4): resize2fs.
  • For file systems (xfs): xfs_growfs.

Epilogue

So, through this introduction, you understand what LVM in Linux is. Good luck.!

Refer: Tìm hiểu LVM trong Linux

Share the news now