new version, prepared for systemd

This commit is contained in:
Michael 2012-09-16 18:35:16 +00:00
parent 546b95d893
commit 56876eaf07
7 changed files with 76 additions and 65 deletions

View File

@ -4,16 +4,25 @@
# Maintainer: george <george[at]chakra-project[dot]org>
pkgname=zramswap
pkgver=0.2
pkgver=0.3
pkgrel=1
pkgdesc="Sets up zram swap devices on boot."
arch=('any')
url="http://www.webupd8.org/2011/10/increased-performance-in-linux-with.html"
license=('GPL')
install=$pkgname.install
source=('zramswap.rc.d')
md5sums=('630d83824f13b192721a113aad862c57')
install=${pkgname}.install
source=('zram.service'
'zram'
'zramstart'
'zramstop')
sha256sums=('3a921f765a4e4b2b80f85f07cbf8fdc7522f0f2ae5a1d19bd7f515e9580da9aa'
'9f578c92683684f77f6f5b13ac86ebdc7deaac2f679a6dd8210a08e8238ca31b'
'dc8bc88c2edf15ae9bcec432b4ff4371e43fa9bc780132d1dbc6a3cc5700b106'
'475150e74087dbc1e1013d5c11eff016e950d7dce7874fda633f25fb306d3f93')
package() {
install -Dm755 zramswap.rc.d "$pkgdir/etc/rc.d/zramswap"
install -Dm644 "${srcdir}/zram.service" "${pkgdir}/usr/lib/systemd/system/zram.service"
install -Dm644 "${srcdir}/zram" "${pkgdir}/etc/default/zram"
install -Dm755 "${srcdir}/zramstart" "${pkgdir}/usr/sbin/zramstart"
install -Dm755 "${srcdir}/zramstop" "${pkgdir}/usr/sbin/zramstop"
}

4
zramswap/zram Normal file
View File

@ -0,0 +1,4 @@
# 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
FACTOR=90

12
zramswap/zram.service Executable file
View File

@ -0,0 +1,12 @@
[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=multi-user.target

20
zramswap/zramstart Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
num_cpus=$(grep -c processor /proc/cpuinfo)
[ "$num_cpus" != 0 ] || num_cpus=1
last_cpu=$((num_cpus - 1))
FACTOR=90
[ -f /etc/default/zram ] && source /etc/default/zram || true
factor=$FACTOR # percentage
memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
modprobe -q zram num_devices=$num_cpus
for i in $(seq 0 $last_cpu); do
echo $mem_by_cpu > /sys/block/zram$i/disksize
mkswap /dev/zram$i
swapon -p 100 /dev/zram$i
done

13
zramswap/zramstop Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
num_cpus=$(grep -c processor /proc/cpuinfo)
[ "$num_cpus" != 0 ] || num_cpus=1
last_cpu=$((num_cpus - 1))
for i in $(seq 0 $last_cpu); do
grep -q "/dev/zram$i" /proc/swaps && swapoff /dev/zram$i
done
sleep 1
rmmod zram

View File

@ -1,7 +1,17 @@
post_install() {
echo "Add zramswap to the DAEMONS array in rc.conf"
echo "==> To enable zramswap, run 'sudo systemctl enable zram.service'"
echo "==> To run the script now, start it with 'sudo systemctl start zram.service'"
}
post_remove() {
echo "Don't forget to remove zramswap from the DAEMONS array in rc.conf"
pre_remove() {
# Stop running services
_service="zram.service"
systemctl is-active ${_service} &>/dev/null
if [[ $? -eq 0 ]] ; then
systemctl stop ${_service}
fi
systemctl is-enabled ${_service} &>/dev/null
if [[ $? -eq 0 ]] ; then
systemctl disable ${_service}
fi
}

View File

@ -1,57 +0,0 @@
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# if something goes wrong, assume we have 1
[[ "$num_cpus" != 0 ]] || num_cpus=1
# set decremented number of CPUs
decr_num_cpus=$((num_cpus - 1))
case "$1" in
start)
stat_busy "Enabling zram-based swap"
# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
mem_total=$((mem_total_kb * 1024))
# load dependency modules
modprobe zram zram_num_devices=$num_cpus || modprobe zram num_devices=$num_cpus
# initialize the devices
for i in $(seq 0 $decr_num_cpus); do
echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
done
# Creating swap filesystems
for i in $(seq 0 $decr_num_cpus); do
mkswap /dev/zram$i
done
# Switch the swaps on
for i in $(seq 0 $decr_num_cpus); do
swapon -p 100 /dev/zram$i
done
stat_done
;;
stop)
stat_busy "Switching off zram-based swap"
# Switching off swap
for i in $(seq 0 $decr_num_cpus); do
if [[ "$(grep /dev/zram$i /proc/swaps)" != "" ]]; then
swapoff /dev/zram$i
fi
done
rmmod zram
stat_done
;;
*)
echo "usage: $0 {start|stop}"
esac
exit 0