- Steps
- Resize the root partition
- Resize the file system
- Force a filesystem check on next reboot
- Online resize of a root ext4 file system – increase the space
- Summary
- STEP 1) Expand the partition, which holds the root partition.
- STEP 2) Resize the file system, on which we expanded the partition.
- Bonus – you can force check the file system on the next reboot
- Bonus 2
- 6 thoughts on “Online resize of a root ext4 file system – increase the space”
Steps
Before we can resize the root file system, we need the OS to detect the new size of the disk.
In this case, we use the fdisk command to show the current disk size. Which in this case is 120GB. (I know a 120GB root file system is not a good idea, but this is a lab.)
[email protected]:~# fdisk -l /dev/sda Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: DDC91865-B329-443D-AFFF-0898C341D65C Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 4198399 4194304 2G Linux filesystem /dev/sda3 4198400 251656191 247457792 118G Linux filesystem [email protected]:~#
We now need to tell the OS to scan all the disks for changes. We do this by running a script I created called DiskScan. This script walks through all the disks and looks for changes.
curl https://raw.githubusercontent.com/mattmattox/DiskScan/master/rescan_disks.sh | bash
[email protected]:~# curl https://raw.githubusercontent.com/mattmattox/DiskScan/master/rescan_disks.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 363 100 363 0 0 1553 0 --:--:-- --:--:-- --:--:-- 1551 Scan for new disk(s). host0 host1 host10 host11 host12 host13 host14 host15 host16 host17 host18 host19 host2 host20 host21 host22 host23 host24 host25 host26 host27 host28 host29 host3 host30 host31 host32 host4 host5 host6 host7 host8 host9 Scan for disk size change. sda [email protected]:~#
We can now see that the disk size has changed. Note, we can ignore the GPT error as we’ll be fixing it as part of the next step.
[email protected]:~# fdisk -l /dev/sda GPT PMBR size mismatch (251658239 != 419430399) will be corrected by write. The backup GPT table is not on the end of the device. Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: DDC91865-B329-443D-AFFF-0898C341D65C Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 4198399 4194304 2G Linux filesystem /dev/sda3 4198400 251656191 247457792 118G Linux filesystem [email protected]:~#
Resize the root partition
At this point, the disk has the available space we need. We can now resize the root partition.
[email protected]:~# parted /dev/sda GNU Parted 3.4 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 167772160 blocks) or continue with the current setting? Fix/Ignore? Fix Model: VMware Virtual disk (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB bios_grub 2 2097kB 2150MB 2147MB ext4 3 2150MB 129GB 127GB (parted) resizepart 3 -1 (parted) p Model: VMware Virtual disk (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB bios_grub 2 2097kB 2150MB 2147MB ext4 3 2150MB 215GB 213GB (parted) q Information: You may need to update /etc/fstab. [email protected]:~#
Note: The -1 is the size of the partition. This tell parted to use the remaining space on the disk.
Resize the file system
At this point, we have expanded the root partition to the new size. We can now resize the file system.
[email protected]:~# resize2fs /dev/sda3 resize2fs 1.46.5 (30-Dec-2021) Filesystem at /dev/sda3 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 13 The filesystem on /dev/sda3 is now 51428620 (4k) blocks long.
Force a filesystem check on next reboot
At this point, we have expanded the file system to the new size. We can now force a filesystem check on the next reboot.
Note: This step is optional but is recommended.
Online resize of a root ext4 file system – increase the space
Here you can see how to online resize your root ext4 file system. The free space of your partition will be increased after the operation. The size of the root file system will grow not shrink. Of course, this could have been any other partition, not exactly the root one, but in most cases, such operations on the root are the more complex and dangerous – SO ALWAYS do backups before such operations!
All services work properly and no shut down of services, no reboot, or umount is required during the resize operation.
Still, we rebooted the server once to force check the file system as a precaution, because it was possible and this server was not in production. The reboot of the server after this kind of resizing is not mandatory.
The following method is tested on CentOS 7, Ubuntu 16 LTS, and Gentoo with kernel 4.15 kernel. So we can assume you may have no problems if your system is newer than ours.
Summary
- Partition resize – Use resizepart in parted command. All Linux distributions have this package with the same name as the needed command “parted”
- File system resize – Use resize2fs from the E2fsprogs package. All Linux distributions include this package mostly with the same name of the package.
STEP 1) Expand the partition, which holds the root partition.
Let’s assume you have changed your disk and now there is more unallocated space to be used or somehow the space of the disk is increased. Look below for a real-world example with one of our virtual servers.
root@srv1 ~ # parted /dev/sda GNU Parted 3.2 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: Model: ATA Samsung SSD 850 (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB bios_grub 2 2097kB 4096MB 4094MB linux-swap(v1) 3 4096MB 24.0GB 19.9GB ext4 (parted) resizepart 3 -1 Warning: Partition /dev/sda3 is being used. Are you sure you want to continue? parted: invalid token: -1 Yes/No? Yes End? [24.0GB]? -1 (parted) p Model: Model: ATA Samsung SSD 850 (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB bios_grub 2 2097kB 4096MB 4094MB linux-swap(v1) 3 4096MB 215GB 211GB ext4 (parted) q Information: You may need to update /etc/fstab.
As you can see from the first print command the partition number 3 is 19.9GB and after the resize command with “-1” is 211GB. There is a warning about the partition is used, but it is normal and not critical.
STEP 2) Resize the file system, on which we expanded the partition.
You need to install E2fsprogs. All Linux distributions have this package, here are some of them:
After installing the e2fsprogs package you will have the online ext4 resizing tool – resize2fs.
root@srv ~ # resize2fs /dev/sda3 resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/sda3 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 13 The filesystem on /dev/sda3 is now 51428620 (4k) blocks long.
Check if everything is OK with
root@srv ~ # dmesg|grep EXT4 [ 449.330140] EXT4-fs (vda3): resizing filesystem from 4859392 to 51428620 blocks [ 449.936044] EXT4-fs (vda3): resized filesystem to 51428620 root@srv ~ # df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 798M 3.5M 795M 1% /run /dev/sda3 193G 3.4G 182G 2% / tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 798M 0 798M 0% /run/user/0
Bonus – you can force check the file system on the next reboot
Probably it is a good idea to force check the file system integrity on the next boot. This step is not mandatory and you may skip it.
For Ubuntu you can do:
root@srv ~ # touch /forcefsck root@srv ~ # reboot
Bonus 2
Fixing the GPT. Newer versions may display warning the GPT table is not using the whole disk space and to fix it. Just type fix to add the new unallocated disk space:
root@srv ~ # parted /dev/sda GNU Parted 3.2 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 188743680 blocks) or continue with the current setting? Fix/Ignore? Fix Model: Virtio Block Device (virtblk) Disk /dev/sda: 118GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB bios_grub 2 2097kB 17.2GB 17.2GB ext4 3 17.2GB 21.5GB 4293MB linux-swap(v1) (parted)
6 thoughts on “Online resize of a root ext4 file system – increase the space”
Yes. In fact, most of the time this resizing is used in virtual environments like Qemu images qcow2, VirtualBox images, VMWare disks, and so on. Stop your machine (power off), increase the size of the disk and follow the tutorial. Of course, backup first!
Worked great on a virtual Ubuntu 20.04 running on ESX But you may have to root@vm: /root# echo “1” > /sys/class/block/sdX/device/rescan after changing disk size in hypervisor, before parted knows about the change.