#!/bin/bash # _____ _ _ # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___ # | __| _| -_| -_| . | . | | . | . | | -_| # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___| # # Freedom in the Cloud # # Enables or disables zram # # License # ======= # # Copyright (C) 2015-2018 Bob Mottram # # This program is free software: you can redistribute it and/or modify # 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 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . 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]'; 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]'; 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