Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 2 down vote accepted

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

share|improve this answer
1  
there's also zram-config in precise now. –  turbo Mar 16 '12 at 13:48
    
Why was it removed from Raring 13.04? –  NoBugs May 20 '13 at 0:54
    
The PPA probably hasn't been updated. –  Ryan Thompson May 20 '13 at 18:55

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.

share|improve this answer

Add the following line to /etc/rc.local, before the exit 0:

Hi,

I have installed zram-config in a 12.04 based derivative : Ubuntu Openbox, and added in rc.local just "service zram-config start".

That works, but I don't know why a simple "initctl start zram-config" does not trigger zram-config to be started at each boot. (The derivative can be found at linuxvillage.org and at the linuxvillage forum).

share|improve this answer
    
initctl enable zram-config should work. This space is supposed to provide answers though. –  turbo Mar 26 at 16:32

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.

share|improve this answer
    
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 6 '11 at 8: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 Thompson Mar 16 '12 at 20:17

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
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.