freedomboneeee/src/freedombone-zram

134 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2015-12-27 13:16:17 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2015-12-27 13:16:17 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2015-12-27 13:16:17 +01:00
#
# Enables or disables zram
2018-04-08 14:30:21 +02:00
#
2015-12-27 13:16:17 +01:00
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2015-12-27 13:16:17 +01:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2015-12-27 13:16:17 +01:00
# 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
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2015-12-27 13:16:17 +01:00
#
2016-02-13 23:09:27 +01:00
# 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/>.
2015-12-27 13:16:17 +01:00
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-zram
export TEXTDOMAINDIR="/usr/share/locale"
DAEMON_FILENAME=/etc/systemd/system/zram.service
function zram_daemon {
2018-02-25 12:27:48 +01:00
{ 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]';
2018-02-25 13:50:46 +01:00
echo 'WantedBy=multi-user.target'; } > $DAEMON_FILENAME
2015-12-27 13:16:17 +01:00
}
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
2018-02-25 12:27:48 +01:00
echo $((mem_total / num_cpus)) > "/sys/block/zram$i/disksize"
2015-12-27 13:16:17 +01:00
done
# Creating swap filesystems
for i in $(seq 0 $decr_num_cpus); do
2018-02-25 12:27:48 +01:00
mkswap "/dev/zram$i"
2015-12-27 13:16:17 +01:00
done
# Switch the swaps on
for i in $(seq 0 $decr_num_cpus); do
2018-02-25 12:27:48 +01:00
swapon -p 100 "/dev/zram$i"
2015-12-27 13:16:17 +01:00
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
2018-02-25 12:27:48 +01:00
if [ "$(grep "/dev/zram$i" /proc/swaps)" != "" ]; then
swapoff "/dev/zram$i"
2015-12-27 13:16:17 +01:00
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
}
2018-02-25 12:27:48 +01:00
if [ ! "$1" ]; then
2015-12-27 13:16:17 +01:00
show_help
else
if [[ "$1" == "on" || "$1" == "enable" || "$1" == "yes" ]]; then
zram_on
else
zram_off
fi
fi
exit 0