Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

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

fs/etc/kdos/zram.conf

/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

fs/etc/inittab, lines 79–84

Usage

Seeing what swap is in use

  1. List every active swap area with its priority:

    swapon --show

    /dev/zram0 is listed at priority 100. A swap file or partition from /etc/fstab follows it at a lower priority, so the kernel uses it only once zram is full.

  2. Show how much the zram device holds and how well it compresses:

    zramctl

    DISKSIZE is the size the script worked out from size in zram.conf, DATA what has been swapped to the device, COMPR the space that data takes once compressed, and TOTAL the RAM the device occupies, its own bookkeeping included.

  3. Ask the service itself:

    service status zram

    It prints zram: /dev/zram0 active, <bytes> bytes nominal. With exit status 1 it prints zram: /dev/zram0 present but not swap when the device exists but is not swap, as after service stop zram, and zram: no zram device when the module is not loaded.

Changing the size or the compression algorithm

  1. Edit the file:

    sudoedit /etc/kdos/zram.conf
  2. Apply it by rebooting, or at once with:

    sudo service restart zram

    stop runs swapoff on 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 that swapoff fails, the restart keeps the old device, prints [FAIL] zram: swapoff /dev/zram0 — it is still swap and then zram: /dev/zram0 is already swap (see Troubleshooting).

  3. Check the result with zramctl. An algorithm the kernel does not carry, or a size outside 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

  1. Stop it at once; zswap and the kernel’s swap read-ahead are put back as the service stops:

    sudo service stop zram
  2. Keep it off at boot:

    sudo service disable zram

    This creates /etc/service.disabled/zram. sudo service enable zram removes 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.

  1. Find the filesystem type of /:

    findmnt -no FSTYPE /
  2. 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 /swapfile

    On xfs and f2fs the installer writes the file out in full, and this does the same. The KDOS kernel’s swapon also accepts a file fallocate made on either, so the ext4 commands work there too; dd is the form that works whatever the kernel:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile

    On btrfs, which needs a file that is neither copy-on-write nor compressed, and answers swapon failed: Invalid argument for one fallocate made; this one command makes it and formats it as swap:

    sudo btrfs filesystem mkswapfile -s 4G /swapfile

    These are the three ways the installer uses, chosen per filesystem in src/system/kinstall-core/conf.c.

  3. Turn it on:

    sudo swapon /swapfile
  4. Add it to /etc/fstab so that rcS turns it on at every boot, in the form the installer writes:

    printf '/swapfile\tnone\tswap\tdefaults\t0 0\n' | sudo tee -a /etc/fstab
  5. Check with swapon --show.

Removing a swap file

  1. sudo swapoff /swapfile
  2. Delete its line from /etc/fstab with sudoedit /etc/fstab.
  3. sudo rm /swapfile

Tips

Troubleshooting

swapon --show lists no /dev/zram0. Read the script’s boot output:

cat /run/kdos-init.zram.log

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