daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Mar 21st, 2018 256 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3.  
  4. not_root() {
  5.     echo "ERROR: You have to be root to execute this script"
  6.     exit 1
  7. }
  8.  
  9. zram_exists() {
  10.     echo "ERROR: /etc/init.d/zram already exists"
  11.     exit 1
  12. }
  13.  
  14. # Check if user is root
  15. [ $EUID != 0 ] && not_root
  16.  
  17. # Check if zram file already exists
  18. [ -f /etc/init.d/zram ] && zram_exists
  19.  
  20.  
  21.  
  22. cat >/etc/init.d/zram <<EOL
  23. #!/bin/sh
  24. ### BEGIN INIT INFO
  25. # Provides:          zram
  26. # Required-Start:    $local_fs
  27. # Required-Stop:     $local_fs
  28. # Default-Start:     S
  29. # Default-Stop:      0 1 6
  30. # Short-Description: Use compressed RAM as in-memory swap
  31. # Description:       Use compressed RAM as in-memory swap
  32. ### END INIT INFO
  33.  
  34. # Author: Antonio Galea <antonio.galea@gmail.com>
  35. # Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
  36.  
  37. FRACTION=75
  38.  
  39. MEMORY=\`perl -ne'/^MemTotal:\s+(\d+)/ && print \$1*1024;' < /proc/meminfo\`
  40. CPUS=\`grep -c processor /proc/cpuinfo\`
  41. SIZE=\$(( MEMORY * FRACTION / 100 / CPUS ))
  42.  
  43. case "\$1" in
  44.   "start")
  45.     param=\`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '\`
  46.     modprobe zram \$param=\$CPUS
  47.     for n in \`seq \$CPUS\`; do
  48.       i=\$((n - 1))
  49.       echo \$SIZE > /sys/block/zram\$i/disksize
  50.       mkswap /dev/zram\$i
  51.       swapon /dev/zram\$i -p 10
  52.     done
  53.     ;;
  54.   "stop")
  55.     for n in \`seq \$CPUS\`; do
  56.       i=\$((n - 1))
  57.       swapoff /dev/zram\$i && echo "disabled disk \$n of \$CPUS" &
  58.     done
  59.     wait
  60.     sleep .5
  61.     modprobe -r zram
  62.     ;;
  63.   *)
  64.     echo "Usage: \`basename \$0\` (start | stop)"
  65.     exit 1
  66.     ;;
  67. esac
  68. EOL
  69.  
  70. chmod +x /etc/init.d/zram
  71.  
  72. insserv zram
  73.  
  74. /etc/init.d/zram start
  75.  
  76. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top