Increase or decrease the size of static partition in Linux using LVM

Durvesh Palkar
6 min readFeb 12, 2021

--

To have introductory knowledge about LVM, read my previous blog on “How to integrate LVM with Hadoop and provide elasticity to Datanode storage?

We know that C: drive is the main drive in windows. If it’s full, we are not able to do some tasks (like installing a new software) because all our programs are stored in this drive.

Similarly in Linux, / directory is the main directory. It contains many important directories like tmp, root, etc. All the softwares we install are stored in this directory. Therefore if this directory is 100% used, we will not be able to install new programs and also can’t do some tasks.

However one thing you might don’t know that / directory in linux is also created using the concept of LVM. Hence we can easily increase the space of / by using the space from VG.

Have you ever imagined what if we can take some space from windows D: drive and add it in C: drive. This is easily possible in linux using the concept of LVM.

Consider a scenario in which you have 2 LVs created from the same VG. One of these LV is very critical (assume as if it is the document root of a server) and is about to get full. Also your luck is so bad, that the VG from which these LVs are created has no free/available space. Here one can thing of attaching a new Harddisk, creating PV from it and then extending the VG size. However the concept of LVM provides one more alternative and powerful way, in which you don’t have to empty your pockets for a new HD.

To demonstrate this, I’ll create 2 LVs from a VG on a RHEL8 Virtual machine using Oracle Virtualbox. Then we’ll see how to take space from one LV and then give it to the other one, when our VG has no space available.

For this refer to my previous blog from STEP 1 to STEP 3 to create a VG of size 80 GiB.

CREATING LOGICAL VOLUMES FROM A VG

We’ve already created a VG named myVG of size 80 GiB. Further from this VG we’ll create 2 LVs of size 40 GiB each, so that our VG will have no free space.

(In the above figure I’ve used 39G for lv2 because we can’t use the whole 80GiB from VG as it is used for internal purposes of VG to store some metadata)

Now if you see the free space available in VG, we hardly have 1 GiB left.

If you try extending lv1 by 10 GiB it will fail because we don’t have sufficient space available in VG.

Before moving ahead, one point you should keep in mind is:

Whenever we extend an LV, it gets space from the VG and whenever we reduce an LV, the space/volume is given back to VG.

✒ Formatting and mounting the created LVs

Now, we’ll format these LVs using the command mkfs.ext4 <lv-path> in ext4 file system.

Then we’ll mount them. For this first create 2 directories on which you are going to mount these LVs.

Now assume that dir2 is your critical directory and is about to get fully occupied, and say dir1 has enough space available. We know that dir1 and dir2 both are LVs taking space from the same VG i.e. myVG. So what we can do is take the space from lv1(dir1) and give it to lv2(dir2). In other words we’ll reduce the space of lv1, which will be given back to VG. Now as we have some space available in VG, we can extend our lv2.

✒ Reducing the volume of one LV

Before reducing, keep one thing in mind that if we are reducing an lv, it is fully formatted and hence may contain some data. In our case the size of lv1 is 40 GiB and if incase it is almost full, then performing reduce operation may lead to loss of data (i.e. the data stored in that 10GiB part will be lost). However if it’s not fully occupied and we have 10–15 GiB volume empty in lv1, then reduce operation will be safe. LVM will always try to take the unused space from the LV. Therefore their will be no data loss.

Reducing an LV include 5 steps :

◼️ Currently your LV is online and thus can be used/accessed by multiple user processes. Therefore if you reduce the size, it is possible that the running processes may hang up or fail. Therefore first unmount this LV using the command umount /dir1

◼️ This step is not compulsory but it is always a good practice to scan and clean your inode table, so that they can get rid of ant unwanted linking or mapping or any other bad sectors if they have. Use the following command for this: e2fsck -f <lv-path>

◼️ In the initial steps we’ve formatted this LV using mkfs command, and whole 40 GiB is formatted i.e. it’s Inode table is using the entire 40 GiB. But now since we’re going to reduce the size of the LV from 40 GiB to 30 GiB, we have to again format it so that the Inode table will now use only 30 GiB. Here if we use mkfs command to format, it will format the whole volume (i.e. create a new Inode table) and hence all our data will be wiped out. Therefore here we’re going to use resize2fs <lv-path> 30G command to keep our data safe.

◼️ You can assume Inode table to the index page of your journal. In the previous step you’ve told it, that initially the book was having 40 pages (40 GiB) but now you (Inode) have to work for only 30 pages (30 GiB). Now in this step, you’ve to actually remove those 10 pages (10 GiB) of the book. Therefore, we are good to perform the reduce operation using the command lvreduce --size -10G <lv-path>

◼️ Now since everything is done right, we’ll again mount this LV back to dir1 . Also check using df -h command to see the reduced size of dir1.

✒ Extending the volume of other LV

Previously our VG was not having enough free/available space. But now if you try checking it’s status you will notice that we have additional 10 GiB space available, which is obtained after reducing 10 GiB from lv1.

Therefore we can now extend the volume of lv2 using the command lvextend --size +10G <lv-path> . Also we’ve format this newly added volume, for which we have to again use the resize2fs <lv-path> command. This command will automatically find the newly added volume and format it. It will do online resizing.

In the above image you can clearly witness that we’ve successfully removed some volume from lv1 and then added it to lv2 and all this was done without facing the need to take lv2 down/off.

--

--

No responses yet