Stretching devices with limited resources

Fewer Memories

Setup

Zswap is easily configured by passing the parameter zswap.enabled=1 to the kernel at boot in GRUB configuration options. Zram is more complicated, and not nearly as well documented as one would expect in most distributions. Listing 2 shows how to set up two swap devices (and how to initialize and tear down the first one) with exclusively manual steps on Ubuntu Server 20.04 LTS. Listing 3 makes use of the zram-config package found in Ubuntu (again, Server version 20.04 LTS) to automate part of the process to mount a second device as a RAM drive for /var/logs.

Listing 2

Initializing Zram the Hard Way

# Aligning versions between kernel and modules referenced by virtual package
$ sudo apt update; sudo apt upgrade
$ sudo apt install -y linux-image-extra-virtual
# Create half a gigabyte ZRAM device at next available device file
$ zramctl --find --size=512M
/dev/zram0
$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       512M   0B    0B    0B       2
# Make the ZRAM block device into a swap partition
$ sudo mkswap /dev/zram0
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=0818f196-4e38-43be-88ad-de6b45f50ce5
# Turn the swap partition on
$ Swapon /dev/zram0
$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       512M   4K   73B   12K       2 [SWAP]
# Cleanup procedure (destroy the ZRAM setup)
$ sudo swapoff /dev/zram0
$ sudo zramctl --reset /dev/zram0

Listing 3

Initializing Zram the Easy Way

# install Ubuntu's ZRAM management package
$ sudo apt install zram-config
# start the service
$ sudo systemctl start zram-config
$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram1 lzo-rle     495.8M   4K   73B   12K       2 [SWAP]
# Now let's make a ramdrive for /var/logs
$ zramctl --find --size=512M
/dev/zram0
# lay a filesystem on the ramdrive
$ sudo mkfs.ext2 -q /dev/zram0
# mount the ramdrive
$ sudo mount /dev/zram0 /var/log
$ zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       512M  208K  3.1K   48K       2 /var/log
/dev/zram1 lzo-rle     495.8M    4K   73B   12K       2 [SWAP]

Infos

  1. "Assess USB performance while exploring storage caching" by Federico Lucifredi, ADMIN , issue 48, 2018, pg. 94, https://www.admin-magazine.com/Archive/2018/48/Assess-USB-performance-while-exploring-storage-caching
  2. Marvell ESPRESSObin v7: http://wiki.espressobin.net/tiki-index.php
  3. Serial 2.12 for Macintosh: https://www.decisivetactics.com/products/serial/
  4. Das U-Boot – the Universal Boot Loader: https://www.denx.de/wiki/U-Boot
  5. Yocto Project: https://www.yoctoproject.org
  6. Armbian – Linux for ARM development boards: https://www.armbian.com/espressobin/
  7. "Swap tricks" by Federico Lucifredi, ADMIN , issue 9, 2012, pg. 83
  8. Kernel docs for zram: https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html
  9. Kernel docs for zswap: https://www.kernel.org/doc/html/latest/admin-guide/mm/zswap.html
  10. free(1) man page: https://linux.die.net/man/1/free

The Author

Federico Lucifredi (@0xf2) is the Product Management Director for Ceph Storage at Red Hat, formerly the Ubuntu Server Product Manager at Canonical, and the Linux "Systems Management Czar" at SUSE. He enjoys arcane hardware issues and shell-scripting mysteries and takes his McFlurry shaken, not stirred. You can read more from him in the O'Reilly title AWS System Administration .

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus