Code Monkey home page Code Monkey logo

zram-swap's Introduction

zram-swap

A simple zram swap script for modern systemd Linux

https://github.com/foundObjects/zram-swap

Why?

I wrote zram-swap because I couldn't find a simple modern replacement for the Ubuntu zram-config package that included basic error handling, didn't make device sizing mistakes and kept user-facing configuration straightforward and easy to understand.

Installation and Usage

Arch Linux:

Install from the AUR: paru -S zram-swap-git

or directly from GitHub:

mkdir zram-swap-git && cd zram-swap-git
wget https://raw.githubusercontent.com/foundObjects/zram-swap/arch-packaging/PKGBUILD
makepkg -Cci

You'll need to start and enable zram-swap.service after installation on Arch. Make any configuration changes to /etc/default/zram-swap first if desired, see below for details.

Others:

git clone https://github.com/foundObjects/zram-swap.git
cd zram-swap && sudo ./install.sh

The install script starts the zram-swap.service automatically after installation and enables the systemd service during boot. The default allocation creates an lz4 zram device that should use around half of physical memory when completely full.

I chose lz4 as the default to give low spec machines (systems that often see the greatest benefit from swap on zram) every performance edge I could. While lzo-rle is quite fast on modern high-performance hardware a machine like a Raspberry Pi or a low spec laptop appreciates every speed advantage I can give it.

Configuration

Edit /etc/default/zram-swap if you'd like to change the compression algorithm or swap allocation and then restart zram-swap with systemctl restart zram-swap.service. The configuration file is heavily commented and self-documenting.

A very simple configuration that's expected to use roughly 2GB RAM might look something like:

# override fractional calculations and specify a fixed swap size
_zram_fixedsize="6G"

# compression algorithm to employ (lzo, lz4, zstd, lzo-rle)
_zram_algorithm="lzo-rle"

Remember that the ZRAM device size references uncompressed data, real memory utilization should be ~2-3x smaller than the zram device size due to compression.

A quick note RE: compression algorithms:

The default configuration using lz4 should work well for most people. lzo may provide slightly better RAM utilization at a cost of slightly more expensive decompression. zstd should provide better compression than lz* and still be moderately fast on most machines. On very modern kernels and reasonably fast hardware the most balanced choice is probably lzo-rle. On low spec machines (ARM SBCs, ARM laptops, thin clients, etc) you'll probably want to stick with lz4.

Debugging

To view a script debug trace either start zram-swap.sh with zram-swap.sh -x (start|stop) or uncomment the debug flag in /etc/default/zram-swap:

# setting _zram_swap_debugging to any non-zero value enables debugging
# default: undefined
_zram_swap_debugging="beep boop"

Compatibility

Tested on Linux 4.4 through Linux 5.14.

Requirements are minimal; Underneath the systemd service wrapper the swap setup script needs only a posix shell, modprobe, zramctl and very basic awk and grep support to function. It should work in pretty much any modern Linux environment.

zram-swap's People

Contributors

foundobjects avatar klement01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zram-swap's Issues

Problems with 8gb RPi 4

Hi,

Your script runs perfectly on a Raspberry Pi 4 with 4gb of ram, but doesn’t show correctly with the new 8gb version. (I’m running the beta boot from SSD which your script run alongside with no problems with the 4gb RPi 4)

Here’s 3 commands:

Al@MAME:~ $ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 223.6G  0 disk 
├─sda1   8:1    0   256M  0 part /boot
└─sda2   8:2    0 223.3G  0 part /
zram0  254:0    0   9.7G  0 disk

Al@MAME:~ $ sudo cat /proc/swaps
Filename                                Type            Size    Used    Priority
Al@MAME:~ $

Al@MAME:~ $ free -h
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       798Mi       5.6Gi       8.0Mi       1.3Gi       6.7Gi
Swap:            0B          0B          0B

In the 4gb version your zram shows in the sudo cat /proc/swaps & the free -h commands.
I deliberately set my swapsize to zero to avoid confusion.

Any ideas?

Thanks,

Alan Cooper.

FATAL: Module zram not found in kernel 5.15

I installed the zram-swap-git package from the AUR in EndeavourOS. I get the following error when I try to start the service.

$ sudo systemctl start zram-swap.service
Job for zram-swap.service failed because the control process exited with error code.
See "systemctl status zram-swap.service" and "journalctl -xeu zram-swap.service" for details.

Here is the status

$ systemctl status zram-swap.service
× zram-swap.service - zram swap service
     Loaded: loaded (/usr/lib/systemd/system/zram-swap.service; disabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Mon 2022-02-07 10:18:09 IST; 8s ago
    Process: 35823 ExecStart=/usr/bin/zram-swap.sh start (code=exited, status=1/FAILURE)
   Main PID: 35823 (code=exited, status=1/FAILURE)
        CPU: 13ms

Feb 07 10:18:09 eOS-KDE systemd[1]: Starting zram swap service...
Feb 07 10:18:09 eOS-KDE zram-swap.sh[35826]: modprobe: FATAL: Module zram not found in directory /lib/modules/5.15.19-1-lts
Feb 07 10:18:09 eOS-KDE zram-swap.sh[35823]: Err main: Failed to load zram module, exiting
Feb 07 10:18:09 eOS-KDE systemd[1]: zram-swap.service: Main process exited, code=exited, status=1/FAILURE
Feb 07 10:18:09 eOS-KDE systemd[1]: zram-swap.service: Failed with result 'exit-code'.
Feb 07 10:18:09 eOS-KDE systemd[1]: Failed to start zram swap service.

question.

Should I install this script without creating a swapfile? Should I install without a swap partition or swap file?

Not using all potential space gained by compression

I have allocated a total size of 0.1GB (~102MB) to /dev/zram0 with compression algo zstd. Compression ratio is about 1:3, which is expected and good.

But after 100MB of compressed data is inside the zram object, it switches over to disk swapping, even though it is possible to fit about 200MB more data into the zram. Do I not understand how this works or is it a bug?

default priority

What is the reasoning behind the default swap priority of 15?

If I understand the swap priority correctly, shouldn't the default swap priority be set to 32767 to consider zram swap before anything else? I can't imagine that any other swap device would be faster?

Service occasionally fails to start

Lenovo-V14-ADA 5.11.0-40-generic #44-Ubuntu SMP Wed Oct 20 16:16:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Xubuntu 21.04

Every now and then service fails to start when computer boots up. I have started noticing it around this month only.
Roughly 1 in 5-10 boots, without any apparent pattern.

-- Boot 26befe3957714f72a5dd0e20bca542f8 --
гру 01 14:29:05 roalyr-Lenovo-V14-ADA systemd[1]: Starting zram swap service...
гру 01 14:29:05 roalyr-Lenovo-V14-ADA zram-swap.sh[646]: zramctl: /dev/zram0: failed to reset: Device or resource busy
гру 01 14:29:05 roalyr-Lenovo-V14-ADA zram-swap.sh[703]: zramctl: /dev/zram0: failed to reset: Device or resource busy
гру 01 14:29:05 roalyr-Lenovo-V14-ADA zram-swap.sh[761]: zramctl: /dev/zram0: failed to reset: Device or resource busy
гру 01 14:29:05 roalyr-Lenovo-V14-ADA zram-swap.sh[589]: Err init: Failed to initialize zram device
гру 01 14:29:05 roalyr-Lenovo-V14-ADA systemd[1]: zram-swap.service: Main process exited, code=exited, status=1/FAILURE
гру 01 14:29:05 roalyr-Lenovo-V14-ADA systemd[1]: zram-swap.service: Failed with result 'exit-code'.
гру 01 14:29:05 roalyr-Lenovo-V14-ADA systemd[1]: Failed to start zram swap service.

Backing device & recompression feature request

This script helps a lot but it still doesn't leverage all of zram's potential, at least when compiled with some optional features.

zram allows memory-constrained machines to not only compress some ram, but also write pages that have been idle for some time to disk. That requires compiling zram with CONFIG_ZRAM_WRITEBACK and CONFIG_ZRAM_TRACK_ENTRY_TIME, then echoing a number of seconds to /sys/block/zramX/idle.

zram also allows use of secondary compression algorithms through the CONFIG_ZRAM_MULTI_COMP option. So for example, if lzo can't compress a page, zstd could have a try.

I hope someone who knows how can add these features to the script.

Passing options to compression algorithms

Is it possible to use e.g. 3 cores for zstd? I tried "zstd -12 -T3" but it considered as invalid argument.
And what is the default compression level?
Thanks a lot

How to tell if zram is properly working?

How can I tell if zram is properly working?

And also are the compression methods generic compression methods, or are they specifically for this purpose?
I found zstd well documented on a github page, are there benchmarks for those compress/decompression methods for specifically zram?

I've noticed my swap partition shows as larger than before. Is this where the zram is? Should I be disabling normal swap and are there any downsides to not disabling it? Edit: I commented out the swap partition in /etc/fstab, is this what I should be doing when using zram? Also is the swap thing from zram supposed to show a larger size than my actual ram?

The script seemed to work quickly out of the box though, thanks!

No longer working on RPi4B Ubuntu 21.10 Impish

Hi there!

Thanks so much for this brilliant little performance booster! However, to my sadness, zram-swap no longer works in any of my Ubuntu 21.10 (Impish)-based endpoints -- I just noticed that after updating some of my 64bit Ubuntu based builds. I've tried re-downloading and re-installing it on a few endpoints now -- alas, the computer says no.

I haven't encountered this problem ever before, so idk if it's a change on Ubuntu's end or what. I can't start the service, and journalctl -xe says:

Oct 21 18:01:02 thismachine zram-swap.sh[2922]: modprobe: FATAL: Module zram not found in directory /lib/modules/5.13.0-1008-raspi
Oct 21 18:01:02 thismachine zram-swap.sh[2902]: Err main: Failed to load zram module, exiting
Oct 21 18:01:01 thismachine systemd[1]: Starting zram swap service...

The odd thing is that I didn't find any references to zram-swap from the other earlier kernel version subdirectories under the /lib/modules/ directory tree, but zram-swap still worked right up until 21.04 nonetheless.

Please advise, zram-swap is one of my favorites on Ubuntu RPi4B builds. I'd really like to see it working again. Cheers and all the best.

Hello?

Hello?
I am a Debian 10 user.
I wanted to use zram on my laptop HP Stream 13, but the installation method was very difficult.
And I installed zram very easily and happily with this you made.
Thank you. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.