Increase Disk Size for a Linux VM

If you are a Linux user you may have come across an issue where after installing several repositories and programs you run out of disk space! that is such a pain that you cannot do anything until you increase size as you will be thrown constant error reminders that system is running out of disk space! In this article, I will show you how you can increase the size of the disk for a Ubuntu VM running on a VMware Workstation. You can apply the same method for any other flavor of Linux.

This is how my disk was configured with 15GB assigned to ext4 (/dev/sda4) and then I had a 500MB efi partition, 5GB of Swap. The 15GB was quickly eaten away as I was installing programs and then I hit the brick wall. This is how I increased the size of my disk from 20G to 40G.

First of all let us have a look the version I am running:

ash@ash-ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.10
Release: 21.10
Codename: impish
ash@ash-ubuntu:~$

I shut down the VM and from the VMware settings increased the size from 20G to 40G and then started the machine. Now I have to add that space to the partition so it can be used.

I thought about installing ‘gparted’ which is a graphical UI for re-sizing but it was too late! System won’t allow me to install anything as I had very minimum disk space.

ash@ash-ubuntu:~$ sudo apt install gparted
[sudo] password for ash:
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
The following packages were automatically installed and are no longer required:
chromium-codecs-ffmpeg-extra gstreamer1.0-vaapi libgstreamer-plugins-bad1.0-0 libva-wayland2
Use ‘sudo apt autoremove’ to remove them.
The following additional packages will be installed:
gparted-common
Suggested packages:
dmraid gpart jfsutils kpartx mtools reiser4progs reiserfsprogs udftools xfsprogs
The following NEW packages will be installed
gparted gparted-common
0 to upgrade, 2 to newly install, 0 to remove and 0 not to upgrade.
Need to get 483 kB of archives.
After this operation, 2,096 kB of additional disk space will be used.
E: You don’t have enough free space in /var/cache/apt/archives/.

I used fdisk to re-size my partitions and it is quite straight forward.

ash@ash-ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 389M 2.0M 388M 1% /run
/dev/sda4 15G 14G 78M 100% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 477M 5.3M 471M 2% /boot/efi
tmpfs 389M 1.3M 388M 1% /run/user/1000
/dev/sr0 3.0G 3.0G 0 100% /media/ash/Ubuntu 21.10 amd64

ash@ash-ubuntu:~$ cat /proc/partitions
major minor #blocks name

7 0 4 loop0
7 1 56840 loop1
7 2 158332 loop2
7 3 253856 loop3
7 4 158324 loop4
7 5 55524 loop5
7 6 63396 loop6
7 7 63392 loop7
11 0 3043440 sr0
8 0 41943040 sda *** This shows my Partition size has increased from 20G to 40G
8 1 4096 sda1
8 2 488448 sda2
8 3 5344256 sda3
8 4 15132672 sda4 *** This is where I need to add space and increase size

7 8 101824 loop8
7 9 66776 loop9
7 10 113140 loop10
7 11 248160 loop11
7 12 55540 loop12
7 13 186836 loop13
7 14 541308 loop14
7 15 186920 loop15

ash@ash-ubuntu:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 55.5M 1 loop /snap/core18/2284
loop2 7:2 0 154.6M 1 loop /snap/firefox/912
loop3 7:3 0 247.9M 1 loop /snap/gnome-3-38-2004/87
loop4 7:4 0 154.6M 1 loop /snap/firefox/886
loop5 7:5 0 54.2M 1 loop /snap/snap-store/557
loop6 7:6 0 61.9M 1 loop /snap/core20/1328
loop7 7:7 0 61.9M 1 loop /snap/core20/1270
loop8 7:8 0 99.4M 1 loop /snap/core/11993
loop9 7:9 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop10 7:10 0 110.5M 1 loop /snap/core/12603
loop11 7:11 0 242.3M 1 loop /snap/gnome-3-38-2004/76
loop12 7:12 0 54.2M 1 loop /snap/snap-store/558
loop13 7:13 0 182.5M 1 loop /snap/storage-explorer/31
loop14 7:14 0 528.6M 1 loop /snap/pycharm-community/267
loop15 7:15 0 182.5M 1 loop /snap/storage-explorer/30
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 4M 0 part
├─sda2 8:2 0 477M 0 part /boot/efi
├─sda3 8:3 0 5.1G 0 part [SWAP]
└─sda4 8:4 0 14.4G 0 part /
sr0 11:0 1 2.9G 0 rom /media/ash/Ubuntu 21.10 amd64
ash@ash-ubuntu:~$
ash@ash-ubuntu:~$

ash@ash-ubuntu:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (41943039 != 83886079) will be corrected by write.

Command (m for help): p

Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: VMware Virtual S
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: 16759726-96A2-4E87-9B02-537B7014B9FB

Device Start End Sectors Size Type
/dev/sda1 2048 10239 8192 4M BIOS boot
/dev/sda2 10240 987135 976896 477M EFI System
/dev/sda3 987136 11675647 10688512 5.1G Linux swap
/dev/sda4 11675648 41940991 30265344 14.4G Linux filesystem

Command (m for help): m

Help:

GPT
M enter protective/hybrid MBR

Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
x extra functionality (experts only)

Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes

Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table

Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: VMware Virtual S
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: 16759726-96A2-4E87-9B02-537B7014B9FB

Device Start End Sectors Size Type
/dev/sda1 2048 10239 8192 4M BIOS boot
/dev/sda2 10240 987135 976896 477M EFI System
/dev/sda3 987136 11675647 10688512 5.1G Linux swap
/dev/sda4 11675648 41940991 30265344 14.4G Linux filesystem

Command (m for help): d
Partition number (1-4, default 4): 4

Partition 4 has been deleted.

Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: VMware Virtual S
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: 16759726-96A2-4E87-9B02-537B7014B9FB

Device Start End Sectors Size Type
/dev/sda1 2048 10239 8192 4M BIOS boot
/dev/sda2 10240 987135 976896 477M EFI System
/dev/sda3 987136 11675647 10688512 5.1G Linux swap

Command (m for help): n
Partition number (4-128, default 4): 4
First sector (11675648-83886046, default 11675648):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (11675648-83886046, default 83886046):

Created a new partition 4 of type ‘Linux filesystem’ and of size 34.4 GiB.
Partition #4 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): w

The partition table has been altered.
Synching disks.

ash@ash-ubuntu:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 55.5M 1 loop /snap/core18/2284
loop2 7:2 0 154.6M 1 loop /snap/firefox/912
loop3 7:3 0 247.9M 1 loop /snap/gnome-3-38-2004/87
loop4 7:4 0 154.6M 1 loop /snap/firefox/886
loop5 7:5 0 54.2M 1 loop /snap/snap-store/557
loop6 7:6 0 61.9M 1 loop /snap/core20/1328
loop7 7:7 0 61.9M 1 loop /snap/core20/1270
loop8 7:8 0 99.4M 1 loop /snap/core/11993
loop9 7:9 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop10 7:10 0 110.5M 1 loop /snap/core/12603
loop11 7:11 0 242.3M 1 loop /snap/gnome-3-38-2004/76
loop12 7:12 0 54.2M 1 loop /snap/snap-store/558
loop13 7:13 0 182.5M 1 loop /snap/storage-explorer/31
loop14 7:14 0 528.6M 1 loop /snap/pycharm-community/267
loop15 7:15 0 182.5M 1 loop /snap/storage-explorer/30
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 4M 0 part
├─sda2 8:2 0 477M 0 part /boot/efi
├─sda3 8:3 0 5.1G 0 part [SWAP]
└─sda4 8:4 0 34.4G 0 part /
sr0 11:0 1 2.9G 0 rom /media/ash/Ubuntu 21.10 amd64

ash@ash-ubuntu:~$ sudo resize2fs /dev/sda4
[sudo] password for ash:
resize2fs 1.46.3 (27-Jul-2021)
Filesystem at /dev/sda4 is mounted on /; online resizing required
old_desc_blocks = 2, new_desc_blocks = 5
The filesystem on /dev/sda4 is now 9026299 (4k) blocks long.

ash@ash-ubuntu:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 55.5M 1 loop /snap/core18/2284
loop2 7:2 0 154.6M 1 loop /snap/firefox/912
loop3 7:3 0 247.9M 1 loop /snap/gnome-3-38-2004/87
loop4 7:4 0 154.6M 1 loop /snap/firefox/886
loop5 7:5 0 54.2M 1 loop /snap/snap-store/557
loop6 7:6 0 61.9M 1 loop /snap/core20/1328
loop7 7:7 0 61.9M 1 loop /snap/core20/1270
loop8 7:8 0 99.4M 1 loop /snap/core/11993
loop9 7:9 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop10 7:10 0 110.5M 1 loop /snap/core/12603
loop11 7:11 0 242.3M 1 loop /snap/gnome-3-38-2004/76
loop12 7:12 0 54.2M 1 loop /snap/snap-store/558
loop13 7:13 0 182.5M 1 loop /snap/storage-explorer/31
loop14 7:14 0 528.6M 1 loop /snap/pycharm-community/267
loop15 7:15 0 182.5M 1 loop /snap/storage-explorer/30
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 4M 0 part
├─sda2 8:2 0 477M 0 part /boot/efi
├─sda3 8:3 0 5.1G 0 part [SWAP]
└─sda4 8:4 0 34.4G 0 part / <<<<<<<<<<<< Partition increased
sr0 11:0 1 2.9G 0 rom /media/ash/Ubuntu 21.10 amd64
ash@ash-ubuntu:~$
ash@ash-ubuntu:~$

ash@ash-ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 389M 2.0M 387M 1% /run
/dev/sda4 34G 14G 19G 42% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 477M 5.3M 471M 2% /boot/efi
tmpfs 389M 1.3M 388M 1% /run/user/1000
/dev/sr0 3.0G 3.0G 0 100% /media/ash/Ubuntu 21.10 amd64
ash@ash-ubuntu:~$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: