Zram

From Funtoo
Jump to navigation Jump to search

Compressed RAM based block devices

The zram module creates RAM based block devices named /dev/zram<id> (<id> = 0, 1, ...). Pages written to these disks are compressed and stored in memory itself. These disks allow very fast I/O and compression provides good amounts of memory savings. Some of the usecases include /tmp storage, use as swap disks, various caches under /var and maybe many more.

Get zram

Latest zram code is into mainline linux kernel. We recommend you to test it with 2.6.37 or later.

Device Drivers  --->
   [*] Staging drivers  ---> 
      <M> Compressed RAM block device support

Compile and install module.

Usage

Load module, create devices

To create one zram device, do

modprobe zram num_devices=1

Set Disksize

Set disk size by writing the value to sysfs node 'disksize' (in bytes). If disksize is not given, default value of 25% of RAM is used.

   Important

The disksize cannot be changed if the disk contains an data. So, for such a disk, you need to issue 'reset' (see below) before you can change its disksize.

  • Set disksize of 50MB for /dev/zram0:
echo $((50*1024*1024)) > /sys/block/zram0/disksize
  • Reset the sysfs node
echo 1 > /sys/block/zram0/reset

Use case

Swap on zram

As example we will use netbook with dualcore Intel Atom with 2G of ram. Disable usual swap, create a zram device and set the disksize to 200Mb and, finally, create a swap

swapoff -a
modprobe zram num_devices=1
echo $((200*1024*1024)) > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p10 /dev/zram0

To test, recompile something big, like chromium, and notice a speed gain.

Swap on zram and a separate /tmp

modprobe zram zram_num_devices=4
  • Initialize /dev/zram0 with 50MB disksize
echo  $((50*1024*1024)) > /sys/block/zram0/disksize
  • Initialize /dev/zram1 with default size (see above)
echo 1 > /sys/block/zram1/initistate
  • Activate
mkswap /dev/zram0
swapon /dev/zram0
mkfs.ext4 /dev/zram1
mount /dev/zram1 /tmp

Start zram automatically via Init Script (the easy way)

emerge zram-init
   Note
You may need to add
=sys-block/zram-init-<version> **
to /etc/portage/package.accept_keywords, particularly on embedded platforms (a likely scenario). Be sure to use version 2.7-r1 for kernels older than 3.15
  • Edit /etc/conf.d/zram-init (set type, size, flags, etc)
  • Add zram-init to the default runlevel
rc-config add zram-init default