From efc174a705f061671933706277d5ded6a84169d3 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Sun, 27 Dec 2015 12:16:17 +0000
Subject: [PATCH] Move zram daemon into its own command

---
 src/freedombone      |  77 ++----------------------
 src/freedombone-zram | 136 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 72 deletions(-)
 create mode 100755 src/freedombone-zram

diff --git a/src/freedombone b/src/freedombone
index f1194ea9b..795916013 100755
--- a/src/freedombone
+++ b/src/freedombone
@@ -3277,81 +3277,14 @@ function enable_zram {
   if grep -Fxq "enable_zram" $COMPLETION_FILE; then
       return
   fi
+
   if [[ $INSTALLED_WITHIN_DOCKER == "yes" || $INSTALLING_ON_BBB != "yes" ]]; then
+      ${PROJECT_NAME}-zram off
       return
   fi
-  if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
-      echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
-  fi
-  echo '#!/bin/bash' > /etc/init.d/zram
-  echo '### BEGIN INIT INFO' >> /etc/init.d/zram
-  echo '# Provides: zram' >> /etc/init.d/zram
-  echo '# Required-Start:' >> /etc/init.d/zram
-  echo '# Required-Stop:' >> /etc/init.d/zram
-  echo '# Default-Start: 2 3 4 5' >> /etc/init.d/zram
-  echo '# Default-Stop: 0 1 6' >> /etc/init.d/zram
-  echo '# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)' >> /etc/init.d/zram
-  echo '# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram' >> /etc/init.d/zram
-  echo '### END INIT INFO' >> /etc/init.d/zram
-  echo 'start() {' >> /etc/init.d/zram
-  echo '    # get the number of CPUs' >> /etc/init.d/zram
-  echo '    num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
-  echo '    # if something goes wrong, assume we have 1' >> /etc/init.d/zram
-  echo '    [ "$num_cpus" != 0 ] || num_cpus=1' >> /etc/init.d/zram
-  echo '    # set decremented number of CPUs' >> /etc/init.d/zram
-  echo '    decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
-  echo '    # get the amount of memory in the machine' >> /etc/init.d/zram
-  echo '    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")' >> /etc/init.d/zram
-  echo '    mem_total=$((mem_total_kb * 1024))' >> /etc/init.d/zram
-  echo '    # load dependency modules' >> /etc/init.d/zram
-  echo '    modprobe zram num_devices=$num_cpus' >> /etc/init.d/zram
-  echo '    # initialize the devices' >> /etc/init.d/zram
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
-  echo '      echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize' >> /etc/init.d/zram
-  echo '    done' >> /etc/init.d/zram
-  echo '    # Creating swap filesystems' >> /etc/init.d/zram
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
-  echo '      mkswap /dev/zram$i' >> /etc/init.d/zram
-  echo '    done' >> /etc/init.d/zram
-  echo '    # Switch the swaps on' >> /etc/init.d/zram
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
-  echo '      swapon -p 100 /dev/zram$i' >> /etc/init.d/zram
-  echo '    done' >> /etc/init.d/zram
-  echo '}' >> /etc/init.d/zram
-  echo 'stop() {' >> /etc/init.d/zram
-  echo '    # get the number of CPUs' >> /etc/init.d/zram
-  echo '    num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
-  echo '    # set decremented number of CPUs' >> /etc/init.d/zram
-  echo '    decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
-  echo '    # Switching off swap' >> /etc/init.d/zram
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
-  echo '      if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then' >> /etc/init.d/zram
-  echo '        swapoff /dev/zram$i' >> /etc/init.d/zram
-  echo '        sleep 1' >> /etc/init.d/zram
-  echo '      fi' >> /etc/init.d/zram
-  echo '    done' >> /etc/init.d/zram
-  echo '    sleep 1' >> /etc/init.d/zram
-  echo '    rmmod zram' >> /etc/init.d/zram
-  echo '}' >> /etc/init.d/zram
-  echo 'case "$1" in' >> /etc/init.d/zram
-  echo '    start)' >> /etc/init.d/zram
-  echo '        start' >> /etc/init.d/zram
-  echo '        ;;' >> /etc/init.d/zram
-  echo '    stop)' >> /etc/init.d/zram
-  echo '        stop' >> /etc/init.d/zram
-  echo '        ;;' >> /etc/init.d/zram
-  echo '    restart)' >> /etc/init.d/zram
-  echo '        stop' >> /etc/init.d/zram
-  echo '        sleep 3' >> /etc/init.d/zram
-  echo '        start' >> /etc/init.d/zram
-  echo '        ;;' >> /etc/init.d/zram
-  echo '    *)' >> /etc/init.d/zram
-  echo '        echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/zram
-  echo '        RETVAL=1' >> /etc/init.d/zram
-  echo 'esac' >> /etc/init.d/zram
-  echo 'exit $RETVAL' >> /etc/init.d/zram
-  chmod +x /etc/init.d/zram
-  update-rc.d zram defaults
+
+  ${PROJECT_NAME}-zram on
+
   echo 'enable_zram' >> $COMPLETION_FILE
 }
 
diff --git a/src/freedombone-zram b/src/freedombone-zram
new file mode 100755
index 000000000..472b7fa98
--- /dev/null
+++ b/src/freedombone-zram
@@ -0,0 +1,136 @@
+#!/bin/bash
+#
+# .---.                  .              .
+# |                      |              |
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
+#
+#                    Freedom in the Cloud
+#
+# Enables or disables zram
+
+# License
+# =======
+#
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+PROJECT_NAME='freedombone'
+
+export TEXTDOMAIN=${PROJECT_NAME}-zram
+export TEXTDOMAINDIR="/usr/share/locale"
+
+DAEMON_FILENAME=/etc/systemd/system/zram.service
+
+function zram_daemon {
+  echo '[Unit]' > $DAEMON_FILENAME
+  echo 'Description=Zeronet Server' >> $DAEMON_FILENAME
+  echo 'After=syslog.target' >> $DAEMON_FILENAME
+  echo 'After=network.target' >> $DAEMON_FILENAME
+  echo '[Service]' >> $DAEMON_FILENAME
+  echo 'Type=simple' >> $DAEMON_FILENAME
+  echo 'User=zram' >> $DAEMON_FILENAME
+  echo 'Group=zram' >> $DAEMON_FILENAME
+  echo 'WorkingDirectory=' >> $DAEMON_FILENAME
+  echo "ExecStart=freedombone-zram on" >> $DAEMON_FILENAME
+  echo '' >> $DAEMON_FILENAME
+  echo '[Install]' >> $DAEMON_FILENAME
+  echo 'WantedBy=multi-user.target' >> $DAEMON_FILENAME
+
+}
+
+function zram_on {
+    if [ ! -f $DAEMON_FILENAME ]; then
+        if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
+            echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
+        fi
+
+        # 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))
+
+        # 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 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
+
+        zram_daemon
+    fi
+}
+
+function zram_off {
+    if [ -f $DAEMON_FILENAME ]; then
+        # get the number of CPUs
+        num_cpus=$(grep -c processor /proc/cpuinfo)
+
+        # set decremented number of CPUs
+        decr_num_cpus=$((num_cpus - 1))
+
+        # Switching off swap
+        for i in $(seq 0 $decr_num_cpus); do
+            if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
+                swapoff /dev/zram$i
+                sleep 1
+            fi
+        done
+
+        sleep 1
+        rmmod zram
+
+        rm $DAEMON_FILENAME
+    fi
+}
+
+function show_help {
+    echo ''
+    echo $"${PROJECT_NAME}-zram [on|off]"
+    echo ''
+    exit 0
+}
+
+if [ ! $1 ]; then
+    show_help
+else
+    if [[ "$1" == "on" || "$1" == "enable" || "$1" == "yes" ]]; then
+        zram_on
+    else
+        zram_off
+    fi
+fi
+
+exit 0
-- 
GitLab