Zram: Difference between revisions
mNo edit summary |
(import new version) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Installation]] | [[Category:Installation]] | ||
{{warning|There are some [https://forum.proxmox.com/threads/bug-soft-lockup.51653/ reports that zRam may cause soft lockups on certain hardware], if you run into such issues try to disable zRam}} | |||
== Introduction == | == Introduction == | ||
<code>zram</code> is a Linux kernel module to create RAM based block devices <ref>https://www.kernel.org/doc/Documentation/blockdev/zram.txt</ref>. | |||
Its main feature distinguishing it from the classical ramdisks such as <code>tmpfs</code> and <code>ramfs</code> is the on-the-fly compression via LZ4, LZO (and LZO-RLE) or ZSTD, | |||
LZO-RLE (since 5.1) being the default <ref>https://en.wikipedia.org/wiki/Zram</ref>. | |||
LZO compression is more efficient, but LZ4 is faster at compression/decompression. | LZO compression is more efficient, but LZ4 is faster at compression/decompression. ZSTD should be between LZO and LZ4 in speed. | ||
Use cases for | Use cases for <code>zram</code> are compressed swap disks or general purpose RAM disks, providing high throughput and low latency. | ||
The compression is of special interest for low memory devices. The huge drawbacks are of course the possible data loss | The compression is of special interest for low memory devices. The huge drawbacks are of course the possible data loss | ||
on power failure and the limited disk size. | on power failure and the limited disk size. | ||
With | With <code>zswap</code> there exists a RAM-based compressed cache for swap devices as alternative. | ||
<code>zswap</code> provides the ability to compress swapped pages and stores less frequent pages to a physical disk, which is therefore required <ref>https://en.wikipedia.org/wiki/Zswap</ref>. | |||
== Setup and usage == | == Setup and usage == | ||
The easiest way to manage | The easiest way to manage <code>zram</code> block devices is via the <code>zramctl</code> utility provided by <code>util-linux</code> package which is already installed on any Proxmox VE. | ||
For this to work, the | For this to work, the <code>zram</code> kernel module, which has been part of the Linux mainline kernel since version 3.14, must be loaded, as Proxmox VE does not load this module by default. | ||
=== Enable a zram-disk === | === Enable a zram-disk === | ||
To load the | To load the <code>zram</code> module, run the following command in a root shell: | ||
modprobe zram | modprobe zram | ||
This will load the kernel module and create a new block device accessible under | This will load the kernel module and create a new block device accessible under <code>/dev/zram0</code>. | ||
With | With an initial disk size of 0B the zram-disk is rather useless, so one might change the size to something usable, e.g. 4GiB. | ||
This is done with the command: | This is done with the command: | ||
zramctl --size 4G /dev/zram0 | zramctl --size 4G /dev/zram0 | ||
Statistics for the newly created block device can be obtained via | Statistics for the newly created block device can be obtained via <code>zramctl</code>. | ||
For example, we can display the properties for the newly created zram-disk with: | For example, we can display the properties for the newly created zram-disk with: | ||
# zramctl | # zramctl | ||
Line 41: | Line 43: | ||
swapon /dev/zram0 -p 10 | swapon /dev/zram0 -p 10 | ||
The | The <code>-p</code> option of <code>swapon</code> gives this swap device a higher priority over others. Using the zram-disk is then preferred over other swap devices. | ||
=== Disable the zram-disk === | === Disable the zram-disk === | ||
Line 53: | Line 55: | ||
zramctl --reset /dev/zram0 | zramctl --reset /dev/zram0 | ||
For further | For further details please refer to the manpage <code>man zramctl</code> or <ref>https://www.kernel.org/doc/Documentation/blockdev/zram.txt</ref>. | ||
== Permanent setup == | == Permanent setup == | ||
Line 59: | Line 61: | ||
This is based on a setup described in <ref>https://wiki.archlinux.org/index.php/Improving_performance#Zram_or_zswap</ref>. | This is based on a setup described in <ref>https://wiki.archlinux.org/index.php/Improving_performance#Zram_or_zswap</ref>. | ||
To permanently set up a zram-disk as swap device, perform the following steps. | To permanently set up a zram-disk as swap device, perform the following steps. | ||
Add a file | Add a file <code>/etc/modules-load.d/zram.conf</code> with the content <code>zram</code> by running: | ||
echo "zram" > /etc/modules-load.d/zram.conf | echo "zram" > /etc/modules-load.d/zram.conf | ||
This will ensure the kernel module is loaded at boot. Next | This will ensure the kernel module is loaded at boot. Next, you need a udev rule to automatically set the hard drive size and create a swap device. | ||
For this, run: | For this, run: | ||
echo 'KERNEL=="zram0", ATTR{disksize}="4G" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules | echo 'KERNEL=="zram0", ACTION=="add", ATTR{comp_algorithm}="zstd", ATTR{disksize}="4G", RUN+="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules | ||
Replace the '4G' | Replace the '4G' disk size and the <code>comp_algorithm</code> with the values you want for your system. In the last step, define the device as a swap disk in your <code>/etc/fstab</code> by executing: | ||
echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab | echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab | ||
Make sure to use | Make sure to use <code>>></code> and not <code>></code> in this command, otherwise you will overwrite the existing configuration. | ||
An additional option 'nofail' can be set in case `/dev/zram0` is not always available on boot to treat it as optional. | |||
=== Setting up multiple disks === | === Setting up multiple disks at boot === | ||
If you wish to create more than one zram-disk at boot, you | If you wish to create more than one zram-disk at boot, you can pass the number of disks you want to have to the kernel module, which is achieved by: | ||
echo options zram num_devices=N > /etc/modprobe.d/zram.conf | echo options zram num_devices=N > /etc/modprobe.d/zram.conf | ||
where N should be replaced by the number of zram-disks you want to create. | where N should be replaced by the number of zram-disks you want to create. Further you have to setup udev rules to format and fstab entries to automount also these disks. | ||
== Alternative Setup using zram-tools == | |||
=== Setup and usage === | |||
An easy alternative way to manage <code>zram</code> block devices is via <code>zram-tools</code> which can be installed on your Proxmox VE. | |||
apt install zram-tools | |||
=== Enable zram === | |||
After installation, the systemd service “zramswap.service” automatically activates zram as swap device with the default configuration from <code>/etc/default/zramswap</code> | |||
<pre> | |||
# Compression algorithm selection | |||
# speed: lz4 > zstd > lzo | |||
# compression: zstd > lzo > lz4 | |||
# This is not inclusive of all that is available in latest kernels | |||
# See /sys/block/zram0/comp_algorithm (when zram module is loaded) to see | |||
# what is currently set and available for your kernel[1] | |||
# [1] https://github.com/torvalds/linux/blob/master/Documentation/blockdev/zram.txt#L86 | |||
#ALGO=lz4 | |||
# Specifies the amount of RAM that should be used for zram | |||
# based on a percentage the total amount of available memory | |||
# This takes precedence and overrides SIZE below | |||
#PERCENT=50 | |||
# Specifies a static amount of RAM that should be used for | |||
# the ZRAM devices, this is in MiB | |||
#SIZE=256 | |||
# Specifies the priority for the swap devices, see swapon(2) | |||
# for more details. Higher number = higher priority | |||
# This should probably be higher than hdd/ssd swaps. | |||
#PRIORITY=100 | |||
</pre> | |||
The status of the zram can be checked with the following command: | |||
<pre> | |||
# zramswap status | |||
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT | |||
/dev/zram0 lz4 256M 4K 63B 20K 40 [SWAP] | |||
</pre> | |||
The additional zram swap space should now be shown in the following command: | |||
<pre> | |||
free -h | |||
total used free shared buff/cache available | |||
Mem: 7.8Gi 1.3Gi 6.4Gi 28Mi 267Mi 6.4Gi | |||
Swap: 255Mi 0B 255Mi | |||
</pre> | |||
=== Disable zram === | |||
To disable your zram-disk you have to execute the following command: | |||
zramswap stop | |||
=== Configuration === | |||
There are three things you can configure: | |||
* compression | |||
* amount of memory (default is 256MB) | |||
* priority | |||
The size of the zram device can be configured either by absolute value (<code>SIZE</code>) or percent (<code>PERCENT</code>) in <code>/etc/default/zramswap</code>. e.g. | |||
PERCENT=20 | |||
To apply the new configuration, restart <code>zramswap</code>. | |||
zramswap restart | |||
=== Limitation === | |||
When configuring <code>zram</code>, the size of a zram device controls the maximum uncompressed amount of data it can store, not the maximum compressed size. You can configure the zram's size to be equal to or even greater than your system's physical RAM capacity, as long as the compressed size on physical RAM will not exceed your system's physical RAM capacity. | |||
Using zramswap with a physical swap is not recommended. Moving pages between zramswap and physical swap can cause high latencies and destabilize your system. | |||
== References == | == References == |
Latest revision as of 09:52, 18 December 2024
Warning: There are some reports that zRam may cause soft lockups on certain hardware, if you run into such issues try to disable zRam |
Introduction
zram
is a Linux kernel module to create RAM based block devices [1].
Its main feature distinguishing it from the classical ramdisks such as tmpfs
and ramfs
is the on-the-fly compression via LZ4, LZO (and LZO-RLE) or ZSTD,
LZO-RLE (since 5.1) being the default [2].
LZO compression is more efficient, but LZ4 is faster at compression/decompression. ZSTD should be between LZO and LZ4 in speed.
Use cases for zram
are compressed swap disks or general purpose RAM disks, providing high throughput and low latency.
The compression is of special interest for low memory devices. The huge drawbacks are of course the possible data loss
on power failure and the limited disk size.
With zswap
there exists a RAM-based compressed cache for swap devices as alternative.
zswap
provides the ability to compress swapped pages and stores less frequent pages to a physical disk, which is therefore required [3].
Setup and usage
The easiest way to manage zram
block devices is via the zramctl
utility provided by util-linux
package which is already installed on any Proxmox VE.
For this to work, the zram
kernel module, which has been part of the Linux mainline kernel since version 3.14, must be loaded, as Proxmox VE does not load this module by default.
Enable a zram-disk
To load the zram
module, run the following command in a root shell:
modprobe zram
This will load the kernel module and create a new block device accessible under /dev/zram0
.
With an initial disk size of 0B the zram-disk is rather useless, so one might change the size to something usable, e.g. 4GiB.
This is done with the command:
zramctl --size 4G /dev/zram0
Statistics for the newly created block device can be obtained via zramctl
.
For example, we can display the properties for the newly created zram-disk with:
# zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lzo 4G 0B 0B 0B 4
To make this device a swap device run the two following commands:
mkswap /dev/zram0 swapon /dev/zram0 -p 10
The -p
option of swapon
gives this swap device a higher priority over others. Using the zram-disk is then preferred over other swap devices.
Disable the zram-disk
To disable the swap and also zram-disk we first run:
swapoff /dev/zram0
to disable it as swap device and finally reset the size of the zram-disk to zero by:
zramctl --reset /dev/zram0
For further details please refer to the manpage man zramctl
or [4].
Permanent setup
This is based on a setup described in [5].
To permanently set up a zram-disk as swap device, perform the following steps.
Add a file /etc/modules-load.d/zram.conf
with the content zram
by running:
echo "zram" > /etc/modules-load.d/zram.conf
This will ensure the kernel module is loaded at boot. Next, you need a udev rule to automatically set the hard drive size and create a swap device. For this, run:
echo 'KERNEL=="zram0", ACTION=="add", ATTR{comp_algorithm}="zstd", ATTR{disksize}="4G", RUN+="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules
Replace the '4G' disk size and the comp_algorithm
with the values you want for your system. In the last step, define the device as a swap disk in your /etc/fstab
by executing:
echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab
Make sure to use >>
and not >
in this command, otherwise you will overwrite the existing configuration.
An additional option 'nofail' can be set in case `/dev/zram0` is not always available on boot to treat it as optional.
Setting up multiple disks at boot
If you wish to create more than one zram-disk at boot, you can pass the number of disks you want to have to the kernel module, which is achieved by:
echo options zram num_devices=N > /etc/modprobe.d/zram.conf
where N should be replaced by the number of zram-disks you want to create. Further you have to setup udev rules to format and fstab entries to automount also these disks.
Alternative Setup using zram-tools
Setup and usage
An easy alternative way to manage zram
block devices is via zram-tools
which can be installed on your Proxmox VE.
apt install zram-tools
Enable zram
After installation, the systemd service “zramswap.service” automatically activates zram as swap device with the default configuration from /etc/default/zramswap
# Compression algorithm selection # speed: lz4 > zstd > lzo # compression: zstd > lzo > lz4 # This is not inclusive of all that is available in latest kernels # See /sys/block/zram0/comp_algorithm (when zram module is loaded) to see # what is currently set and available for your kernel[1] # [1] https://github.com/torvalds/linux/blob/master/Documentation/blockdev/zram.txt#L86 #ALGO=lz4 # Specifies the amount of RAM that should be used for zram # based on a percentage the total amount of available memory # This takes precedence and overrides SIZE below #PERCENT=50 # Specifies a static amount of RAM that should be used for # the ZRAM devices, this is in MiB #SIZE=256 # Specifies the priority for the swap devices, see swapon(2) # for more details. Higher number = higher priority # This should probably be higher than hdd/ssd swaps. #PRIORITY=100
The status of the zram can be checked with the following command:
# zramswap status NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 256M 4K 63B 20K 40 [SWAP]
The additional zram swap space should now be shown in the following command:
free -h total used free shared buff/cache available Mem: 7.8Gi 1.3Gi 6.4Gi 28Mi 267Mi 6.4Gi Swap: 255Mi 0B 255Mi
Disable zram
To disable your zram-disk you have to execute the following command:
zramswap stop
Configuration
There are three things you can configure:
- compression
- amount of memory (default is 256MB)
- priority
The size of the zram device can be configured either by absolute value (SIZE
) or percent (PERCENT
) in /etc/default/zramswap
. e.g.
PERCENT=20
To apply the new configuration, restart zramswap
.
zramswap restart
Limitation
When configuring zram
, the size of a zram device controls the maximum uncompressed amount of data it can store, not the maximum compressed size. You can configure the zram's size to be equal to or even greater than your system's physical RAM capacity, as long as the compressed size on physical RAM will not exceed your system's physical RAM capacity.
Using zramswap with a physical swap is not recommended. Moving pages between zramswap and physical swap can cause high latencies and destabilize your system.