Resize VPS

By default the VPS has pre-defined values on CPU, RAM and disk space. With most VPS providers, you have the option to upgrade the VPS specification. Typically this comes as a bundle upgrade where by all 3 aspects will be upgraded at once. The upgrade of CPU and RAM is done relatively easy. Its simply a process of upgrading the VPS in the backend and then performing a reboot so that the new hardware is detected by the OS.

The process of upgrading the disk space is a little tricky as the filesystem already has the partition layout defined. We would need to change the partition layout for the VPS in order to make full use of the disk space on the server. To determine the current disk space we can run the following commands:

root@mysuperweb.co.uk:/home# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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
Disk identifier: 0x000ea485

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    33554431    16776192   83  Linux
/dev/sda2        33556478    41940991     4192257    5  Extended
/dev/sda5        33556480    41940991     4192256   82  Linux swap / Solaris

We can see from above that our VPS has been upgraded to 42.9GB worth of disk space. This is how much we can use in total.

root@mysuperweb.co.uk:/home# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        16G  8.8G  6.1G  60% /
udev            2.0G  4.0K  2.0G   1% /dev
tmpfs           396M  744K  395M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G   76K  2.0G   1% /run/shm

When we check the disk usage for the operating system we can see that of the 42.9GB disk capacity we are only using 16GB of the space. This means a total of 26.9GB of space is currently unallocated.

To make use of the full disk capacity we will need to redefine our partition table, however we will need to first delete our existing partition table layout, we can do this by running the following command

root@mysuperweb.co.uk:/home# fdisk /dev/sda

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the DOS compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): d
Partition number (1-5): 1

Command (m for help): d
Partition number (1-5): 2

By deleting the partition table we will lose the partition scheme, at this stage we will look to add a new partition with “n”. The deletion of the partition table will only delete the partition layout, the data on the server will remain.

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

The partition table will auto detect the largest partition size in which it can create based on the disk capacity. The next part is to write the new partition layout to the file system.

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)
Syncing disks.

To make use of the new partition table we will look to reboot the server. After the reboot we will look to check our current disk capacity and its usage:

fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
171 heads, 5 sectors/track, 98112 cylinders, total 83886080 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
Disk identifier: 0x000ea485

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    83886079    41942016   83  Linux
df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        16G  8.8G  6.1G  60% /
udev            2.0G   12K  2.0G   1% /dev
tmpfs           396M  736K  395M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G   76K  2.0G   1% /run/shm

The file system now expects the new partition layout, the last part is to resize the disk capacity. We can do this with the following command:

root@mysuperweb.co.uk:/home# resize2fs /dev/sda1

resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 3
The filesystem on /dev/sda1 is now 10485504 blocks long.

We have now successfully resized our VPS storage:

root@mysuperweb.co.uk:/home# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        40G  8.9G   29G  24% /
udev            2.0G   12K  2.0G   1% /dev
tmpfs           396M  736K  395M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G   76K  2.0G   1% /run/shm