Zram disks

This article describes use of zram and ram for ram disks. zram in place of tmpfs requires less ram, at the expense of greater processing overhead.

Prerequisites

 * >=sys-kernel/{gentoo,hardened,vanilla,zen,*}-sources-2.6.37.1
 * OR =sys-kernel/{gentoo,hardened,vanilla,zen,*}-sources-2.6.37 & this patch
 * CONFIG_ZRAM=y

Configuring Linux
In prescribing built-in zram, this article assumes persistent use, rather than transient.

zram-init
zram-init can be used, an ebuild for which is available in Martin Vaeth's user overlay. An alternative, manual method follows.

init configuration
It is assumed that mtime is not needed.

{{file |/etc/conf.d/local | ...

local_start { ...

echo $size > /sys/block/zram0/disksize mkfs.ext4 -O dir_nlink,extent,extra_isize,flex_bg,^has_journal,uninit_bg -m0 -b 4096 -N $inodes -L "zram0" /dev/zram0 mount -o barrier=0,commit=240,noatime,nodev,noexec,nosuid /dev/zram0 /$path_to/$mount_point chmod [aogu]+[rwx] /$mount_point

echo $size > /sys/block/zram[n]/disksize mkfs.ext4 -O dir_nlink,extent,extra_isize,flex_bg,^has_journal,uninit_bg -m0 -b 4096 -N $inodes -L "zram[...]" /dev/zram[...] mount -o barrier=0,commit=240,noatime,nodev,noexec,nosuid /dev/zram[...] /$path_to/$mount_point chmod [aogu]+[rwx] /$mount_point

echo $size > /sys/block/zram3/disksize mkfs.ext4 -O dir_nlink,extent,extra_isize,flex_bg,^has_journal,uninit_bg -m0 -b 4096 -N $inodes -L "zram[n]" /dev/zram[n] mount -o barrier=0,commit=240,noatime,nodev,noexec,nosuid /dev/zram[n] /$path_to/$mount_point chmod [aogu]+[rwx] /$mount_point

# We should always return 0 ... }}


 * The unit of $size is bytes.
 * Volume name doesn't matter.
 * $inodes should be an integer set greater than or equal to the number of files the zram disk will be required to hold.

zram /tmp
X populates /tmp after xinit, thus local must start before xdm for /tmp to be a zram disk.

zram portage temporary directory
Let this directory be /var/tmp

Portage requires exec bit: Note the difference between mount lines above and below.

{{file | /etc/conf.d/local | ...

local_start { ...

# Adjust disksize according to how much ram the machine has.

echo 2147483648 > /sys/block/zram0/disksize mkfs.ext4 -O dir_nlink,extent,extra_isize,flex_bg,^has_journal,uninit_bg -m0 -b 4096 -N 262144 -L "portage_tmp" /dev/zram0 mount -o barrier=0,commit=240,noatime,nodev /dev/zram0 /var/tmp chmod a+w /var/tmp ... }}

Setup for newer versions of the kernel
Running a more current version of the kernel (3.5 in this case), fewer commands are required. Note that it is recommended to use one zram device for each CPU core. In this case, a Core i5 machine with 8gb of ram gets 4 zram devices with 256mb each. At a typical 3:1 compression ratio, zrams will use about 333mb RAM with 1gb swapped out, for a net gain of 667mb, but worst case you lose 1 gb.