Grow the Disk After an Upgrade and Add Swap
Make a Linux VPS use the extra disk after a plan upgrade with growpart and resize2fs or xfs_growfs, add a swap file safely, and reclaim wasted space.
Two disk questions arrive together on almost every VPS: "I upgraded my plan, why is df still showing the old size?" and "should this small server have swap?" Both take a couple of minutes on the command line.
Grow the filesystem after an upgrade#
An upgrade makes the virtual disk larger. The partition table and filesystem inside your operating system do not know that until you tell them. Our images grow the root filesystem automatically on the next boot in most cases (cloud-init handles it), so reboot first and check:
lsblk # disk size vs partition size
df -h / # filesystem size the OS is actually using
If lsblk shows the disk (say vda, 100G) larger than the root partition (say vda1, 50G), grow it by hand. Take a snapshot first; then, as root:
# 1. grow the partition to fill the disk (partition 1 on /dev/vda here)
apt install -y cloud-guest-utils # Ubuntu / Debian; dnf install -y cloud-utils-growpart on AlmaLinux / Rocky
growpart /dev/vda 1
# 2. grow the filesystem to fill the partition
resize2fs /dev/vda1 # ext4 (Ubuntu, Debian)
xfs_growfs / # xfs (AlmaLinux, Rocky)
df -h /
Both commands are safe on a mounted, running root filesystem. If your image uses LVM (lsblk shows an lvm layer), grow the physical volume and logical volume instead:
growpart /dev/vda 3 # whichever partition holds the PV
pvresize /dev/vda3
lvextend -l +100%FREE -r /dev/mapper/*-root # -r resizes the filesystem too
Windows: open Disk Management, right-click the C: volume, and choose Extend Volume into the unallocated space.
Add swap#
Swap is disk space the kernel can use as overflow memory. On a small VPS it turns "the OOM killer shot my database at 3 a.m." into "the server got slow for a minute", which is a fair trade. A swap file the size of your RAM (up to a few GB) is a sensible default:
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
sysctl vm.swappiness=10 && echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
free -h
A low swappiness tells the kernel to use swap as a last resort rather than eagerly, which is what you want on NVMe-backed servers: fast enough to save you, not so eager it slows normal operation. If your workload lives in swap all the time, the answer is a plan with more RAM, not more swap.
Reclaim space instead of adding it#
Before upgrading for disk, look at what is using it. The usual suspects: journal logs, package caches, old kernels, Docker images, and application logs.
du -xsh /var/log /var/cache /var/lib/docker /home 2>/dev/null
journalctl --vacuum-size=200M
apt autoremove -y && apt clean # or: dnf autoremove -y && dnf clean all
docker system prune -a # if you use Docker; removes unused images
Related#
- Usage Graphs and the Activity Log for watching disk fill up before it bites
- Performance VPS, Foundational VPS, or VDS? if what you really need is a bigger box
