Nicolas Le Manchet

Resizing swap on Debian Buster

The Debian installer allocates a swap partition matching the amount of RAM the machine has. This is done mostly to support hibernation which dumps the memory to the swap partition and powers off the computer.

For those who prefer to use sleep rather than hibernate, having a gigantic swap makes little sense. By removing the swap partition and instead creating a swap file it is simple to grow or reduce the amount of swap latter on.

Removing the swap partition and resizing the root file system [0]:

swapoff -a
lvremove /dev/debian-vg/swap_1  # Change debian with the name of your machine
lvextend -l +100%FREE /dev/debian-vg/root
resize2fs /dev/debian-vg/root
sed -i -e 's/^/# /' /etc/initramfs-tools/conf.d/resume

Creating a swap file [1]:

dd if=/dev/zero of=/swapfile bs=1024 count=1048576  # 1 GB swap file
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

Finally replace the swap partition in /etc/fstab by /swapfile and run:

update-initramfs -u

More information: