ZRam

   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 
  12 start() {
  13     # get the number of CPUs
  14     num_cpus=$(grep -c processor /proc/cpuinfo)
  15     # if something goes wrong, assume we have 1
  16     [ "$num_cpus" != 0 ] || num_cpus=1
  17 
  18     # set decremented number of CPUs
  19     decr_num_cpus=$((num_cpus - 1))
  20 
  21     # get the amount of memory in the machine
  22     mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
  23     mem_total=$((mem_total_kb * 1024))
  24 
  25     # load dependency modules
  26     modpwqrobe zram num_devices=$num_cpus
  27 
  28     # initialize the devices
  29     for i in $(seq 0 $decr_num_cpus); do
  30     echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
  31     done
  32 
  33     # Creating swap filesystems
  34     for i in $(seq 0 $decr_num_cpus); do
  35     mkswap /dev/zram$i
  36     done
  37 
  38     # Switch the swaps on
  39     for i in $(seq 0 $decr_num_cpus); do
  40     swapon -p 100 /dev/zram$i
  41     done
  42 }
  43 
  44 stop() {
  45     # get the number of CPUs
  46     num_cpus=$(grep -c processor /proc/cpuinfo)
  47 
  48     # set decremented number of CPUs
  49     decr_num_cpus=$((num_cpus - 1))
  50 
  51     # Switching off swap
  52     for i in $(seq 0 $decr_num_cpus); do
  53     if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
  54         swapoff /dev/zram$i
  55         sleep 1
  56     fi
  57     done
  58 
  59     sleep 1
  60     rmmod zram
  61 }
  62 
  63 case "$1" in
  64     start)
  65         start
  66         ;;
  67     stop)
  68         stop
  69         ;;
  70     restart)
  71         stop
  72         sleep 3
  73         start
  74         ;;
  75     *)
  76         echo "Usage: $0 {start|stop|restart}"
  77         RETVAL=1
  78 esac
  79 exit $RETVAL

Wikinger: ZRam (zuletzt geändert am 2015-01-29 15:36:47 durch Robert)