zramctl: Quick Start and Practical Guide


zramctl: Quick Start and Practical Guide

A pragmatic, example-driven guide to setting up and controlling zram devices using zramctl. We’ll cover quick checks, basic setup, common tasks, and a few gotchas.

Quick sanity checks

  • Check if the zram module is loaded before anything else:
lsmod | grep -i zram
  • If nothing shows up, load the module (dynamic number of devices can be configured later):
sudo modprobe zram
  • If you want to reserve a specific number of zram devices right away:
sudo modprobe zram num_devices=2

Getting started: what you’ll typically run

  • Start with a light diagnostic to confirm zram is available and ready:
lsmod | grep -i zram
  • Enable zram with a dynamic number of devices (let zramctl handle device management):
sudo modprobe zram
  • Or explicitly enable exactly 2 devices upfront:
sudo modprobe zram num_devices=2

Tip: If you’re unsure how many devices you’ll need, start with the default (no num_devices) and adjust later with zramctl.

Create and initialize a zram device

  • Find the next free zram device and initialize it to a 2 GB drive using the LZ4 compression algorithm:
sudo zramctl -f -s 2GB -a lz4

Notes:

  • -f or —find tells zramctl to use the next available zram device.

  • -s or —size sets the virtual disk size (2GB in this example).

  • -a or —algorithm selects the compression (lz4 is common and fast).

  • After creating, you may want to format it for filesystem use or swap:

    • For a filesystem:
# Example: create a ext4 filesystem on the new zram device
# Identify the device path first, e.g., /dev/zram0 (adjust as needed)
sudo mkfs.ext4 /dev/zram0
  • For swap:
sudo mkswap /dev/zram0
sudo swapon /dev/zram0

List current zram devices

  • To see which devices exist and their sizes/usage:
sudo zramctl

Practical workflow: cleaning up and reconfiguring

  • If you need to reset or remove devices:
sudo swapoff /dev/zram0 2>/dev/null || true
sudo zramctl -d /dev/zram0
  • Re-create with new size or algorithm as needed, repeating the creation step above.

Common pitfalls

  • Forgetting to load the module or mismatched device count:
    • Ensure you either load with a specific number of devices or stop at the default and let zramctl configure devices dynamically.
  • Using the wrong block device path when formatting (e.g., trying to format /dev/zram1 before it exists):
    • Always verify with sudo zramctl to confirm device names.
  • Not updating the swap configuration after adding a zram device:
    • If you swap on a single zram device, and you later remove it, remember to swapoff and clean up.

Troubleshooting quick checks

  • If zram devices aren’t appearing after creation:
    • Re-check module load: sudo modprobe zram num_devices=2
    • Re-scan devices: sudo zramctl
  • If writes to zram feel slow, consider a different compression algorithm or verify free memory and CPU load, as compression speed can impact performance.

TL;DR recap

  • Check and load zram module: lsmod | grep -i zram; sudo modprobe zram
  • Optional: predefine device count: sudo modprobe zram num_devices=2
  • Create a 2GB zram with LZ4: sudo zramctl -f -s 2GB -a lz4
  • List: sudo zramctl
  • Optional: format as filesystem or swap: sudo mkfs.ext4 /dev/zram0 or sudo mkswap /dev/zram0; sudo swapon /dev/zram0

If you’re new to zram, start simple with one device, verify it’s usable, then scale up as needed. The zramctl command surface is small but powerful for hands-on, on-the-fly RAM-based devices.

See Also