Skip to content

Instantly share code, notes, and snippets.

@raphaelcastaneda
Last active April 17, 2018 00:30
Show Gist options
  • Save raphaelcastaneda/3942439508a0e9ffc3d658a0899c18e9 to your computer and use it in GitHub Desktop.
Save raphaelcastaneda/3942439508a0e9ffc3d658a0899c18e9 to your computer and use it in GitHub Desktop.

Resizing the primary partition on an Ubuntu machine

So you've added space to a VM but Ubuntu still shows the old size? The partition was created with a certain size, so that new space will just be unallocated until you add it to a partition. You could just add a new partition, but chances are you want the root partition to be bigger, yes?

The steps

Before proceeding, realize that there is a very real possibility that you will lose data if you mess this up. Still here? Cool.

Before you begin

  • You will need to reboot
  • I have not tested this with partitions "after" the one I am resizing. There was a swap partition, but I unswapped and deleted it.:
    • If I had to guess, I would say that this would not work if you had any partitions after the one you are resizing
  • This guide comes with no warranty. If you do this and you lose data, don't come crying to me.

Step 1: Resize the partition

Use fdisk to first delete the partition and then recreate it with a larger size.

$ sudo fdisk /dev/sda

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     9437183     4717568   83  Linux

Command (m for help): d
Selected partition 1

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    10485759     5241856   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)

Note in the above example that the starting position of the new partition is the same as the one we deleted. THIS IS CRITICAL! IF YOU DO NOT DO THAT, YOU WILL LOSE DATA!

To be on the safe side, you'll likely want to also use the a command to add the boot flag to this newly-created partition. Don't forget to hit w to write the parition table again.

Step 2: Reload the partition table

The safest thing to do here is reboot. Hold your breath. Say a little prayer.

Step 3: Resize the filesystem

Still not done... if you do df you'll still see the old size. The final step will resize the ext filesystem to the size of the partition

sudo resize2fs /dev/sda1

If all is well, you'll now have a big ol' partition to fill up with cat gifs or whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment