cielavenir

/etc/init.d/zramswap for system without upstart

Mar 14th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. #! /bin/sh
  2. #
  3. # zramswap - Script to setup zramswap.
  4. # based on atftpd and zramswap-enabler.
  5. #
  6. ### BEGIN INIT INFO
  7. # Provides:          atftpd
  8. # Required-Start:    $syslog $network $remote_fs
  9. # Required-Stop:     $syslog $network $remote_fs
  10. # Should-Start:      $local_fs
  11. # Should-Stop:       $local_fs
  12. # Default-Start:     2 3 4 5
  13. # Default-Stop:      0 1 6
  14. # Short-Description: Setup zramswap
  15. # Description:       Setup zramswap, to compress memory.
  16. ### END INIT INFO
  17.  
  18. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  19. NAME=zramswap
  20. DESC="zram swapping"
  21. OPTIONS=""
  22.  
  23. set -e
  24.  
  25. # get the number of CPUs
  26. num_cpus=$(grep -c processor /proc/cpuinfo)
  27. # if something goes wrong, assume we have 1
  28. [ "$num_cpus" != 0 ] || num_cpus=1
  29. # set decremented number of CPUs
  30. decr_num_cpus=$((num_cpus - 1))
  31. # get the amount of memory in the machine
  32. mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
  33. mem_total=$((mem_total_kb * 1024))
  34.  
  35. case "$1" in
  36.   start)
  37.         echo -n "Starting $DESC: "
  38.         # load dependency modules
  39.         modprobe zram zram_num_devices=$num_cpus
  40.  
  41.         # initialize the devices
  42.         for i in $(seq 0 $decr_num_cpus); do
  43.         echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
  44.         done
  45.  
  46.         # Creating swap filesystems
  47.         for i in $(seq 0 $decr_num_cpus); do
  48.         mkswap /dev/zram$i
  49.         done
  50.  
  51.         # Switch the swaps on
  52.         for i in $(seq 0 $decr_num_cpus); do
  53.         swapon -p 100 /dev/zram$i
  54.         done
  55.         echo "$NAME."
  56.         ;;
  57.   stop)
  58.         echo -n "Stopping $DESC: "
  59.         # Switching off swap
  60.         for i in $(seq 0 $decr_num_cpus); do
  61.         if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
  62.         swapoff /dev/zram$i
  63.         fi
  64.         done
  65.  
  66.         rmmod zram
  67.         echo "$NAME."
  68.         ;;
  69.   restart|reload|force-reload)
  70.         /etc/init.d/zramswap stop
  71.         sleep 1
  72.         /etc/init.d/zramswap start
  73.         ;;
  74.   *)
  75.         N=/etc/init.d/$NAME
  76.         echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
  77.         exit 1
  78.         ;;
  79. esac
  80.  
  81. exit 0
Advertisement
Add Comment
Please, Sign In to add comment