Advertisement
Guest User

debian jessie /etc/init.d/zram

a guest
Jun 16th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.96 KB | None | 0 0
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: zram
  4. # Required-Start:
  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. ### END INIT INFO
  11. start() {
  12. # get the number of CPUs
  13. num_cpus=$(grep -c processor /proc/cpuinfo)
  14. # if something goes wrong, assume we have 1
  15. [ "$num_cpus" != 0 ] || num_cpus=1
  16. # set decremented number of CPUs
  17. last_cpu=$((num_cpus - 1))
  18. FACTOR=40
  19. #& put the above single line in /etc/default/zram with the value you want
  20. [ -f /etc/default/zram ] && source /etc/default/zram || true
  21. factor=$FACTOR # percentage
  22. # get the amount of memory in the machine
  23. memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
  24. mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
  25. # load dependency modules
  26. echo $num_cpus
  27. if ! modprobe zram zram_num_devices=$num_cpus
  28. then
  29. echo -e "Your Kernel needs to be compiled with ZRAM support:" \
  30. "\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \
  31. "\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)"
  32. exit 1
  33. fi
  34. echo "zram devices probed successfully"
  35. # initialize the devices
  36. for i in $(seq 0 $last_cpu); do
  37. echo $mem_by_cpu > /sys/block/zram$i/disksize
  38. # Creating swap filesystems
  39. mkswap /dev/zram$i
  40. # Switch the swaps on
  41. swapon -p 100 /dev/zram$i
  42. done
  43. }
  44. stop() {
  45. # get the number of CPUs
  46. num_cpus=$(grep -c processor /proc/cpuinfo)
  47. # set decremented number of CPUs
  48. last_cpu=$((num_cpus - 1))
  49. # Switching off swap
  50. for i in $(seq 0 $last_cpu); do
  51. if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
  52. swapoff /dev/zram$i
  53. sleep 1
  54. fi
  55. done
  56. sleep 1
  57. rmmod zram
  58. }
  59. case "$1" in
  60. start)
  61. start
  62. ;;
  63. stop)
  64. stop
  65. ;;
  66. restart)
  67. stop
  68. sleep 3
  69. start
  70. ;;
  71. *)
  72. echo "Usage: $0 {start|stop|restart}"
  73. RETVAL=1
  74. esac
  75. exit $RETVAL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement