|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# NextCloudPi ZRAM settings |
| 4 | +# |
| 5 | +# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> |
| 6 | +# GPL licensed (see end of file) * Use at your own risk! |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# |
| 10 | +# ./installer.sh nc-zram.sh <IP> (<img>) |
| 11 | +# |
| 12 | +# See installer.sh instructions for details |
| 13 | +# |
| 14 | +# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/ |
| 15 | +# |
| 16 | + |
| 17 | +ACTIVE_=no |
| 18 | +DESCRIPTION="Enable compressed RAM to improve swap performance" |
| 19 | + |
| 20 | +install() |
| 21 | +{ |
| 22 | + cat > /etc/systemd/system/zram.service <<EOF |
| 23 | +[Unit] |
| 24 | +Description=Set up ZRAM |
| 25 | +
|
| 26 | +[Service] |
| 27 | +Type=oneshot |
| 28 | +ExecStart=/usr/local/bin/ncp-zram start |
| 29 | +ExecStop=/usr/local/bin/ncp-zram stop |
| 30 | +RemainAfterExit=yes |
| 31 | +
|
| 32 | +[Install] |
| 33 | +WantedBy=sysinit.target |
| 34 | +EOF |
| 35 | + |
| 36 | +cat > /usr/local/bin/ncp-zram <<'EOF' |
| 37 | +#!/bin/bash |
| 38 | +# inspired by https://github.com/novaspirit/rpi_zram/blob/master/zram.sh |
| 39 | +
|
| 40 | +case "$1" in |
| 41 | + start) |
| 42 | + CORES=$(nproc --all) |
| 43 | + modprobe zram num_devices=$CORES || exit 1 |
| 44 | +
|
| 45 | + swapoff -a |
| 46 | +
|
| 47 | + TOTALMEM=`free | grep -e "^Mem:" | awk '{print $2}'` |
| 48 | + MEM=$(( ($TOTALMEM / $CORES)* 1024 )) |
| 49 | +
|
| 50 | + core=0 |
| 51 | + while [ $core -lt $CORES ]; do |
| 52 | + echo $MEM > /sys/block/zram$core/disksize |
| 53 | + mkswap /dev/zram$core |
| 54 | + swapon -p 5 /dev/zram$core |
| 55 | + let core=core+1 |
| 56 | + done |
| 57 | + ;; |
| 58 | +
|
| 59 | + stop) |
| 60 | + swapoff -a |
| 61 | + rmmod zram |
| 62 | + ;; |
| 63 | + *) |
| 64 | + echo "Usage: $0 {start|stop}" >&2 |
| 65 | + exit 1 |
| 66 | + ;; |
| 67 | +esac |
| 68 | +EOF |
| 69 | +chmod +x /usr/local/bin/ncp-zram |
| 70 | +} |
| 71 | + |
| 72 | +configure() |
| 73 | +{ |
| 74 | + [[ $ACTIVE_ != "yes" ]] && { |
| 75 | + systemctl stop zram |
| 76 | + systemctl disable zram |
| 77 | + echo "ZRAM disabled" |
| 78 | + return 0 |
| 79 | + } |
| 80 | + systemctl start zram |
| 81 | + systemctl enable zram |
| 82 | + echo "ZRAM enabled" |
| 83 | +} |
| 84 | + |
| 85 | +# License |
| 86 | +# |
| 87 | +# This script is free software; you can redistribute it and/or modify it |
| 88 | +# under the terms of the GNU General Public License as published by |
| 89 | +# the Free Software Foundation; either version 2 of the License, or |
| 90 | +# (at your option) any later version. |
| 91 | +# |
| 92 | +# This script is distributed in the hope that it will be useful, |
| 93 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 94 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 95 | +# GNU General Public License for more details. |
| 96 | +# |
| 97 | +# You should have received a copy of the GNU General Public License |
| 98 | +# along with this script; if not, write to the |
| 99 | +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 100 | +# Boston, MA 02111-1307 USA |
| 101 | + |
0 commit comments