What is LVM in Linux | How to configure Logical Volume Manager in Linux

Logical Volume Manager

1. What is LVM and why do we go for LVM?

LVM means Logical Volume Management. The combination of 2 or more physical disks in order to make a big logical disk is called Logical Volume. If a normal Linux partition is full and an application requires some more disk space, then the normal partition cannot be extended for that application's requirement. For this first we have to take a backup of that normal partition, delete that partition, and again create that partition with more disk space, format and mount that partition, and finally restore the application from the backup. This process requires downtime. So, to overcome this problem LVM concept is coming into the picture. Using this LVM we can extend or reduce the file systems as per requirement without loss of any data.

2. What are the components of the LVM?

  • Physical Volume (PV)
  • Physical Extent (PE)
  • Volume Group (VG)
  • Logical Volume (LV)
  • Logical Extent (LE)
Physical Volume (PV) :

It is the standard partition that we add to the LVM. Normally a physical volume is a standard primary or logical partition with the partition code as 8e.

Physical Extent (PE) :

It is a chunk of disk space. Every physical volume is divided into a number of equal-sized PEs.

Volume Group (VG) :

It is composed of a group of physical volumes and logical volumes. It is the organizational group of LVM.

Logical Volume (LV) :

It is composed of a group of LEs. We can format (make a file system) and mount any file system on the logical volume. The size of these logical volumes can easily be increased or decreased as per the requirement.

Logical Extent (LE) :

It is also a chunk of disk space. Every logical extent is mapped to a specific physical extent.

3. How to create the LVM, make a file system and mount that permanently?

(i) Take two physical disks for example / dev/sdb and /dev/sdc. if there is no second disk then make the required partitions using # fdisk command and change the partition code as 8e.
(ii) Convert the Physical disk into physical volumes by,

# pvcreate /dev/sdb /dev/sdc 

(iii) Then create the volume group by combining physical volumes by,

# vgcreate <volume group name><physical volume names> or
# vgcreate -s <PE size in MBs><volume group name><physical volume names>

(iv) Then create the logical volume on the above created volume group by,

# lvcreate -L +<size in MBs> -n <logical volume name><Volume group name> or
# lvcreate -l <no. of PEs> -n <logical volume name><volume group name>

(v) Make a file system on the above created logical volume by,

# mkfs.ext2/ext3/ext4/xfs /dev/<volume group name>/<logical volume name>

(vi) Create a mount point to mount the above-created LVM file system by,

# mkdir /mnt/<directory name> 

(vii) Mount the LVM on the above-created mount point temporarily by,

# mount /dev/<volume group name>/<logical volume name><mount point>or Mount the LVM on the mount point permanently by,
# vim /etc/fstab
/dev/<VG name>/<LV name> /mnt/<directory> <file system type> defaults   0 0 
Esc+:+wq!
# mount -a 
# df -hT (to see the mounted partitions with file system types)

4. How to see the details of the Physical Volumes?

# pvs (displays all physical volumes with fewer details)

# pvdisplay (displays all physical volumes with more details)
 
# pvdisplay <physical volume name> (displays the details of the specified physical volume)
 
# pvscan (to scan all the physical volumes)
 
# pvscan <PV name> (to scan the specified physical volume)

5. How to see the details of the Volume Groups?

# vgs (displays all volume groups with fewer details)
 # vgdisplay (displays all volume groups with more details)
 # vgdisplay <VG name> (displays the specified volume group with more details)
 # vgscan (to scan all the volume groups)
 # vgscan <VG name> (to scan the specified volume group)
 

6. How to see the details of the Logical Volumes?

 # lvs (displays all logical volumes with less details)
 # lvdisplay (displays all logical volumes with more details)
 # lvdisplay <LV name> (displays the specified logical volume details)
 # lvscan (to scan all the logical volumes)
 # lvscan <LV name> (to scan the specified logical volume)
 

7. How to extend the Volume Group?

• Extending the volume group is actually adding a new physical volume to the volume group. • To extend the volume group we need to create a new partition using the

# fdisk command  and make sure that its partition id should be 8e, save the changes and update the partition table by 
 # partprobe
 • Create a physical volume on the newly created partition using the 
 # pvcreate command. 
 • Add the partition to the volume group using the  
 # vgextend command 
 

Example :

# fdisk /dev/sdb 
  Command (m for help) : n 
  First cylinder : press Enter for default one 
  Last cylinder : +500M (create 500MB partition) 
  Command (m for help) : t (to change the partition id) 
  Select the partition : type the partition number 
  Specify the Hexa code : 8e
  Command (m for help) : w (to save the changes)
	
# partprobe /dev/sdb1 
# pvcreate /dev/sdb1 
# vgextend <VG name> /dev/sdb1
# vgdisplay <VG name>     (to check the size of the volume group) 
 

8. How to extend the logical volume and update its file system?

• Sometimes the file system size may be full, so we need to increase the size of the logical volume to continue adding the data in it.
• The size of the logical volume can be increased online, no downtime required.
• Check the current size of the logical volume by # lvdisplay <LV name>and the size of the file system by # df -hT command.
• Increase the size of the logical volume by # lvextend or # lvresize commands.
• Then finally update the file system by # resize2fs or # xfs_growfs commands.


Example : # df -hT

# df -hT
# lvextend -L +<size in MB></dev/vgname/lvname> or
# lvresize -L +<size in MB></dev/vgname/lvname>
# resize2fs </dev/vgname/lvname>
# lvdisplay (to check the size of the logical volume) 
# df -hT (to check the size of the file system)

9. How to reduce the logical volume and update the file system?

  • Reducing the size of the logical volume is a complicated task and we have to remember some points before reducing the logical volume, otherwise, the file system may be damaged.
  • Logical volume size cannot be reduced online and it requires downtime because we have to unmount the file system by the # umount <file system mount point> command.
  • Check the consistency of the file system by # e2fsck <device or partition name> command.
  • Reduce the logical volume by # lvreduce -L - <Size of in MB></dev/vgname/lvname> command.
  • Then update the file system by # resize2fs </dev/vgname/lvname>
  • Finally, mount the file system by # mount -a

Example : # umount <file system mount point>

# e2fsck <device or partition name>
# lvreduce -L -<size in MB></dev/vgname/lvname>
# resize2fs </dev/vgname/lvname>
# lvdisplay </dev/vgname/lvname> (to check the size of the logical volume>
# mount -a (to mount the file system)
# df -hT (to check the size of the file system)
 

10. How to move or migrate the logical volume data from one physical volume to another physical volume?

  • There might be a situation where the physical volume might be failing and it is required to replace. In such a case, we need to migrate or move the logical volume data from the failed physical volume new physical volume and isolate (remove) the failed physical volume.
  • First, access the mount point of the failing physical volume and check the data in it.
  • Verify the size of the physical volume by # pvs or # pvdisplay </dev/vgname/lvname> command.
  • Unmount the file system of that physical volume by # umount <file system mount point>
  • Add a new physical volume and the size should be the same size or higher than that failing physical volume.
  • Migrate the physical volume contents to the new physical volume using # pvmove <old PV><new PV>
  • Mount back the logical volume, access the mount point, and verify the data in it.
  • Remove the failed the physical volume by # vgreduce <vgname><pvname> command.

Example : # cd <file system mount point>

  
# ls
# pvs <pvname> or # pvdisplay <pvname>
# umount <file system mount point>
# pvcreate <device or partition name>
# vgextend <vgname><pvname>
# pvmove <old pvname><new pvname>
# mount -a
# vgreduce <vgname><failed pvname>
# cd <file system mount point>
# ls

11. How to delete or remove the logical volume?

• To delete or remove the logical volume, first unmount the file system by # umount <mount point>
• Remove the entry in /etc/fstab file.
• Remove the logical volume by # lvremove </dev/vgname/lvname>command.
• Verify whether the logical volume is removed or not by # lvs or # lvdisplay command.
Example : # umount <file system mount point>

# vim /etc/fstab (delete the entry of the logical volume)
Esc+:+wq!    (save and exit the file) 
# lvremove    </dev/vgname/lvname>
# lvs
# lvdisplay (to verify whether logical volume is removed or not)

12. How to delete or remove the volume group?


• To delete or remove the volume group, first make sure that any logical volume should not be mounted because removing a volume group will delete or remove the logical volumes in that volume group.
• Then delete or remove the volume group by the # vgremove <vgname> command.
• Verify whether the volume group is removed or not by # vgs or # vgdisplay command.
Example:

# umount file system mount point  (to unmount the file system if there is any LV)
# vim  /etc/fstab  (delete the entry of the logical volume) 
Esc+:+wq!  (save and exit the file)
# vgremove <vgname>
# vgs or 
# vgdisplay (to verify whether the volume group is removed or not)

13. How to delete or remove the physical volume?

• Deleting or removing a physical volume is very simple and the only thing we should check that the physical volume we are going to delete should not belong to any volume group ie., we can only delete or remove the physical volume which is free.
• Then delete or remove the physical volume by the # pvremove <pvname>command.
• Verify whether the physical volume is removed or not by the # pvs or # pvdisplay command.
Example : # pvremove <pvname>

  
# pvs  or  
# pvdisplay (to verify whether the physical volume is removed or not)
  

14. How to restore the volume group which is removed mistakenly?

• First unmount the file system by the # umount <file system mount point> command.
• Check the volume group backup list by # vgcfgrestore --list <volume group name> command.
• Then remove the logical volume by # lvremove </dev/vgname/lvname> command.
• Copy the backup file which is taken a backup before removing the volume group from the above backup list and paste it in this command # vgcfgrestore -f <paste the above-copied file name <vgname>
• The logical volume is created automatically after restoring the volume group but the volume group and logical volumes both will be in the inactive state. So, check the state of the volume group by # vgscanand the logical volume state by # lvscan commands.
• Then activate that volume group by # vgchange -ay <volume group name> command and activate the logical volume by # lvchange -ay <logical volume name> command.
• Mount the logical volume file system by the # mount -a command.
Example : # umount <file system mount point>

# vgcfgrestore --list   <volume group name (copy the backup file from the list) 
# lvremove </dev/vgname/lvname>
# vgcfgrestore  -f <paste the above copied file> <volume group name>
# vgscan  (to check the status of the volume group)  inactive state)
# lvscan  (to check the status of the logical volume)
# vgchange -ay <volume group name> (activate the volume group if it is in inactive state)
# lvchange -ay <logical volume name> (activate the logical volume if it is in inactive state)
  
Note: Option a means active VG or LV and option y means yes.
  
# mount -a

15. How to change the volume group name and other parameters?

  
# vgrename  <existing volume group name><new volume group name> (to rename the volume group)

 By default, unlimited logical volumes can be created per volume group. But we can control this limit by
  
# vgchange -l <no.><volume group> (to limit max. no. of logical volumes to the specified number) 
# vgchange  -l 2 <vgname>    (to limit max. 2 logical volumes that can be created in this volume group) 
# vgchange   -p <no.><volume group>    (to limit max. no. of physical volumes to the specified number) 
# vgchange  -p 2 <vgname>    (to limit max. 2 physical volumes that can be added to this volume group) 
# vgchange -s <block size in no.><volume group> (to change the block size of the volume group)
# vgchange   -s  4 <vgname>    (to change the volume group block size to 4MB) 

16. How to change the logical volume name and other parameters?

  
# lvrename   <existing lvname><new lvname>    (to rename the logical volume) # lvchange  -pr  <logical volume>   (to put the logical volume into read-only mode) # lvs    (to see the logical volume permissions) 
# lvchange -prw <logical volume>  (to put the logical volume into reading and write mode)

17. How to disable the volume group and logical volume?

# vgchange    -an   <volume group>     (to disable the volume group) 
# lvchange    -an   <logical volume>     (to disable the logical volume) 

18. How to take a backup of the volume group?

# vgcfgbackup     (to take a backup of all volume groups) 
# vgcfgbackup <volume group>  (to take a backup of the specified volume group)

19. What is the configuration file of the logical volume?

# cat /etc/lvm/lvm.conf     (to see the contents of the LVM configuration file) 

20. What are the locations of the logical volume and volume groups?

# cd /etc/lvm/backup  (the logical volumes backup location) 
# cd /etc/lvm/archive (the volume groups backup location)

21. How to know the current version of the LVM package?

# rpm -qa  lvm*  (to know the current version of the LVM package)

22. What are the attributes of the volume group?

# vgs     (to see the attributes of the volume group) 
[ The attributes are  w ---->  writable   z ----> extendable    n ----> normal ] 
# vgs   -v  (to check the UUID of the volume group) 

23. How to extend the logical volume to max. disk space and half of the disk space?

# lvextend    -l  +100% FREE  <logical volume>     (to extend the logical volume by adding the volume group's total available space) 
# lvextend -l 50% <vgname>  (to extend the logical volume by adding the 50% free space of the volume group)>vgname>

24. How to check on which physical volume the data is written in the logical volume?

# lvdisplay -m  (to check on which physical volume the data is currently writing from all logical volumes)
# lvdisplay -m <lvname>  (to check on which physical volume the data is writing from the specified logical volume)

25. How many types of file systems are available?

  • ext2 ---->Second extended file system (default in RHEL - 3 & 4)
  • ext3 ---->Third extended file system (default in RHEL - 5)
  • ext4 ----> Fourth extended file system (default in RHEL - 6)
  • xfs ----> Extended file system (default in RHEL - 7)
  • ufs ----> Unix file system (default in Solaris)
  • jfs ----> Journal file system (default in IBM-AIX)
  • hfs ----> High performance file system (default in HP-UX)
  • vxfs ----> Veritas file system
  • procfs ----> Process file system (temporary)
  • tempfs ----> Temporary file system (temporary)
  • cdfs ----> Compact disk file system
  • hdfs ----> DVD file system
  • iso9660 ----> To read the CD/DVD.iso image format files in Linux

26. How to scan and detect the luns over the network?

# ls /sys/class/fc_host  (to check the available fibre channels)
# echo "---" > /sys/class/scsi_host/<lun no.>/scan  (to scan and detect the luns over the network)

27. How to mount a pen drive in Linux?

# lsusb or # fdisk -l  (to know the pen drive name)
# mkdir /mnt/pendrive  (to create a mount point for the pen drive)
# mount <pen drive name><mount point> ( to mount the pen drive on the above-created mount point)
# cd /mnt/pendrive  (to access the pen drive)

28. How to mount CD/DVD ROM drives in Linux?

The CD/DVD ROM device name in Linux is /dev/cdrom

# mkdir /mnt/mycdrom  (to create the mount point for CD/DVD)
# mount /dev/cdrom /mnt/mycdrom  (to mount the CD/DVD on the above-created mount point)
# cd /mnt/mycdrom  (to access the CD/DVD ROM drives)

29. How to mount the " .iso " image files in Linux?

# mount -t iso9660 /root/rhel6.iso /iso -o ro, loop  (to mount the .iso image files)
# cdrecord /root/Desktop/rhel6.iso  (to write the CD/DVD ROM. Before executing this command put the empty CD/DVD into the CD/DVD drive)
# eject  (to eject the CD/DVD drive tray)
# eject -t  (to insert and close the CD/DVD drive tray)

36. How will you troubleshoot if one of the eight disks failed in LVM?

First, umount the file system and add the new disk with the same size as the failed disk to the volume group. Then move the data from failed physical volume to the newly added physical volume and then remove the failed physical volume from the volume group. And finally, mount the file system.

37. What is pvmove and when it is used in LVM?

The pvmove command is used to move the data from failed physical volume to newly added physical volume. This command is used when one of the physical volume is failed in the LVM.

38. How to inform the client and then troubleshoot if the disk is full?

First check which files are accessing more disk space by # du -h |sort - r command. if any temporary and junk files are present remove them from the disk to make a room for new or updated data. Then inform the actual situation to the client, take the permission from the client to get the lun from storage and extend the file system by adding that lun to the LVM.

39. Did you work on storage?

Actually, I did not work on storage but I know the procedure of how to export the lun from storage to the client using iSCSI target. Then scan that lun at the client side and add the lun to the LVM. I also know the storage hardware from Emc square, Netapp, and others. And I am dreaming to work on storage, cloud, and virtualization.

42. How many types of disk space issues can we normally get?


(i) Disk is full.
(ii) Disk is failing or failed.
(iii) File system corrupted or crashed.
(iv) O/S is not recognizing the remote luns when scanning, ...etc.,

Jay

I love keeping up with the latest tech trends and emerging technologies like Linux, Azure, AWS, GCP, and other cutting-edge systems. With experience working with various technology tools and platforms, I enjoy sharing my knowledge through writing. I have a talent for simplifying complex technical concepts to make my articles accessible to all readers. Always looking for fresh ideas, I enjoy the challenge of presenting technical information in engaging ways. My ultimate aim is to help readers stay informed and empowered on their tech journeys.

Post a Comment

Previous Post Next Post

Contact Form