SHARE
TWEET

zram-b

a guest Aug 11th, 2012 171 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. # Copy this script (as root) from /usr/local/bin to /etc/init.d and then #update-rc.d zram defaults
  13. # After booting verify the module is loaded with: lsmod | grep zram
  14. ### END INIT INFO
  15.  
  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.  
  22.     # set decremented number of CPUs
  23.     last_cpu=$((num_cpus - 1))
  24.    
  25.     #default Factor % = 90 change this value here or create /etc/default/zram
  26.     FACTOR=90
  27.     #& put the above single line in /etc/default/zram with the value you want
  28.         [ -f /etc/default/zram ] && source /etc/default/zram || true
  29.         factor=$FACTOR # percentage
  30.  
  31.     # get the amount of memory in the machine
  32. #    memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
  33.      memtotal=$(grep MemTotal /proc/meminfo | sed 's/[^0-9]\+//g')
  34.    mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
  35.  
  36.     # load dependency modules
  37.     # kernels 3.4 onwards
  38.    
  39.     modprobe zram num_devices=$num_cpus
  40.    
  41.     if [ $? -gt 0 ]; then
  42.           # kernels 3.1 - 3.3
  43.       #modprobe zram zram_num_devices=$num_cpus        
  44.     #else
  45.       echo -e "Your Kernel needs to be compiled with ZRAM support:" \
  46.       "\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \
  47.       "\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)" \
  48.       "\n\nThe Liquorix Kernel (http://liquorix.net) has ZRAM support built in."
  49.       exit 1
  50.     fi
  51.     echo "zram devices probed successfully"
  52.    
  53.     # initialize the devices
  54.     for i in $(seq 0 $last_cpu); do
  55.     echo $mem_by_cpu > /sys/block/zram$i/disksize
  56.     # Creating swap filesystems
  57.     mkswap /dev/zram$i
  58.     # Switch the swaps on
  59.     swapon -p 100 /dev/zram$i
  60.     done
  61. }
  62.  
  63. stop() {
  64.     # get the number of CPUs
  65.     num_cpus=$(grep -c processor /proc/cpuinfo)
  66.  
  67.     # set decremented number of CPUs
  68.     last_cpu=$((num_cpus - 1))
  69.  
  70.     # Switching off swap
  71.     for i in $(seq 0 $last_cpu); do
  72.     if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
  73.     swapoff /dev/zram$i
  74.     sleep 1
  75.     fi
  76.     done
  77.  
  78.     sleep 1
  79.     rmmod zram
  80. }
  81.  
  82. case "$1" in
  83.     start)
  84.         start
  85.         ;;
  86.     stop)
  87.         stop
  88.         ;;
  89.     restart)
  90.         stop
  91.         sleep 3
  92.         start
  93.         ;;
  94.     *)
  95.         echo "Usage: $0 {start|stop|restart}"
  96.         RETVAL=1
  97. esac
  98. exit $RETVAL
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