Extend Linux Partition

The Scenario

My Debian (Jessie 8.4) server has a 20GB root partition. It is a guest VM running in a VMware ESXi4.1 cluster.

The Problem

The root partition of the Debian server is only 20GB and the application hosted on this server won’t start as it is reporting the disk is full. There are no files i can delete from the disk so I need to expand it.

The Solution

There are two parts to this solution;

  1. Increase the vmdk file of the VM.
  2. Increase the disk size within the OS itself to utilise all the new disk capacity.

Phase 1: Increase the VM guest disk (.VMDK) file
In the vclient UI right click the VM and select settings then the hard disk you want to expand. Under disk provisioning increase the disk provisioned size to the required size. NB If this option is greyed out it is because there is at least one snapshot of that disk, you will have to delete all snapshots before proceeding).

vm_disk_size_orig

In my case I increased the disk to 50GB, then click OK to have disk increased.

Phase 2: Backup Data on Disk

Whilst I have never experienced any data loss completing this operation, any disk reconfiguration does present a risk. Have a full data backup at the ready before proceeding to Phase 3.

Phase 3: Increase the disk within the OS
At this point the OS has no idea you have expanded the capacity of its disk. You need to use the FDISK command to do this.

Step 1 – Check Operating System Detects New Disk Size:
Confirm that the disk has increased by running lsblk

lsblk

In this example you can see that sda is the disk, which has been increased from 20GB on the VMware console to 50GB. The next step is to increase the partition size of /dev/sda1 to use all of this additional space

Step 2 – Remove Swap:
First unmount any Swap mount using swapoff -a

Step 3 – Print Partition Table:
Run fdisk /dev/sda (or whatever your main disk is called – see above). Then press P to print the Partition table. In this examples there is a single partition called /dev/sda1 . In this example it’s also importnat note that the * in the boot column, (no prizes for guessing this is the boot partition). If your partition is also a boot partition you will require an extra step once the new partition is created (see Step 6)

fdisk

Important! The key number out of this entire list is the start number (in this example 2048) as you need this later in this process

Step 4 – Delete the Current Partition:
Yes, you heard me correctly, in order to expand a Linux partition you have to delete the existing one, then create a new larger partition. Please note that your data is secure during this process (though do have your backup at the ready).

Press d to delete /dev/sda1 partition. Upon pressing d you’ll be asked for the Id number of the partition you want to delete. In this example I want to delete /dev/sda1 which has the Id 83. If I had more than one partition I would have to stipulate Id 83 and press enter. However as this is my only partition it is automatically selected.

Important! DO NOT press w at this point, wait until the new partition has been created later in the process.

Press p to confirm /dev/sda1 has been deleted

Step 5 – Create a New Partition:
Press n to create a new primary partition. Select partition number 1, the start sector 2048 (the same start number as shown above) and select the default last sector i.e. the whole disk.

new-part1

Step 6 – Make New Partition Bootable:
Press a to toggle the bootable flag on the new /dev/sda1

bootable

Step 7  – Write Changes
Press w to write the new partition table to disk. You’ll get a message telling that the kernel couldn’t re-read the partition table because the device is busy, but that it will applied at the next reboot.

write

Reboot the server with the reboot command.

Step 8 – Resize Partition

At this point the partition knows to use the full size of the new disk but it has yet to complete this process. The final task is to initiate the resizing of the partition.

Login as root and run resize2fs /dev/sda1 – this form will default to making the filesystem to take all available space on the partition. NB This process can take several minute depending on the size of the new disk.

Run df -h to confirm your new partition (/dev/sda1) is the size required.

 

Disclaimer: provided “AS IS” with no warranties and confer no rights

Advertisements

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 )

Google+ photo

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

Connecting to %s