Browse Source

Move zram daemon into its own command

Bob Mottram 9 years ago
parent
commit
efc174a705
2 changed files with 141 additions and 72 deletions
  1. 5
    72
      src/freedombone
  2. 136
    0
      src/freedombone-zram

+ 5
- 72
src/freedombone View File

3277
   if grep -Fxq "enable_zram" $COMPLETION_FILE; then
3277
   if grep -Fxq "enable_zram" $COMPLETION_FILE; then
3278
       return
3278
       return
3279
   fi
3279
   fi
3280
+
3280
   if [[ $INSTALLED_WITHIN_DOCKER == "yes" || $INSTALLING_ON_BBB != "yes" ]]; then
3281
   if [[ $INSTALLED_WITHIN_DOCKER == "yes" || $INSTALLING_ON_BBB != "yes" ]]; then
3282
+      ${PROJECT_NAME}-zram off
3281
       return
3283
       return
3282
   fi
3284
   fi
3283
-  if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
3284
-      echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
3285
-  fi
3286
-  echo '#!/bin/bash' > /etc/init.d/zram
3287
-  echo '### BEGIN INIT INFO' >> /etc/init.d/zram
3288
-  echo '# Provides: zram' >> /etc/init.d/zram
3289
-  echo '# Required-Start:' >> /etc/init.d/zram
3290
-  echo '# Required-Stop:' >> /etc/init.d/zram
3291
-  echo '# Default-Start: 2 3 4 5' >> /etc/init.d/zram
3292
-  echo '# Default-Stop: 0 1 6' >> /etc/init.d/zram
3293
-  echo '# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)' >> /etc/init.d/zram
3294
-  echo '# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram' >> /etc/init.d/zram
3295
-  echo '### END INIT INFO' >> /etc/init.d/zram
3296
-  echo 'start() {' >> /etc/init.d/zram
3297
-  echo '    # get the number of CPUs' >> /etc/init.d/zram
3298
-  echo '    num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
3299
-  echo '    # if something goes wrong, assume we have 1' >> /etc/init.d/zram
3300
-  echo '    [ "$num_cpus" != 0 ] || num_cpus=1' >> /etc/init.d/zram
3301
-  echo '    # set decremented number of CPUs' >> /etc/init.d/zram
3302
-  echo '    decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
3303
-  echo '    # get the amount of memory in the machine' >> /etc/init.d/zram
3304
-  echo '    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")' >> /etc/init.d/zram
3305
-  echo '    mem_total=$((mem_total_kb * 1024))' >> /etc/init.d/zram
3306
-  echo '    # load dependency modules' >> /etc/init.d/zram
3307
-  echo '    modprobe zram num_devices=$num_cpus' >> /etc/init.d/zram
3308
-  echo '    # initialize the devices' >> /etc/init.d/zram
3309
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
3310
-  echo '      echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize' >> /etc/init.d/zram
3311
-  echo '    done' >> /etc/init.d/zram
3312
-  echo '    # Creating swap filesystems' >> /etc/init.d/zram
3313
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
3314
-  echo '      mkswap /dev/zram$i' >> /etc/init.d/zram
3315
-  echo '    done' >> /etc/init.d/zram
3316
-  echo '    # Switch the swaps on' >> /etc/init.d/zram
3317
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
3318
-  echo '      swapon -p 100 /dev/zram$i' >> /etc/init.d/zram
3319
-  echo '    done' >> /etc/init.d/zram
3320
-  echo '}' >> /etc/init.d/zram
3321
-  echo 'stop() {' >> /etc/init.d/zram
3322
-  echo '    # get the number of CPUs' >> /etc/init.d/zram
3323
-  echo '    num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
3324
-  echo '    # set decremented number of CPUs' >> /etc/init.d/zram
3325
-  echo '    decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
3326
-  echo '    # Switching off swap' >> /etc/init.d/zram
3327
-  echo '    for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
3328
-  echo '      if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then' >> /etc/init.d/zram
3329
-  echo '        swapoff /dev/zram$i' >> /etc/init.d/zram
3330
-  echo '        sleep 1' >> /etc/init.d/zram
3331
-  echo '      fi' >> /etc/init.d/zram
3332
-  echo '    done' >> /etc/init.d/zram
3333
-  echo '    sleep 1' >> /etc/init.d/zram
3334
-  echo '    rmmod zram' >> /etc/init.d/zram
3335
-  echo '}' >> /etc/init.d/zram
3336
-  echo 'case "$1" in' >> /etc/init.d/zram
3337
-  echo '    start)' >> /etc/init.d/zram
3338
-  echo '        start' >> /etc/init.d/zram
3339
-  echo '        ;;' >> /etc/init.d/zram
3340
-  echo '    stop)' >> /etc/init.d/zram
3341
-  echo '        stop' >> /etc/init.d/zram
3342
-  echo '        ;;' >> /etc/init.d/zram
3343
-  echo '    restart)' >> /etc/init.d/zram
3344
-  echo '        stop' >> /etc/init.d/zram
3345
-  echo '        sleep 3' >> /etc/init.d/zram
3346
-  echo '        start' >> /etc/init.d/zram
3347
-  echo '        ;;' >> /etc/init.d/zram
3348
-  echo '    *)' >> /etc/init.d/zram
3349
-  echo '        echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/zram
3350
-  echo '        RETVAL=1' >> /etc/init.d/zram
3351
-  echo 'esac' >> /etc/init.d/zram
3352
-  echo 'exit $RETVAL' >> /etc/init.d/zram
3353
-  chmod +x /etc/init.d/zram
3354
-  update-rc.d zram defaults
3285
+
3286
+  ${PROJECT_NAME}-zram on
3287
+
3355
   echo 'enable_zram' >> $COMPLETION_FILE
3288
   echo 'enable_zram' >> $COMPLETION_FILE
3356
 }
3289
 }
3357
 
3290
 

+ 136
- 0
src/freedombone-zram View File

1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# Enables or disables zram
12
+
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
17
+#
18
+# This program is free software: you can redistribute it and/or modify
19
+# it under the terms of the GNU General Public License as published by
20
+# the Free Software Foundation, either version 3 of the License, or
21
+# (at your option) any later version.
22
+#
23
+# This program is distributed in the hope that it will be useful,
24
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
+# GNU General Public License for more details.
27
+#
28
+# You should have received a copy of the GNU General Public License
29
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
30
+
31
+PROJECT_NAME='freedombone'
32
+
33
+export TEXTDOMAIN=${PROJECT_NAME}-zram
34
+export TEXTDOMAINDIR="/usr/share/locale"
35
+
36
+DAEMON_FILENAME=/etc/systemd/system/zram.service
37
+
38
+function zram_daemon {
39
+  echo '[Unit]' > $DAEMON_FILENAME
40
+  echo 'Description=Zeronet Server' >> $DAEMON_FILENAME
41
+  echo 'After=syslog.target' >> $DAEMON_FILENAME
42
+  echo 'After=network.target' >> $DAEMON_FILENAME
43
+  echo '[Service]' >> $DAEMON_FILENAME
44
+  echo 'Type=simple' >> $DAEMON_FILENAME
45
+  echo 'User=zram' >> $DAEMON_FILENAME
46
+  echo 'Group=zram' >> $DAEMON_FILENAME
47
+  echo 'WorkingDirectory=' >> $DAEMON_FILENAME
48
+  echo "ExecStart=freedombone-zram on" >> $DAEMON_FILENAME
49
+  echo '' >> $DAEMON_FILENAME
50
+  echo '[Install]' >> $DAEMON_FILENAME
51
+  echo 'WantedBy=multi-user.target' >> $DAEMON_FILENAME
52
+
53
+}
54
+
55
+function zram_on {
56
+    if [ ! -f $DAEMON_FILENAME ]; then
57
+        if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
58
+            echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
59
+        fi
60
+
61
+        # get the number of CPUs
62
+        num_cpus=$(grep -c processor /proc/cpuinfo)
63
+
64
+        # if something goes wrong, assume we have 1
65
+        [ "$num_cpus" != 0 ] || num_cpus=1
66
+
67
+        # set decremented number of CPUs
68
+        decr_num_cpus=$((num_cpus - 1))
69
+
70
+        # get the amount of memory in the machine
71
+        mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")
72
+        mem_total=$((mem_total_kb * 1024))
73
+
74
+        # load dependency modules
75
+        modprobe zram num_devices=$num_cpus
76
+
77
+        # initialize the devices
78
+        for i in $(seq 0 $decr_num_cpus); do
79
+            echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
80
+        done
81
+
82
+        # Creating swap filesystems
83
+        for i in $(seq 0 $decr_num_cpus); do
84
+            mkswap /dev/zram$i
85
+        done
86
+
87
+        # Switch the swaps on
88
+        for i in $(seq 0 $decr_num_cpus); do
89
+            swapon -p 100 /dev/zram$i
90
+        done
91
+
92
+        zram_daemon
93
+    fi
94
+}
95
+
96
+function zram_off {
97
+    if [ -f $DAEMON_FILENAME ]; then
98
+        # get the number of CPUs
99
+        num_cpus=$(grep -c processor /proc/cpuinfo)
100
+
101
+        # set decremented number of CPUs
102
+        decr_num_cpus=$((num_cpus - 1))
103
+
104
+        # Switching off swap
105
+        for i in $(seq 0 $decr_num_cpus); do
106
+            if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
107
+                swapoff /dev/zram$i
108
+                sleep 1
109
+            fi
110
+        done
111
+
112
+        sleep 1
113
+        rmmod zram
114
+
115
+        rm $DAEMON_FILENAME
116
+    fi
117
+}
118
+
119
+function show_help {
120
+    echo ''
121
+    echo $"${PROJECT_NAME}-zram [on|off]"
122
+    echo ''
123
+    exit 0
124
+}
125
+
126
+if [ ! $1 ]; then
127
+    show_help
128
+else
129
+    if [[ "$1" == "on" || "$1" == "enable" || "$1" == "yes" ]]; then
130
+        zram_on
131
+    else
132
+        zram_off
133
+    fi
134
+fi
135
+
136
+exit 0