Linux extend file system after resize disk volume

last updated in Categories , , , ,

I increased the size of the volume in Linux using my cloud service providers’ online tool. My original disk size was 200GB, and I grew to 400GB. However, I only able to use 200GB and not 400GB. How do I resizing my disk and extend the file system resizing a disk volume?

After you increase the size of disk volume (such as AWS ESB and Disks under Google cloud and others service providers), you must use file system–specific commands to extend the file system to the larger size. In this tutorial, I am using Google cloud with the XFS file system, and we will extend a Linux File System after resizing a volume. The following instructions were tested on CentOS, RHEL, Ubuntu, and Debian Linux but should work with other Linux distros too.

ADVERTISEMENTS


Linux extend file system after resize disk volume

First you need to find information about the disks. As soon as Linux detects disk size change, you will see a message in your log as follows using the dmesg command:
sudo dmesg
sudo dmesg | more

[   12.834446] bpfilter: Loaded bpfilter_umh pid 1037
[   12.917195] new mount options do not match the existing superblock, will be ignored
[262733.527584] sd 0:0:2:0: [sdb] 838860800 512-byte logical blocks: (429 GB/400 GiB)
[262733.527587] sd 0:0:2:0: [sdb] 4096-byte physical blocks
[262733.528263] sdb: detected capacity change from 214748364800 to 429496729600

Please note down the disk name sdb.

Warning: Before you type any one of the following commands, make a backup of all your data. It is crucial to create a snapshot of the disk and backup data somewhere else safely. The author and nixCraft site is not responsible for data loss caused when you extend a Linux file system after resizing a block storage/volume.

Verify new disk size

Type the following fdisk command:
sudo fdisk -l /dev/sdb
Sample outputs:

Disk /dev/sdb: 400 GiB, 429496729600 bytes, 838860800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xd3a4f1f7

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 419430399 419428352  200G 83 Linux

How to list block devices

One can run the following lsblk command to show information about the block devices attached to your cloud VM:
sudo lsblk

Linux list the disks that are attached to your VM instance
lsblk command in action showing information about the block devices attached to my Linux server

Find the current disk size and partiton type

Run the following df command and file command:
df -HT
df -HT | grep /backup1
df -HT | grep /dev/sdb1
sudo file -s /dev/sdb*

/dev/sdb:  DOS/MBR boot sector; partition 1 : ID=0x83, ...
/dev/sdb1: SGI XFS filesystem data 

So now we know that xfs is the file system for /dev/sdb1 partition.

Unmount the partition if mounted

Run the following command:
sudo umount /dev/sdb1

Extending a Linux partition

Since our resized volume has a partition and the partition does not reflect the new size (400 GB) of the disk volume, we use the growpart command on Linux to extend the partition size.

How to use growpart to resize your image partition

The syntax is:
growpart /dev/DEVICE_NAME    PARTITION_NUMBER
Please note that there is a space between the DEVICE_NAMEand the PARTITION_NUMBER. For example:
sudo growpart --dry-run /dev/sdb 1
Sample outputs:

CHANGE: partition=1 start=2048 old: size=419428352 end=419430400 new: size=838858719,end=838860767
# === old sfdisk -d ===
label: dos
label-id: 0xd3a4f1f7
device: /dev/sdb
unit: sectors

/dev/sdb1 : start=        2048, size=   419428352, type=83
# === new sfdisk -d ===
label: dos
label-id: 0xd3a4f1f7
device: /dev/sdb
unit: sectors

/dev/sdb1 : start=        2048, size=   838858719, type=83

The --dry-run option only reports what would be done. To resize it, run:
sudo growpart /dev/sdb 1

Linux extend the partition on the disk volume using growpart command
We used growpart command to grow the partition

You can verify that the partition size increased using the lsblk command command again:
sudo lsblk

Mount the file system again

Execute the following command:
sudo mount /dev/sdb1 /backup1/
Verify disk size:
df -H
Sample outputs:

Filesystem      Size  Used Avail Use% Mounted on
udev            501M     0  501M   0% /dev
tmpfs           103M  938k  102M   1% /run
/dev/sda1        11G  3.1G  7.2G  30% /
tmpfs           515M     0  515M   0% /dev/shm
tmpfs           5.3M     0  5.3M   0% /run/lock
tmpfs           515M     0  515M   0% /sys/fs/cgroup
/dev/sda15      110M  3.8M  106M   4% /boot/efi
tmpfs           103M     0  103M   0% /run/user/1001
/dev/sdb1       215G   36G  180G  17% /backup1

Extending the file system

As you can size mounted file system still shows 200GB total size for the /dev/sdb1 partition.

How to extend the XFS file system on Linux

The syntax is:
sudo xfs_growfs /mount_point
sudo xfs_growfs /backup1/

Linux extend file system using xfs_growfs for XFS

How to extend the ext4 file system on Linux

The syntax is:
sudo resize2fs /dev/DEVICE_NAME_PARTITION_NUMBER
## note /dev/sdb1 must be unmouted ##
sudo resize2fs /dev/sdb1

Verification – Extending a Linux file system after resizing a disk volume

Simply run the df command to verify that your disk sized increased from 200GB to 400GB:
df -HT
Linux resizing your persistent disk in cloud server

Conclusion

Most cloud service provider allows you to add storage capacity to your VM. One can extend such block storage under Linux when the need arises as your business grows. You learned how to resize the disk volume of an instance from 200 GB to 400 GB using various Linux command-line options.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


ADVERTISEMENTS

1 comment

    Still, have a question? Get help on our forum!