Logical Volume Management
Logical Volume Management (LVM) can allow to manage active partitions. Before LVM, we had no easy way to increase or reduce the size of a partition after Linux was installed. For example, if find that we have extra space on the /home directory partition and need more space on /var directory partition for log files, LVM will let reallocate the space. Alternatively, if we are managing a server on a growing network, new users will be common. May reach the point at which we need more room on our /home directory partition. With LVM, we can add a new physical disk and allocate its storage capacity to an existing /home directory partition.
Install Red Hat Linux with LVM
Boot with Red Hat Linux DVD. Click on “Create custom layout” to create partitions.
1st create a “/boot” partition of 200 MB.
2nd create LVM partition of 50 GB or as per requirement.
Select newly created LVM (Physical Volume) > Create > Select LVM Volume Group > Create. Click on Add > Select Mount Point “/” and set its size approx 10 GB > Ok.
Same as create -
/home 5 GB
/tmp 5 GB
/usr 5 GB
/var 5 GB
/usr/local 5 GB
/opt 5 GB
Then click on ok.
Create a Swap partition, Select its size to double of RAM space.
Click on Ok > Click on Write changes on disk.
Follow the instructions and click on next. The installation is starting.
Extend a Logical Volume:
1. Unmount the logical volume if it’s mounted. (Note necessary)
# umount /home
2. Extend the logical volume with 50GB.
# lvextend -L +50G /dev/vg1/log2
3. Check the volume group.
#e2fsck -f /dev/vg1/log2
4. Resize the filesystem on the logical volume (ext3 in this case).
# resize2fs /dev/vg1/log2
5. Mount the logical volume again.
# mount /dev/vg1/log2 /home (Note necessary)
Reduce a Logical Volume: Make a backup of logical volume before reduction and to use around 5 GB of safe space to avoid data loss.
1. Umount the logical volume.
# umount /home
2. Check the filesystem integrity.
# fsck -f -y -v /dev/vg1/log2
3. Resize the file system to something smaller than the final size (around 30GB in this case):
# resize2fs /dev/vg1/log2 30000M
4. Check the filesystem integrity again.
# fsck -f -y -v /dev/vg1/log2
5. Reduce the logical volume
# lvreduce -L 30G /dev/vg1/log2
6. Resize the filesystem to fit the logical volume.
# resize2fs /dev/vg1/log2
7. Check the filesystem to know whether the reduction went fine.
# fsck -f -y -v /dev/vg1/log2
8. Mount the logical volume.
# mount /dev/vg1/log2 /home
9. Check the /home file system size:
# df -h | grep /home
NOTE: In LVM we can increase the size of any partitions on live system without un-mounting the partition but we can't decrease all partitions on live system. Because for decreasing the size of any partition that is necessary to un-mount that partition and then decrease partition size otherwise data will be lost, and we can't un-mount these partitions /, /var, /usr, /tmp. So we can decrease only /home, /opt, /usr/local partitions. If we want to decrease the size of /, /var, /usr, /tmp partitions then boot system in Rescue mode with Linux DVD and do this.
Create LVM Partitions after installing OS
To create a new LVM system, we need to create a new PV (Physical Volume), using a command such as pvcreate, assign the space to a VG (Volume Group) with a command such as vgcreate, and allocate the space from some part of available VGs to an LV (Logical Volume) with a command such as lvcreate.
Creating a Physical Volume: The first step in creating an LVM is to start with a physical disk. In that disk create to partition for example /dev/sda10 and /dev/sda11.After creating partition which id is 8e we can configure it like this.
# fdisk /dev/sda
Command (m for help) : t
Partition number (1-11): 10
Partition ID (L to list options): 8e
Command (m for help) : w
Save the partition table with “w” and run the command partprobe.
# partprobe
Creating Physical Volume: Run these command to create Physical Volume.
# pvcreate /dev/sda10 /dev/sda11 (This command creates the physical volume)
# pvs (It show the physical volumes)
Creating a Volume Group: Once we have two or more PVs, we can create a volume group (VG) by the following command, substitute the name of our choice for volume group name.
# vgcreate vg1 /dev/sdb10 /dev/sdb11
Where vg1 is a volume group name. When Volume Group successfully create then use this command to see the volume group.
# vgs
We can add more physical volume to any VG. Assume there's an existing /dev/sda12 partition, using a Linux LVM type, and the pvcreate command has been applied to that partition. We can then add that partition to an existing VG with the following command:
# vgextend vg1 /dev/sda12
Creating a Logical Volume: However, a VG doesn't create and mount a filesystem on it. So we need to create a logical volume (LV) for this purpose. The following command creates an LV. We can add as many chunks of disk space as our need.
# lvcreate –L +250M –n /dev/vg1/log1
The following command creates a device named log1 in /dev/vg1 of 250 MB size. We can format this device as if it were a regular disk partition, and then mount on a newly created directory. Using the “df -h” command to check the size of the volume after we've mounted on a directory on.
#lvs (Show the logical volume)
After creating a logical volume, format that partition by running this command.
# mke2fs –j /dev/vg1/log1
Then mount that partition and add its entry in fstab file like this.
dev/vg1/log1 /nilesh ext3 defaults 0 0
Removing a Logical Volume: Removing an existing LV requires a straightforward command. The basic command is lvremove. If you've created an LV in the previous section and want to remove it, the basic steps are simple. However, it will work only from a rescue environment such as the linux rescue mode from a CD/DVD-based system. Save any data in directories that are mounted on the LV.
1. Unmount any directories associated with the LV. Based on the example in the previous section; you would use the following command:
# umount /dev/vg1/log1
2. Apply the lvremove command to the LV with a command such as:
# lvremove /dev/vg1/log1
You should now have the PV from this LV free for use in other LV.
Resize logical volume and their filesystem.
1. Unmount your mounted lvm partition.
2. Run the command
# lvextend –L2G /dev/iiht/log1 (It will extend lvm partition to 2 Gb)
3. Then run the command
# e2fsck –f /dev/vg1/log1 (It will check ext3 partition)
4. Now run the command
# resize2fs /dev/iiht/log1 (This will resize lvm partition)
Then mount it again and run the command “df –h” to see it is extended or not.
# df -h
Remove Physical Volume:
# pvremove /dev/sda11
Commands Description
partprobe To notify kernel about the change in device
pvcreate To create physical volume
pvdisplay To display the size and other information
about physical volume
pvremove To remove the physical volume
pvresize To modify the size of physical volume
pvscan To scan the physical volume
vgextend To extend the size of volume group
vgreduce To reduce the size of volume group
vgscan To scan the volume group for change
vgdisplay To display the status and information
about volume group
lvdisplay To display the status of lvm
lvreduce To reduce the size of lvm
lvextend To extend the size of lvm
lvcreate To create the lvm
lvremove To remove the lvm partition
lvscan To scan lvm for change