How to expand a disk manually on a Linux system

Normally we expand the disk and filesystem on a system with cloud-init but it can be this isn't working. The main reasons are because cloud-init is deinstalled or the operating system is too old which hasn't got cloud-init installed (in the early days we didn't use cloud-init to provision VPSs).

When doing this the partition to expand must be the only or last partition on the disk

When your disk is expanded but you can't see it in your system you can perform the following steps:

Check your disks

With 'lsblk' you can see the actual size of your disk and partitions

[user@host ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 20G 0 disk
`-vda1 253:1 0 10G 0 part /
vdb 253:16 0 1G 0 disk
`-vdb1 253:17 0 1024M 0 part [SWAP]

In this example you see the disk is 20G but the root-partition is only 10G. We have some unused space in there.

Install the tools

To expand the partition vda1 we need the tool growpart as this is the easiest and safest way to expand your partition.

Ubuntu/Debian

First update the apt-cache with 'apt-get update'

user@host:~$ sudo apt-get update
[sudo] password for user:
Hit:1 http://nl.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://nl.archive.ubuntu.com/ubuntu xenial-updates InRelease [99.8 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [99.8 kB]
Get:4 http://nl.archive.ubuntu.com/ubuntu xenial-backports InRelease [97.4 kB]
Fetched 297 kB in 0s (372 kB/s)
Reading package lists... Done

Next is to install the tools with 'apt-get install cloud-guest-utils'

user@host:~$ sudo apt-get install cloud-guest-utils
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
cloud-guest-utils
0 upgraded, 1 newly installed, 0 to remove and 100 not upgraded.
Need to get 0 B/15.1 kB of archives.
After this operation, 56.3 kB of additional disk space will be used.
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_US:en",
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LC_TERMINAL_VERSION = "3.4.19",
LC_TERMINAL = "iTerm2",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package cloud-guest-utils.
(Reading database ... 60483 files and directories currently installed.)
Preparing to unpack .../cloud-guest-utils_0.27-0ubuntu25.2_all.deb ...
Unpacking cloud-guest-utils (0.27-0ubuntu25.2) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up cloud-guest-utils (0.27-0ubuntu25.2) ...

CentOS/Almalinux/Rocky linux

Install the tools with 'yum install cloud-utils-growpart -y'

[user@host ~]# sudo yum install cloud-utils-growpart -y
[sudo] password for user: Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: centos.excellmedia.net * epel: epel.mirror.angkasa.id * extras: centos.excellmedia.net * updates: centos.excellmedia.net Resolving Dependencies --> Running transaction check ---> Package cloud-utils-growpart.noarch 0:0.29-5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: cloud-utils-growpart noarch 0.29-5.el7 base 27 k Transaction Summary ======================================================================================================================================================================== Install 1 Package Total download size: 27 k Installed size: 61 k Downloading packages: cloud-utils-growpart-0.29-5.el7.noarch.rpm | 27 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : cloud-utils-growpart-0.29-5.el7.noarch 1/1 Verifying : cloud-utils-growpart-0.29-5.el7.noarch 1/1 Installed: cloud-utils-growpart.noarch 0:0.29-5.el7 Complete!

Expand the partition

In the example we want to expand vda1 on disk vda as this partition is containing the root-filesystem. We can do the following

growpart <disk> <partition>

user@host:~$ sudo growpart /dev/vda 1
CHANGED: partition=1 start=2048 old: size=18968576 end=18970624 new: size=42735583,end=42737631

[user@host ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 20G 0 disk
`-vda1 253:1 0 20G 0 part /
vdb 253:16 0 1G 0 disk
`-vdb1 253:17 0 1024M 0 part [SWAP]

Expand the filesystem

The last step is to expand the filesystem so it also uses the complete partition. We can use 'resize2fs <partition>' for this (in our case resize2fs /dev/vda1)

user@host:~$ sudo resize2fs /dev/vda1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vda1 is now 5341947 (4k) blocks long.

user@host:~$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.6G 18G 9% /

This is it. At this point you can use all the space which is allocated to your system.

Was this article helpful?
4 out of 4 found this helpful

Comments

0 comments

Please sign in to leave a comment.

Articles in this section

See more