With LVM, we can configure logical volume (or partition) that can be resized. It means that you can extend or reduce the logical volume size without loosing data!
How is it organized?
To setup an LVM, we will need to create one or more Physical Volume (partition). You can use fdisk and this documentation. It’s necessary that physical volumes are from the same hard drive. So actually you can prepare one physical volume from sdA and another from sdB.
The next step is group them into a Volume Group which represent a “logical hard drive” :p. Its size is the sum of every physical volumes you put. So if you have sda1 with 1Gib and sdb4 with 3Gib as physical volume into the vg0 as volume group, vg0 will have 4Gib. Then logical volume will be created from the volume group vg0.
How to create LVM ?
Prepare the Physical Volume
Make sure that there is no data in your partition. If you still have not prepared a partition have look at ‘how to use fdisk‘ . Then execute the following:
root@sigmoun-redhat ~] pvcreate /dev/sdaN
To display the physical volume execute the following:
root@sigmoun-redhat ~] pvdisplay /dev/sdaN
Create / Remove a Volume Group
To create the volume group execute:
root@sigmoun-redhat ~] vgcreate vgname /dev/sdaN
To display the volume group execute:
root@sigmoun-redhat ~] vgdisplay /dev/vgname
To remove the volume group execute:
root@sigmoun-redhat ~] vgremove vgname
Resize Volume Group
Resizing a volume group only consist of add or removing physical volume to / from a volume group.
Extend Volume Group
root@sigmoun-redhat ~] vgextend /dev/vgname /dev/sdaN+1
Reduce Volume Group
Make sur the physical volume you remove from the volume group does not containt any data.
root@sigmoun-redhat ~] vgreduce vgname /dev/sdaN
Create / Remove Logical Volume
We create the logical volume:
root@sigmoun-redhat ~] lvcreate -n lvname -L 2G vgname
Then we install a file system into the new logical volume:
root@sigmoun-redhat ~] mkfs.ext4 /dev/vgname/lvname
If you want to mount it:
root@sigmoun-redhat ~] mkdir /mounting_point root@sigmoun-redhat ~] mount /dev/vgname/lvname /mounting_point
To display the logical volume execute the following:
root@sigmoun-redhat ~] lvdisplay /dev/vgname/lvname
Extend Logical Volume
We extend the lvname to 3G:
root@sigmoun-redhat ~] lvextend -L 3G /dev/vgname/lvname
Then we need format the added part and so it can be used:
root@sigmoun-redhat ~] resize2fs -p /dev/vgname/lvname
Reduce Logical Volume
Before reducing a logical volume we need to unmount it:
root@sigmoun-redhat ~] umount /mounting_point
We check the file system:
root@sigmoun-redhat ~] fsck -f /dev/vgname/lvname
Then and we inform the file system of the logical volume its new size (512M) with resize2fs
root@sigmoun-redhat ~] resi2fs -p /dev/vgname/lvname 512M
Finally we reduce the logical volume:
root@sigmoun-redhat ~] lvreduce -L 512M /dev/vgname/lvname
To remove a logical volume execute:
root@sigmoun-redhat ~] lvremove /dev/vgname/lvname
That’s it! LVM is something very interesting. It lets you have easly manage your disks and partition. It will be very interesting to use it with RAID…
Anyway, if you have any suggestion or comment please let me know and share it!