zram
Zram (以前は compcache と呼ばれていました) は、RAM 内に圧縮ブロックデバイスを作成するための Linux カーネルモジュールです。つまり、オンザフライのディスク圧縮の RAM ディスクです。zram で作成されたブロックデバイスは、スワップとして使ったり、汎用 RAM ディスクとして使ったりすることができます。zram の最も一般的な2つの使用法は、一時ファイルのストレージ (/tmp) とスワップデバイスです。初期の zram には後者の機能しか存在しておらず、ゆえに元の名前は "compcache" ("compressed cache") でした。
swap としての使用法
zswap を無効化する
Since it is enabled by default, disable zswap if you decide to use zram to avoid zswap acting as a swap cache in front of it. Having both enabled also results in incorrect zramctl(8) statistics as zram remains mostly unused; this is because zswap intercepts and compresses memory pages being swapped out before they can reach zram.
自動的に
zram-generator provides a systemd-zram-setup@.service
unit to automatically initialize zram devices without users needing to enable/start the template or its instances. See zram-generator(8) and zram-generator.conf(5).
For example, to create a zram swap device using zstd
and the entire available ram, install zram-generator, then create /etc/systemd/zram-generator.conf
with the following:
/etc/systemd/zram-generator.conf
[zram0] zram-size = ram compression-algorithm = zstd
Reboot, then check the swap status of your configured /dev/zramN
devices by reading the unit status of the systemd-zram-setup@zramN.service
instance(s), or by using zramctl(8).
Alternatively, zramswapAUR provides an automated script for setting up a swap with a higher priority and a default size of 20% of the RAM size of your system. To do this automatically on every boot, enable zramswap.service
. zramdAUR allows to setup zram automatically using zstd compression by default, its configuration can be changed at /etc/default/zramd
. It can be started at boot by enabling the zramd.service
unit.
手動で
To set up one lz4 compressed zram device with 32GiB capacity and a higher-than-normal priority (only for the current session):
# modprobe zram # echo lz4 > /sys/block/zram0/comp_algorithm # echo 32G > /sys/block/zram0/disksize # mkswap --label zram0 /dev/zram0 # swapon --priority 100 /dev/zram0
To disable it again, either reboot or run
# swapoff /dev/zram0 # rmmod zram
A detailed explanation of all steps, options and potential problems is provided in the official documentation of the zram module.
udev ルールを使って zram 上にスワップする
The example below describes how to set up swap on zram automatically at boot with a single udev rule. No extra package should be needed to make this work.
Explicitly load the module at boot:
/etc/modules-load.d/zram.conf
zram
Configure the number of /dev/zram
nodes you need:
/etc/modprobe.d/zram.conf
options zram num_devices=2
Create the udev rule as shown in the example:
/etc/udev/rules.d/99-zram.rules
KERNEL=="zram0", ATTR{disksize}="512M" RUN="/usr/bin/mkswap /dev/zram0", TAG+="systemd" KERNEL=="zram1", ATTR{disksize}="512M" RUN="/usr/bin/mkswap /dev/zram1", TAG+="systemd"
Add /dev/zram
to your fstab.
/etc/fstab
/dev/zram0 none swap defaults 0 0 /dev/zram1 none swap defaults 0 0