Ubuntu expand disk space – Command Line

Warning: Be extremely careful when making changes to partitions and disk as it can lead to broken systems and lost data. Make sure you have a backup.

This scenario is done on a basic Ubuntu install. No fancy LVM stuff going on. If you need that, refer to here

Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 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
Disklabel type: dos
Disk identifier: 0x2062ec28
Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 65011711 65009664   31G 83 Linux
/dev/sda2       65013758 67106815  2093058 1022M  5 Extended
/dev/sda5       65013760 67106815  2093056 1022M 82 Linux swap / Solaris

From the above output of fdisk -l, we see that the disk has 64GiB available, but the primary partition is only 31G. To make the primary partition larger we need to

  • Run fdisk “fdisk /dev/sda”
  • Delete partitions 2 and 5,
  • Delete Partition 1
  • Create Partition 1 again on the same starting boundary
  • Put the end boundary close to the end so we end up with ~62GiB for that partition
  • Recreate sda2, the 1GiB extended partition
  • Write changes to disk
  • Run resize2fs to resize the filesystem

You may need to boot up in recovery to get this command working. Also if you boot up in recovery, you’ll need to remount the root / partition read/write. More info here.

resize2fs /dev/sda1

Helpful Links
https://access.redhat.com/articles/1190213
https://access.redhat.com/articles/1196353

Parted resizing notes

Launch parted with the following command.  Specify the disk you want to modify.  In this case /dev/sda.

sudo parted /dev/sda

Print partition and available free space.  If you just run print, it will not show you the available free space.

(parted) print free

You can resize, or rather extend a partition using the “resizepart” command inside parted.  Command syntax is as follows

(parted) resizepart PartitionNumber End

Example: Resize partition 1 to 30GB.  Note you’ll need to specify the end part with the GB, otherwise, you’ll be shrinking the partition or making a mess…  You can see what space is available with the “print free” command above

(parted) resizepart 1 30GB

After you have run the resizepart in parted, you’ll need to grow the filesystem, so run one of the following commands from the local shell.

resize2fs /dev/sda1

Or for XFS partitions

xfs_growfs /dev/sda1

You can check the partition size with

df -h

or

sudo fdisk -l /dev/sda