Introduction
In this tutorial, we will go through the process of shrinking the root partition of a Linux server and creating a new data partition in the freed space. This guide is specifically tailored for servers running in the Hetzner environment, making use of the Hetzner Rescue System.
This tutorial assumes you have already activated the Rescue System from your Hetzner Robot or Cloud Console.
Warning: The steps below may destroy existing data on the device.
Prerequisites
- A Hetzner server with root access
- A backup of all important data (strongly recommended) - You can make a Snapshot in Hetzner Console if you have data on the machine
Step 1 - Boot into the Rescue System
If you haven't already, boot the server into the Hetzner Rescue System. Ensure that the root partition is not mounted. E.g. check with:
lsblk
If the root partition is mounted, it will look something like this:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 38.1G 0 disk
├─sda1 8:1 0 20.2G 0 part /
...
Step 2 - Resize the Partition
-
Check and Shrink the Filesystem
Run the following commands to check and shrink the filesystem:
e2fsck -f -y /dev/sda1 resize2fs /dev/sda1 20G
This reduces the root filesystem to 20 GB.
-
Adjust the Partition
Resize the root partition to a slightly larger size than the filesystem to allow some buffer (22 GB in this case):
parted /dev/sda (parted) print (parted) resizepart 1 22GB (parted) quit
-
Repair the Filesystem
e2fsck -f -y /dev/sda1
This verifies and repairs the resized filesystem.
-
Create a New Partition
Use the remaining free space for a new partition:
parted /dev/sda mkpart primary ext4 22GB 100% mkfs.ext4 /dev/sda2
This creates and formats a new ext4 partition.
Step 3 - Mountpoint Preparation
Prepare and mount the new partition:
mkdir /data
mount /dev/sda2 /data
df -h | grep data
Then check your partitions again, for example with:
lsblk
If this look good, reboot the server to exit the Rescue System and boot normally.
After reboot, the /data
directory might no longer be available. If it is missing, create it again.
Step 4 - Configure fstab
- Check the UUID of the new partition:
Save the UUID for the fstab configuration.
blkid /dev/sda2
-
Edit
/etc/fstab
and add the following line:Replace
<uuid-data>
with the UUID of/dev/sda2
.UUID=<uuid-data> /data ext4 defaults 0 2
Step 5 - Test the Setup
-
Reload systemd and remount all:
systemctl daemon-reload mount -a
-
Verify with:
df -h
-
Reboot the server to ensure the configuration persists:
reboot df -h
Conclusion
- The root partition (
/
) has been reduced to 20 GB. - A new data partition (
/data
) uses the remaining disk space. - Automatic mounting of
/data
is configured viafstab
.