Saturday, May 10, 2014

LVM in Linux - (Logical Volume Management)

Logical Volume Management is widely used technique for deploying logical storage rather than physical.

First check what device blocks do you have with fdisk utility 

In my VM I had /dev/sdb, /dev/sdc, and /dev/sdd empty new blocks to use

You can create partitions with fdisk and make sure to take extra step before -w (write) command to convert the partition to LVM (8e) type. Before writing changes select (t) option and change partition to LVM.

Example disks output of fdisk -l:

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1044     8385898+  8e  Linux LVM

Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1044     8385898+  8e  Linux LVM

Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        1044     8385898+  8e  Linux LVM


1) Once, all the partitions are done we start creating physical  volumes.

# pvcreate /dev/sdb1
# pvcreate /dev/sdc1
# pvcreate /dev/sdd1

Check your newly created physical volumes with

# pvs
or
#pvdisplay

2) We now will create a volume group that will consist of physical volumes.

# vgcreate volume_group_new /dev/sdb1 /dev/sdc1 /dev/sdd1

Check your volume group with

#vgs
#vgdisplay

You can extend or remove physical volumes from the volume group

# vgextend volume_group_new /dev/sdb2

# vgreduce volume_group_new /dev/sdb2

To remove volume group itself

# vgremove Volume Group Name

3) After you have created volume group now you can create LVM blocks and allocate size to them

# lvcreate -L 10G -n logical_volume_new volume_group_new

Now you will have a 10 GB LVM block in the /dev/mapper/volume_group_new/logical_volume_new

# lvs
# lvdisplay

NOTE: You will need to format the LVM block after you have created

# mkfs.ext4 /dev/mapper/volume_group_new/logical_volume_new

Next step is create a directory to mount and mount the lvm

# mkdir /lvmextra
# mount /dev/mapper/volume_group_new/logical_volume_new /lvmextra

Please, make sure you enter the entries to the /etc/fstab file to make it persistent to reboot

# blkid - this will give you UUID of all the blocks so you can enter its UUID to /etc/fstab file

4) Here is some helpful and most important commands of LVM

To extend a space on lv disk (cool part of LVM)
# lvextend -L +1G /dev/mapper/volume_group_new/logical_volume_new
# resize2fs /dev/mapper/volume_group_new/logical_volume_new

To reduce the space on lvm (which you might not use it often)
Please, be sure you might loose some data here and do this if you know what you are doing.

First unmount the device
# umount /lvmextra
# fsck -f /dev/mapper/volume_group_new/logical_volume_new

Then do the opposite of the extending procedure. Let us say you want the total disk size be 7GB.
# resize2fs /dev/mapper/volume_group_new/logical_volume_new 7G
# lvresize -L 7G /dev/mapper/volume_group_new/logical_volume_new

Now, mount your device or just run the # lvs command to check the new resized disk space.

*** There is GUI form of LVM if you are interested to learn. Above material might have typos or mistakes please read about LVM at www.redhat.com documentation or learn some before using the commands.

No comments:

Post a Comment