Linux lvm - Logical Volume Manager

Create Partitions

For this Linux lvm example you need an unpartitioned hard disk /dev/sdb. First you need to create physical volumes. To do this you need partitions or a whole disk. It is possible to run pvcreate command on /dev/sdb, or use partitions to create physical volumes. Don't forget to setup the partition type to 8e

[root@host]# fdisk /dev/sda

Create physical volumes
Use the pvcreate command to create physical volumes.

[root@host]# pvcreate /dev/sdb1
[root@host]# pvcreate /dev/sdb2 

The pvdisplay command displays all physical volumes on your system.

[root@host]# pvdisplay 

Alternatively the following command should be used:

[root@host]# pvdisplay /dev/sdb1

Create Virtual Group

At this stage you need to create a virtual group which will serve as a container for your physical volumes. To create a virtual group with the name "mynew_vg" which will include /dev/sdb1 partition, you can issue the following command:

[root@host]# vgcreate mynew_vg /dev/sdb1

To include both partitions at once you can use this command:

[root@host]# vgcreate mynew_vg /dev/sdb1 /dev/sdb2 

Feel free to add new physical volumes to a virtual group by using the vgextend command.

[root@host]# vgextend mynew_vg /dev/sdb2 

Create Logical Volumes

From your big cake (virtual group) you can cut pieces (logical volumes) which will be treated as a partitions for your linux system.

[root@host]# lvcreate -L 1000 -n vol02 mynew_vg

In this case you have created a logical volume with a size of 1GB and the name of vol02

Create File system on logical volumes

The logical volume is almost ready to use. All you need to do is to create a filesystem.:

[root@host]# mkfs.ext3 -m 0 /dev/mynew_vg/vol01

the -m option specifies the percentage reserved for the super-user, set this to 0 if you wish not to waste any space, the default is 5%.

Edit /etc/fstab

Add an entry for your newly created logical volume into /etc/fstab

/dev/mynew_vg/vol01 /home/foobar ext3 defaults 0 2 

Mount logical volumes

You mount the logical volume in the same manner you mount any other block device

[root@host]# mkdir /home/foobar 
[root@host]# mount /dev/mynew_vg/vol01 home/foobar

Extend logical volume

The biggest advantage of logical volume manager is that you can extend your logical volumes any time you are running out of free space. To increase the size of a logical volume by another 800 MB you can run this command:

[root@host]# lvextend -L +800 /dev/mynew_vg/vol01

The command above does not actually increase the size of the file system on that logical volume. To do that you need to run:

[root@host]# resize2fs /dev/mynew_vg/vol01 

Remove logical volume

The command lvremove can be used to remove logical volumes. Make sure that before you attempt to remove logical volumes your logical volume does not have any valuable data stored on it, moreover, make sure the volume is unmounted.

[root@host]# lvdisplay
[root@host]# lvremove /dev/mynew_vg/vol02 

Here's a picture of typical LVM implementation: