12

EDIT: In precise there's now zram-config. It's an upstart job compressing up to half of your ram spread over $(number of CPU cores) swap devices. It didn't allways start at boot but issuing sudo service zram-config start works.

I enabled compcache="256 M" in /etc/initramfs-tools/initramfs.conf as described here (by me :P). This - I believe - creates /dev/ramzswap0 but it is never enabled as swap. It works only after mkswap && swapon.

Then there is the module zram that creates /dev/zram. Is it something else? It works the same way but /dev/ramzswap is created from the module ramzswap.

At the end of the day I wanna have a compressed swap in ram and use the better of the two and for that I need to know how to enable it permanently in a non hackish way. How is this done?

I wrote about ramzswap in Lucid here but things have changed in Natty. You can still enable ramzswap in initramfs.conf but it doesn't get activated.

P.S.:I scanned all udev rules in /lib and/etc but found nothing of interest.

turbo
  • 4,622

4 Answers4

2

Straight from the Debian wiki. For me, this is the easiest.

First, copy and paste this code into /etc/init.d/zram

### 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>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization

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 && echo "disabled disk $n of $CPUS" &
    done
    wait
    sleep .5
    modprobe -r zram
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac

Next, execute these two commands:

sudo chmod +x /etc/init.d/zram
sudo /etc/init.d/zram start

Finally, to add zram at startup:

sudo update-rc.d zram defaults

Done.

mchid
  • 43,546
  • 8
  • 97
  • 150
2

I was struggling with the same problem.

Today I found an excellent blog post about it. http://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/

Although "sudo start zramswap" didn't work, when I restarted my PC it solved my problem perfectly.

Try it.

  • That's actually the first time I even noticed that there is /etc/init/. Normally I use /etc/init.d/ for starting stuff at boot. Fascinating... – turbo Sep 06 '11 at 08:10
  • /etc/init/ is where Upstart init scripts live. The ones in /etc/init.d are mostly just compatibility wrappers that call the ones in /etc/init. – Ryan C. Thompson Mar 16 '12 at 20:17
2

There's now a PPA that installs a proper Upstart script for enabling zram at boot-time. It chooses the correct size and number of compressed swap devices for your system.

https://launchpad.net/~shnatsel/+archive/zram

1

Here's the cheap solution. Add the following line to /etc/rc.local, before the exit 0:

find /dev/ -maxdepth 1 -name 'ramzswap*' | while read dev; do
    mkswap $dev
    swapon -p 1000 $dev
done
  • for i in /dev/ramzswap*;do ... would the better solution I think. Still a tad too hackish in my book. – turbo Aug 14 '11 at 19:49
  • How about find /dev/ -maxdepth 1 -name 'ramzswap*' -print0 | while read -d0 dev; do ... ? It'll obviate the problem of strange filename with newline in it. – Cbhihe Aug 28 '16 at 14:24
  • @turbo: old stuff here, but I believe Ryan's answer above is actually more general and safer from a scripting viewpoint than what you propose in yr comment. Generally speaking, yr for loop might cause trouble for file names with space and or special characters. This being said you do spare your system a process. – Cbhihe Aug 28 '16 at 14:27