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.
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
free -h swapon --show lsblk /dev/sdb # confirm the sdb2 swap partition
Concept review. What swap is and how it is used.
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.
/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.
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.
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.
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.
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.
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.
# 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.
# 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.