Resize partition at VPS without losing data
I have small VPS server with 40 GB disk space. I bought extra 40 GB and now I would like to extend /
partition size.
My partitions looks like this:
/dev/vda1
is /boot
and /dev/vda5
is my /
. My VPS provider added /dev/vda2
. I'm not sure if this is correct but I've tried to remove /dev/vda5
and /dev/vda2
and create /dev/vda5
but I don't have that option, I only can re-create devices with ID 2-4 and not 5 where my /
is.
Solution 1:
There is a utility named growpart
that will safely grow partitions once your provider has expanded your virtual disk. The only trick in your case is that they have unnecessarily created an extended/logical DOS partition when it was not needed. Thus you will have to resize the extended partition, then the logical one it contains.
sudo apt install cloud-guest-utils
The growpart
utility accepts the disk and partition number as separate arguments, so to resize /dev/vda2
you will write:
sudo growpart /dev/vda 2
Resize the extended partition /dev/vda2
first, then resize /dev/vda5
the same way.
sudo growpart /dev/vda 2
sudo growpart /dev/vda 5
Now you should be able to resize your filesystem. If it is ext4 then resize2fs /dev/vda5
will work. For XFS, use xfs_growfs /
.
Solution 2:
From your fdisk
output, it looks that you indeed have a 80 GB disk with 40 GB extended partition (/dev/vda2
) which contains inside a 40 GB logical partition /dev/vda5
which holds your /
filesystem. Extended partition does not actually contain any files, it's just a "container" for logical partitions that allows to overcome the 4 partitions limit that I mention below. Then there is 40 GB of unallocated space which fdisk
doesn't show in any way.
You may have several options how to use that unallocated space. I assume you are not using LVM because if you do, the procedure would be completely different, as LVM has it's own tools to resize volumes. However your fdisk
output does not indicate any mention of LVM partition.
I would not recommend deleting and recreating partitions in fdisk
as this always causes a risk to lose data. So if you want to do it safely, you can try the following:
First option is to have two 40 GB partitions and intelligently split your files between these two. If you for example keep a lot of large files in /home
, you can use the new partition for /home
and everything else except /home
will stay on the old partition. To do this:
-
use
fdisk
to create another primary partition (it will probably appear as/dev/vda3
!) in that unallocated space and format it as a new Linux filesystem withmkfs /dev/vda3
. There can be maximum 4 primary or extended partitions in total, with at most one of them being extended, so there's room for another partition. It is not a recommended partition layout where a primary partition comes after the extended partition, but it probably will work. -
rename your
/home
directory to/old_home
(make sure it's not being used at the moment, so do it from root with no regular user logged in) and create a new/home
directory with the same owner and permissions as the old one. -
mount the
/dev/vda3
filesystem under/home
with the command:mount /dev/vda3 /home
. If this succeeds, you should edit/etc/fstab
to have the new filesystem properly automounted when the system boots. Probably your/etc/fstab
currently contains two lines for vda1 and vda5, something like this (I'm making this one up now, yours may be different):/dev/vda5 / ext4 defaults 0 1
/dev/vda1 /boot ext4 defaults 0 2
Add a similar line for vda3 that says for example:
/dev/vda3 /home ext4 defaults 0 2
(if your file has something else instead of "ext4" or "defaults" just copy these parameters to the new line). Now write any file to the new
/home
directory and reboot the system. If after reboot the file you put in the/home
directory is still there, it means/home
has been correctly mounted at boot. -
Now you can move everything from
/old_home
to/home
and then delete the empty/old_home
directory.
I consider this method usually safest and easiest for me and have used it multiple times. However, it has the drawback of having actually 2x40 GB instead of single 80 GB of space, which may be sometimes good, sometimes bad - for example when you want to avoid accidental filling up of the system disk with user files, it's better to have /home
as separate partition.
So another option is to use GNU parted program to resize your vda2 and vda5 partitions (first vda2 and then vda5, as the former contains the latter) to 80 GB. parted
is designed specifically to modify partitions without losing data, which fdisk
can not guarantee. I'm sure parted
is in the Debian repository, so you can install it with apt
.
After you have resized the partition, use the command resize2fs /dev/vda5
to extend your /
filesystem to full 80 GB.
Solution 3:
Typically in this case you would use fdisk /dev/vda
.
- Make a note of the start sectors of partitions 2 and 5.
- Remove partition 5.
- Remove partition 2.
- Re-create partition 2 with the old start sector and the new end (default value). This is an extended partition.
- Re-create partition 5 with the old start sector and the new end (default value). This is a logical partition.
After the table is written and active (e.g. reboot), you would still need to extend the filesystem on /dev/vda5
using resize2fs
or whatever is appropriate for the filesystem on that partition.
Above all, make sure you have a good backup before attempting this.