Automating LVM Partition using Python-Script
Automation is the technology by which a process or procedure is performed with minimal human assistance. Automation, or automatic control, is the use of various control systems.
We can automate the process of creating LVM partition using python script.
Logical volume management
Logical volume management (LVM) is a form of storage virtualization that offers system administrators a more flexible approach to managing disk storage space than traditional partitioning. The goal of LVM is to facilitate managing the sometimes conflicting storage needs of multiple end users.
What is LVM used for?
It is a system of managing logical volumes, or filesystems, that is much more advanced and flexible than the traditional method of partitioning a disk into one or more segments and formatting that partition with a filesystem.
We can increase the size of a fixed partition without losing any data, but it is better to create a dynamic partition using LVM if, in the future, we wish to increase or decrease the size of the hard disk.
There are two memory management techniques: Contiguous, and Non-Contiguous. In the contiguous technique, the executing process must be loaded entirely in main memory.
Contiguous Technique can be divided into:
- Fixed (or static) partitioning
- Variable (or dynamic) partitioning
- LVM (Logical Volume Management) is a concept used to pool storage from different physical storage devices, giving a choice to create multiple partitions over the Logical volume and other features to manage the logical volumes.
- Volume group is used to pool the storage from the physical drives and create a new virtual hard disk. It is created using the command #vgcreate <vg_name> <drive_name_1> <drive_name_2> and so on
- We create a common node and all the storage(Physical Volumes) is connected to the node(Volume Group).
- Logical Volume is created from the volume group which can also be regarded as a storage partition.
- First, we need to create a partition then format it and finally mount the storage.
- #fdisk -l displays all the harddisks connected to the system.
- We create a physical volume using the command #pvcreate <hd_name>. For example, pvcreate /dev/sdc.
- #pvdisplay /dev/sdc command displays the physical volume.
- To create a Volume Group (VG) from Physical Volumes, use the # vgcreate command and assign a name to LG and give names of PV from which you want to create VG. For example, the #vgcreate myvg1 /dev/sdb /dev/sdc /dev/sdd command will create VG from 3 PVs.
- #vgdisplay <vg_name> displays the volume group details.
- To create a Logical Volume (LV) from VG we use the #lvcreate command. For example, #lvcreate — size 10G — name mylv1 myvg1 creates mylv1 (LV) from myvg1 (VG).
- #lvdisplay <vg_name>/lv_name> displays the details of a given logical volume.
- After creating LV, we need to format it using the #mkfs command. Syntax: mkfs.ext4 <path_of_partition> //can be obtained from lvdisplay and ext4 is the format type.
- Now we need to mount the formatted LV on the folder using #mount command.
- We can extend the partition by:
a)To extend the LV use the #lvextend command. For example, #lvextend — size +5G /dev/myvg1/mylv1.
b)After extending the size of LV, resize the formatted LV using #resize2fs command. For example #resize2fs /dev/myvg1/mylv1. - For decreasing the size of the LV, use the command #lvreduce — size -5G <lv_name>.
- #df -h is used to check the partition is mounted on which directory.
- If one LV is almost full and we need some space then we can either take up the space from another LV or from another hard disk. To take the space from another LV we need to reduce the LV so that space is allocated back to the VG pool. Both logical volumes should be of the same volume group.
- Reducing the size of one Logical volume will increase the size of the volume group and from the volume group, we can increase the size of another logical volume.
- To reduce the size of the logical volume we have to follow five steps:
a. Unmount the partition using the unmount command.
b. Use the e2fsck -f command on the partition (Ex: e2fsck -f /dev/sdb1) to scan and clean the inode tables for any garbage/bad blocks.
c. Format the partition using the resize2fs command. Basically, we are just updating the inode table and saying that your max sector size has been reduced without losing the data.
Here at this point, we have storage/space in our logical volume but according to the inode table, we have less space.
d. Reduce the partition using the lvreduce command.
e. Mount the partition so that it is live again. - After these five steps, we can increase the size of VG using the vgextend command and then from the volume group we can increase the size of the logical volume.
- resize2fs is strictly supported for ext4 type of storage space. resize2fs works for both lvextend and lvreduce.
- xfs_growfs is strictly used for xfs type of storage space and it only supports lvextend. It does not support lvreduce. In RHEL8, we use a mount point in place of the partition name with the xfs-growfs command.
- When VG goes to the PV then the blocks are called physical extends or extents whereas when VG goes to LV then the blocks are called Logical extends or extents.
- By default, the block size is set to 4 MiB. We can also change it using the -s option field. Whenever we ask for storage space say suppose 10 MiB then it will always give us 12 MiB storage as 12 is the multiple of 4 which is the block size.