Sunday, March 30, 2014

Logical Volume Management in linux

LVM is a userspace toolset that provide logical volume management on linux. With this tool we can create logical volumes, expand, delete, take snapshots of existing logical volumes etc.

For LVM to work properly, your kernel should support device-mapper libraries.

To install the latest LVM software, download the package from the link below,

wget https://git.fedorahosted.org/cgit/lvm2.git/snapshot/lvm2-2_02_105.tar.gz

tar -zxvf lvm2-2_02_105.tar.gz
cd lvm2-2_02_105

./configure
make && make install

If you are on ubuntu, you can simply install it by issuing the below command.

apt-get install lvm2

First we can create physical volumes that will be used to create the lvm

pvcreate /dev/sda8 /dev/sda9

We can view the physical volume by issuing the below commands,

pvscan
pvdisplay

Now we can create volume group which contain two or more physical volumes.

vgcreate vol_group /dev/sda8 /dev/sda9

We can view the volume group by issuing the below commands,

vgscan
vgdisplay

Now we can create our first logical volume (lv1) having 500 MB space.

lvcreate -L 500 -n lv1 vol_group

We can extend the size to 600 MB either by,

lvextend -L 600 /dev/vol_group/lv1

or by

lvextend -L+100 /dev/vol_group/lv1

We can view the logical volume details by issuing the below commands,

lvdisplay

Now format the logical volume to any format, in my case it is ext4.

mkfs.ext4 /dev/vol_group/lv1

Mount the logical volume as a file system under /mnt.

mount /dev/vol_group/lv1 /mnt/

After extending the logical volume size, issue the below commands for extending the file system.

e2fsck -f /dev/vol_group/lv1
resize2fs /dev/vol_group/lv1

For XFS filesystem. (Used mainly for Database snapshots)

apt-get install xfsprogs

The logical volume should be in xfs format,

mkfs.xfs /dev/vol_group/lv1

mount /dev/vol_group/lv1 /mnt/

Extend the file system by,

xfs_growfs /mnt

LVM snapshot is the best method to take MySQL backup from running live database servers.

Here MySQL data directory should be there in the logical volume (lv1). Make sure that enough unallocated free space is there in that volume group.

Flush the table datas in to the disk and lock the tables,

mysql -u root
 > FLUSH TABLES WITH READ LOCK;

Now the tables remain unchanged and can take the snapshot of the data directory by issuing the below command,

lvcreate --snapshot --size=500 -n backup /dev/vol_group/lv1

Now unlock the tables..
mysql -u root
 > UNLOCK TABLES;

Mount the snapshot to a folder and backup.

mkdir /mysql
mount -o ro /dev/vol_group/backup /mysql
tar -zcvf mysql.tar.gz /mysql
umount /mysql/

We can now remove the snapshot and give back the unallocated free space to the volume group.

lvremove -f /dev/vol_group/backup

Thanks for reading,

Cheers /-

No comments:

Post a Comment