By default, when you install the CentOS/RHEL OS, it creates a default layout consisting of a /boot partition and swap logical volume and then allocates all remaining space to one logical volume to be used for the root file system. Placing all data and system files in one file system is not always the best approach for large or production systems.
It is possible to reduce the size of the root file system and the logical volume on which it resides allowing the space to be allocated to auxiliary volumes without reinstalling the system. We can reduce the root filesystem in the rescue mode. For the purpose of this example, we will take /dev/VolGroup00/LogVol00 as the root filesystem.
Reduce the size of root filesystem
1. First, boot the system into rescue mode.
2. Activate the logical volume to be reduced. In this example, /dev/VolGroup00/LogVol00 was made available with the following command:
# lvm lvchange -ay /dev/VolGroup00/LogVol00
3. Reduce the size of the file system and logical volume on /dev/VolGroup00/LogVol00. Ensure there is sufficient space left on the root file system and that the logical volume is large enough to contain all the data that was previously present. Attempts to reduce the size of a file system beyond its current utilization will fail with an error message indicating that there is insufficient space. Before resizing file system, run the e2fsck command to perform a file system check:
# e2fsck -f /dev/VolGroup00/LogVol00 # resize2fs /dev/VolGroup00/LogVol00 3000M
4. Finally reduce the size of the logical volume containing the root file system:
# lvm lvreduce -L 3000M /dev/VolGroup00/LogVol00
The commands used in these examples specify the final size of the file system rather than the amount of space by which it should be reduced.
Alternate command to lvreduce – lvresize
The LVM2 commands support the alternative lvresize command which accepts relative sizes should this be more convenient. For example:
# lvresize -L -2000G VolGroup00/LogVol00
5. Finally, verify the modification and reboot the system by exiting the rescue shell.
# lvm vgdisplay VolGroup00
To leave the rescue shell:
Note on XFS filesystem
CentOS/RHEL 7 has a default filesystem of XFS which cannot be reduced in size. If a custom setup was created during installation which utilized an EXT filesystem instead, the above steps should also apply.
While the XFS file systems can be grown while mounted, their size cannot be reduced as the feature is deemed to be complex and risky, and limited critical use cases are believed to exist. In some cases, the following workarounds may be acceptable:
- Use thin-provisioned LVM volumes below the XFS filesystem.
- Use some backup/restore solution, such as xfsdump/xfsrestore. This may be useful especially if the filesystem is mostly empty.