Skip to content
Snippets Groups Projects
freedombone-zram 3.55 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
#
# Enables or disables zram
Bob Mottram's avatar
Bob Mottram committed
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero 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 {
Bob Mottram's avatar
Bob Mottram committed
    { echo '[Unit]';
      echo 'Description=Zeronet Server';
      echo 'After=syslog.target';
      echo 'After=network.target';
      echo '[Service]';
      echo 'Type=simple';
      echo 'User=zram';
      echo 'Group=zram';
      echo 'WorkingDirectory=';
      echo "ExecStart=${PROJECT_NAME}-zram on";
      echo '';
      echo '[Install]';
Bob Mottram's avatar
Bob Mottram committed
      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
Bob Mottram's avatar
Bob Mottram committed
            echo $((mem_total / num_cpus)) > "/sys/block/zram$i/disksize"
        done

        # Creating swap filesystems
        for i in $(seq 0 $decr_num_cpus); do
Bob Mottram's avatar
Bob Mottram committed
            mkswap "/dev/zram$i"
        done

        # Switch the swaps on
        for i in $(seq 0 $decr_num_cpus); do
Bob Mottram's avatar
Bob Mottram committed
            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
Bob Mottram's avatar
Bob Mottram committed
            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
}

Bob Mottram's avatar
Bob Mottram committed
if [ ! "$1" ]; then
    show_help
else
    if [[ "$1" == "on" || "$1" == "enable" || "$1" == "yes" ]]; then
        zram_on
    else
        zram_off
    fi
fi

exit 0