Debian /etc/init.d/zram


SUBMITTED BY: Guest

DATE: May 27, 2013, 10:33 p.m.

FORMAT: Text only

SIZE: 3.2 kB

HITS: 1506

  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: zram
  4. # Required-Start: $local_fs
  5. # Required-Stop:
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
  9. # Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
  10. # Included as part of antix-goodies package by anticapitalista <antiX@operamail.com>
  11. # This script was written by tradetaxfree and is found at http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/
  12. # Extraneous commented (disabled) commands removed by djohnston at http://linuxvillage.net/
  13. # Copy this script (as root) from /usr/local/bin to /etc/init.d and then #update-rc.d zram defaults
  14. # After booting verify the module is loaded with: lsmod | grep zram
  15. ### END INIT INFO
  16. start() {
  17. # get the number of CPUs
  18. num_cpus=$(grep -c processor /proc/cpuinfo)
  19. # if something goes wrong, assume we have 1
  20. [ "$num_cpus" != 0 ] || num_cpus=1
  21. # set decremented number of CPUs
  22. last_cpu=$((num_cpus - 1))
  23. #default Factor % = 90 change this value here or create /etc/default/zram[/b][/color]
  24. FACTOR=90
  25. #& put the above single line in /etc/default/zram with the value you want
  26. [ -f /etc/default/zram ] && source /etc/default/zram || true
  27. factor=$FACTOR # percentage
  28. # get the amount of memory in the machine
  29. memtotal=$(grep MemTotal /proc/meminfo | sed 's/[^0-9]\+//g')
  30. mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
  31. # load dependency modules
  32. modprobe zram num_devices=$num_cpus
  33. if [ $? -gt 0 ]; then
  34. echo -e "Your Kernel needs to be compiled with ZRAM support:" \
  35. "\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \
  36. "\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)" \
  37. "\n\nThe Liquorix Kernel (http://liquorix.net) has ZRAM support built in."
  38. exit 1
  39. fi
  40. echo "zram devices probed successfully"
  41. # initialize the devices
  42. for i in $(seq 0 $last_cpu); do
  43. echo $mem_by_cpu > /sys/block/zram$i/disksize
  44. # Creating swap filesystems
  45. mkswap /dev/zram$i
  46. # Switch the swaps on
  47. swapon -p 100 /dev/zram$i
  48. done
  49. }
  50. stop() {
  51. # get the number of CPUs
  52. num_cpus=$(grep -c processor /proc/cpuinfo)
  53. # set decremented number of CPUs
  54. last_cpu=$((num_cpus - 1))
  55. # Switching off swap
  56. for i in $(seq 0 $last_cpu); do
  57. if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
  58. swapoff /dev/zram$i
  59. sleep 1
  60. fi
  61. done
  62. sleep 1
  63. rmmod zram
  64. }
  65. case "$1" in
  66. start)
  67. start
  68. ;;
  69. stop)
  70. stop
  71. ;;
  72. restart)
  73. stop
  74. sleep 3
  75. start
  76. ;;
  77. *)
  78. echo "Usage: $0 {start|stop|restart}"
  79. RETVAL=1
  80. esac
  81. exit $RETVAL

comments powered by Disqus