Complete guide to file system shrinking and reducing the size of the underlying logical volume

Red Hat Enterprise Linux v7 – RHCSA EX200 exam task: reduce the size of a file system.

If you will be shrinking the size of the file system in your production Environment in work there are two things which you have to consider:

1) file system type musts support shrinking – XFS which is default file system in Red Hat Enterprise Linux 7 does not support shrinking at all.
2) ext2, ext3 and ext4 file systems don’t support on-line shrinking so they must be unmounted during shrinking.

Also best practice is to have up to date backup before you perform file system shrinking.

So our given task is to shrink file system /u03 to 1GB.

1) First we will run “df -h” command to find out current size of our /u03 file system.

2) We will run “mount | grep u03” command to find out what is the type of the file system.

3) ext2, ext3 and ext4 file systems don’t support on-line shrinking so we will unmount the file system with command:
unmount /u03

4) Before we will shrink file system we will perform file system check with command:
e2fsck -f /dev/datavg/lv_u03

where: -f option will force checking even if the file system seems clean.

NOTE: you can use both /dev/mapper/datavg-lv_u03 or /dev/datavg/lv_u03 as parameter to command e2fsck and commands which we will use later because
both of /dev/mapper/datavg-lv_u03 and /dev/datavg/lv_u03 are symbolic links to /dev/dm-4 in our case.

5) Now we can shrink file system with command:
resize2fs -p /dev/datavg/lv_u03 1G

where:
-p option prints out a percentage completion bars for each resize2fs operation, so we can track what the program is doing.
1G : will set the size of the file system to 1GB – you can use M for Megabytes or K for kilobytes.

6) We can use output of resize2fs command to calculate new exact size of the logical volume. Size of the Logical Volume should be the same as size of a file system. It is not efficient but logical volume size can be a little bit bigger then size of the file system – we can then for example extend file system size to match logical volume size, but size of the logical volume can not be smaller then size of the file system.

7) Reduce the size of logical volume to 1GB:
lvreduce -L 1048576K /dev/datavg/lv_u03

where:
-L 1048576K :
will set the size of the logical volume to 1GB
you can use M for Megabytes or G for gigabytes and so on …

NOTE: you can use also below alternatives to lvreduce command in our scenario:
lvreduce -L -3145728K /dev/datavg/lv_u03

where:
-3145728K means that current size of logical volume will be reduced by 3145728K
or
lvreduce -l 256 /dev/datavg/lv_u03

where:
-l 256: means that size of the logical volume will be set to 256 logical extents which is 1GB in our scenario because size of logical extent is 4MB in our case
or
lvreduce -l -768 /dev/datavg/lv_u03

where:
-l -768 means that size of the logical volume will be reduced by 768 logical extents which is 3GB in our scenario because the size of the logical extent is 4MB in our case.

NOTE: You can find out size of the logical extent by running “vgdisplay datavg”.

8) Now we will check file system once again with command:
e2fsck -f /dev/datavg/lv_u03

where: -f option will force checking even if the file system seems clean.

9) We will mount file system
mount /u03

10) We will verify the NEW size of the file system with command:
df -h /u03

File system shrinking and reducing the size of the underlying logical volume with only one command.

In this case we will shrink /u02 file system from 4GB to 1 GB again.

1) First we will run “df -h” command to find out current size of our /u02 file system.

2) Shrink file system and reduce the size of underlying logical volume:
lvresize -L 1G -r /dev/mapper/datavg-lv_u02

where:
-L 1G : will set the size of the logical volume to 1GB. You can use M for Megabytes or K for kilobytes and so on …

-r option will re-size the file system together with the logical volume.

Command “lvresize -L 1G -r /dev/mapper/datavg-lv_u02” performed all tasks which we had to do manually before:
– unmounted our file system
– performed file system check
– shrank file system to 1GB
– and also reduced the size of logical volume to 1GB as well.

3) We will confirm new size of the file system with “df -h /u02” command.

Please remember that:
– first you have to shrink size of the file system
– and only after size of the file system has been successfully reduced then you can reduce size of the underlying logical volume.

Related Video Tutorials:

RHCSA (EX200) Fast Track

Shrink ext2 ext3 and ext4 file system and reduce the size of a logical volume with only one command