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 fixed partition without losing any data but it is to create a dynamic partition using LVM if in future if we wish to increase or decrease the size of hard-disk.

There are two Memory Management Techniques: Contiguous, and Non-Contiguous. In Contiguous Technique, executing process must be loaded entirely in main-memory.

Contiguous Technique can be divided into:

  1. Fixed (or static) partitioning
  2. Variable (or dynamic) partitioning
  • LVM (Logical Volume Management) is a concept used to pool storage from different physical storage devices, give 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 storages 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) are connected to the node(Volume Group).
  • Logical Volume is created from the volume group which can also be regarded as storage partition.
  • First we need to create a partition then format it and finally mount the storage.
  • #fdisk -l displays all the harddisks connected on 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 Volume Group (VG) from Physical Volumes, use # vgcreate command and assign name to LG and give names of PV from which you want to create VG. For example, #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 Logical Volume (LV) from VG we use #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 given logical volume.
  • After creating LV, we need to format it using #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 #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 same volume group.
  • Reducing size of one Logical volume will increase size of volume group and from volume group we can increase size of another logical volume.
  • To reduce size of logical volume we have to follow five steps:
    a. Unmount the partition using the unmount command.
    b. Use the e2fsck -f command on partition (Ex: e2fsck -f /dev/sdb1) to scan and clean the inode tables for any garbage/bad blocks.
    c. Format the partition using 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 inode table we have less space.
    d. Reduce the partition using lvreduce command
    e. Mount the partition so that it is live again.
  • After these five steps we can increase size of vg using vgextend command and then from volume group we can increase size of 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 mount point in place of partition name with xfs-growfs command.
  • When VG goes to the PV then the blocks are called as physical extends or extents whereas when VG goes to LV then the blocks are called as 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.

GitHub URL:




Author @ freeCodeCamp | GDSC | IBM Z | GoogleCloudReady Facilitator | Photographer | Quantum Computing | ARTH | IIEC Rise | MLOps

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Variables in Metabase

“{{var}}” literally written on plain white background. You don’t miss too much so you can as well just read the post. ╮(─▽─)╭

Project Management Timeline | 5 Best Timeline Software for Agile Professionals In 2020

Integrate security solutions in Microsoft Defender for Cloud

Contribution to the tech community

T Challenge — English Proficiency — TOIEC Test

Data Platform from the group up-Setup Superset in production (High availability/Async) with…

Another week with APItalist — communities and platform development

The Most Important Salesforce Lingo You Should Know!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gursimar Singh

Gursimar Singh

Author @ freeCodeCamp | GDSC | IBM Z | GoogleCloudReady Facilitator | Photographer | Quantum Computing | ARTH | IIEC Rise | MLOps

More from Medium

My Data Science Journey: An Update

Task:- Store your data in AWS EFS and attach EFS with multiple EC2 instances

Hosting a Static Website with Amazon S3 and CloudFront

I'm writing this post to share my experience in clearing the CNCF’s CKA — Certified Kubernetes…