Build rpm example:zram
rpmbuild
#ll zram-1.0.0
total 32
-rw-r--r-- 1 root root 948 Aug 21 16:44 Makefile
-rw-r--r-- 1 root root 221 Aug 21 16:44 mkzram.service
-rw-r--r-- 1 root root 2633 Aug 21 16:44 README.md
-rw-r--r-- 1 root root 395 Aug 23 11:29 zram
-rw-r--r-- 1 root root 1780 Aug 21 16:44 zram.spec
-rwxr-xr-x 1 root root 988 Aug 22 11:17 zramstart
-rwxr-xr-x 1 root root 931 Aug 22 12:32 zramstat
-rwxr-xr-x 1 root root 162 Aug 21 16:44 zramstop
#ls
zram-1.0.0
#tar -czf zram-1.0.0.tar.gz zram-1.0.0
#cp zram-1.0.0.tar.gz /root/rpmbuild/SOURCES/
#cp zram-1.0.0/zram.spec /root/rpmbuild/SPECS/zram.spec
#rpmbuild -ba /root/rpmbuild/SPECS/zram.spec
文件准备
/usr/lib/systemd/system/mkzram.service
[Unit]
Description=Enable compressed swap in memory using zram
After=multi-user.target
[Service]
RemainAfterExit=yes
ExecStart=/usr/sbin/zramstart
ExecStop=/usr/sbin/zramstop
Type=oneshot
[Install]
WantedBy=swap.target
#cat /usr/sbin/zramstart
#!/bin/sh
#$(nproc) get cpu core number
num_cpus=$(nproc)
[ "$num_cpus" != 0 ] || num_cpus=1
#memory percentage,If have mem:100G cpu:24, default value per disksize=33G/24
MEM_FACTOR_PERCENT=33
num_device=$num_cpus
if [[ -f /etc/sysconfig/zram ]];then
source /etc/sysconfig/zram
if [[ -n $MEM_FACTOR_PERCENT ]];then
mem_percentage=$MEM_FACTOR_PERCENT # memory percentage
fi
if [[ -n $DEVICE_NUM ]];then
num_device=$DEVICE_NUM #device number
fi
fi
memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
mem_per_device=$(($memtotal/${num_device}*${mem_percentage}/100*1024))
modprobe -q zram num_devices=${num_device}
last_num=$(($num_device - 1))
for i in $(seq 0 $last_num); do
#enable lz4 if that supported
if [[ -e "/sys/block/zram$i/comp_algorithm" ]];then
grep -q lz4 /sys/block/zram$i/comp_algorithm && echo lz4 > /sys/block/zram$i/comp_algorithm
fi
echo $mem_per_device > /sys/block/zram$i/disksize
mkswap /dev/zram$i
swapon -p 100 /dev/zram$i
done
#cat /usr/sbin/zramstat
#!/bin/sh
ls /sys/block/zram* > /dev/null 2>&1 || exit 0
for i in /sys/block/zram*; do
each_zram=`echo ${i##*/}`
compr=$(< $i/compr_data_size)
orig=$(< $i/orig_data_size)
ratio=0
if [ $compr -gt 0 ]; then
ratio=$(echo "scale=2; $orig/$compr" | bc -q)
#ratio=$(echo "scale=2; $compr/$orig*100" | bc -q)
fi
orig_human_MB=`scale=2;echo "$orig/1024/1024" | bc`
compr_human_MB=`scale=2;echo "$compr/1024/1024" | bc`
swap_size=`cat /proc/swaps | grep -w "${each_zram}" | awk '{print $3}'`
swap_used=`cat /proc/swaps | grep -w "${each_zram}" | awk '{print $4}'`
swap_ratio=$(printf "%.2f" `echo "scale=2;${swap_used}/${swap_size}*100"|bc`)
#echo -e "/dev/${i/*\/}:\t${ratio}% (${orig} bytes (${orig_human_MB} MB) -> ${compr} bytes (${compr_human_MB} MB))"
echo -e "/dev/${i/*\/}:\tswap_used:${swap_ratio}% compress_ratio:${ratio}:1 (${orig} bytes (${orig_human_MB} MB) -> ${compr} bytes (${compr_human_MB} MB))"
done
#cat /usr/sbin/zramstop
#!/bin/sh
for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do
swapoff "$i"
done
if grep -q "^zram " /proc/modules; then
sleep 1
rmmod zram
fi
#cat /etc/sysconfig/zram
# The factor is how much (from 0 to 100, percentage)
# of system RAM to allocate to ZRAM block devices
# Too big, and your system will start killing off processes
# If total_mem=100G, MEM_FACTOR_PERCENT=33, /dev/zram0+/dev/zram1+... = 33G ,default value is 33%
MEM_FACTOR_PERCENT=33
#Define /dev/zramxx number,if you do not define , default DEVICE_NUM value equal cpu core number
#DEVICE_NUM=1
spec文件准备
#cat zram.spec
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.gz
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
muahao@aliyun.com