In this article we will share the step by step guide to resize root LVM partition on RHEL/CentOS 7/8 Linux with examples. The basic requirement to increase or decrease partition size is that the respective partition must not be mounted. Now you cannot unmount root partition so online resize is not possible. You must boot into rescue mode to resize root LVM partition.
2 easy methods to resize (extend or shrink) non LVM primary partition in Linux with examples
/
" in production environment. As these are not officially supported by any vendor such as Red Hat, SuSE etc and it can also destroy your system.
Lab Environment
We have verified the steps from this article to (extend and shrink) resize root LVM partition (ext4) on RHEL/CentOS 7 and 8 Linux environment.
The screenshots are captured from CentOS 8 node running on Proxmox installed on a Linux server.
We have a very simple environment with minimal partitions.
[root@centos-8 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 2.4G 0 2.4G 0% /dev tmpfs 2.4G 0 2.4G 0% /dev/shm tmpfs 2.4G 8.6M 2.4G 1% /run tmpfs 2.4G 0 2.4G 0% /sys/fs/cgroup /dev/mapper/rhel-root 14G 1.8G 11G 15% / /dev/sda1 488M 130M 323M 29% /boot tmpfs 479M 0 479M 0% /run/user/0
Below are the available logical volumes. As you see we have not created too many logical volumes.
[root@centos-8 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 13.56g
swap rhel -wi-ao---- 956.00m
Step 1: Backup your data (Optional but recommended)
Since you plan to shrink root LVM partition it is important that you backup your data before starting with the process. As if anything goes wrong you will end up with an unusable system.
Step 2: Boot into rescue mode
You cannot shrink root LVM partition online without reboot. Also you will need your RHEL/CentOS 7/8 ISO DVD depending on your environment to boot into rescue mode. Here we are using CentOS 8 so we will use the vanilla DVD of CentOS 8 to go to rescue mode.
/
partition will be in mounted state and to shrink a partition it is important that the respective partition is not mounted.Once you boot your Linux node with rescue DVD you can enable SSH and configure network in rescue mode. In this example we have disabled consistent network device naming and assigned a static IP address but you can also use DHCP by using ip=dhcp
For more information on the used syntax you can check Network boot options
In the next screen, select "3" to skip to shell. Make sure the file system is not mounted. If you select 1 or 2 then the root partition will get mounted on /mnt/sysimage
which will not allow you to resize root LVM partition.
Since we have enabled SSH and configured network for my rescue mode, we have connected using Putty. Currently the system is mounted with LIVE DVD as you can see so our LVM root partition is not in mounted state so we can decrease or increase root partition size.
[anaconda root@centos-8 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 2.4G 0 2.4G 0% /dev tmpfs 2.5G 12K 2.5G 1% /dev/shm tmpfs 2.5G 17M 2.5G 1% /run tmpfs 2.5G 0 2.5G 0% /sys/fs/cgroup /dev/sr0 6.7G 6.7G 0 100% /run/install/repo /dev/mapper/live-rw 2.9G 1.8G 1.2G 61% / tmpfs 2.5G 724K 2.5G 1% /tmp
Step 3: Activate Logical Volume
Next we must activate our logical volume before we can shrink root LVM filesystem.
[anaconda root@centos-8 ~]# vgchange -ay 2 logical volume(s) in volume group "rhel" now active
To list the available logical volumes and volume groups:
[anaconda root@centos-8 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rhel -wi------- 13.56g swap rhel -wi------- 956.00m [anaconda root@centos-8 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- <14.50g 0
Step 4: Perform File system Check
Next run a file system check to make sure the file system is proper and not corrupted. Without a file system check you cannot shrink root LVM partition or any other partition.
[anaconda root@centos-8 ~]# e2fsck -f /dev/mapper/rhel-root e2fsck 1.44.3 (10-July-2018) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/mapper/rhel-root: 49755/522240 files (0.2% non-contiguous), 502880/2097152 blocks
So our file system check has passed.
Step 5: Resize root LVM partition
Once you boot in rescue mode, you can resize root LVM partition or any other partition which you cannot un-mount runtime in default.target such as boot partition. In the below examples we will decrease root LVM partition size and also increase root LVM partition size.
Reduce or Shrink root LVM partition size in Linux
Next use resize2fs
to resize root LVM partition. Currently my "/
" file system is 13.5 GB which I want to decrease root partition size to 7GB
[anaconda root@centos-8 ~]# resize2fs /dev/mapper/rhel-root 7G
resize2fs 1.44.3 (10-July-2018)
Resizing the filesystem on /dev/mapper/rhel-root to 1835008 (4k) blocks.
The filesystem on /dev/mapper/rhel-root is now 1835008 (4k) blocks long.
Next use lvreduce
with the same size (7G) as used in above command to decrease root partition size.
[anaconda root@centos-8 ~]# lvreduce -L 7G /dev/mapper/rhel-root WARNING: Reducing active logical volume to 7.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce rhel/root? [y/n]: y Size of logical volume rhel/root changed from 13.56 GiB (3472 extents) to 7.00 GiB (1792 extents). Logical volume rhel/root successfully resized.
So the resize root LVM partition was successful. You can mount the root file system on /mnt/sysimage
to verify the size
[anaconda root@centos-8 ~]# mount /dev/mapper/rhel-root /mnt/sysimage/
As you see my root partition is now 7GB
[anaconda root@centos-8 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.4G 0 2.4G 0% /dev
tmpfs 2.5G 12K 2.5G 1% /dev/shm
tmpfs 2.5G 17M 2.5G 1% /run
tmpfs 2.5G 0 2.5G 0% /sys/fs/cgroup
/dev/sr0 6.7G 6.7G 0 100% /run/install/repo
/dev/mapper/live-rw 2.9G 1.8G 1.2G 61% /
tmpfs 2.5G 724K 2.5G 1% /tmp
/dev/mapper/rhel-root 6.9G 1.8G 4.8G 27% /mnt/sysimage
Increase root partition size in Linux
Now to extend root partition size I will use the same node. Here we will increase root partition size by 1GB from 7G to 8G.
[anaconda root@centos-8 ~]# lvextend -L +1G /dev/mapper/rhel-root
Size of logical volume rhel/root changed from 7.00 GiB (1792 extents) to 8.00 GiB (2048 extents).
Logical volume rhel/root successfully resized.
lvextend
command was successful, next we will execute resize2fs
to increase root partition size
[anaconda root@centos-8 ~]# resize2fs /dev/mapper/rhel-root
resize2fs 1.44.3 (10-July-2018)
Resizing the filesystem on /dev/mapper/rhel-root to 2097152 (4k) blocks.
The filesystem on /dev/mapper/rhel-root is now 2097152 (4k) blocks long.
Post resize2fs
you can verify the changes for which you must mount the root partition. Since we are in rescue mode we will mount it on /mnt/sysimage
[anaconda root@centos-8 ~]# mount /dev/mapper/rhel-root /mnt/sysimage/
Next using df
command we see we were successfully able to increase root partition size
[anaconda root@centos-8 ~]# df -h /mnt/sysimage/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 7.9G 1.8G 5.7G 24% /mnt/sysimage
Verify the new size of root partition
Now above command will only mount the root partition. Next we will mount all other partitions to properly verify our changes before we boot into default.target
[anaconda root@centos-8 ~]# mount -o bind /dev /mnt/sysimage/dev [anaconda root@centos-8 ~]# mount -o bind /usr /mnt/sysimage/usr [anaconda root@centos-8 ~]# mount -o bind /sys /mnt/sysimage/sys [anaconda root@centos-8 ~]# mount -t proc /proc /mnt/sysimage/proc [anaconda root@centos-8 ~]# mount -o bind /dev/shm /mnt/sysimage/dev/shm
Since we have already mounted root partition, next we will mount boot partition. Now if you have any other partitions on your system then you can mount them as well.
[anaconda root@centos-8 ~]# mount /dev/sda1 /mnt/sysimage/boot
Next use chroot
to change file system to /mnt/sysimage
.
[anaconda root@centos-8 ~]# chroot /mnt/sysimage/
Verify if everything is working as expected before you reboot the node to default.target
[anaconda root@centos-8 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 7.9G 1.8G 5.7G 24% /
/dev/sda1 488M 130M 323M 29% /boot
devtmpfs 2.4G 0 2.4G 0% /dev
/dev/mapper/live-rw 2.9G 1.8G 1.2G 61% /usr
tmpfs 2.5G 12K 2.5G 1% /dev/shm
Lastly we hope the steps from the article to resize root LVM partition on RHEL/CentOS 7/8 Linux was helpful.