Monday, July 16, 2012

Logical Volume Management (LVM)

Logical Volume Management (LVM) is the concept of creating Volume Group by combining one or more physical partitions and then allocating logical volume on top it creates a layer of sepration between actual hard disk and file system. This is helpful while increasing the size of allocated file system space by adding of new hard disks to volume group and then extending file system online (without downtime). If the Physical partitions are directly mapped then it is not possible to increase the space. Only option left is to replace the old hard disk with higher capacity ones.

Physical Volume - New Hard Drive / new Partition available physically is labelled as Primary volume that can be used for LVM.
Volume Group - Combination of one or more Primary Volumes acting as single unit.
Logical Volume - Each Volume Group will divided into logical divisions which contains actual file system. The space allocated for
Logical Volume will be in the unit of chucks. 1 chunk = 4MB.

Steps to create Logical Volume.

#Create a Physical Volume using physical drives /dev/sda{6,7,8}.
pvcreate /dev/sda{6,7,8}

#Displays information about Physical Volumes.
pvdisplay

#Short Display about Physical Volumes.
pvs

#Create a Volume Group. Physical Volumes need to be created before this using pvcreate. Say /dev/sda{6,7,8}.
vgcreate vgo /dev/sda{6,7,8}

#Displays information about Volume groups. 
vgdisplay

#Short Display about Volume groups.
vgs
#Create a Logical Volume on Volume group vgo with size 2G with name lvm1. 
lvcreate -L 2G vgo -n lvm1

#Displays information about Logical Volumes. 
lvdisplay

#Short Display about Logical Volumes.
lvs

#Create file system on logical Volume. Say ext4.
mkfs.ext4 /dev/vgo/lvm1

#Mounting Logical Volume temprorly. For permanent mount put entry in fstab
mount /dev/vgo/lvm1 /mnt

#Verify the size of Logical Volume.
df -h

Steps to increase te size of Logical Volume by adding new Hard disk say /dev/sda9

#Create physical volume.
pvcreate /dev/sda9

#Add the new physical disk to a volume group (vgo).
vgextend vgo /dev/sda9

#Increase the size of a Logical Volume by size 1GB.
lvextend -L +1G /dev/vgo/lvm1

#To resize file system to extend the logical volume.
resize2fs /dev/vgo/lvm1

Steps to decrease te size of Logical Volume and removing a Hard disk

#First umount the logical volume.
umount /dev/vgo/lvm1

#Make system to collect freespace available.
e2fsck -f /dev/vgo/lvm1

#To resize file system from to 1G.
resize2fs /dev/vgo/lvm1 1G

#Decrease the size of a Logical Volume.
lvreduce -L -2G /dev/vgo/lvm1

#Removes a Logical Volume lvm1. (Frees the storage collected by e2fsck)
lvremove /dev/vgo/lvm1

#Remove a Volume Group /dev/vgo. 
vgremove /dev/vgo

#Remove a Physical Volume label created using pvcreate.
pvremove /dev/sda{6,7,8}

Note:

If the volume is under use by some user it wont allow to remove it. In such case use the command fuser. fuser can be used to find users currently using the file, folder, device, etc..
#To find the users using the device.
fuser /dev/vgo/lvm1

#To kill these user sessions using /dev/vgo/lvm1
fuser -k /dev/vgo/lvm1
Enhanced by Zemanta

No comments:

Post a Comment