1

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:

Partitions size

/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.

Odin
  • 23
  • 1
  • 6
  • 2
    I doubt your provider added `/dev/vda2`, that is an extended partition containing `/dev/vda5`. Your provider has most likely extended the size of `/dev/vda` leaving 40GB of unallocated space at the end. – tater Jan 14 '21 at 12:18
  • @tater So what should I do? Remove `vda5` and `vda2`, create `vda2` which starts from 196606 as the "Extended" and than create `vda5` starting form 196608 as the "Primary" ? Than just use `w` to save, reboot and that's it? How can I create `vda5` inside `vda2`? `fdisk /dev/vda2` ? – Odin Jan 14 '21 at 12:38
  • 3
    Make sure to take backup first. –  Jan 14 '21 at 14:37
  • 1
    Could you add the output of `lsblk` to your question? – Mircea Vutcovici Jan 14 '21 at 14:49
  • Many VPS platforms cater to non-expert users and either your VPS itself or their provisioning system will ensure that your file system gets resized to make use of all additional space on reboot (or a power off followed by a power on). So if your provider does as well, that might be the easiest and least error prone method for you to use – Bob Jan 14 '21 at 16:01

3 Answers3

3

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 /.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thank you all for help. For sure @raj and tater answers would work, they are default approach but using growpart was so easy. Thank you! – Odin Feb 11 '21 at 22:38
2

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.

tater
  • 1,445
  • 2
  • 10
  • 12
  • I would recommend to test this procedure first on a testsystem, for example on a local virtualmachine with Oracle Virtualbox (its free). Just to get familiar with the procedure, as it is a critical one. As Odin shows, the test vm should be setup with a 40gb disk first containing the partition layout as shown in the picture. And as @tater mentioned, first you should create a backup of your stuff. – Snooops Jan 14 '21 at 14:51
  • 1
    I think you are making too many assumptions: you do not know if they are using LVM (it is possible to use LVM even if they partition type is not LVM), You do not know that the `/` volume is `ext4` (or ext3). – Mircea Vutcovici Jan 14 '21 at 15:14
  • Partition 2 is an **extended** partition, not a **primary**. Do not make a mistake of creating partition 2 as a primary partition! – raj Jan 14 '21 at 15:27
  • @raj Good catch, I'll update it. – tater Jan 14 '21 at 20:42
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:

  1. 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 with mkfs /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.

  2. 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.

  3. 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.

  4. 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.

raj
  • 542
  • 2
  • 8