How To : Extend ext4 partition Linux (Centos 7)

  Linux

WARNING ! BEFORE PROCEED, THIS GUIDE IS ONLY FOR NEWLY PROVISIONED LINUX VM, ANY LIVE ENVIRONMENT REQUIRE DIFFERENT SET OF STEPS

Once you have provision a Linux VM from a template and configured the network.

The default partition size for the VM is : 16 GB

[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 16G 1.7G 14G 12% /
devtmpfs 909M 0 909M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.8M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 0 184M 0% /run/user/0
[root@centos7 ~]#

Customer should have their own required HDD size, and you should make sure you have changed it on the VM settings.

For this guide, the customer would want 200GB for their VM.

Enter the partitioning tools fdisk :

[root@centos7 ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

use p to print the current partition :

Command (m for help): p
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a25aa
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 33554431 16776192 83 Linux

use d to delete the current partition :

Command (m for help): d
Selected partition 1
Partition 1 is deleted

use n to create a new partition and follow as below :

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-419430399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399):
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set

use w to write/save the partition :

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Once we have resize the partition, we need to either reboot or run below command to make sure it take effect before resizing the file system.

[root@centos7 ~]# partprobe /dev/sda

Once done, run below command to resize the filesystem to follow the new partition size.

[root@centos7 ~]# resize2fs /dev/sda1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 25
The filesystem on /dev/sda1 is now 52428544 blocks long.
[root@centos7 ~]#

run df -h to double check whether it is successfully.

[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 197G 1.7G 187G 1% /
devtmpfs 909M 0 909M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.8M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 0 184M 0% /run/user/0
[root@centos7 ~]#

As we can see above we successfully resize the partition for root partition.

To double confirm everything is okay, run reboot command :

init 6

If the VM boot fine after reboot, and the new size is there. You can proceed any other task for the VM.