What is Logical Volume Manager?
- LVM is a logical volume manager for the Linux kernel that manages disk drives and similar mass-storage devices
- It is a tool for logical volume management which is used to allocating disks, striping, mirroring and resizing logical volumes
- LVM can span across multiple physical disks
First off, lets discuss life without LVM. Back in the bad old days, you had a hard drive. This hard drive could have partitions. You could install file systems on these partitions, and then use those filesystems. Uphill both ways. It looked a lot like this:
Logical Volume manager:
Step:1 Creating Physical Volumes for LVM:
The underlying physical storage unit of an LVM logical volume is a block device such as a partition or whole disk. To use the device for an LVM logical volume the device must be initialized as a physical volume (PV). Initializing a block device as a physical volume places a label near the start of the device.
By default, the LVM label is placed in the second 512-byte sector. You can overwrite this default by placing the label on any of the first 4 sectors. This allows LVM volumes to co-exist with other users of these sectors, if necessary.
An LVM label provides correct identification and device ordering for a physical device, since devices can come up in any order when the system is booted. An LVM label remains persistent across reboots and throughout a cluster.
To create physical Volume
# pvcreate /dev/sdb
# pvcreate /dev/sdb /dev/sdc /dev/sdc ( You can create PV adding multiple disk)
To verify the PV’s:
# pvs or pvdisplay
Step:2 Creating Volume Group for LVM:
Physical volumes are combined into volume groups (VGs). This creates a pool of disk space out of which logical volumes can be allocated.
Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated, Within a physical volume, extents are referred to as physical extents.
To Create Volume group
# vgcreate appvg /dev/sdb (mininum one disk required)
# vgcreate appvg /dev/sdb /dev/sdc /dev/sdd (If you want to add more disk at the time of creation)
To Verify the VG’s:
# vgs or vgdisplay
Step:3 Creating Logical Volume
In LVM, a volume group is divided up into logical volumes. It is similar to partition on disk.
To create logical volume:
# lvcreate -n app01-vol -L 100M appvg
# lvcreate -n app02-vol -L 2G appvg
To Verify the LV’s
# lvs or lvdisplay
Step:4 Creating File system
To create file system
# mkfs -t ext4 /dev/appvg/app01-vol
# mkfs -t ext4 /dev/appvg/app02-vol
Step:5 Mounting File system
To mount file system, you need to create empty directory under root (/)
# mkdir /app01
# mkdir /app02
# mount -t ext4 /dev/appvg/app01-vol /app01
# mount -t ext4 /dev/appvg/app02-vol /app02
Verify file system status:
# df -h
Step:6 Update the file system in “/etc/fstab” file
This step will ensure the following file systems are mounted even after rebooting the server.
/dev/appvg/app01-vol /app01 ext4 defaults 0 0
/dev/appvg/app02-vol /app02 ext4 defaults 0 0
Well, we are done !!
1. pvcreate–> 2. vgcreate- -> 3.lvcreate –> 4.mkfs –> mount –> update /etc/fstab
Now.. let us see how to extend the existing Volume Group,Logical Volume & File system
Before you extend the file system, follow the below steps:
1. Check file system current size & usage # df -h
2. Check free space in volume group # vgdisplay appvg
3. Check the size of logical volume # lvdisplay app01-vol
If you have enough space in volume group, you can simply extend the logical volume and file system by following following steps:
For example, your file system /app01 is 1 GB, you wanted to extend this to 1.5 GB.
a. # lvextend -L +500M /dev/appvg/app01-lv –> (+500M , will just add another 500MB to the LV)
b. # resize2fs /dev/appvg/app01-lv –> This step will increase the file system to 1.5 GB
If you see no enough space available in volume group, first extend the volume group by adding new disks & extend LV & file system.
a. Add new disk to the server
b. Check the new disk name by running # fdisk -l | grep Disk (example, your new disk is /dev/sdf – 2 GB)
c. Convert the physical disk as “PV” # pvcreate /dev/sdf
d. Verify the PV creation step # pvdisplay
e. Add this disk to existing Volume Group # vgextend appvg /dev/sdf
f. Verify the Volume Group # vgdisplay
g. Now, extend the logical volume # lvextend -L +500M /dev/appvg/app01-lv —> (+500M , will just add another 500MB to the LV)
h. Increase the file system # resize2fs /dev/appvg/app01-lv –> This step will increase the file system to 1.5 GB
Now.. let us see how to decrease the existing File system / Logical Volume & Volume Group
Assume your /app01 file system is 2 GB, & you wanted to reduce it to 1 GB.
a. Unmount the file system # umount /app01
Note: It is highly recommended to unmount the file system to avoid data corruption during size reduce.
b. Run file system check # e2fsck –f /dev/appvg/applv
c. Reduce the file system size # resize2fs /dev/appvg/applv 1G (Example you want to reduce from 2 GB to 1 GB)
d. Reduce the logical volume size # lvresize –L -1G /dev/appvg/applv
e. Mount the file system # mount /app01
f. To remove PV from VG # vgreduce appvg /dev/sde
g. To remove PV from OS # pvremove /dev/sde