Increase or Decrease the Size of Static Partition in Linux.

Mohd Sabir
5 min readMar 14, 2021

--

What is LVM(Logical Volume Management)?

LVM is a framework of the Linux operating system that has been introduced for the easier management of physical storage devices. The concept of logical volume management is very much similar to the concept of virtualization, i.e. you can create as many virtual storage volumes on top of a single storage device as you want. The logical storage volumes thus created can be expanded or shrunk according to your growing or reducing storage needs.

LVM Storage Management Structures

LVM functions by layering abstractions on top of physical storage devices. The basic layers that LVM uses, starting with the most primitive, are.

Physical Volumes:

  • LVM utility prefix: pv...
  • Description: Physical block devices or other disk-like devices (for example, other devices created by device mapper, like RAID arrays) are used by LVM as the raw building material for higher levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management.

Volume Groups:

  • LVM utility prefix: vg...
  • Description: LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with combined storage capacity of the component physical volumes.

Logical Volumes:

  • LVM utility prefix: lv...
  • Description: A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. Logical volumes are the primary component that users and applications will interact with.

Task Description 📄

🔅 Increase or Decrease the Size of Static Partition in Linux.

Let’s start doing our task:-

Increase or Decrease the Size of Static Partition in Linux.

If we want to increase and decrease the size of Partition then we can use LVM( Logical Volume Management ) concept.

Let’s create partition:-

Let’s check how many hdd available in my instance

fdisk -l 

My Linux instance running in AWS cloud , so I will create a EBS volume as external Hdd and attach it to with my instance.

-> Go to ec2 dashboard
-> Click on Volume
-> Create Volumes
-> Attach Volume with your Instance
click create volume
select volume size

Volume created successfully and it is available , as I will attach this volume with my instance It will show in-use

let’s attach

click on attach

Now it is in use. let’s check from command line

attach properly

Let’s create LVM partition , we have already discuss storage structure of LVM , first we have to create PV( physical volume)

              "pvcreate /dev/xvdf"

To check pv creates or not we can use command

        pvdisplay
created

PV created now we will create the VG( volume group )

                vgcreate task /dev/sdf
successfully created

Now we will create lv(Logical volume)

lvcreate --size 4G --name lv1 task
Created successfully

Now format this partition and mount it

#formatting
mkfs.ext4 /dev/task/lv1
#mount this partition
mount /dev/task/lv1 /mnt

Now we can use this partition.

We have created 4GB partition now let’s increase the partition size from 4GB to 7GB

lvextend --size +3G /dev/task/lv1

partition resize successfully

let’s check

Partition extend successfully but it is not showing here why ?

because we have not format our partition , first we have to format this , there are lots of type to format a partition , last we use mkfs.ext4 command to format, but when we extend our partition then some data already available in this partition if We use mkfs.ext4 then all the data will be goes, So we can’t use this , we have another command “resize2fs” to format extend partition, This command only format remaining part which we have extend , it is also known as on-line resizing.

resize2fs /dev/task/lv1

Now it extend successfully

Now Let’s Decrease the partition size

for decreasing the partition we have to follow five steps:-

-> Step-1
unmount the partition
-> Step-2
scan your Inode table
-> Step-3
online formatting
-> Step-4
reduce space
-> Step-5
mount the partition

Let’s follow these steps:-

-> Step-1
unmount the partition
"umount /dev/task/lv1"
partition unmount
-> Step-2
scan your inode table
"e2fsck -f /dev/task/lv1"
-> Step-3
online formatting
"resize2fs /dev/task/lv1 5G"
-> Step-4
reduce the partition size
"lvreduce --size -2G /dev/task/lv1"
resized
-> Step-5
mount the partition
"mount /dev/task/lv1 /mnt"

Task has been done

Thanks for Reading

For any query or Improve this article you can connect with me on

LinkedIn — https://www.linkedin.com/in/mohd-sabir-394858184

Gmail — feedbacksabir@gmail.com

--

--

Mohd Sabir
Mohd Sabir

Written by Mohd Sabir

DevOps Enthusiastic || Kubernetes || GCP || Terraform || Jenkins || Scripting || Linux ,, Don’t hesitate to contact on : https://www.linkedin.com/in/mohdsabir

No responses yet