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 vgcreate, lvcreate and lvextend
Preparation: add 3 more physical hard disks sdb, sdc, sdd Capacity 10G
2. Create Physical Volume
Run the following command to create a physical volume (PV) on /dev/sdb, /dev/sdc, and /dev/sdd
1 2 | [root@localhost ~]# pvcreate /dev/sdb /dev/sdc /dev/sdd |
List the newly created physical volumes (PVs), run as follows:
1 2 | [root@localhost ~]# pvs |
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:
1 2 | [root@localhost ~]# pvdisplay /dev/sdb |
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:
1 2 | [root@localhost ~]# vgcreate vg0 /dev/sdb /dev/sdc |
View information about the newly created volume group:
1 2 | [root@localhost ~]# vgdisplay vg0 |
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:
1 2 | [root@localhost ~]# vgs |
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:
1 2 3 4 | [root@localhost ~]# lvcreate -n projects -L 10G vg0 Logical volume "projects" created. [root@localhost ~]# lvcreate -n backups -l 100%FREE vg0 |
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:
1 2 | [root@localhost ~]# lvs |
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:
1 2 | [root@localhost ~]# lvdisplay vg0/projects |
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:
1 2 | [root@localhost ~]# mkfs.ext4 /dev/vg0/projects |
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:
1 2 3 | [root@localhost ~]# mkdir /projects [root@localhost ~]# mkdir /backups |
Run the following command to mount:
1 2 3 | [root@localhost ~]# mount /dev/vg0/projects /projects/ [root@localhost ~]# mount /dev/vg0/backups /backups/ |
Check filesystem disk space usage:
1 2 | [root@localhost ~]# df -TH |
Use the following command to add /dev/sdd to the volume group vg0:
1 2 | [root@localhost ~]# vgextend vg0 /dev/sdd |
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
1 2 | [root@localhost ~]# vgdisplay vg0 |
After running the command vgextend vg0 /dev/sdd
1 2 | [root@localhost ~]# vgdisplay vg0 |
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:
1 2 | [root@localhost ~]# lvextend -l +2000 /dev/vg0/projects |
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.
1 2 3 | [root@localhost ~]# resize2fs /dev/vg0/projects [root@localhost ~]# df -TH |
Epilogue
So, through this introduction, you understand what LVM in Linux is. Good luck.!
Refer: Tìm hiểu LVM trong Linux