This article focuses on virtual private servers as a practical use case, but it is also a guide on how to reduce the size of a partition on any ( Debian ) Linux machine using the command line.
After you get the VPS from your hosting provider, you can customize the section layout. However, often you have no choice: you choose the OS you want, and then it is installed on one partition, taking up all the available disk space.
However, VPS is not only for single-partition web servers! Depending on your needs you can add a swap partition, another one for your data …
Fortunately, most hosting providers offer recovery mode, an alternate operating system on a different hard drive with which you can set up a normal OS and a “normal” hard drive.
The rest of this manual assumes that the only existing section is of type Ext4 (or Ext2 / 3); this is the case if you have installed Debian or Ubuntu .
Don’t try this method from inside the system, use recovery mode. If unsure, please check with your VPS provider to see if they provide access to the rescue console. Resizing the file system and partition does not erase data because the new desired size is larger than the current amount of data. However, please make a backup on an external drive / device / server.
Let’s say you need to shrink a partition from 40 GiB to 2 GiB (starting at the beginning of the disk) in order to create other partitions after it.
Step 1. Switch to recovery mode and identify the drive.
The first thing to do is reboot into recovery mode, which is usually a Debian GNU / Linux system. You will be given a temporary root password for this temporary system, just SSH.
Then identify the disk used by your regular system; you can use
The disk / dev / sdb on which we want to change the partition scheme, and so far the only partition is / dev / sdb1. / dev / sda is the drive that contains the temporary rescue system. Of course, in the remainder of this guide, replace sdb1 if it is the wrong device name.
Step 2: shrink the filesystem
Check the filesystem with:
e2fsck -f /dev/sdb1
(otherwise the next resize2fs command might not work).
e2fsck and resize2fs are most likely already installed on the recovery system, but if not, install the e2fsprogs package. On Debian, run:
apt install e2fsprogs
Also make sure the file system size is less than 2 GiB. Providing / mnt is empty, mount the filesystem with:
mount /dev/sdb1 /mnt
and check its size with df.
Unmount the filesystem:
Before proceeding with the following commands, and in case you have already configured the system or added important data, it is recommended to make a backup of your files!
resize2fs /dev/sdb1 2G
Step 3: Shrink the section
So far, you’ve shrunk the filesystem, which means you can’t add data to it that doesn’t fit its logical 2 GiB size. But the partition on which the file system resides still takes up all of the disk space.
We recommend parted for Linux partition management. On Debian, get this with apt install parted. This tool can be used interactively. Run it with an argument:
Use unit s to have all sizes displayed as number of sectors (instead of MiB, GiB, etc.), which is useful for fine control. Otherwise, you may run into problems such as “The resulting partition is not aligned correctly for best performance.” The print free command allows you to see the status of the disk.
You want partition 1 to be 2 GiB long, which is 2 * 1024 * 1024 * 1024/512 = 4194304 sectors.
Run the command:
You will then be prompted for a location on the disk, expressed in sectors from the beginning of the disk, where the partition should end. Since there is a reserved area before the section ending with sector 2047, you must enter 4194304 + 2047 = 4196351s.
Don’t forget the trailing “s”.
Answer “Yes” after the warning message, then print free again to confirm the resizing of the partition.
In our experience, stopping here and rebooting can “restore” the partition structure to its previous state, that is, as if you never shrunk the partition!
To “check” the changes, it is best to add at least one partition to the free space now.
Step 4: add other sections
Let’s say you want to add a 12 GiB Ext4 partition after the partition occupied by the OS.
First, you must “declare” the partition, so the entry will be added to the partition table (somewhere in the reserved area). Use the following command:
mkpart primary ext4
Then exit parted (C ^ d) and actually create Ext4 filesystem on sdb2. To apply the default Ext4 settings, run the following command:
This article was just an introduction to how to manage partitions and filesystems from the command line, focusing on the practical case of servers having one large default partition (and where you might want to split it). We hope this was helpful.