Proxmox Add Swap

Adding swap to an exsiting Proxmox server.

Create Swap File

Creating a 20Gb file to be used as swap space.
formula: block_count = block_size * (file_size_kb / block_size)
with block_size = 1024 and file_size = 20Gb
1024 * ((20 * 1024 * 1024) / 1024) = 20971520

if your block size is 1024 then the formula can be simplified to:
block_count = file_size_kb

1dd if=/dev/zero of=/swap-01 bs=1024 count=20971520

Command Breakdown:

What Description
dd This is the command to create a file.
if=/dev/zero This is the input file. /dev/zero is a special file that provides as many null characters as you need.
of=/swap-01 This is the output file.
bs=1024 This is the block size.
count=20971520 This is the number of blocks to copy from the input file.

What this does is create a file called swap-01 with a size of 20Gb. The file is filled with zeros.

Secure Swap File

The swap file needs to be secured so that only root can read and write to it.

1chown root:root /swap-01
2chmod 0600 /swap-01

Enable Swap File

First we need to format the file as swap space.

1mkswap /swap-01

Then we need to enable the swap space.

1swapon /swap-01

The swap space is now enabled but will not survive a reboot. To make it permanent we need to add it to the fstab file.

Make Swap File Permanent

Open your fstab is your preferred editor. I use nano.

1nano /etc/fstab

Add the following line to the bottom of the file.

1/swap-01 none swap sw 0 0

Check Swap Space

To check that the swap space is enabled and working use the following command.

1swapon --show