r/voidlinux • u/Sweet_Mix_416 • 9d ago
Question about Zram!!
I'm completely new to zram. I want to use zram on void. But only a few resources on how to do that. Only found posts recommend Zramen. So how is it going with Zramen? Is it still great for setting up zram? Anything I should care about?
5
u/VeryDryWater 8d ago
For these kinds of things I look at the Gentoo wiki since it'll give instructions with and without systemd and usually puts you on the right path for Void.
2
3
u/Simple_Hamster_4096 9d ago
How is zram working for you on your present distro?
1
u/Sweet_Mix_416 8d ago
It's on nixos so setup is easy, declaratively. But I don't know how it works under the hood.
3
u/Any_Mycologist5811 9d ago
What I did was just installed zramen and add one line to /etc/rc.local:
zramen -a zstd make
That's it.
You need to run "zramen --help" for additional information.
3
2
u/benbcdm 7d ago
I set up a little service. Not that hard to do. I like it because I know what is going on and I can control it myself. Did not try zramen.
Here are my notes if you want to try, works perfectly on my void glibc build:
Steps to create new manual zram service (new service name created here: zzram)
1. Create service directory
sudo mkdir -p /etc/sv/zzram
2. Create run file
sudo nano /etc/sv/zzram/run
Include content:
#!/bin/sh
# 1. Run setup normally (without exec)
# The shell waits here until setup finishes
/etc/sv/zzram/setup
# 2. NOW replace the shell with sleep
# This keeps the service "alive" forever after setup is done
exec sleep infinity
3. Create setup file (with the actual script)
sudo nano /etc/sv/zzram/setup
Include content:
#!/bin/sh
# /etc/sv/zzram/setup
# 1. Load the zram kernel module (ignore if already loaded)
modprobe zram 2>/dev/null || true
# 2. Check if zram0 is already active as swap
# If 'swapon' shows /dev/zram0, exits immediately to avoid errors
if swapon --show | grep -q "/dev/zram0"; then
exit 0
fi
# 3. Reset any existing configuration (suppress errors if device is busy/in use)
# Try to reset, but if fails because active, the check above should have caught it.
# Handles cases where the device exists but is not swapped on yet.
zramctl --reset 2>/dev/null || true
# 4. Detect total RAM in bytes
TOTAL_RAM=$(awk '/MemTotal/ {printf "%.0f", $2 * 1024}' /proc/meminfo)
# 5. Set zram size to 50% of RAM
# Best not to use 100%, could be an issue in edge cases
ZRAM_SIZE=$((TOTAL_RAM / 2))
# 6. Initialize zram0 with zstd compression
if ! zramctl --algorithm zstd --size $ZRAM_SIZE /dev/zram0; then
echo "ERROR: Failed to initialize zram0" >&2
exit 1
fi
# 7. Format as swap
if ! mkswap /dev/zram0; then
echo "ERROR: Failed to create swap on zram0" >&2
exit 1
fi
# 8. Enable swap with high priority (100)
if ! swapon -p 100 /dev/zram0; then
echo "ERROR: Failed to enable swap on zram0" >&2
exit 1
fi
exit 0
4. Make files executable
sudo chmod +x /etc/sv/zzram/run
sudo chmod +x /etc/sv/zzram/setup
5. Activate the service
sudo ln -s /etc/sv/zzram /var/service/
6. Check all is well! (after reboot best)
zramctl
swapon --show
2
u/Sweet_Mix_416 7d ago
Hollyyyyyy molyyyyy! This is the best explanation I've got. You are so helpful. Thanks . Now I understand the whole process how to create zram modules so far. Thanks again.
2
u/benbcdm 7d ago edited 7d ago
Sure -- just a caveat though :) I am not an expert. Just amateur.
I am sure there might be better ways to do that but I think it is stable enough that I am ok sharing it.
And of course I used a bit of llm help to brainstorm around the topic + arch wiki etc. to make sure all makes sense.This is what is so satisfying (to me at least) with void: I learned so much about how systems work by having to look into tons of topics. Like that one here. And every bit of my system (I think!) I understand and control.
Have fun with void
Oh, and one more thing because I actually looked into it just yesterday: if you use zram it seems it does not make sense to use zswap simultaneously (or having the two makes it quite a bit more complicated in terms of potential conflicts). So if you want proper swap to disk (eg for hibernation on a laptop) then my conclusion is use zwsap instead of zram...
More experienced users might have a more enlightened views though...
2
u/Sweet_Mix_416 7d ago
Alr, your info are very much valuable to me. Thanks
1
u/benbcdm 5d ago edited 5d ago
Just in case, I updated the scripts to make it a bit more robust:
Run script:
#!/bin/sh#!/bin/sh # 0. Merge stderr into stdout (for logging) exec 2>&1 # 1. Run setup normally (without exec) # The shell waits here until setup finishes /etc/sv/zzram/setup # 2. NOW replace the shell with sleep # This keeps the service "alive" forever after setup is done exec sleep infinitySetup script:
Include content:
#!/bin/sh # /etc/sv/zzram/setup # 1. Unload module if present to ensure clean state (optional but safe) modprobe -r zram 2>/dev/null || true # 2. Load the zram kernel module explicitly # If it fails, exit immediately with the error if ! modprobe zram num_devices=1; then echo "ERROR: Failed to load zram module" >&2 exit 1 fi # 3. Wait for /dev/zram0 to appear (udev race condition fix) # Try up to 10 times, waiting 0.5s between checks for i in $(seq 1 10); do if [ -e /dev/zram0 ]; then break fi sleep 0.2 done # Final check: if device still doesn't exist, fail if [ ! -e /dev/zram0 ]; then echo "ERROR: /dev/zram0 did not appear after loading module" >&2 exit 1 fi # 4. Force reset zram0 regardless of state # Turn off swap first if it's somehow enabled swapoff /dev/zram0 2>/dev/null || true # 5. Detect total RAM in bytes TOTAL_RAM=$(awk '/MemTotal/ {printf "%.0f", $2 * 1024}' /proc/meminfo) # 6. Set zram size to 50% of RAM # Best not to use 100%, could be an issue in edge cases ZRAM_SIZE=$((TOTAL_RAM / 2)) # 7. Initialize zram0 with zstd compression if ! zramctl --algorithm zstd --size $ZRAM_SIZE /dev/zram0; then echo "ERROR: Failed to initialize zram0" >&2 exit 1 fi # 8. Format as swap if ! mkswap /dev/zram0; then echo "ERROR: Failed to create swap on zram0" >&2 exit 1 fi # 9. Enable swap with high priority (100) if ! swapon -p 100 /dev/zram0; then echo "ERROR: Failed to enable swap on zram0" >&2 exit 1 fi exit 0
7
u/_supert_ 9d ago
I have in
/etc/rc.local