系统: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
对我来说不起作用所以我不得不添加/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
现在,问题是系统swapon
在设置 SWAP ( ) 之前尝试挂载它 ( 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
有没有办法改变顺序,或者有更好的方法来做到这一点?
答案1
示例:设置一个容量为 32GiB 且优先级高于正常的 lz4 压缩 zram 设备(仅适用于当前会话):
# 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
使用 udev 规则交换 zRAM
下面的示例描述了如何使用单个 udev 规则在启动时自动在 zRAM 上设置交换。无需额外的软件包即可实现此功能。
首先,启用模块:
/etc/modules-load.d/zram.conf:
zram
配置所需的 /dev/zram 节点数量。
/etc/modprobe.d/zram.conf:
options zram num_devices=2
按照示例所示创建 udev 规则。
/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"
将 /dev/zram 添加到您的 fstab。
/etc/fstab:
/dev/zram0 none swap defaults 0 0
/dev/zram1 none swap defaults 0 0
答案2
是的,不要使用udev. 按照老方法做就行了。
添加到 /etc/rc.local (Debian/Ubuntu)
echo 512M > /sys/block/zram0/mem_limit
mkswap /dev/zram0
swapon /dev/zram0
答案3
它是 Fedora 24 的一部分(我这个笨蛋...我将我的主机移到了 Fedora 然后就忘记了。)
我根据在某处找到的 RPM 规范文件(为方便后人复制在下面)为 CentOS 7 构建了一个软件包。
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 <[email protected]> - 1.0.0-1
- Spec file cleanup
* Mon Nov 25 2013 Doncho Gunchev <[email protected]> - 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 <[email protected]> - 0:1.0.0-1
- Add Darren Steven's build fix for fedora 18
* Tue Mar 19 2013 Doncho Gunchev <[email protected]> - 0:1.0.0-0
- Initial package