From this blog you will come to know about logical volume, how to increase or decrease the volume, automating the process with python.
Logical Volume Manager: - In Linux, Logical Volume Manager is a device mapper framework that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware to the point of being able to have their root file systems on a logical volume.
You may get a doubt why to use LVM?
Storage is one of the many components in a computer device, without it we can’t do anything. As storage is only limited to a number for example 10 Gb (Gigabytes). Where there is more information to store our system run out of storage. Then LVM comes into play to increase the volume and sometimes we don’t need the extra storage, then we can reduce.
Process for creating the logical volume
First, we need to create a PV (Physical volume). It is useful to provide a volume group from a storage device.
$ fdisk -l # to view your storage paths$ pvcreate /disk_path #provide a storage pathWe can also use pvdisplay to display the physical volume$ pvdisplay /disk_path #displaying the properties
Now we must create a volume group. In this we can combine multiple storage drives to work together and share their storage.
$ vgcreate vg_name /disk_path_1 # for only single drive$ vgcreate vg_name /disk_path_1 /disk_path_2 #for multiple drives
here <vg_name> is the name of your volume group
We can use vgdisplay to display the properties of volume group
#For showing the disk information$ vgdisplay vg_name
Now we can create a logical volume from volume group. Let’s say that we have 30Gb of VG from this we can create 20 GB of Logical volume and provide it to our system with the help of a folder.
#For partitioning the above drivelvcreate — size 20G — name lvm_name vg_name#<lvm_name> is the name of your logical volume
To display the properties of logical volume.
#For displaying the lvm$ lvdisplay vg_name/lvm_name#To see all the lvm disks$ lvdisplay
To use this storage devices we have to format the disk. There are lots of formatting available, here we use ext4.
#Formatting with ext4$ mkfs.ext4 /disk_path
Your newly created logical volume has a specific disk path. Commonly it will be as below.
#Here the /disk_path has many names/dev/vg_name/lvm_name/dev/mapper/vg_name-lvm_name/dm3 #3 will be any number based on number of the disks
Or you can also know it by
$ fdisk -l# The device with lv storage as provided earlier will be the path or http://www.linux-databook.info/?page_id=922it will be from above formats
Finally, we can mount the storage to a folder.
$ mount /dev/mapper/vg_name-lvm_name /myfolder# <myfolder> can be the name of your folder
To extend the logical volume
We can extend the logical volume upto extra 10 GB as we mentioned 30 GB above. Here we can give the storage size to be improved.
#Extending the size of lvm disk$ lvextend — size +5G /dev/vg_name/lvm_name$ resize2fs /dev/vg_name/lvm_name
The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on a device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.
Reduce the logical volume.
First, we want to unmount the storage drive from the folder.
$ umount /dev/vg_name/lvm_name
e2fsck is used to examine ext2/ext3/ext4 filesystems for errors and fsck checks and can optionally repair a Linux filesystem; it is basically a front-end for a range of filesystem checkers (fsck.fstype for example fsck.ext3, fsck.sfx etc) offered under Linux.
As we are doing ext4 formatting we can use e2fsck.
Here we want to reduce 4Gb from the 14 Gb drive. So that our drive size becomes 10Gb.
#Reducing the size of lvm diskumount /dev/vg_name/lvm_namee2fsck -f /dev/dev/vg_name/lvm_nameresize2fs /dev/vg_name/lvm_name 10G # <10G is the new drive size>lvreduce -L -4G /dev//dev/vg_name/lvm_name #< 4G is the reduced size>mount /dev/vg_name/lvm_name /myfolder
LVM automation with Python
The above process is huge and takes time, living in the fast-paced environment everything is automated and runs in the cloud.
So, we can take scenario where we are running our application in Amazon web services EC2 instance. You can also use any other cloud provider with a ssh key. Redhat OS is mandatory.
I am using python programming language; this script can only run in Linux operating systems as ssh is not supported by Windows.
provide your ec2 instance name and ip, most probably your instance name would be ec2-user.
Options for making our task extremely easy
options for LVM
By default, lvm commands are provided by the RedHat operating system. You can only install the commands in Redhat OS.
mounting lvm to drive
Increasing the drive from 8 Gb to 9 Gb.
Increase storage size
Now our drive size is 9 Gb, I want to reduce 4 Gb of the drive. So, the final drive size is 5Gb.
So, you have learned about LVM, step by step process of creating LVM and using it through a folder.
If you have any doubts or suggestions, please comment below.
Thank you for reading the blog and the python script is provided below.