Skip to content

Instantly share code, notes, and snippets.

@setop
Forked from voyeg3r/zram.sh
Created October 7, 2015 21:35
Show Gist options
  • Save setop/45de6b7ef562698c3951 to your computer and use it in GitHub Desktop.
Save setop/45de6b7ef562698c3951 to your computer and use it in GitHub Desktop.
How enable zram module on kernel (debian lik distro)?
#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
### END INIT INFO
# source: http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/%EF%BB%BF
# see also: http://mystilleef.blogspot.com/2011/10/enable-zram-in-fedora.html
# https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/
# download: wget -c https://raw.github.com/gist/1571378 -O /etc/init.d/zram && chmod +x $_
# warning: try this before --> modprobe -nv zram
# /dev/zram0 none swap sw,pri=5 0 0
######################################################################################
# para sistemas debian puros, para ubuntu onde o init é diferente tente outros meios
# Make the script executable & add it to default run levels:
# sudo chmod +x /etc/init.d/zram
# sudo update-rc.d zram defaults
# After booting verify the module is loaded with:
# lsmod | grep zram
# para ubuntu faça:
# https://gist.github.com/1583992
# sudo add-apt-repository ppa:shnatsel/zram
# sudo apt-get update
# sudo apt-get install zramswap-enabler
# leia também: http://vtnc.org/1638
#################################################################
start() {
# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# if something goes wrong, assume we have 1
[ "$num_cpus" != 0 ] || num_cpus=1
# set decremented number of CPUs
decr_num_cpus=$((num_cpus - 1))
# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
mem_total=$((mem_total_kb * 1024))
# load dependency modules
modprobe zram num_devices=$num_cpus
# initialize the devices
for i in $(seq 0 $decr_num_cpus); do
echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
done
# Creating swap filesystems
for i in $(seq 0 $decr_num_cpus); do
mkswap /dev/zram$i
done
# Switch the swaps on
for i in $(seq 0 $decr_num_cpus); do
swapon -p 100 /dev/zram$i
done
}
stop() {
# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# set decremented number of CPUs
decr_num_cpus=$((num_cpus - 1))
# Switching off swap
for i in $(seq 0 $decr_num_cpus); do
if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
swapoff /dev/zram$i
sleep 1
fi
done
sleep 1
rmmod zram
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
@setop
Copy link
Author

setop commented Nov 18, 2016

For multicore systems:

  • for linux < 3.15,configure one zram swap per cpu core.
  • for Linux >= 3.15, set maxs equal to the number of cores.

In this later case, the script cant be rewritten as the following:

case "$1" in
  "start")
    # load zram modules
    modprobe zram num_devices=1
    echo 4 > /sys/block/zram0/max_comp_streams
    echo $((8 * 1000 * 1000 * 1000 / 3)) > /sys/block/zram0/disksize
    mkswap /dev/zram0
    swapon -p 1000 /dev/zram0
    ;;
  "stop")
    swapoff /dev/zram0
    echo 1 > /sys/block/zram0/reset
    modprobe -r zram
    ;;
  *)
    echo "Usage: $(basename $0) (start | stop)"
    exit 1
    ;;
esac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment