Linux | Cloud | DevOps | Scripting

Breaking

Tuesday 2 September 2014

LVM (Logical Volume Manager)

LOGICAL VOLUME MANAGER (LVM):

LVM is a logical volume manager for the Linux kernel that manages disk drives. In other words, we use LVM partitions to re-size partitions. For example; we have root partition (/) of size 5G. Now, we want to extend its size to 10G. There is no possibility to extend its size without taking any backup. But if we create it as a lvm partition, we can easily extend its size without taking any backup. 

There are now two version of LVM for Linux:

LVM 2 - The latest and greatest version of LVM for Linux.
  • LVM 2 is almost completely backward compatible with volumes created with LVM 1. The exception to this is snapshots (You must remove snapshot volumes before upgrading to LVM 2)
  • LVM 2 uses the device mapper kernel driver. Device mapper support is in the 2.6 kernel tree and there are patches available for current 2.4 kernels.
LVM 1 - The version that is in the 2.4 series kernel,
  • LVM 1 is a mature product that has been considered stable for a couple of years. The kernel driver for LVM 1 is included in the 2.4 series kernels, but this does not mean that your 2.4.x kernel is up to date with the latest version of LVM.
FEATURES OF LVM:
  • Resize volume groups online by absorbing new physical volumes (PV) or ejecting existing ones.
  • Resize logical volumes (LV) online by concatenating extents onto them or truncating extents from them.
  • Create read-only snapshots of logical volumes (LVM1).
  • Create read-write snapshots of logical volumes (LVM2).
  • Create RAID logical volumes (available in newer LVM implementations): RAID 1, RAID 5, RAID 6, etc.
  • Stripe whole or parts of logical volumes across multiple PVs, in a fashion similar to RAID 0.
  • Configure a RAID 1 backend device (a PV) as write-mostly, resulting in reads being avoided to such devices unless necessary.
  • Move online logical volumes between PVs.

========================================================================
GRAPHICAL TOOL FOR LVM:

# system-config-lvm
========================================================================

PRACTICAL:
Create three partitions of 100M. Create a LVM partition of 150M by using these three partitions, where Volume Group name is "vgname" and Logical Volume name is "lvname" and mount it permanent on /mylvm.

Step1: First create partitions, i.g. we are creating three partitions sda5, sda6 and sda7.

# fdisk  -cu  /dev/sda
# p
# n
# +100M
# +100M
# +100M
# t                           //to give system id to these three partitions
# 5
# 8e
# t
# 6 
# 8e
# t
# 7
# 8e
# w
# init  6 [Enter]

Step 2: Create Physical Volume
# pvcreate  /dev/sda{5,6,7}
# pvdisplay     OR     # pvs     OR     # pvscan

Step 3: Create Volume Group
# vgcreate  vgname  /dev/sda{5,6,7}
# vgdisplay  vgname     OR     # vgs     OR     # vgscan

Step 4: Now, Create Logical Volume
# lvcreate  -L  150M  -n  lvname  vgname
# lvs     OR     # lvscan     OR     # lvdisplay  /dev/vgname/lvname

Step 5: Format this Logical Volume
# mkfs.ext4  /dev/vgn/lvm1

Step 6: Create mount point
# mkdir  /mnt/mylvm

Step 7: Mount it permanent by using file /etc/fstab
# vim  /etc/fstab
/dev/vgname/lvname     /mnt/mylvm     ext4     defaults     0  0
        [save and exit]

# mount  /mnt/mylvm
=====================================================================

REDUSE SIZE OF YOUR LVM PARTITION TO 100M:

# umount  /dev/vgname/lvname          //to unmount lvm partition
# e2fsck  -f  /dev/vgname/lvname          //to check filesystem of your lvm  partition
# resize2fs  /dev/vgname/lvname  100M          //to resize ext4 filesystem
# lvreduce  -L  100M  /dev/vgname/lvname          //to reduce size of your lvm partition
confirm  
             y   [Enter]
# mount  -a
# mount
# lvs     OR     # lvscan     OR     # lvdisplay  /dev/vgname/lvname
# df  -hT
=====================================================================

EXTEND YOUR LVM PARTITION:

# lvextend  -L  +50M  /dev/vgname/lvname
# resize2fs  /dev/vgname/lvname
# mount  -a
# df  -hT
# lvs     OR     # lvscan     OR     # lvdisplay  /dev/vgname/lvname
=====================================================================

REMOVE YOUR LVM PARTITION:

# umount  /dev/vgname/lvname
Now, remove entry from fstab  
          (/dev/vgname/lvname     /mnt/mylvm     ext4     defaults     0  0)
# lvremove  /dev/vgname/lvname
confirm  
                y [Enter]
# vgremove  vgname
# pvremove  /dev/sda{6,7,8}

Partition remove
# fdisk  -cu  /dev/sda
# p
# d                                   //now give partition numbers one by one
=====================================================================

LVM BY PHYSICAL EXTENT:

Create logical volume “lvm1” belongs to volume group “vgn” with extend 100. All logical volume in volume group “vgn” must have extend size 8M. Format logical volume “lvm1” as vfat type and mount it /mnt/blah upon reboot.

Make a partition of 900M and set system ID 8e
# init 6

# pvcreate  /dev/sda8
# vgcreate  -s  8M  vgn  /dev/sda8
# lvcreate  -l  100  -n  lvm1  vgn

# lvdisplay  /dev/vgn/lvm1
# mkfs.ext4  /dev/sda8 //if we didn’t format /dev/sda8 then we have to                                                                            use resize2fs command.
=====================================================================

HOW TO CREATE SNAPSHOT OF LVM:

# lvcreate  -L  100M  -s  -n  lvm2  /dev/vgn/lvm1
# mkdir  /media/d2
# mount  /dev/vgn/lvm2  /media/d2
# ll  /media/d2
=====================================================================

Thanks for reading this article...

No comments:

Post a Comment

Pages