1

System: CentOS 7

cat /etc/udev/rules.d/10-zram.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="zram0", DRIVER=="", ATTR{disksize}=="0", ATTR{disksize}="512M", RUN+="/usr/bin/systemd-run /sbin/mkswap $env{DEVNAME}"

(/sbin/mkswap didn't work for me so I had to add /usr/bin/systemd-run /sbin/mkswap)

# cat /etc/modules-load.d/zram.conf 
zram
# cat /etc/modprobe.d/zram.conf 
options zram num_devices=1

Now, the problem is that system tries to mount it (swapon) before SWAP is set (mkswap):

# journalctl -o short | grep zram
Aug 04 21:28:54 system kernel: zram: module is from the staging directory, the quality is unknown, you have been warned.
Aug 04 21:28:54 system kernel: zram: Created 1 device(s) ...
Aug 04 21:28:54 system systemd-modules-load[459]: Inserted module 'zram'
Aug 04 21:28:55 system kernel: zram: Initialization done!
Aug 04 21:28:55 system systemd[1]: Found device /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Activating swap /dev/zram0...
Aug 04 21:28:55 system swapon[494]: swapon: /dev/zram0: read swap header failed: Invalid argument
Aug 04 21:28:55 system systemd[1]: dev-zram0.swap swap process exited, code=exited status=255
Aug 04 21:28:55 system systemd[1]: Failed to activate swap /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Unit dev-zram0.swap entered failed state.
Aug 04 21:28:55 system systemd[1]: Started /sbin/mkswap /dev/zram0.
Aug 04 21:28:55 system systemd[1]: Starting /sbin/mkswap /dev/zram0...
Aug 04 21:29:15 system dracut[3292]: -rw-r--r--   1 root     root           27 Aug  4 21:28 etc/modprobe.d/zram.conf
Aug 04 21:29:15 system dracut[3292]: -rw-r--r--   1 root     root            5 Aug  4 15:09 etc/modules-load.d/zram.conf
Aug 04 21:29:16 system dracut[3292]: drwxr-xr-x   2 root     root            0 Aug  4 21:29 usr/lib/modules/3.10.0-327.10.1.el7.x86_64/kernel/drivers/staging/zram
Aug 04 21:29:16 system dracut[3292]: -rw-r--r--   1 root     root        28701 Feb 16 17:45 usr/lib/modules/3.10.0-327.10.1.el7.x86_64/kernel/drivers/staging/zram/zram.ko

Is there a way to change the order or perhaps a better way to do this?

Colyn1337
  • 2,387
  • 2
  • 22
  • 38
HTF
  • 3,050
  • 14
  • 49
  • 78

3 Answers3

1

Arch has this covered quite good

Example: 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

Swap on zRAM using a udev rule

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.

First, enable the module:

/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
theking2
  • 159
  • 1
  • 8
0

Yes, don't use udev. Just do it the old way.

Add to /etc/rc.local (Debian/Ubuntu)

echo 512M > /sys/block/zram0/mem_limit
mkswap /dev/zram0
swapon /dev/zram0
Ryan Babchishin
  • 6,160
  • 2
  • 16
  • 36
0

It's part of Fedora 24 (dummy me... I moved my primary machine over to Fedora and forgotten.)

I built a package for CentOS 7 from an RPM spec file I found somewhere (copied below for posterity).

Summary: Enable compressed swap in memory
Name: zram
Version: 1.0.0
Release: 2%{?dist}
License: GPLv2
Group: System Environment/Daemons
Source0: %{name}-%{version}.tar.bz2
BuildArch: noarch

BuildRequires: systemd-units
Requires(post): systemd-sysv
Requires(post): systemd-units
Requires(preun): systemd-units
Requires(postun): systemd-units
Requires: filesystem >= 2.0.1, initscripts, bc > 1.0
# No debug info for bare scripts, right?
%define debug_package %{nil}
# http://fedoraproject.org/wiki/Changes/UnversionedDocdirs
%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}
%global _docdir_fmt %{name}

%description
zram compresses swap partitions into RAM for performance.

You need Linux kernel version 2.6.37.1 or better to use zram.


%prep
%setup -q


%build


%install
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig
mkdir -p $RPM_BUILD_ROOT%{_unitdir}
ln -s $RPM_BUILD_ROOT/usr/lib $RPM_BUILD_ROOT/lib
mkdir -p $RPM_BUILD_ROOT%{_sbindir}
%makeinstall DESTDIR=$RPM_BUILD_ROOT


%post
%systemd_post mkzram.service

%preun
%systemd_preun mkzram.service

%postun
%systemd_postun_with_restart mkzram.service

%files
%doc README.md
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%{_unitdir}/mkzram.service
%{_sbindir}/zramstart
%{_sbindir}/zramstop
%{_sbindir}/zramstat
%exclude /lib


%changelog
* Tue Nov 25 2014 Juan Orti <jorti@fedoraproject.org> - 1.0.0-1
- Spec file cleanup

* Mon Nov 25 2013 Doncho Gunchev <dgunchev@gmail.com> - 0:1.0.0-2
- http://fedoraproject.org/wiki/Changes/UnversionedDocdirs
- Added kmod-staging dependency
- Test on Fedora 19

* Mon Sep 02 2013 Doncho Gunchev <dgunchev@gmail.com> - 0:1.0.0-1
- Add Darren Steven's build fix for fedora 18

* Tue Mar 19 2013 Doncho Gunchev <dgunchev@gmail.com> - 0:1.0.0-0
- Initial package
kronenpj
  • 233
  • 1
  • 4