Compress RAM with zRam in Slackware

Update: you can find the tarball for this script at: http://github.com/downloads/otzy007/enable-zRam-in-Slackware/enable-zram-noarch.tgz

Referring to this article it seems that using a swap disk is slower than compressing the data in the memory. Enabling this under Slackware Linux it’s pretty easy. Create the file /etc/rc.d/rc.zram and put this script into it:

#!/bin/bash
#
# /etc/rc.d/rc.zram
# Script to start zRam (Virtual Swap Compressed in RAM)
#
# Size of swap space in MB
# default 1GB

SIZE=1024

start() {
  modprobe zram
  echo $SIZE*1024*1024 | bc > /sys/block/zram0/disksize
  mkswap /dev/zram0
  swapon /dev/zram0
}

stop() {
  swapoff /dev/zram0
}

case "$1" in
  start)
    start
  ;;

  stop)
    stop
  ;;

  restart)
    echo 1 > /sys/block/zram0/reset
  ;;

  *)
  echo "Usage: $0 (start|stop|restart)"
esac

Remember to make it executable: chmod +x /etc/rc.d/rc.zram

Also copy this lines to /etc/rc.d/rc.local or /etc/rc.d/rc.M

# Start zram swap space
if [ -x /etc/rc.d/rc.zram ]
then
        /etc/rc.d/rc.zram start
fi