Differences between revisions 2 and 23 (spanning 21 versions)
Revision 2 as of 2014-03-26 23:23:21
Size: 1538
Editor: ?AntonioGalea
Comment:
Revision 23 as of 2022-09-20 03:04:45
Size: 2066
Editor: PaulWise
Comment: formatting
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#language en
'''zram''' (previously called '''compcache''') can create RAM based compressed block devices. It is a module of the Linux kernel since 3.2.
Line 2: Line 4:
'''zram''' (previously called '''compcache''') can create RAM based block devices. It is an experimental (staging) module of the Linux kernel since 3.2. If physical swap devices exist, they will also be used by the system, but with a lower priority than a Zram device.
Line 4: Line 6:
== Background information == See [[https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html|Linux zram documentation]] and WikiPedia:Zram for more info.
Line 6: Line 8:
 * https://code.google.com/p/compcache/
 * http://en.wikipedia.org/wiki/Zram
Similar results as with Zram can be achieved with [[Zswap]], though Zswap needs a physical swap device (or swap file).
Line 9: Line 10:
== Startup script == <<TableOfContents>>
Line 11: Line 12:
Copy the following script to /etc/init.d/zram: == Automated setup ==

The packages DebianPackage:zram-tools and DebianPackage:systemd-zram-generator can be used to automatically setup zram devices.

=== zram-tools ===

DebianPackage:zram-tools is available since Debian buster.

It currently only sets up zram devices on systemd-based systems.

To allow up to 60% of the RAM to be used as a zstd compressed swap space:
Line 13: Line 25:
## /etc/init.d/zram
#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: S
# Default-Stop: 0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description: Use compressed RAM as in-memory swap
### END INIT INFO

# Author: Antonio Galea <antonio.galea@gmail.com>

FRACTION=75

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))

case "$1" in
  "start")
    param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
    modprobe zram $param=$CPUS
    for n in `seq $CPUS`; do
      i=$((n - 1))
      echo $SIZE > /sys/block/zram$i/disksize
      mkswap /dev/zram$i
      swapon /dev/zram$i -p 10
    done
    ;;
  "stop")
    for n in `seq $CPUS`; do
      i=$((n - 1))
      swapoff /dev/zram$i
    done
    sleep .5
    modprobe -r zram
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac
sudo apt install zram-tools
echo -e "ALGO=zstd\nPERCENT=60" | sudo tee -a /etc/default/zramswap
sudo service zramswap reload
Line 59: Line 30:
then instruct you system to start it at boot time, with the command ==== sysvinit ====

zram-tools [[DebianBug:960524|does not yet support sysvinit systems]] but there is an [[https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=960524;filename=zramswap;msg=10|init script]] available. Download it and save it to ''/etc/init.d/zramswap'', then make the script executable and then instruct your system to start it at boot time:
Line 61: Line 35:
  insserv zram sudo chmod +x /etc/init.d/zramswap
sudo apt install insserv
sudo insserv zramswap
Line 63: Line 39:

=== systemd-zram-generator ===

systemd-zram-generator integrates zram devices into systemd using generated swap (and ext2 mount) units. This allows managing them individually with `systemctl` when desired.

== Manual setup ==

DebianMan:zramctl from DebianPackage:util-linux can list the zram devices present and their status and control them.

{{{
$ sudo zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 4G 2.1G 248.6M 260.4M 8 [SWAP]
}}}

zram (previously called compcache) can create RAM based compressed block devices. It is a module of the Linux kernel since 3.2.

If physical swap devices exist, they will also be used by the system, but with a lower priority than a Zram device.

See Linux zram documentation and Zram for more info.

Similar results as with Zram can be achieved with Zswap, though Zswap needs a physical swap device (or swap file).

Automated setup

The packages zram-tools and systemd-zram-generator can be used to automatically setup zram devices.

zram-tools

zram-tools is available since Debian buster.

It currently only sets up zram devices on systemd-based systems.

To allow up to 60% of the RAM to be used as a zstd compressed swap space:

sudo apt install zram-tools
echo -e "ALGO=zstd\nPERCENT=60" | sudo tee -a /etc/default/zramswap
sudo service zramswap reload

sysvinit

zram-tools does not yet support sysvinit systems but there is an init script available. Download it and save it to /etc/init.d/zramswap, then make the script executable and then instruct your system to start it at boot time:

sudo chmod +x /etc/init.d/zramswap
sudo apt install insserv
sudo insserv zramswap

systemd-zram-generator

systemd-zram-generator integrates zram devices into systemd using generated swap (and ext2 mount) units. This allows managing them individually with systemctl when desired.

Manual setup

zramctl from util-linux can list the zram devices present and their status and control them.

$ sudo zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4             4G  2.1G 248.6M 260.4M       8 [SWAP]