390. zram and swap
A KDOS machine swaps first to zram, a compressed block device held in RAM, and then to any swap partition or swap file listed in /etc/fstab. This page shows where each is set up, how to see what is in use, how to resize or turn off zram, and how to add or remove a swap file on an installed system. It is for anyone whose machine runs short of memory, or who wants to change how much swap it has. Why zram turns zswap and swap read-ahead off is explained in the KDOS book’s Storage section.
Installation
Nothing needs installing. The zram device is made by /etc/init.d/12_zram.sh, which comes from the tree’s fs/ overlay rather than from a port, and its programs come from util-linux: swapon, swapoff, mkswap, zramctl, findmnt and fallocate. A swap file on btrfs is made with btrfs from btrfs-progs. The driver is a kernel module of the linux port, zram, which the script loads itself.
The service is enabled on the image: /etc/service.disabled/ holds no zram marker, so rcS runs the script at every boot, on the live medium and on an installed system alike. It is one of the scripts that start no daemon; see Services with no daemon.
Disk swap is the installer’s choice. On the erase plan it offers a swap file, a swap partition or none (see Swap), and writes the /etc/fstab line for it. The reuse plan shows no swap choice and installs with the erase plan’s last setting, a 4096 MiB swap file unless it was changed; a swap partition setting installs no swap there, since only the erase plan makes partitions. The live medium has no disk swap.
Configuration
/etc/kdos/zram.conf, from fs/etc/kdos/zram.conf. Both keys are optional; a missing file means the defaults. Every key, its range and when it applies are in /etc/kdos/zram.conf.
# Compressed swap in RAM. 12_zram.sh reads this; both keys are optional and
# an absent file means the defaults below.
#
# size is a PERCENTAGE OF RAM (1-90), not a byte count: the compressed pages
# live in the same memory, so this is how much swap the device may claim to
# hold, not how much it will occupy.
size = 50
# Any backend this kernel carries — zstd, lz4, lz4hc, deflate, 842, lzo. One it
# does not is reported and the kernel's own default is kept.
algorithm = zstd
/etc/init.d/12_zram.sh, from fs/etc/init.d/12_zram.sh, reads that file. The part of start that sizes the device, makes it swap at priority 100 and then changes the two kernel settings:
_kb=$(sed -n 's/^MemTotal:[[:space:]]*\([0-9]*\).*/\1/p' /proc/meminfo)
[ -n "$_kb" ] || { echo "[SKIP] $NAME: cannot read MemTotal"; exit 0; }
_bytes=$(( _kb * 1024 / 100 * size ))
echo "$_bytes" > "$SYS/disksize" || {
echo "[FAIL] $NAME: the kernel refused a ${size}% disksize"
exit 1
}
mkswap "$DEV" >/dev/null 2>&1 || { echo "[FAIL] $NAME: mkswap $DEV"; exit 1; }
# Priority above any disk swap fstab carries. Swapping to compressed
# RAM and swapping to a disk are not the same operation at the same
# cost, and equal priorities would round-robin between them.
swapon -p 100 "$DEV" || { echo "[FAIL] $NAME: swapon $DEV"; exit 1; }
# ZSWAP IS TURNED OFF ONCE ZRAM IS SWAP. The kernel starts with zswap
# on, and zswap is a compressed cache in front of whatever swap sits
# behind it: with zram there, a page is compressed into zswap's pool,
# then decompressed and compressed again into zram, and the pool holds
# RAM that zram would have used. Only after swapon succeeds — a machine
# whose zram failed keeps zswap in front of its disk swap.
[ -w "$ZSWAP" ] && echo N > "$ZSWAP" 2>/dev/null
# SWAP-IN READ-AHEAD IS OFF WHILE ZRAM IS SWAP. page-cluster reads
# 2^n pages per swap-in, sized for a disk's seek; on zram every extra
# page is a decompression nobody asked for. It is one setting for
# every swap device, so it is set here and not in sysctl.conf: a
# machine left with only disk swap keeps the kernel's read-ahead.
echo 0 > /proc/sys/vm/page-cluster 2>/dev/null
echo "[KDOS] $NAME: $DEV, ${size}% of RAM, $algorithm"
fs/etc/init.d/12_zram.sh, lines 69–98
/etc/fstab carries any disk swap. The image’s own fs/etc/fstab has no swap line; the installer writes its own lines, swap among them, ahead of the image’s, in one of these two forms, from src/system/kinstall-core/install.c:
if (swap_uuid[0]) {
char l[256];
snprintf(l, sizeof(l), "UUID=%s\tnone\tswap\tdefaults\t0 0\n",
swap_uuid);
cat(fst, sizeof(fst), l);
}
if (cfg.swap == SWAP_FILE && cfg.swap_mb > 0 && !ki_ab() &&
make_swapfile())
cat(fst, sizeof(fst), "/swapfile\tnone\tswap\tdefaults\t0 0\n");
cat(fst, sizeof(fst), "\n");
src/system/kinstall-core/install.c, lines 2961–2970
The first is a swap partition, or on the A/B layout the swap volume beside the two root slots. The second, a swap file, is written only on a single-root layout: a file inside one slot’s root would be missing from the other slot after an update switches to it (see A/B slots and rollback). make_swapfile() makes the file before the table is written, and the line is written only when the file was made whole; a file that could not be made is deleted and never mkswapped, and the install warns that the machine has no swap and goes on.
rcS turns on every swap line of /etc/fstab once the filesystems are mounted, from fs/etc/init.d/rcS:
# fstab may carry a swap entry (the installer writes one for a swap partition
# or a /swapfile). `mount -a` ignores swap lines, and inittab already has the
# matching swapoff on shutdown, so this is the only place it gets turned on.
swapon -a 2>/dev/null || true
fs/etc/init.d/rcS, lines 164–167
and /etc/inittab, from fs/etc/inittab, turns all swap off again at power-off and reboot, after the services are stopped and before the persistence store is made read-only and the filesystems are unmounted:
k0:06:wait:/etc/init.d/rcK
k1:06:wait:/etc/init.d/killprocs
k2:06:wait:/sbin/swapoff -a
k3:06:wait:/bin/sh -c '/bin/mountpoint -q /mnt/persist && exec /bin/mount -o remount,ro /mnt/persist'
k4:06:wait:/bin/umount -a -r
k5:06:wait:/etc/init.d/killpower
Usage
Seeing what swap is in use
List every active swap area with its priority:
swapon --show/dev/zram0is listed at priority 100. A swap file or partition from/etc/fstabfollows it at a lower priority, so the kernel uses it only once zram is full.Show how much the zram device holds and how well it compresses:
zramctlDISKSIZEis the size the script worked out fromsizeinzram.conf,DATAwhat has been swapped to the device,COMPRthe space that data takes once compressed, andTOTALthe RAM the device occupies, its own bookkeeping included.Ask the service itself:
service status zramIt prints
zram: /dev/zram0 active, <bytes> bytes nominal. With exit status 1 it printszram: /dev/zram0 present but not swapwhen the device exists but is not swap, as afterservice stop zram, andzram: no zram devicewhen the module is not loaded.
Changing the size or the compression algorithm
Edit the file:
sudoedit /etc/kdos/zram.confApply it by rebooting, or at once with:
sudo service restart zramstoprunsswapoffon the device, which has to bring every page held in it back into RAM before the device can be reset; on a machine that is short of memory, reboot instead. If thatswapofffails, the restart keeps the old device, prints[FAIL] zram: swapoff /dev/zram0 — it is still swapand thenzram: /dev/zram0 is already swap(see Troubleshooting).Check the result with
zramctl. An algorithm the kernel does not carry, or asizeoutside 1–90, is reported in the script’s output and replaced (see Troubleshooting).
On the live medium the edit lasts until power-off, like every other change to /etc, unless the medium has a persistence store (kdos persist).
Turning zram off
Stop it at once; zswap and the kernel’s swap read-ahead are put back as the service stops:
sudo service stop zramKeep it off at boot:
sudo service disable zramThis creates
/etc/service.disabled/zram.sudo service enable zramremoves it again. Type the name exactly; see/etc/service.disabled/<name>.
Adding a swap file on an installed system
An installed machine may already have one: the installer’s swap file is /swapfile, and swapon --show lists it. The commands below use that name, so pick another, in every step, to add a second file.
The way the file is made depends on the root filesystem. swapon refuses a file made the wrong way, and at boot rcS throws that error away, so a wrong file in /etc/fstab leaves the machine with no disk swap and no message on the screen.
Find the filesystem type of
/:findmnt -no FSTYPE /Create the file, here 4 GiB, with the command for that filesystem.
On ext4:
sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfileOn xfs and f2fs the installer writes the file out in full, and this does the same. The KDOS kernel’s
swaponalso accepts a filefallocatemade on either, so the ext4 commands work there too;ddis the form that works whatever the kernel:sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 sudo chmod 600 /swapfile sudo mkswap /swapfileOn btrfs, which needs a file that is neither copy-on-write nor compressed, and answers
swapon failed: Invalid argumentfor onefallocatemade; this one command makes it and formats it as swap:sudo btrfs filesystem mkswapfile -s 4G /swapfileThese are the three ways the installer uses, chosen per filesystem in
src/system/kinstall-core/conf.c.Turn it on:
sudo swapon /swapfileAdd it to
/etc/fstabso thatrcSturns it on at every boot, in the form the installer writes:printf '/swapfile\tnone\tswap\tdefaults\t0 0\n' | sudo tee -a /etc/fstabCheck with
swapon --show.
Removing a swap file
sudo swapoff /swapfile- Delete its line from
/etc/fstabwithsudoedit /etc/fstab. sudo rm /swapfile
Tips
- zram’s
sizeis the most the device may claim to hold, not the memory it occupies. Raising it does not reserve RAM; the compressed pages take RAM only as they arrive. - A full swap is not what kdos-oomd watches. It acts when the machine spends time stalled on memory, which is what the desktop feels, and
MemAvailableis at most a tenth ofMemTotalas well (how it decides), whether or not swap is full. kdos explain swapfileprints the reasoning behind the three ways of making a swap file.
Troubleshooting
swapon --show lists no /dev/zram0. Read the script’s boot output:
cat /run/kdos-init.zram.log
[SKIP] zram: the zram module will not load: the kernel has nozrammodule, or the modules of the running kernel are not installed. Check that/usr/lib/modules/$(uname -r)exists, then trysudo modprobe zramand read its error.[FAIL] zram: the kernel refused a N% disksize,mkswap /dev/zram0orswapon /dev/zram0: the step named failed.sudo dmesg | tailusually says why; the kernel lets only root read its log.- No log at all: the service is disabled.
ls /etc/service.disabled/shows azrammarker;sudo service enable zramremoves it.
The log says no '<name>' backend in this kernel — keeping the default. The algorithm in zram.conf is not one this kernel offers. The KDOS kernel carries all six that zram.conf lists, so on it this is usually a misspelt name. The script reads a value only up to its first character that is not a letter or a digit, so lzo-rle is read as lzo. List the algorithms the kernel offers; the bracketed name is in use:
cat /sys/block/zram0/comp_algorithm
The log says size N is outside 1-90, using 50. size is a percentage of RAM, not a byte count. A value with a letter in it, such as 4G, is ignored without a message and the device is made at 50 per cent; zramctl shows the size it got.
/etc/fstab lists a swap file, but after a boot there is no disk swap. Turn it on by hand to see the error the boot threw away:
sudo swapon /swapfile
On btrfs, swapon failed: Invalid argument means the file is copy-on-write or compressed, which is what btrfs filesystem mkswapfile avoids. Remove the file and make it again as above.
service restart zram hangs, or prints [FAIL] zram: swapoff /dev/zram0 — it is still swap. swapoff could not bring the pages held in zram back into RAM. stop exits 1 at that point and touches nothing else: the device keeps its old size and algorithm, zswap stays off and vm.page-cluster stays 0. restart runs start anyway, which finds the device still in use and prints zram: /dev/zram0 is already swap after the failure. Close some programs and run the restart again, or reboot.
Known gaps
Hibernation is not offered at all: No hibernation in Known gaps and What happens around a suspend say why.
See also
- Storage: zram, zswap,
vm.page-clusterand the boot-time swap /etc/kdos/zram.conf: the keys, their ranges and when they apply- Swap: what the installer offers
- Shutdown: where swap is turned off
- The live medium and persistence: why a change on the live medium does not last
- Out of memory: what acts when swap is not enough
- Services:
serviceand the disable markers - Filesystems and fstab: the rest of
/etc/fstab - Live persistence: keeping a change on the live medium