LVM Series – EXT2/EXT3/EXT4 filesystem management

LVM series continues. If you want to start from the beginning, check our first post. You can review our last post, about LV Managment.
In this post we are going to review how to manage an EXT2,EXT3/EXT4 filesystem over LVM.
For the exercises we will start with an LV called lv_extfssample in VG vg_storage1.

Formatting LV with EXT4

Let’s format this LV with EXT4 filesystem. Everything we are seeing in this post applies to EXT2/EXT3 as well.

mkfs.ext4 /dev/vg_storage1/lv_extfssample

Mounting the filesystem

We will work with the filesystem mounted whenever is possible.
Usually it will be configured in fstab, but we are going to mount our filesystem manually in /mnt/temp.

mkdir /mnt/temp
mount /dev/vg_storage1/lv_extfssample /mnt/temp

Increasing the size of the filesystem

This is one of the most important reasons to use LVM, to be able to modify the size of our volumes.
It is important to note that this process depends on the filesystem.
This is what we are about to do and the order we will follow:

  • Make sure the VG has space enough.
  • Increase the size of the LV.
  • Resize the filesystem.

Checking VG free space

If you don’t know what VG you need to check, use tools such as df, lvdisplay or vgdisplay, as we saw in some of the last posts.
In our examples, our VG is vg_storage1, so you can check how much free space is available like this:

vgdisplay vg_storage1 | grep Free

If there is a free amount of space equal or bigger than the amount we want to increase, we can keep going with the process.
If there is not, you have to raise the storage in that VG or reduce some other LV to release space.

Increasing the size of the LV

The next step is to raise the size of the logical volume, we will do that with “lvextend“.
In this example, we will increase the size of lv_extfssample in 1 GB. Let’s remember that our filesystem is mounted in /mnt/temp, so no need to unmount it, we can do this without affecting service.

lvextend -L+1G /dev/vg_storage1/lv_extfssample

Resizing the filesystem

At this moment, you can take a few minutes to check the size of the filesystem and the size of the LV. You will see that LV is 1 GB bigger than the filesystem.
So now we have to resize the filesystem to be able to use all space available in the LV.
It is done by “resize2fs” tool.
Remember that we didn’t umount the filesystem, so we can do this online.

resize2fs /dev/vg_storage1/lv_extfssample

Now we have the filesystem using all available space in LV.

Decreasing the size of the filesystem

Working with storage is always risky, but this kind of operations are specially dangerous.
I’m sure it is not necessary, your backups are all working as expected and in shape. But at this point, it would be worth checking it.
The order we are going to follow to decrease the size is more or less the opposite we did in the last section.

  • Check the space occupation in the filesytem.
  • Unmount filesystem.
  • Resize the filesystem.
  • Decrease the size of the logical volume.

You must reduce the filesystem BEFORE you reduce the logical volume.

Checking the space occupation in the filesytem

Review the amount of space available in the filesystem and make sure there is more free space that the amount of space you want to reduce.
If you don’t have enough free space, you will have to delete some data or reduce the filesystem less than expected.

Unmounting the filesystem

Unfortunately, reducing the filesystem size in EXT2/EXT3/EXT4 is not implemented. So, we will have to unmount it.
Remember this to plan this tasks if you are working on a production environment.
Just unmount it, in our case /mnt/temp:

umount /mnt/temp

Resizing the filesystem

It is time to reduce the filesystem. We will do it using “resize2fs“, the same tool used before.
In our example, we are setting the new total size of the LV to 1 GB in lv_extfssample:

e2fsck -f /dev/vg_storage1/lv_extfssample
resize2fs -p /dev/vg_storage1/lv_extfssample 1GB

Decreasing the size of the logical volume

Now we can reduce the size of our logical volume. Of course we can reduce less or equal space than the reduced in the last step.
We are using “lvreduce” to do this.
In our example, we are reducing 1 GB our LV lv_extfssample:

lvreduce -L-1G /dev/vg_storage1/lv_extfssample

It is only left to mount the volume again and we will see the new size.
We can also check the volume group vg_storage1, it will now have the free space released from lv.

Decreasing the size of the filesystem and LV in one step

We can reduce the size of the filesystem and logical volume in one step using “–resizefs” flag in “lvreduce” command. Let’s do the same reduction as the last example:

lvresize --resizefs --size 1G/dev/vg_storage1/lv_extfssample

We will continue our LVM series with XFS filesystem management.

Leave a Reply

Your email address will not be published. Required fields are marked *