In this article I 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
I 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 Oracle VirtualBox installed on a Linux server.
I 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 I 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 I am using CentOS 8 so I 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 I 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 I have enabled SSH and configured network for my rescue mode, I 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 I hope the steps from the article to resize root LVM partition on RHEL/CentOS 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Hi, I am using windows and centos 8 stream dual boot. I want to extend the size of the centos root. So I shrank some memory from my windows and created a new partition using GParted. And in the rescue mode i created the physical volume of this partition and tried to use this to extend the root of the centos. When I tried to run e2fsck -f /dev/mapper/cs-root, it is not running and showing me superblock invalid, bad magic number in super block, run e2fsck -b 8193 and /dev/mapper/cs-root contains a xfs file system. Due to this i could not use resize2fs command even though i can use lvextend command. Due to have any solution for this problem?
Did you try performing a format on the newly created partition on the windows and then create a Physical Volume on top of it?
No, but I used “lvextend” and then used “fsadm resize /dev/mapper/cs-root”, when the filesystem is mounted. It successfully extended the size of the root.
Thank you for very good manual ! I did it step by step and everything works ! It’s not so easy but it’s understandable for me.
did it, works, root can have 2 physical extents groups…
Great, thanks for sharing this with us.
Thanks for the write up!
Following this instructions, I had trouble with trying to
resize / (root)
under/dev/mapper/vg_blah-lv_root
.But it was successfully done with parameter pointing to /dev/vg_blah-lv_root.
Error message when it could not resize via
/dev/mapper/vg_blah-lv
_root was : ”/usr/sbin/fsadm: execvp failed: Input/output error \n Filesystem check failed" despite I ran e2fsck -f /dev/mapper/vg_blah-lv_root
without any bad block before and after that.root lvm needs to be continuous on hard disk or not? Can we have the original root lvm, then the original swap or var or opt lvm and then the root lvm extended part?
If it is LVM then the scenario what you are explaining is not possible because LVM are always combined, we only extend VG to make sure LVM is extended. Even with non-lvm I don’t think we can have a setup with such combination
is this a theoretical question or you have a setup like this?
Can you show us lvdisplay –maps from root or other lvextend-ed lvm…
I’d like to see if it is continuous after lvextend… lvextend is of course possible just is some space is available in vg. But if we originally had root lvm, then swap lvm, then var lvm (all that on one disk, one pv, one vg) and then extened root, does –maps for root show 2 Physical extents groups. thanks
I don’t have the same setup any more. You can try this locally on any VM with your scenario to verify.
I came across the bad logic number error when going through step 4 and step 5. It blocked me here and I googled, not finding a solution that works for me.
You can try to repair the file system but in such cases most likely you have to first fix the hard disk before attempting the resize
If you have CentOS7, you should make sure if your filesystem is ext, not xfs.
You can check your filesystem with
If your filesystem is xfs, then step 4 and 5 will not work since those steps are for ext filesystem.