LearnRHCSA (EX200)Local Storage

Swap: Partitions and Logical Volumes

mkswap - a hands-on Linux lab on a real virtual machine.

Add swap to a RHEL 10 system non-destructively and make it survive reboots. Build a swap area from a partition or a logical volume, then run the fixed three steps: format with mkswap, activate with swapon, and persist with an /etc/fstab line by UUID (the swap line uses the word swap for both mount point and type). Confirm with swapon --show and free -h. Serves EX200 local-storage task 9: add swap to a system non-destructively.

A batch job kicks off overnight and quietly eats every gigabyte of RAM. On a machine with no room to spare, the kernel's out-of-memory killer wakes up and starts shooting processes to survive. Your database is one of them. You arrive to a dead service and a log line that just says Killed.

There is a pressure valve that would have saved it: swap. Swap is disk space the kernel uses as overflow when RAM fills up, moving idle pages out to disk so nothing has to be killed. Adding a swap area that survives reboots is EX200 local-storage task 9, add swap to a system non-destructively. By the end of this lesson you will build a swap area from a spare disk, turn it on, and wire it into /etc/fstab so it comes back on every boot.

The black boxes below are a practice terminal: a safe sandbox that checks the one command each step teaches. The outputs shown are from a real RHEL 10 machine (AlmaLinux 10.2) that had a spare disk /dev/vdc. The swap size (639 MiB here) and the UUIDs are that machine's live values, so yours will read differently. What stays true everywhere is the SEQUENCE, format, activate, persist, and that sequence is the lesson.

Swap is not a special kind of file. It is ordinary storage that the kernel formats with a swap signature and then treats as an extension of memory. When RAM runs low, inactive pages get written out to the swap area and pulled back when needed. It is slower than RAM, but it is the difference between a slow machine and a dead one.

On the exam, swap comes from one of two places, and both follow the exact same three steps once the device exists. A swap partition is a partition on a disk given the swap type. A swap logical volume is an LV carved out of a volume group. Either way you then format it as swap, activate it, and record it in /etc/fstab.

When you build the device, you tell the partition table or LVM that it is meant for swap. On RHEL 10 fdisk accepts type ALIASES, so you type swap instead of memorising a hex code, and parted sets the same intent. That type is a label of purpose, it does not by itself make the area usable. The real formatting is a separate command, mkswap, and forgetting it is the single most common swap mistake on the exam.

The recommended swap size varies with RAM and whether the machine hibernates, but the exam task usually hands you a spare disk and a target size. Build what the task asks for. The steps are identical whether the swap is 512 MiB or 8 GiB.

You have a spare disk /dev/vdc with a swap-type partition /dev/vdc1 already cut on it. The first real step is to write the swap signature onto that partition with mkswap. This is the step people skip, and skipping it makes the next command fail with a read swap header failed error.

Before you run it, decide what you expect: mkswap should report that it set up a swap area and print its size. Format the partition:

mkswap /dev/vdc1

prompt: [root@servera ~]# answer: mkswap /dev/vdc1 output: Setting up swapspace version 1, size = 639 MiB (670035968 bytes) hint: The formatter is mkswap followed by the device: mkswap /dev/vdc1

Setting up swapspace version 1. That line is mkswap telling you it wrote the swap header onto /dev/vdc1 and the area is now a valid swap device, 639 MiB on this machine. Nothing is using it yet, formatting only prepares the device. Notice the size in the message: that comes from the partition you built, so a bigger partition prints a bigger number. This same mkswap <device> works identically on a logical volume: mkswap /dev/swapvg/swaplv formats an LV as swap the very same way.

The exact size here, 639 MiB, is this machine's spare partition. Yours will match whatever size you cut. What is stable is the shape of the message: Setting up swapspace version 1, size = .... If you see that line, the format worked.

Formatting made the device a swap area. swapon actually hands it to the kernel to use. Point swapon at the same device you just formatted and the kernel starts counting that space as available overflow immediately.

One honest wrinkle worth burning in now: swapon activates swap for the current session only. It does NOT survive a reboot on its own. That persistence comes from /etc/fstab, two steps from now. For this rep, just switch it on:

swapon /dev/vdc1

prompt: [root@servera ~]# answer: swapon /dev/vdc1 output: hint: The activator is swapon followed by the same device you formatted: swapon /dev/vdc1

swapon prints nothing on success, which is the Unix way of saying it worked. The kernel is now using /dev/vdc1 as swap. If you had forgotten mkswap, this is where it would have blown up with swapon: /dev/vdc1: read swap header failed, the device has no swap signature to read. And if the device were already on, you would see swapon failed: Device or resource busy. Silence means success.

Never trust a silent command. Prove the swap is live. swapon --show lists every active swap device in a clean table: the name, whether it is a partition or a file, its size, how much is in use, and its priority. Your new device should appear as a fresh row alongside the swap the system already had.

List the active swap areas:

swapon --show

prompt: [root@servera ~]# answer: swapon --show output: NAME TYPE SIZE USED PRIO /dev/dm-1 partition 1024M 0B -2 /dev/vdc1 partition 639M 0B -3 hint: The clean listing flag is --show: swapon --show

Two rows now. /dev/dm-1 is the swap the system was installed with, the LVM swap volume on the root disk. /dev/vdc1 is the one you just added, 639M, in use 0B because nothing has needed it yet. The PRIO column is priority: the kernel fills higher-priority swap first, and when you do not set one it assigns descending negatives (-2, then -3). Both areas are live. That second row is your proof the format-then-activate sequence worked.

So far the swap is on, but a reboot would drop it, because swapon only affects the running system. Persistence lives in /etc/fstab, the file systemd reads at boot to activate storage. A swap line there turns your swap back on automatically every time the machine starts.

A swap fstab line has a shape all its own. You reference the device by its UUID (a permanent identifier that never changes, unlike /dev/vdc1 which can shift if disks are added), and the swap line uses the literal word swap for BOTH the mount point and the filesystem type, because swap is never mounted into the directory tree. First get the UUID with blkid:

blkid /dev/vdc1

prompt: [root@servera ~]# answer: blkid /dev/vdc1 output: /dev/vdc1: UUID="<varies>" TYPE="swap" PARTLABEL="primary" PARTUUID="<varies>" hint: blkid prints the UUID and type of a device: blkid /dev/vdc1

TYPE="swap" confirms the format took, and the UUID= is the permanent handle you put in /etc/fstab. The line you append looks like UUID=<the-uuid> swap swap defaults 0 0: the UUID first, then swap as the mount point, swap as the type, defaults for options, and 0 0 for dump and fsck (both zero for swap). On RHEL 10 there is one more step after editing the file: run systemctl daemon-reload so systemd re-reads the mount units, then swapon -a activates everything fstab lists.

Two failures fail this task at grading time, and both are silent until reboot. First, forgetting /etc/fstab entirely: your swap works now, but the exam reboots the machine and your swap is gone. Second, writing /dev/vdc1 instead of the UUID: it works today, but if a disk is added and vdc becomes vdd, the line points at the wrong device and boot can drop to emergency mode. Always reference swap by UUID.

Scaffolding off. No command is printed this time. You have every piece you need.

You have added and activated your swap, and now you want the one-line proof that total swap went up. Think about the command that shows memory AND swap totals together, and the flag that prints those totals in friendly human units (G and M) instead of raw bytes. The line you want is the Swap: row, and its total should be larger than before you started.

prompt: [root@servera ~]# answer: free -h output: Swap: 1.6Gi 0B 1.6Gi hint: The memory-and-swap summary is free, and the human-readable flag is -h. Build free, then -h.

free -h. The -h gave you human units, and the Swap: line reads 1.6Gi total: the original 1 GiB system swap plus the 639 MiB you just added, rounded together. That total climbing is the exam grader's proof that your new swap is active. Your own number will differ with your disk size, but the move is the same: free -h, read the Swap: row, confirm it grew. Pair it with swapon --show and you have named both the total and the individual devices.

You earned this cheat sheet. Every row is a step you just ran or built:

The one thing to burn in for the exam: the three steps are format (mkswap), activate (swapon), persist (fstab by UUID). Miss mkswap and activation fails. Miss the fstab line and the swap vanishes at the reboot the grader triggers.

The order is fixed and unforgiving. mkswap before swapon, always, or you get read swap header failed. And the fstab entry is what the exam actually grades, because it reboots the machine, so add the UUID line the moment the swap is on, not last.

The practice terminal has shown you the whole swap sequence: mkswap to format the device, swapon to turn it on, swapon --show and free -h to prove it is live, and the blkid plus fstab-by-UUID move that makes it survive a reboot. Every one of those you typed yourself, and it works the same on a partition or a logical volume.

The local-storage module mission is where you run these against a real RHEL 10 machine. A full VM boots for you, with spare disks /dev/vdb and /dev/vdc waiting to be built into storage. The mission hands you objectives that use exactly what you practiced here: cut a partition or an LV, format it as swap, activate it, and record it in /etc/fstab so it comes back on every boot. One difference from this lesson: the mission shows no commands. You read the objective, recall mkswap then swapon then the fstab line, and type it. That recall is what makes it stick on exam day.

Finish the other local-storage lessons, then go add swap to a real machine and make it outlive a reboot.

Practice Swap: Partitions and Logical Volumes in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.