From 56876eaf0797459e30c4cdebd9b90221b9444798 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 Sep 2012 18:35:16 +0000 Subject: [PATCH] new version, prepared for systemd --- zramswap/PKGBUILD | 19 +++++++++---- zramswap/zram | 4 +++ zramswap/zram.service | 12 +++++++++ zramswap/zramstart | 20 ++++++++++++++ zramswap/zramstop | 13 +++++++++ zramswap/zramswap.install | 16 ++++++++--- zramswap/zramswap.rc.d | 57 --------------------------------------- 7 files changed, 76 insertions(+), 65 deletions(-) create mode 100644 zramswap/zram create mode 100755 zramswap/zram.service create mode 100755 zramswap/zramstart create mode 100755 zramswap/zramstop delete mode 100644 zramswap/zramswap.rc.d diff --git a/zramswap/PKGBUILD b/zramswap/PKGBUILD index 054b36db9..0ea3365eb 100644 --- a/zramswap/PKGBUILD +++ b/zramswap/PKGBUILD @@ -4,16 +4,25 @@ # Maintainer: george 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" } diff --git a/zramswap/zram b/zramswap/zram new file mode 100644 index 000000000..161d34953 --- /dev/null +++ b/zramswap/zram @@ -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 diff --git a/zramswap/zram.service b/zramswap/zram.service new file mode 100755 index 000000000..a0e9ff9c4 --- /dev/null +++ b/zramswap/zram.service @@ -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 diff --git a/zramswap/zramstart b/zramswap/zramstart new file mode 100755 index 000000000..c009cd846 --- /dev/null +++ b/zramswap/zramstart @@ -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 diff --git a/zramswap/zramstop b/zramswap/zramstop new file mode 100755 index 000000000..5d4e04c72 --- /dev/null +++ b/zramswap/zramstop @@ -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 diff --git a/zramswap/zramswap.install b/zramswap/zramswap.install index a7fc961a7..64f8f2bef 100644 --- a/zramswap/zramswap.install +++ b/zramswap/zramswap.install @@ -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 } diff --git a/zramswap/zramswap.rc.d b/zramswap/zramswap.rc.d deleted file mode 100644 index c4e076f3a..000000000 --- a/zramswap/zramswap.rc.d +++ /dev/null @@ -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