Extend FS Partition after Resizing Disk Size in Linux

From Gejoreuy
Jump to navigation Jump to search

Let say after upgrading disk size in our virtual machine, we got condition like this:

root@gejoreuy:~# lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0     7:0    0  63.3M  1 loop /snap/core20/1822
loop1     7:1    0  63.3M  1 loop /snap/core20/1828
loop2     7:2    0  91.9M  1 loop /snap/lxd/24061
loop3     7:3    0  91.8M  1 loop /snap/lxd/23991
loop4     7:4    0  49.8M  1 loop /snap/snapd/17950
loop5     7:5    0  49.9M  1 loop /snap/snapd/18357
sda       8:0    0   256G  0 disk
├─sda1    8:1    0 255.9G  0 part /
├─sda14   8:14   0     4M  0 part
└─sda15   8:15   0   106M  0 part /boot/efi
sdb       8:16   0   1.2T  0 disk
└─sdb1    8:17   0   1.2T  0 part /mnt
sdc       8:32   0     1T  0 disk
└─sdc1    8:33   0   200G  0 part /pg


In this case, we see /dev/sdc is having 1TB disk size while /dev/sdc1 for /pg is only 200GB.
We're going to extend /dev/sdc1 for /pg to use all free disk space in /dev/sdc without loosing the data, of course.
What we can do is simple. Just run growpart /dev/sdc 1 and then continued with resize2fs /dev/sdc1.

As example, first, check the free disk space in /dev/sdc by running this command:

root@gejoreuy:~# parted /dev/sdc print free
Model: Msft Virtual Disk (scsi)
Disk /dev/sdc: 1100GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        1024B   1049kB  1048kB           Free Space
 1      1049kB  215GB   215GB   primary  ext4
        215GB   1100GB  885GB            Free Space


Extend the disk partition size by run growpart command:

root@gejoreuy:~# growpart /dev/sdc 1
CHANGED: partition=1 start=2048 old: size=419428352 end=419430400 new: size=2147481567 end=2147483615
root@gejoreuy:/home/operations# resize2fs /dev/sdc1
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/sdc1 is mounted on /pg; on-line resizing required
old_desc_blocks = 25, new_desc_blocks = 128
The filesystem on /dev/sdc1 is now 268435195 (4k) blocks long.


Then continue to extend the fs by running resize2fs for fs partition:

root@gejoreuy:~# resize2fs /dev/sdc1
resize2fs 1.45.5 (9-Mar-2023)


Check if the FS disk space is already increased.

root@gejoreuy:/home/operations# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       248G  8.5G  240G   4% /
devtmpfs         63G     0   63G   0% /dev
tmpfs            63G  244K   63G   1% /dev/shm
tmpfs            13G  1.1M   13G   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            63G     0   63G   0% /sys/fs/cgroup
/dev/sdc1      1007G   64G  901G   7% /pg
/dev/loop0       64M   64M     0 100% /snap/core20/1822
/dev/loop1       64M   64M     0 100% /snap/core20/1828
/dev/loop2       92M   92M     0 100% /snap/lxd/24061
/dev/sda15      105M  5.2M  100M   5% /boot/efi
/dev/loop3       92M   92M     0 100% /snap/lxd/23991
/dev/loop4       50M   50M     0 100% /snap/snapd/17950
/dev/loop5       50M   50M     0 100% /snap/snapd/18357
/dev/sdb1       1.2T   28K  1.1T   1% /mnt
tmpfs            13G     0   13G   0% /run/user/1000