What is LVM?
Logical Volume Management or LVM allow the server to utilize multiple hard drives and combine them to single storage. It allows an easy way to extend or shrink partition depending on how the user needs.
LVM also supports snapshots and cloning for easy backup of system images. LVM is used to allocating disks, striping, mirroring and resizing logical volumes. This tutorial will walk you through using LVM to manage disk partitions and filesystem sizes.
LVM Terms
- Hard Drive – pertains to physical storage media
- Physical Volume – pertains to hard drive partitions assigned to LVM as hard drives
- Volume Group – pertains to a group of physical volumes allocated/joined together to become a storage pool.
- Logical Volume – pertains to partitions created from the volume group storage pool. These partitions are the one used by the OS and can be dynamically resized.
In this example assume the following
- Create 3 partitions of size each 100MB.
- Convert them into physical volumes.
- Combine physical volumes into volume group.
- Finally create a logical volume from the volume group.
Here’s a tutorial on managing disk partitions.
Create Partitions
Use fdisk command to create and manage partions.
To view the existing partitions use following command
[root@server ~]# fdisk -l
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007b12c
Device Boot Start End Blocks Id System
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ac451
Device Boot Start End Blocks Id System
/dev/sda1 * 1 128 1024000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 128 291 1310720 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 291 1045 6052864 83 Linux
The above output shows us two physical hard disks. The /dev/sda contains three partitions and no space to create additional partions. And the second drive /dev/sdb contains no partions yet. So let us use the second one in this tutorial.
Now let us create three partions of each size 100MB using fdisk command.
[root@server ~]# fdisk /dev/sdb
Update the kernel to save the changes without restarting the system.
[root@server ~]# partprobe
Again we will check the existing partitions using fdisk command and you should now have three partitions.
Create Physical Volumes
We need to install the LVM packages first in order to use LVM tools
[root@server ~]# yum install lvm2
Now create physical volumes using the command pvcreate.
[root@server ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
To verify the newly created physical volumes use the command pvdisplay.
[root@server ~]# pvdisplay
Create Volume Groups
Create a new volume group called vg1 using two physical volumes /dev/sdb1 and /dev/sdb2 using the command vgcreate.
[root@server ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
To verify the volume group has been created or not use the command vgdisplay.
[root@server ~]# vgdisplay
Create Logical Volume
To create logical volume use the command lvcreate. Let us create a logical volume called lv1 with size 200MB.
[root@server ~]# lvcreate -L 200M vg1 -n lv1
Verify the logical volume is created or not using command lvdisplay.
[root@server ~]# lvdisplay
Format and Mount the logical volume
Now format the newly created logical volume and mount it in the /mnt directory or wherever you want.
[root@server ~]# mkfs.ext4 /dev/vg1/lv1
And mount the logical volume in the /mnt mount point.
[root@server ~]# mount /dev/vg1/lv1 /mnt/
Now that the logical volume is successfully mounted in /mnt. You can use the new logicals volume to store your data.
Extend Volume Group Size
You can extend the Volume Group using vgextend.
[root@server mnt]# vgextend vg1 /dev/sdb3
Then resize the logical vloume lv1.
[root@server mnt]# lvresize -L +100M /dev/vg1/lv1
Resize the filesystem of logical volume lv1.
[root@server mnt]# resize2fs /dev/vg1/lv1
Now verify the new size of the logical volume lv1.
[root@server mnt]# lvdisplay /dev/vg1/lv1
Remove Logical Volume
Come out of the /mnt mount point, unmount the logical volume lv1 and remove it using command lvremove.
[root@server mnt]# cd ..
[root@server /]# umount /mnt/
[root@server /]# lvremove /dev/vg1/lv1
Remove Volume Group
To remove volume groups, use the following syntax:
[root@server /]# vgremove /dev/vg1
Remove Physical Volume
To remove physical volumes, use the following syntax:
[root@server /]# pvremove /dev/sdb1 /dev/sdb2 /dev/sdb3
Example syntax for existing LVMs
Configured /dev/vdd as a physical volume with:
pvcreate /dev/vdd
Extended volume group nfs_vg with:
vgextend nfs_vg /dev/vdd
Create a logical volume
lvcreate --extents +100%FREE -n cbs_lv cbs_vg
Resized logical volume nfs_lv with:
lvresize --extents +100%FREE /dev/nfs_vg/nfs_lv
Grow the xfs file system:
xfs_growfs /nfs
Growing an ext4 partition
resize2fs /dev/mapper/lv_partition
– masterkenneth