Zram: Difference between revisions
m Added categories and navbox |
No edit summary |
||
| Line 1: | Line 1: | ||
==Quick | zram is a Linux kernel module that creates a RAM backed block device with features including on-the-fly compression. | ||
Because zram can transparently compress data, it may be used as a swapfile which effectively allows RAM to be compressed as it is paged out. This is particularly useful for memory constrained systems. | |||
zswap is closely related with the biggest difference that less frequently used pages can be swapped out from RAM to physical disk. | |||
==Quick usage guide== | |||
Firstly, load the <code>zram</code> kernel module as root. This should create <code>/dev/zram0</code> which you can configure with <code>zramctl</code>. You may set the size and compression algorithm when creating the block device. | |||
Supported compression algorithms include: <code>lz4</code> (more efficient compression but slower), <code>lzo</code> (less efficient but faster). | |||
{{Highlight | {{Highlight | ||
| code = ## Load the zram module. It should have created /dev/zram0 by default. | | code = ## Load the zram module. It should have created /dev/zram0 by default. | ||
| Line 9: | Line 17: | ||
## Define the disk size and algorithm of zram0 | ## Define the disk size and algorithm of zram0 | ||
# zramctl --size 128m --algorithm lz4 /dev/zram0 | # zramctl --size 128m --algorithm lz4 /dev/zram0 | ||
| lang = terminal | |||
}} | |||
## Make a filesystem and mount it | === Using zram with a Ext4 filesystem === | ||
Once the block device is created (as <code>/dev/zram0</code>), you can use it as any normal block device and create a normal filesystem or swap space with it. We'll first make a ext4 filesystem to show off some additional features with zram. | |||
{{Highlight | |||
| code = ## Make a filesystem and mount it | |||
# mkfs.ext4 /dev/zram0 | # mkfs.ext4 /dev/zram0 | ||
# mount /dev/zram0 /mnt | # mount /dev/zram0 /mnt | ||
| Line 18: | Line 31: | ||
| lang = terminal | | lang = terminal | ||
}} | }} | ||
The actual memory consumption of the block device can be viewed with <code>zramctl</code>. After running <code>dd if=/dev/zero of=test.bin bs=512 count=100000</code>, we now see the filesystem being approximately 50% full whilst only using 4.1KB of actual memory. | |||
{{Highlight | {{Highlight | ||
| code = ## Write all zeros to a file, which should compress very well | | code = ## Write all zeros to a file, which should compress very well | ||
| Line 64: | Line 77: | ||
... | ... | ||
| lang = terminal | | lang = terminal | ||
}}< | }} | ||
==== Unmounting ==== | |||
You may unmount the filesystem with <code>umount /mnt</code>. To ensure everything is wiped from memory, you can use <code>zramctl --reset /dev/zram0</code> to zero out the block device. | |||
=== Using zram as swap space === | |||
Continuing on, we can also use it as swap space by first initializing the swap space with <code>mkswap</code> and then enabling the swap space with <code>swapon</code>. We will specify a higher priority to ensure that the system uses the zram swap device before other devices (such as disk which may be slower). | |||
{{Highlight | |||
| code = # mkswap /dev/zram0 | |||
# swapon /dev/zram0 -p 10 | |||
| lang = terminal | |||
}} | |||
To disable the swap device, use the swapoff command. | |||
{{Highlight | |||
| code = # swapoff /dev/zram0 | |||
| lang = terminal | |||
}} | |||
To ensure everything is wiped from memory, you can use <code>zramctl --reset /dev/zram0</code> to zero out the block device. | |||
==== Persistent setup ==== | |||
To make the zram swap space come up with the system, you will have to do a few things: | |||
{{Highlight | |||
| code = ## Load the zram module on startup | |||
# echo "zram" > /etc/modules-load.d/zram.conf | |||
## Create a udev rule that automatically sets up the swap space | |||
# echo 'KERNEL=="zram0", ATTR{disksize}="4G" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules | |||
## Add the zram block device into fstab | |||
# echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab | |||
| lang = terminal | |||
}} | |||
==Tasks== | ==Tasks== | ||
=== | ===Show available compression algorithms=== | ||
The algorithm that is available can be listed from the <code>comp_algorithm</code> pseudofile. | The algorithm that is available can be listed from the <code>comp_algorithm</code> pseudofile. | ||
{{Highlight | {{Highlight | ||
| Line 76: | Line 120: | ||
}} | }} | ||
=== | === Clearing the zram block device === | ||
Run: <code>zramctl --reset /dev/zram0</code> | |||
< | |||
== See Also == | == See Also == | ||
| Line 85: | Line 127: | ||
* Log2ram, which is a script that mounts zram/tmpfs for logs: https://github.com/azlux/log2ram/blob/master/log2ram | * Log2ram, which is a script that mounts zram/tmpfs for logs: https://github.com/azlux/log2ram/blob/master/log2ram | ||
* Swap to zram. More at https://ikarus.sg/using-zram-to-get-more-out-of-your-raspberry-pi/ | * Swap to zram. More at https://ikarus.sg/using-zram-to-get-more-out-of-your-raspberry-pi/ | ||
* Proxmox's guide to zram is also very useful: https://pve.proxmox.com/wiki/Zram | |||
Revision as of 17:56, 18 August 2023
zram is a Linux kernel module that creates a RAM backed block device with features including on-the-fly compression.
Because zram can transparently compress data, it may be used as a swapfile which effectively allows RAM to be compressed as it is paged out. This is particularly useful for memory constrained systems.
zswap is closely related with the biggest difference that less frequently used pages can be swapped out from RAM to physical disk.
Quick usage guide
Firstly, load the zram kernel module as root. This should create /dev/zram0 which you can configure with zramctl. You may set the size and compression algorithm when creating the block device.
Supported compression algorithms include: lz4 (more efficient compression but slower), lzo (less efficient but faster).
## Load the zram module. It should have created /dev/zram0 by default.
# modprobe zram
# ls -al /dev/zram*
/dev/zram0
## Define the disk size and algorithm of zram0
# zramctl --size 128m --algorithm lz4 /dev/zram0
Using zram with a Ext4 filesystem
Once the block device is created (as /dev/zram0), you can use it as any normal block device and create a normal filesystem or swap space with it. We'll first make a ext4 filesystem to show off some additional features with zram.
## Make a filesystem and mount it
# mkfs.ext4 /dev/zram0
# mount /dev/zram0 /mnt
# df -h /mnt
Filesystem Size Used Avail Use% Mounted on
/dev/zram0 108M 84K 99M 1% /mnt
The actual memory consumption of the block device can be viewed with zramctl. After running dd if=/dev/zero of=test.bin bs=512 count=100000, we now see the filesystem being approximately 50% full whilst only using 4.1KB of actual memory.
## Write all zeros to a file, which should compress very well
# dd if=/dev/zero of=/mnt/test.bin bs=512 count=100000
100000+0 records in
100000+0 records out
## The filesystem is now 50% used
# df -h /mnt
Filesystem Size Used Avail Use% Mounted on
/dev/zram0 108M 49M 51M 50% /mnt
## But it's only using 4.1KB of memory
# zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo 128M 53.9M 4.8K 60K 4 /mnt
You may define an upper-limit to the amount of memory that the zram device can use by writing to /sys/block/zram0/mem_limit. For example, to limit the total memory to 10MB:
## Maximum of 10MB of memory (after compression)
# echo 10m > /sys/block/zram0/mem_limit
Use this with care since exceeding this value will cause writes to fail and will most likely corrupt the filesystem using this device. Continuing on with the example above, even though /mnt appears to be 128MB in size, because zram0 can only use 10MB of ram, we cannot write data that compresses larger than 10MB. So, if we were to run:
## Writing 50MB of random data
# dd if=/dev/urandom of=test.bin bs=512 count=100000
# df -h /mnt
Filesystem Size Used Avail Use% Mounted on
/dev/zram0 108M 49M 51M 50% /mnt
## Actual memory usage is capped to 10MB, so something got truncated...
# zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo 128M 10.1M 10M 10M 4 /mnt
## Uh-oh
# dmesg | tail
[ 2793.272512] EXT4-fs warning (device zram0): ext4_end_bio:323: I/O error 10 writing to inode 12 (offset 8388608 size 2101248 starting block 7194)
[ 2793.274915] buffer_io_error: 12490 callbacks suppressed
[ 2793.274917] Buffer I/O error on device zram0, logical block 7194
[ 2793.277186] Buffer I/O error on device zram0, logical block 7195
[ 2793.278343] Buffer I/O error on device zram0, logical block 7196
...
Unmounting
You may unmount the filesystem with umount /mnt. To ensure everything is wiped from memory, you can use zramctl --reset /dev/zram0 to zero out the block device.
Using zram as swap space
Continuing on, we can also use it as swap space by first initializing the swap space with mkswap and then enabling the swap space with swapon. We will specify a higher priority to ensure that the system uses the zram swap device before other devices (such as disk which may be slower).
# mkswap /dev/zram0
# swapon /dev/zram0 -p 10
To disable the swap device, use the swapoff command.
# swapoff /dev/zram0
To ensure everything is wiped from memory, you can use zramctl --reset /dev/zram0 to zero out the block device.
Persistent setup
To make the zram swap space come up with the system, you will have to do a few things:
## Load the zram module on startup
# echo "zram" > /etc/modules-load.d/zram.conf
## Create a udev rule that automatically sets up the swap space
# echo 'KERNEL=="zram0", ATTR{disksize}="4G" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules
## Add the zram block device into fstab
# echo "/dev/zram0 none swap defaults,pri=10 0 0" >> /etc/fstab
Tasks
Show available compression algorithms
The algorithm that is available can be listed from the comp_algorithm pseudofile.
# cat /sys/block/zram0/comp_algorithm
lzo [lz4]
Clearing the zram block device
Run: zramctl --reset /dev/zram0
See Also
- Log2ram, which is a script that mounts zram/tmpfs for logs: https://github.com/azlux/log2ram/blob/master/log2ram
- Swap to zram. More at https://ikarus.sg/using-zram-to-get-more-out-of-your-raspberry-pi/
- Proxmox's guide to zram is also very useful: https://pve.proxmox.com/wiki/Zram