freedombone-zram 3.5KB

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