Giter Club home page Giter Club logo

zramd's Introduction

zramd

Automatically setup swap on zram ✨

Why swap on zram?

  • Significantly improves system responsiveness, especially when swap is under pressure.
  • More secure, user data leaks into swap are on volatile media.
  • Without swap-on-drive, there's better utilization of a limited resource: benefit of swap without the drive space consumption.
  • Further reduces the time to out-of-memory kill, when workloads exceed limits.

See also https://fedoraproject.org/wiki/Changes/SwapOnZRAM#Benefit_to_Fedora

Installation

Install on Arch Linux from the AUR

  • Install the zramd package form the AUR.
  • Enable and start the service:
    sudo systemctl enable --now zramd

Install on Ubuntu / Debian / Raspberry Pi OS

  • Head to the releases section and download the .deb file corresponding to your system architecture
  • Install using sudo dpkg -i DEB_FILE

Manual installation on any distribution without systemd

  • Head to the releases section and download the .tar.gz file corresponding to your system architecture
  • Extract the downloaded file e.g. tar xf TAR_FILE
  • Copy the zramd binary to /usr/local/bin.
  • There are various ways to setup autostart depending on your init system, for example you can add a line to /etc/rc.local e.g.
    /usr/local/bin/zramd start

Usage

  • zramd --help

    Usage: zramd <command> [<args>]
    
    Options:
      --help, -h             display this help and exit
      --version              display version and exit
    
    Commands:
      start                  load zram module and setup swap devices
      stop                   stop swap devices and unload zram module
    
  • zramd start --help

    Usage: zramd start [--algorithm ALGORITHM] [--fraction FRACTION] [--max-size MAX_SIZE] [--num-devices NUM_DEVICES] [--priority PRIORITY] [--skip-vm]
    
    Options:
      --algorithm ALGORITHM, -a ALGORITHM
                             zram compression algorithm
                             [default: zstd, env: ALGORITHM]
      --fraction FRACTION, -f FRACTION
                             maximum percentage of RAM allowed to use
                             [default: 1.0, env: FRACTION]
      --max-size MAX_SIZE, -m MAX_SIZE
                             maximum total MB of swap to allocate
                             [default: 8192, env: MAX_SIZE]
      --num-devices NUM_DEVICES, -n NUM_DEVICES
                             maximum number of zram devices to create
                             [default: 1, env: NUM_DEVICES]
      --priority PRIORITY, -p PRIORITY
                             swap priority
                             [default: 100, env: PRIORITY]
      --skip-vm, -s          skip initialization if running on a VM
                             [default: false, env: SKIP_VM]
      --help, -h             display this help and exit
      --version              display version and exit
    

Compilation

With Docker

  • Choose a valid git tag and run the make docker command e.g.
    # This command will create builds (.tar.gz and .deb) for all supported architectures
    CURRENT_TAG=v0.8.5 make docker

Manual Compilation

  • Install go (at least version 1.16), the command may be different depending on the distribution:
    # ArchLinux
    sudo pacman -S go
    
    # Ubuntu
    sudo apt-get install golang
  • You can run make to create a build with the same architecture as the current system:
    # If you cloned this repository you can just run make
    make
    
    # To create a Raspberry Pi build you need to specify the arch e.g.
    GOOS=linux GOARCH=arm GOARM=7 make
    
    # If you downloaded a .tar.gz or .zip (instead of cloning this repo) you need to specify additional info
    CURRENT_DATE=$(date --iso-8601=seconds) VERSION=Unknown make
    
    # So, to target the Raspberry Pi (without a repo) the command would look like
    CURRENT_DATE=$(date --iso-8601=seconds) VERSION=Unknown GOOS=linux GOARCH=arm GOARM=7 make
  • A new executable called zramd.bin will be created under the dist/ directory, now you can uninstall go if you like.

Configuration

With systemd

  • The default configuration file is located at /etc/default/zramd, just edit the variables as you like and restart the zramd service i.e. sudo systemctl restart zramd

Without systemd

  • Just change the arguments as you like, e.g. zramd start --max-size 1024 or zramd start --fraction 0.5 --priority 0

Troubleshooting

  • modprobe: FATAL: Module zram not found in directory /lib/modules/...
    It can happen if you try to start the zramd service after a kernel upgrade, you just need to restart your computer.
  • error: swapon: /dev/zramX: swapon failed: Operation not permitted
    First make sure that you are running as root (or at least that you have the required capabilities), also keep in mind that Linux only supports up to 32 swap devices (although it can start throwing the error from above when using a high value like 24).

Notes

  • Avoid using other zram-related packages along this one, zramd loads and unloads the zram kernel module assuming that the system is not using zram for other stuff (like mounting /tmp over zram).
  • Do not use zswap with zram, it would unnecessarily cause data to be compressed and decompressed back and forth.
  • When dealing with virtual machines, zram should be used on the host OS so guest memory can be compressed transparently, see also comments on original zram implementation.
    • If you boot the same system on a real computer as well as on a virtual machine, you can use the --skip-vm parameter to avoid initialization when running inside a virtual machine.
  • For best results install systemd-oomd or earlyoom (they may not be available on all distributions).
  • You can use swapon -show or zramctl to see all swap devices currently in use, this is useful if you want to confirm that all of the zram devices were setup correctly.
  • To quickly fill the memory, you can use tail /dev/zero but keep in mind that your system may become unresponsive if you do not have an application like earlyoom to kill tail just before it reaches the memory limit.
  • To test some zramd commands under the same conditions as the systemd unit you can use systemd-run e.g.
    sudo systemd-run -t \
      -p ProtectHostname=yes \
      -p PrivateNetwork=yes \
      -p IPAddressDeny=any \
      -p NoNewPrivileges=yes \
      -p RestrictNamespaces=yes \
      -p RestrictRealtime=yes \
      -p RestrictSUIDSGID=yes \
      -p MemoryDenyWriteExecute=yes \
      -p LockPersonality=yes \
      -p 'CapabilityBoundingSet=CAP_SYS_ADMIN CAP_SYS_MODULE' \
      -p 'SystemCallFilter=@module @swap @system-service' \
      -p SystemCallArchitectures=native \
      -p SystemCallErrorNumber=EPERM \
      -p 'DeviceAllow=block-* rw' \
      -p DevicePolicy=closed \
      -p RestrictAddressFamilies=AF_UNIX \
      -p RestrictAddressFamilies=~AF_UNIX \
      /usr/bin/zramd --version

zramd's People

Contributors

maximumadmin avatar monosans 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

Watchers

 avatar  avatar  avatar  avatar

zramd's Issues

systemd-oomd does not notice swap if zram is used

System Info:

    ~  inxi -Fazy                                                       ✔ 
System:
  Kernel: 5.15.28-1-MANJARO arch: x86_64 bits: 64 compiler: gcc v: 11.2.0
    parameters: BOOT_IMAGE=/@/boot/vmlinuz-5.15-x86_64
    root=UUID=2858d787-58d2-4b32-bfe2-dcd848223859 rw rootflags=subvol=@
    cryptdevice=UUID=ec00de08-393b-4d8c-a1ed-853a4de311ee:cryptroot apparmor=1
    security=apparmor bootsplash.bootfile=bootsplash-themes/manjaro/bootsplash
  Desktop: Xfce v: 4.16.0 tk: Gtk v: 3.24.29 info: xfce4-panel wm: xfwm
    v: 4.16.1 vt: 7 dm: LightDM v: 1.30.0 Distro: Manjaro Linux base: Arch Linux
Machine:
  Type: Desktop Mobo: ASUSTeK model: PRIME X570-PRO v: Rev X.0x
    serial: <superuser required> UEFI: American Megatrends v: 4204
    date: 02/24/2022
CPU:
  Info: model: AMD Ryzen 9 3900X bits: 64 type: MT MCP arch: Zen 2
    family: 0x17 (23) model-id: 0x71 (113) stepping: 0 microcode: 0x8701021
  Topology: cpus: 1x cores: 12 tpc: 2 threads: 24 smt: enabled cache:
    L1: 768 KiB desc: d-12x32 KiB; i-12x32 KiB L2: 6 MiB desc: 12x512 KiB
    L3: 64 MiB desc: 4x16 MiB
  Speed (MHz): avg: 3991 high: 4015 min/max: 2200/4918 boost: disabled
    scaling: driver: acpi-cpufreq governor: schedutil cores: 1: 3999 2: 3993
    3: 3989 4: 3980 5: 3998 6: 3995 7: 3972 8: 3995 9: 4015 10: 3964 11: 4002
    12: 4008 13: 3975 14: 3980 15: 4012 16: 4010 17: 3971 18: 3983 19: 4014
    20: 3994 21: 3983 22: 3989 23: 3987 24: 3994 bogomips: 191698
  Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm
  Vulnerabilities:
  Type: itlb_multihit status: Not affected
  Type: l1tf status: Not affected
  Type: mds status: Not affected
  Type: meltdown status: Not affected
  Type: spec_store_bypass
    mitigation: Speculative Store Bypass disabled via prctl and seccomp
  Type: spectre_v1
    mitigation: usercopy/swapgs barriers and __user pointer sanitization
  Type: spectre_v2
    mitigation: Retpolines, IBPB: conditional, STIBP: conditional, RSB filling
  Type: srbds status: Not affected
  Type: tsx_async_abort status: Not affected
Graphics:
  Device-1: AMD Navi 10 [Radeon RX 5600 OEM/5600 XT / 5700/5700 XT]
    vendor: ASUSTeK driver: amdgpu v: kernel pcie: gen: 4 speed: 16 GT/s
    lanes: 16 ports: active: HDMI-A-1 empty: DP-1,DP-2,DP-3 bus-ID: 0b:00.0
    chip-ID: 1002:731f class-ID: 0300
  Display: x11 server: X.Org v: 1.21.1.3 compositor: xfwm v: 4.16.1 driver:
    X: loaded: amdgpu unloaded: modesetting,radeon alternate: fbdev,vesa
    gpu: amdgpu display-ID: :0.0 screens: 1
  Screen-1: 0 s-res: 1920x1080 s-dpi: 96 s-size: 508x285mm (20.00x11.22")
    s-diag: 582mm (22.93")
  Monitor-1: HDMI-A-1 mapped: HDMI-A-0 model: Samsung SyncMaster
    serial: <filter> built: 2009 res: 1920x1080 hz: 60 dpi: 102 gamma: 1.2
    size: 477x268mm (18.78x10.55") diag: 587mm (23.1") ratio: 16:9, 15:9
    modes: max: 1920x1080 min: 640x480
  Message: Unable to show GL data. Required tool glxinfo missing.
Audio:
  Device-1: AMD Navi 10 HDMI Audio driver: snd_hda_intel v: kernel pcie:
    gen: 4 speed: 16 GT/s lanes: 16 bus-ID: 0b:00.1 chip-ID: 1002:ab38
    class-ID: 0403
  Device-2: AMD Starship/Matisse HD Audio vendor: ASUSTeK
    driver: snd_hda_intel v: kernel pcie: gen: 4 speed: 16 GT/s lanes: 16
    bus-ID: 0d:00.4 chip-ID: 1022:1487 class-ID: 0403
  Sound Server-1: ALSA v: k5.15.28-1-MANJARO running: yes
  Sound Server-2: JACK v: 1.9.20 running: no
  Sound Server-3: PulseAudio v: 15.0 running: yes
  Sound Server-4: PipeWire v: 0.3.48 running: yes
Network:
  Device-1: Intel I211 Gigabit Network vendor: ASUSTeK driver: igb v: kernel
    pcie: gen: 1 speed: 2.5 GT/s lanes: 1 port: f000 bus-ID: 05:00.0
    chip-ID: 8086:1539 class-ID: 0200
  IF: enp5s0 state: up speed: 1000 Mbps duplex: full mac: <filter>
  IF-ID-1: vmnet1 state: unknown speed: N/A duplex: N/A mac: <filter>
  IF-ID-2: vmnet8 state: unknown speed: N/A duplex: N/A mac: <filter>
Bluetooth:
  Device-1: Cambridge Silicon Radio Bluetooth Dongle (HCI mode) type: USB
    driver: btusb v: 0.8 bus-ID: 1-2:2 chip-ID: 0a12:0001 class-ID: e001
  Report: rfkill ID: hci0 rfk-id: 0 state: up address: see --recommends
Drives:
  Local Storage: total: 1.82 TiB used: 154.94 GiB (8.3%)
  SMART Message: Unable to run smartctl. Root privileges required.
  ID-1: /dev/nvme0n1 maj-min: 259:1 vendor: Samsung model: SSD 970 EVO 500GB
    size: 465.76 GiB block-size: physical: 512 B logical: 512 B speed: 31.6 Gb/s
    lanes: 4 type: SSD serial: <filter> rev: 2B2QEXE7 temp: 37.9 C scheme: GPT
  ID-2: /dev/nvme1n1 maj-min: 259:0 vendor: Samsung model: SSD 970 EVO 500GB
    size: 465.76 GiB block-size: physical: 512 B logical: 512 B speed: 31.6 Gb/s
    lanes: 4 type: SSD serial: <filter> rev: 2B2QEXE7 temp: 24.9 C scheme: GPT
  ID-3: /dev/sda maj-min: 8:0 vendor: Samsung model: SSD 870 QVO 1TB
    size: 931.51 GiB block-size: physical: 512 B logical: 512 B speed: 6.0 Gb/s
    type: SSD serial: <filter> rev: 2B6Q scheme: GPT
Partition:
  ID-1: / raw-size: 419.26 GiB size: 419.26 GiB (100.00%)
    used: 30.22 GiB (7.2%) fs: btrfs dev: /dev/dm-0 maj-min: 254:0
    mapped: cryptroot
  ID-2: /boot/efi raw-size: 512 MiB size: 511 MiB (99.80%)
    used: 26.1 MiB (5.1%) fs: vfat dev: /dev/nvme0n1p1 maj-min: 259:5
  ID-3: /home raw-size: 419.26 GiB size: 419.26 GiB (100.00%)
    used: 30.22 GiB (7.2%) fs: btrfs dev: /dev/dm-0 maj-min: 254:0
    mapped: cryptroot
Swap:
  Kernel: swappiness: 60 (default) cache-pressure: 100 (default)
  ID-1: swap-1 type: zram size: 8 GiB used: 0 KiB (0.0%) priority: 100
    dev: /dev/zram0
Sensors:
  System Temperatures: cpu: N/A mobo: N/A gpu: amdgpu temp: 37.0 C mem: 34.0 C
  Fan Speeds (RPM): N/A gpu: amdgpu fan: 0
Info:
  Processes: 472 Uptime: 5m wakeups: 0 Memory: 31.32 GiB used: 3.4 GiB (10.9%)
  Init: systemd v: 250 tool: systemctl Compilers: gcc: 11.2.0 clang: 13.0.1
  Packages: 1219 pacman: 1210 lib: 340 flatpak: 9 Shell: Zsh v: 5.8.1
  running-in: xfce4-terminal inxi: 3.3.15

systemctl status systemd-oomd

Apr 15 17:47:48 BlackBox systemd-oomd[884]: Swap is currently not detected; memory pressure usage will be degraded

zstd compression level.

Please give an easy way to control the zstd compression level.
It is tunable with compression levels ranging from negative 7 (fastest) to 22 (slowest in compression speed, but best compression ratio).
Compression speed can vary by a factor of 20 or more between the fastest and slowest levels, while decompression is uniformly fast, varying by less than 20% between the fastest and slowest levels.
Ref: https://en.wikipedia.org/wiki/Zstd

Error statting /zram0: No such file or directory at boot

After enabling the service and rebooting (using the default configuration), I get bunch of Error statting /zram0: No such file or directory coming from udiskd at random intervals for about a minute after booting:

mai 30 19:04:56 terence-desktop systemd[1]: Starting Automatically setup swap on zram...
mai 30 19:04:56 terence-desktop kernel: zram: Added device: zram0
mai 30 19:04:56 terence-desktop systemd[1]: Created slice system-udisks2\x2dzram\x2dsetup.slice.
mai 30 19:04:56 terence-desktop systemd[1]: Starting UDisks2 managed ZRAM device zram0 setup...
mai 30 19:04:56 terence-desktop kernel: zram0: detected capacity change from 0 to 16777216
mai 30 19:04:56 terence-desktop systemd[1]: [email protected]: Deactivated successfully.
mai 30 19:04:56 terence-desktop systemd[1]: Finished UDisks2 managed ZRAM device zram0 setup.
mai 30 19:04:56 terence-desktop kernel: Adding 8388604k swap on /dev/zram0.  Priority:100 extents:1 across:8388604k SSFS
mai 30 19:04:56 terence-desktop systemd[1]: Finished Automatically setup swap on zram.
mai 30 19:04:56 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:04:56 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:05:04 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:05:07 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:05:07 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:05:08 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory
mai 30 19:05:20 terence-desktop udisksd[623]: Error statting /zram0: No such file or directory

I still seem to get a functioning zram swap:

               total        used        free      shared  buff/cache   available
Mem:            15Gi       2,7Gi       8,3Gi       194Mi       4,6Gi        11Gi
Swap:          8,0Gi          0B       8,0Gi

Since the message is coming from udisks2, should an issue be opened there or is there something zramd might do incorrectly?

Cannot start zramd

I am attempting to use zramd on Artix Linux with dinit as the unit system and I cannot start zramd. I get error: failed to execute "modprobe zram num_devices=1" Does zramd need a program to start before it to work?

Does --max-size matter on low memory systems?

By default, zramd will allocate a maximum of eight gigabytes for swap. On systems such as the RPI, this is significantly higher than the actual amount of memory installed by the manufacturer. Do you recommend adjusting this parameter to cater to low memory systems, or does the default configuration simply work as well?

systemd v254 changes swapon output for zram device

Hi, let me start by expressing my gratitude for creating/offering this excellent application. Have been using it without hiccups for a long time. But after my Arch Linux box got the systemd v254 recently, I did notice some peculiarity. Where before this upgrade swapon consistently reported /dev/zram0, it now shows /zram0 instead.

$ sudo swapon --noheadings --raw --show=name,size,used
/dev/sda2 4,2G 0B
/zram0 1024M 0B

UPDATE:
After doing some more experimenting it turned out we can keep PrivateNetwork and explicitly disable PrivateMounts.

Nothing crucial apparently, the zram device is created and still resides under /dev/zram0. But it's confusing and I opted to disable PrivateNetwork PrivateMounts to get back to the output one would expect from swapon.

PrivateNetwork=yes

$ cat /etc/systemd/system/zramd.service.d/fix-swapon-output.conf
[Service]
# systemd v254 introduced some changes that apparently affected
# swapon output - bring back known good behaviour
# https://github.com/systemd/systemd/commit/6b2d576f2b38c0385bb056af328754ec8966f9fd
PrivateMounts=no

Not sure whether there might be better ways to deal with this systemd change. I'll leave that up to you.

Zramctl does not show the correct size of the swap

I have set the fraction to 0.5 and the maximum_size to 16384MB, but running zramctl or the system monitor shows the size of the zram swap to be 7.5GB. It’s supposed to be 16GB, as that is the uncompressed maximum. This is a problem, because I want to run a script which automatically frees the swap if there is enough free memory. If the number reported is incorrect, so e.g. there are 5GB of pages in swap but the uncompressed size is more like 10GB, this could crash my system by leading to an out-of-memory condition.

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.