Extend a partition in Ubuntu

I recently upgraded one of my CloudAtCost servers from Developer 1 to Developer 3 because I needed more disk space. The upgrade got off to a rocky start. It took a week for the work to actually get done and then I was informed that I needed to extend my partition. I had no idea how to do that, and if CloudAtCost’s tech support did, they weren’t sharing.

As it turns out, this is all pretty easy. This document simply condenses the instructions I found here for my own future reference.

0. Back up your work!

I like rsync. The following

rsync -azP daniel@example.com:/opt/git .

copies everything out of the hypothetical git repositories on my server to my local machine.

1. Take a look at the partition table

Login to your server.

ssh daniel@example.com

You’ll need to know where the new partition lives:

sudo fdisk -l /dev/sda

After the next step is complete, there will be a new device in the list (probably at /dev/sda6)

Also, make note of the file system’s current size:

df -hT

This will be helpful in verifying that the following steps actually worked.

2. Create a new logical partition

sudo cfdisk

From the interface provided:

  1. Select the unallocated space
  2. Select New
  3. Select Logical and allocate all the available space (shown by default)
  4. Select Type and enter 8E
  5. Select Write, type yes, and hit Enter
  6. Quit cfdisk
  7. Reboot the server (sudo reboot)

3. Take another look at the partition table

Once rebooted and re-logged in, run

sudo fdisk -l /dev/sda

and verify that there is, in fact, a new device that wasn’t there before (again, probably mounted at /dev/sda6). If there isn’t, you’re out of luck, because this is all just magic to me.

4. Create a new physical volume

Assuming the new device lives at /dev/sda6

sudo pvcreate /dev/sda6

5. Extend the existing volume group

sudo vgdisplay

Make note of the name associated with VG Name. Mine was called ubuntu1204-vg even though I’m running Ubuntu 14.04.

sudo vgextend ubuntu1204-vg /dev/sda6

6. Extend the logical volume

sudo lvdisplay

Make note of the path associated with LV Name used for the root filesystem (mine was /dev/ubuntu1204-vg/root)

sudo lvextend -l 100%FREE /dev/ubuntu1204-vg/root

6. Extend the file system

sudo resize2fs /dev/ubuntu1204-vg/root

7. Verify

df -hT

should reveal a file system much bigger than what you say in Step 1.

The preceding steps were ripped off from these guys, who, in turn, ripped off their info from here.