Skip to main content
Topic: Dinit Service for Zram Swap (No zramen or zramctl Needed) (Read 2825 times) previous topic - next topic
0 Members and 2 Guests are viewing this topic.

Dinit Service for Zram Swap (No zramen or zramctl Needed)

If you want to use zram swap on Artix Linux without relying on zramen or zramctl, here’s a dinit service setup that initializes a zram device at boot and cleans it up on stop, using the kernel sysfs interface directly.

Why Use zram?

zram creates a compressed block of memory that acts like swap or a temporary drive. Instead of writing less-used data to your slower SSD or hard drive, the system compresses it and keeps it in RAM. This gives you more effective memory, keeps things faster, and reduces disk wear.

When to Use It:

  • Systems with 2–8 GB RAM – Helps keep your system responsive under load by compressing memory instead of using slow disk swap
  • SSD-based systems – Reduces swap-related disk writes, helping to extend the lifespan of your SSD
  • Systems without a swap partition – Easy way to add swap-like functionality without touching your disk
  • Embedded devices and containers – Saves memory and improves performance in resource-constrained environments
---

1. Optional configuration file

Code: [Select]
# /etc/dinit.d/config/zram.conf
# zram configuration
ZRAM_DEVICE=/dev/zram0
ZRAM_ALGO=zstd

---

2. Dinit service

Code: [Select]
# /etc/dinit.d/zram
type         = scripted
command      = /usr/bin/init-zram.sh
stop-command = /usr/bin/stop-zram.sh
env-file     = /etc/dinit.d/config/zram.conf
logfile      = /var/log/dinit/zram.log
depends-on   = early-devices.target
after        = early-fs-local.target

---

3. Initialization script

Code: [Select]
#!/bin/sh
# /usr/bin/init-zram.sh (manual sysfs version)

# Load zram module
modprobe zram || { echo "Failed to load zram module" >&2; exit 1; }

# Add a zram device (usually /dev/zram0)
echo 1 > /sys/class/zram-control/hot_add

# Default values
ZRAM_DEVICE=${ZRAM_DEVICE:-/dev/zram0}
ZRAM_ALGO=${ZRAM_ALGO:-zstd}

# Determine zram device number from path
ZRAM_NUM=$(basename "$ZRAM_DEVICE" | sed 's/zram//')

# Configure device
echo "$ZRAM_ALGO" > "/sys/block/zram$ZRAM_NUM/comp_algorithm"

# Set size to half of RAM
SIZE_KB="$(($(grep -Po 'MemTotal:\s*\K\d+' /proc/meminfo)/2))"
echo "${SIZE_KB}K" > "/sys/block/zram$ZRAM_NUM/disksize"

# Format as swap and enable
mkswap -U clear "$ZRAM_DEVICE"
swapon --discard --priority 100 "$ZRAM_DEVICE"

---

4. Stop script

Code: [Select]
#!/bin/sh
# /usr/bin/stop-zram.sh
# Disable zram swap and unload module

swapoff "${ZRAM_DEVICE:-/dev/zram0}"
modprobe -r zram

---

5. Make scripts executable

Code: [Select]
sudo chmod +x /usr/bin/init-zram.sh
sudo chmod +x /usr/bin/stop-zram.sh

---

Notes:

- Defaults: /dev/zram0 and zstd compression, configurable via /etc/dinit.d/config/zram.conf.
- Logs are written to /var/log/dinit/zram.log.
- Starts after early devices and filesystem are ready.
- No external wrapper (zramen) or zramctl is required—everything uses kernel interfaces directly.
- Enable the service with:
Code: [Select]
sudo dinitctl enable zram
- Check if it’s working with:
Code: [Select]
swapon --show
cat /sys/block/zram0/comp_algorithm
cat /sys/block/zram0/disksize

 

Re: Dinit Service for Zram Swap (No zramen Needed)

Reply #1
Nice and clean!

You can even make it not relying on zramctl either with /sys/class/zram-control/hot_add and /sys/block/zramX/{comp_algorithm,disksize}
;)

Re: Dinit Service for Zram Swap (No zramen or zramctl Needed)

Reply #2
Nice and clean!

You can even make it not relying on zramctl either with /sys/class/zram-control/hot_add and /sys/block/zramX/{comp_algorithm,disksize}
;)

Hitman, you genius! 💖

I’ve updated the init script using your suggestion and also added zram info at the beginning.

Re: Dinit Service for Zram Swap (No zramen or zramctl Needed)

Reply #3
https://gitlab.com/-/snippets/4879208
I wrote this one some time ago, you just plug it into /etc/rc.local from wherever, preferably /usr/local/bin/.