Disk Management: LVM

What is LVM

Logical volume manager (LVM) introduces an extra layer between the physical disks and the file system allowing file systems to be :
– Resized and moved easily and online without requiring a system-wide outage.
– Using discontinuous space on disk
– Meaningful names to volumes, rather than the usual cryptic device names.
– Span multiple physical disks

Physical Volume (PV)
Physical Volume can be a disk partition or disk.

Volume Group (VG)
A Volume Group gathers together a collection of Logical Volumes and Physical Volumes.

Logical Volume (LV)
Logical volumes are block devices which are created from the physical extents present in the same volume group

File system(FS)
File systems are built on top of logical volumes.



Lets create new LVM Volumes.

Before starting checking LVM packages installed.

# rpm -qa | grep lvm
# rpm -qi lvm2-2.02.180-8.el7.x86_64

Scan new disks add to system using the below command  

# echo "- - -" > /sys/class/scsi_host/host0/scan
# echo "- - -" > /sys/class/scsi_host/host1/scan
# echo "- - -" > /sys/class/scsi_host/host2/scan

Step:1 Create Physical Volumes:
# pvcreate /dev/sdb

Display physical volumes
# pvdisplay /dev/sdb

Step:2 Create a Volume Group
# vgcreate vg01 /dev/sdb

Displaying the VG information
# vgdisplay vg01

Step:3 Create Logical Volume
# lvcreate -L +1.5G -n lv_ext4 vg01
# lvcreate -L +1.5G -n lv_xfs vg01

Displaying the LV information
# lvdisplay  
# vgdisplay -v vg01

Step:4 Creating File system
Below command is for ext4 file system. 
# mkfs.ext4 /dev/vg01/lv_ext4 

Below command is for xfs file system. 
# mkfs.xfs /dev/vg01/lv_xfs

Step:5  Create a folder to mount the directory
# mkdir /lvm_ext4
# mkdir /lvm_xfs

Step:6 Mount the LVM volume on data01 directory
# mount /dev/vg01/lv_ext4 /lvm_ext4
# mount /dev/vg01/lv_xfs /lvm_xfs

Step:7 Update fstab, so that from next boot it will mount automatically.  
# vi /etc/fstab
/dev/vg01/lv_ext4 /lvm_ext4 ext4 defaults 0 0  ### For EXT4 file system.
/dev/vg01/lv_xfs /lvm_xfs xfs defaults 0 0   ### For XFS file system.

Check disk is mounted.
# df -h /lvm_ext4 ; df -h /lvm_xfs

Let now extend the LVM Partition:

The below command is to created file of 1G file in /lvm_ext4 & /lvm_xfs directory.

# dd if=/dev/zero of=/lvm_ext4/1g.bin bs=1G count=1
# dd if=/dev/zero of=/lvm_xfs/1g.bin bs=1G count=1

Step:1 Type ‘ df -h’ command to list the file system to view total ,used and available disk space
#  df -PTh /lvm_ext4 ; df -PTh /lvm_xfs

Step:2  Now check whether free space is available space in the volume group
#  vgdisplay vg01

Step:3a Use lvextend command to increase the size.
# lvextend -L +1G /dev/vg01/lv_ext4
# lvextend -L +1G /dev/vg01/lv_xfs

Note: Above command will extend the file system size by 1GB. You can also specify the size in MB , just replace G with M.

Step:4a Run the resize2fs command for ext4 partition.
# resize2fs /dev/vg01/lv_ext4

Step:4b Run the xfs_growfs command for xfs partition.
# xfs_growfs /dev/vg01/lv_xfs

Step:5 Use df command and verify partition .
#  df -PTh /lvm_ext4 ; df -PTh /lvm_xfs

Step:3b: To extend the file system without using resize2fs or xfs_growfs command. 
# lvextend -L +1G /dev/vg01/lv_ext4 -rn
# lvextend -L +1G /dev/vg01/lv_xfs -rn
 
Step:3c To extend the file system with complete available free space in VG.
Note: Execute the below command after lv reduce.
# lvextend -l +100%FREE /dev/vg01/lv_xfs -rn  

Note: If there is not free space available in Volume Group(VG). Kindly add the new disk to VM and extend the Volume Group.

Add the disk into LVM using pvcreate. 
# pvcreate /dev/sdc

Extend the partition.
# vgextend vg01 /dev/sdc 

Let now reduce the partition by 500M
(This can be done only on EXT4 file system)

Note: 
* Reducing/Shrinking the logical volume is the highest risk of data corruption.
* It is always recommended to make a backup before shrinking an LVM.

Step:1 Unmount the file system.
# umount /lvm_ext4/

Step:2 Then check for the file-system error
# e2fsck -f /dev/vg01/lv_ext4

Step:3 Shrink the file system.
The below command will reduce the “lv_ext4” file system from 2.5GB to 1.5GB.
# resize2fs /dev/vg01/lv_ext4 1536M

Step:4 Reduce the Logical Volume (LVM)
# lvreduce -L 1536M /dev/vg01/lv_ext4

Step:5 Optional: Check the file system for any Errors
# resize2fs /dev/vg01/lv_ext4

Step:6 Mount the file system and check the reduced size
# mount /dev/vg01/lv_ext4 /lvm_ext4
#  df -PTh /lvm_ext4 ; df -PTh /lvm_xfs

Let now remove the LVM partition which we created.

Step:1 Firstly, delete the entry of the mount point from the /etc/fstab file:
# vi /etc/fstab
/dev/vg01/lv_ext4 /lvm_ext4 ext4 defaults 0 0  ### For EXT4 file system.
/dev/vg01/lv_xfs /lvm_xfs xfs defaults 0 0   ### For XFS file system.

Step:2 Unmount the mount point using the umount command:
# umount /lvm_ext4
# umount /lvm_xfs

Step:3 Disable the “lv_ext4 & lv_xfs” logical volume:
# lvchange -an /dev/vg01/lv_ext4
# lvchange -an /dev/vg01/lv_xfs

Step:4 Finally, delete the “lv_ext4 & lv_xfs”  logical volume:
# lvremove /dev/vg01/lv_ext4
# lvremove /dev/vg01/lv_xfs

Step:5 Disable the “vg01” volume group as shown below:
# vgchange -an vg01

Step:6 Delete the “vg01” volume group:
# vgremove vg01

Step:7 Remove physical volumes from LVM:
# pvremove /dev/sdb





1 comment:

  1. Hello sir I am looking job as Linux administrator on basis of 4 year experience ..I have done rhsca and rhce ...should I go for VERITAS , PACEMAKER OR SAN NAS OR AWS TO GET GOOD GOOD LEARNING FOR FUTURE POINT OF PERSPECTIVE and TO GET A JOB 🙏🙏🙏 THANK YOU

    ReplyDelete