Differences between revisions 12 and 20 (spanning 8 versions)
Revision 12 as of 2016-09-02 12:30:37
Size: 1960
Editor: ?MertDirik
Comment: fix # of CPUs
Revision 20 as of 2019-12-16 17:17:18
Size: 2187
Editor: JorgePeixoto
Comment: buster has been released
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
'''zram''' (previously called '''compcache''') can create RAM based block devices. It is an experimental (staging) module of the Linux kernel since 3.2. '''zram''' (previously called '''compcache''') can create RAM based block devices. It is a module of the Linux kernel since 3.2.
Line 4: Line 4:
== Background information == See https://code.google.com/p/compcache/ and WikiPedia:Zram for more info.
Line 6: Line 6:
 * https://code.google.com/p/compcache/
 * WikiPedia:Zram
== Installation and use ==
Line 9: Line 8:
== Startup script == Starting at buster (released July 2019) it's possible to simply install the '''zram-tools''' package and customize '''/etc/default/zramswap''' for your needs.
Line 11: Line 10:
Copy the following script to ''/etc/init.d/zram'': For systems using a standard SysV-based init system, it's possible to copy the following script to ''/etc/init.d/zram'':
Line 13: Line 12:
#!sh
#!/bin/sh

#! /bin/sh
# Author: Antonio Galea <antonio.galea@gmail.com>
#
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or later, see terms at
# https://gnu.org/licenses/gpl-3.0.txt
Line 25: Line 30:
# Author: Antonio Galea <antonio.galea@gmail.com>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or above, see terms at
# https://gnu.org/licenses/gpl-3.0.txt
Line 31: Line 31:

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`nproc`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))
MEMORY=$(perl -ne '/^MemTotal:\s+(\d+)/ && print $1*1024' < /proc/meminfo)
CPUS=$(nproc)
SIZE=$((MEMORY * FRACTION / 100 / CPUS))
Line 37: Line 36:
  "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 && echo "disabled disk $n of $CPUS" &
    done
wait
    sleep .5
    modprobe -r zram
;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
    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 --priority 10
        done
    
;;
    stop)
    
for n in $(seq $CPUS)
        do
      
i=$((n - 1))
       swapoff /dev/zram$i && echo "zram: disabled disk $n of $CPUS" &
        done

    
wait
     sleep .5
        modprobe --remove zram
    
;;
    *)
     echo "Usage: $(basename $0) (start | stop)"
        exit 1
     ;;
Line 61: Line 64:

# End of file
Line 63: Line 68:
then you may have to make the script executable: then make the script executable:
Line 68: Line 73:
then instruct your system to start it at boot time, with the command then instruct your system to start it at boot time, with the command (skip this on systemd)
Line 70: Line 75:
  insserv zram apt-get install insserv
insserv zram
Line 72: Line 78:

== NB! ==

For the version with the systemd should be used package [[http://packages.ubuntu.com/vivid/zram-config|Ubuntu zram-config 0.3]] and newer.

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

See https://code.google.com/p/compcache/ and Zram for more info.

Installation and use

Starting at buster (released July 2019) it's possible to simply install the zram-tools package and customize /etc/default/zramswap for your needs.

For systems using a standard SysV-based init system, it's possible to copy the following script to /etc/init.d/zram:

# Author: Antonio Galea <antonio.galea@gmail.com>
#
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or later, see terms at
# https://gnu.org/licenses/gpl-3.0.txt

### 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

FRACTION=75
MEMORY=$(perl -ne '/^MemTotal:\s+(\d+)/ && print $1*1024' < /proc/meminfo)
CPUS=$(nproc)
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 --priority 10
        done
        ;;
    stop)
        for n in $(seq $CPUS)
        do
            i=$((n - 1))
            swapoff /dev/zram$i && echo "zram: disabled disk $n of $CPUS" &
        done

        wait
        sleep .5
        modprobe --remove zram
        ;;
    *)
        echo "Usage: $(basename $0) (start | stop)"
        exit 1
        ;;
esac

# End of file

then make the script executable:

chmod +x /etc/init.d/zram

then instruct your system to start it at boot time, with the command (skip this on systemd)

apt-get install insserv
insserv zram