How to extend / (root) filesystem without reinstalling the operating system ?. Is it possible to extend the root filesystem without destroying data? Is it possible to extend on the fly? The answer is yes if you are using LVM. This article will walk you through extending the root filesystem on Redhat Linux 8 (RHEL 8 / CentOS 8). In this LAB, I have used VMware virtual machine where RHEL8 is up and running. My root disk size is 40GB and it got full. In any hypervisor or Cloud, you can extend the disk by stopping the VM or on the fly. This procedure is applicable to all the environments including public clouds (AWS, Azure, GCP).
Let’s quickly have look at the root volume details.
[root@okd4 ~]# df -h / Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_okd4-root 35G 35G 154M 100% / [root@okd4 ~]#
The above command output shows that VM is using LVM (/dev/mapper) for the root filesystem. Let’s look at the partition details.
[root@okd4 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 780M 0 rom nvme0n1 259:0 0 60G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 39G 0 part ├─rhel_okd4-root 253:0 0 35G 0 lvm / └─rhel_okd4-swap 253:1 0 4G 0 lvm [SWAP] [root@okd4 ~]#
Let’s look at the LVM details.
[root@okd4 ~]# pvs PV VG Fmt Attr PSize PFree /dev/nvme0n1p2 rhel_okd4 lvm2 a-- <39.00g 0 [root@okd4 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rhel_okd4 -wi-ao---- <35.00g swap rhel_okd4 -wi-ao---- 4.00g [root@okd4 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel_okd4 1 2 0 wz--n- <39.00g 0 [root@okd4 ~]#
Install packages:
1. To extend the disk partition, you need growpart utility. Let’s install the “growpart” package.
[lingesh@okd4 ~]$ sudo su - [root@okd4 ~]# yum install cloud-utils-growpart -y Updating Subscription Management repositories. Repository google-chrome is listed more than once in the configuration Last metadata expiration check: 3:19:54 ago on Tue 29 Mar 2022 07:50:58 PM IST. Dependencies resolved. ==================================================================================== Package Architecture Version Repository Size ==================================================================================== Installing: cloud-utils-growpart noarch 0.31-3.el8 rhel-8-for-x86_64-appstream-rpms 32 k Transaction Summary ==================================================================================== Install 1 Package Total download size: 32 k Installed size: 63 k Downloading Packages: cloud-utils-growpart-0.31-3.el8.noarch.rpm 63 kB/s | 32 kB 00:00 ------------------------------------------------------------------------------------ Total 63 kB/s | 32 kB 00:00 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : cloud-utils-growpart-0.31-3.el8.noarch 1/1 Running scriptlet: cloud-utils-growpart-0.31-3.el8.noarch 1/1 Verifying : cloud-utils-growpart-0.31-3.el8.noarch 1/1 Installed products updated. Installed: cloud-utils-growpart-0.31-3.el8.noarch Complete!
2. Run the “growpart” with help to understand various options.
[root@okd4 ~]# growpart -h growpart disk partition rewrite partition table so that partition takes up all the space it can options: -h | --help print Usage and exit --fudge F if part could be resized, but change would be less than 'F' bytes, do not resize (default: 1048576) -N | --dry-run only report what would be done, show new 'sfdisk -d' -v | --verbose increase verbosity / debug -u | --update R update the the kernel partition table info after growing this requires kernel support and 'partx --update' R is one of: - 'auto' : [default] update partition if possible - 'force' : try despite sanity checks (fail on failure) - 'off' : do not attempt - 'on' : fail if sanity checks indicate no support Example: - growpart /dev/sda 1 Resize partition 1 on /dev/sda must supply partition-number
Extend root disk & filesystem
1. Ensure that root disk size has been increased from the hypervisor. (ex: VirtualBox, VMware ). My current disk size is 40GB and I have increased it to 60GB.
2. Run the “growpart” command with the dry run option.
[root@okd4 ~]# growpart /dev/nvme0n1 2 -N CHANGE: partition=2 start=2099200 old: size=81786880 end=83886080 new: size=123729887 end=125829087 # === old sfdisk -d === label: dos label-id: 0xd2e428c6 device: /dev/nvme0n1 unit: sectors /dev/nvme0n1p1 : start= 2048, size= 2097152, type=83, bootable /dev/nvme0n1p2 : start= 2099200, size= 81786880, type=8e # === new sfdisk -d === label: dos label-id: 0xd2e428c6 device: /dev/nvme0n1 unit: sectors /dev/nvme0n1p1 : start= 2048, size= 2097152, type=83, bootable /dev/nvme0n1p2 : start= 2099200, size= 123729887, type=8e [root@okd4 ~]#
3. If no errors are reported in the dry run, you can execute the growpart command to extend the partition. Note: Partition 1 is a swap. I am passing “2” to represent partition 2.
[root@okd4 ~]# growpart /dev/nvme0n1 2 CHANGED: partition=2 start=2099200 old: size=81786880 end=83886080 new: size=123729887 end=125829087 [root@okd4 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 780M 0 rom nvme0n1 259:0 0 60G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 59G 0 part ├─rhel_okd4-root 253:0 0 35G 0 lvm / └─rhel_okd4-swap 253:1 0 4G 0 lvm [SWAP] [root@okd4 ~]# pvs PV VG Fmt Attr PSize PFree /dev/nvme0n1p2 rhel_okd4 lvm2 a-- <39.00g 0
4. You need to explicitly run the “pvresize” command to extend the PV.
[root@okd4 ~]# pvresize /dev/nvme0n1p2 Physical volume "/dev/nvme0n1p2" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized [root@okd4 ~]# [root@okd4 ~]# pvs PV VG Fmt Attr PSize PFree /dev/nvme0n1p2 rhel_okd4 lvm2 a-- <59.00g 20.00g [root@okd4 ~]#
5. You can see that vgs command shows 20GB of free space.
[root@okd4 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel_okd4 1 2 0 wz--n- <59.00g 20.00g [root@okd4 ~]#
6. Let’s extend the Logical Volume (lv) with available free space in the volume group. (100% represents available free space in the VG).
- -l – extend or set the logical volume size in units of logical extent
- –r – Resize the underlying filesystem together with the logical volume
[root@okd4 ~]# lvextend -r -l +100%FREE /dev/rhel_okd4/root Size of logical volume rhel_okd4/root changed from <35.00 GiB (8959 extents) to <55.00 GiB (14079 extents). Logical volume rhel_okd4/root successfully resized. meta-data=/dev/mapper/rhel_okd4-root isize=512 agcount=4, agsize=2293504 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=9174016, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=4479, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 9174016 to 14416896 [root@okd4 ~]#
7. We have successfully extended the root filesystem in RedHat 8 /CentOS 8.
[root@okd4 ~]# df -h / Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_okd4-root 55G 35G 21G 64% / [root@okd4 ~]#
Hope this article is informative to you.
Glossy says
Worked great in Rocky Linux 9! Only difference was that /dev/rhel_okd4/root was /dev/rl/root
yossi says
Thanks ,very good guide
Just to make sure ,when type lsblk : I see this :
sdb 8:16 0 558.9G 0 disk
├─sdb1 8:17 0 1G 0 part /boot
└─sdb2 8:18 0 557.9G 0 part
├─cl-root 253:0 0 50G 0 lvm /
├─cl-swap 253:1 0 4G 0 lvm [SWAP]
└─cl-home 253:2 0 503.9G 0 lvm /home
so by default centos8 installed all my data in cl-root ,which is only 50G ,not enough space
can I extend my workspace using cl-home 503G partition ,do I need to backup data before
Elisbao says
Thaks mann!
on the first try