sysadmin · June 8, 2026 · 13 min read

Emergency swap.
a swap file, a swap partition, and priority.

Nightly batch jobs are getting OOM-killed. Until more RAM arrives, dbadmin1 needs swap added live, both a file and a partition, made persistent, with the faster partition used first.

// what we’re getting into
  1. The ticket
  2. Concept review. What swap is and how it is used.
  3. Create the 512MB swap file
  4. Activate the swap partition
  5. Make both persistent in fstab
  6. Test fstab without rebooting
  7. Tune swappiness
  8. Exam questions
  9. Final checklist: confirm everything works

The ticket: long-running analytics queries are getting OOM-killed during nightly batch runs. Until more RAM is approved, dbadmin1 needs swap as a buffer. Add a 512MB swap file at /extraswap immediately, no reboot, and make it persistent. Also activate the swap partition /dev/sdb2 from the partitioning ticket, and make the partition used first because it is slightly faster.

The real problem: swap is straightforward until you need priority and persistence at the same time. Both pieces have to survive a reboot, and the faster device should win.

What we are doing: adding swap as both a file and a partition, setting priority, and persisting both.

The ticket

bash
free -h
swapon --show
lsblk /dev/sdb    # confirm the sdb2 swap partition

Concept review. What swap is and how it is used.

bash
man mkswap swapon swapoff

Swap is overflow for RAM. When physical memory fills, the kernel pages inactive memory out to swap. It is slower than RAM, but it stops the out-of-memory killer from killing your process. You can provide it as a partition, which is slightly faster, or as a file, which is flexible and needs no repartitioning. Priority decides which is used first, where a higher number wins and the default is negative.

fstab swap entries
/swapfile   none   swap   defaults         0 0
UUID=xxxx   none   swap   defaults,pri=10  0 0

Create the 512MB swap file

Allocate the space, lock the permissions down before formatting, format it as swap, then turn it on. The permission step matters: if the swap file is world readable, swapon refuses it.

bash
sudo dd if=/dev/zero of=/extraswap bs=1M count=512 status=progress
sudo chmod 600 /extraswap    # required, or swapon complains about unsafe permissions
sudo mkswap /extraswap
sudo swapon /extraswap
swapon --show
free -h

Activate the swap partition

The partition was already tagged as swap. Format it, then turn it on with a higher priority so it is used before the file.

bash
sudo mkswap /dev/sdb2
sudo blkid /dev/sdb2
sudo swapon --priority 10 /dev/sdb2
swapon --show    # two swap devices now, with different priorities

Make both persistent in fstab

Back up fstab, then add both entries. Use the UUID for the partition so it survives device renaming, and set its priority in the options.

bash
sudo cp /etc/fstab /etc/fstab.bak.$(date +%Y%m%d)

echo "/extraswap   none   swap   defaults         0 0" | sudo tee -a /etc/fstab

SWAP_UUID=$(sudo blkid -s UUID -o value /dev/sdb2)
echo "UUID=$SWAP_UUID   none   swap   defaults,pri=10   0 0" | sudo tee -a /etc/fstab
tail -5 /etc/fstab

Test fstab without rebooting

Turn everything off, then let fstab turn it back on. If this works, the reboot will too.

bash
sudo swapoff -a
swapon --show    # empty

sudo swapon -a   # activates everything marked swap in fstab
swapon --show    # both back
free -h

Tune swappiness

Swappiness controls how eagerly the kernel swaps, from 0 to 100. For a database workload you usually lower it so the box uses RAM longer before swapping.

bash
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10

# make it persistent
echo "vm.swappiness = 10" | sudo tee /etc/sysctl.d/99-server1-swap.conf
sudo sysctl --system | grep swappiness

Exam questions

Write the command first.

Q1. Create a 256MB swap file at /swap256, activate it, and verify it is active.

Q2. Make both a swap file and a swap partition persistent across reboots. Show the fstab entries.

Q3. RAM is exhausted mid-batch. Add 1GB of swap immediately, no reboot, no disruption to running processes. Walk through every command.

Answers.

bash
# A1
sudo dd if=/dev/zero of=/swap256 bs=1M count=256
sudo chmod 600 /swap256
sudo mkswap /swap256
sudo swapon /swap256
swapon --show
# A2 (fstab)
# /extraswap                  none  swap  defaults         0 0
# UUID=<swap-partition-uuid>  none  swap  defaults,pri=10  0 0
# A3
sudo dd if=/dev/zero of=/swap1g bs=1M count=1024
sudo chmod 600 /swap1g; sudo mkswap /swap1g; sudo swapon /swap1g
echo '/swap1g none swap defaults 0 0' | sudo tee -a /etc/fstab

Final checklist: confirm everything works

If every check passes, the ticket is done.

bash
# 1. swap file active, mode 600
ls -l /extraswap; swapon --show | grep extraswap

# 2. swap partition active with priority 10
swapon --show

# 3. both survive swapoff -a then swapon -a (fstab valid)
# 4. all three exam commands written from scratch
# 5. tracker entry checked off

Reply to dbadmin1: 512MB swap file plus the sdb2 partition are both active and persistent. The partition is prioritized at pri=10 so it is used first. Swappiness lowered to 10 for the database workload.

next post
Installing tools and building a local repo mirror