0

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?

  • Try add TAG+="systemd" to your udev rules. – Federico Sierra Aug 5 '16 at 2:26
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
| improve this answer | |
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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.