Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.42 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides: zram
  5. # Required-Start:
  6. # Required-Stop:
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
  10. # Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
  11. ### END INIT INFO
  12.  
  13. DEFAULTS="/etc/default/zram"
  14.  
  15. # Include defaults if available
  16. [ -r "$DEFAULTS" ] && . "$DEFAULTS"
  17.  
  18. # Get lsb functions
  19. . /lib/lsb/init-functions
  20.  
  21. start() {
  22.     # Add support of defaults file parameter - ZRAM_NDEVS
  23.     if [ -z "$ZRAM_NDEVS" ] || [ "$ZRAM_NDEVS" -le "0" ]; then
  24.         # get the number of CPUs
  25.         num_cpus=$(grep -c processor /proc/cpuinfo)
  26.     else
  27.         # or just use user specified
  28.         num_cpus=$ZRAM_NDEVS
  29.     fi
  30.  
  31.     # if something goes wrong, assume we have 1
  32.     [ "$num_cpus" != 0 ] || num_cpus=1
  33.  
  34.     decr_num_cpus=$((num_cpus - 1))
  35.  
  36.     # find module and it's param name
  37.     error_file=$(mktemp ${RC_NAME}XXXXXX) # storage for error message
  38.     param_name=$(/usr/bin/env modinfo --parameters zram 2>$error_file | head -1 | cut -d: -f1)
  39.     error_message=$(<$error_file)
  40.     rm -f $error_file
  41.     mod_found=$?
  42.  
  43.  
  44.     # load dependency modules
  45.     if [ "$mod_found" == 0 ]; then
  46.         log_begin_msg "Loading zRAM kernel module"
  47.         modprobe zram "$param_name=$num_cpus"
  48.         log_end_msg $?
  49.     else
  50.         log_failure_msg "Failed to load zRAM kernel module: ${error_message}"
  51.         # shit happens, yep.
  52.         exit $mod_found
  53.     fi
  54.  
  55.     # get the amount of memory in the machine
  56.     mem_total_kb=$(fgrep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
  57.     mem_total=$((mem_total_kb * 1024))
  58.  
  59.     # Add support of defaults file parameter - ZRAM_USE_PERCENT
  60.     if [ -z "$ZRAM_USE_PERCENT" ] || [ "$ZRAM_USE_PERCENT" -le "0" ] || [ "$ZRAM_USE_PERCENT" -gt "100" ]; then
  61.         # use 100% of ram for zRAM storage (suitable for low-ram laptops)
  62.         use_percent=100
  63.     else
  64.         # or just use user specified
  65.         use_percent=$ZRAM_USE_PERCENT
  66.     fi
  67.  
  68.     zsize=$((use_percent * mem_total / num_cpus / 100))
  69.  
  70.     # initialize the devices
  71.     for i in $(seq 0 $decr_num_cpus); do
  72.         echo $zsize > /sys/block/zram$i/disksize
  73.     done
  74.  
  75.     # get page size
  76.     page_size=$(/usr/bin/env getconf PAGESIZE || /usr/bin/env getconf PAGE_SIZE)
  77.  
  78.     # Creating swap filesystems
  79.     for i in $(seq 0 $decr_num_cpus); do
  80.         msg=">  Making swap on /dev/zram$i"
  81.         mkswap -p $page_size /dev/zram$i 2>&1 >/dev/null
  82.         [ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg
  83.     done
  84.  
  85.     # Switch the swaps on
  86.     for i in $(seq 0 $decr_num_cpus); do
  87.         msg=">>  Activating swap on /dev/zram$i"
  88.         swapon -p 100 /dev/zram$i
  89.         [ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg
  90.     done
  91.  
  92.     # TODO FIXME need strict error reporting
  93.     # log_end_msg 0
  94. }
  95.  
  96. stop() {
  97.     # Switching off swap ASAP
  98.     for zswap in $(fgrep /dev/zram /proc/swaps | cut -d' ' -f1); do
  99.             swapoff $zswap
  100.     done
  101.  
  102.     # Custom kernels can contain zram compiled-in
  103.     [ -z "$(fgrep zram /proc/modules)" ] || rmmod zram
  104. }
  105.  
  106. case "$1" in
  107.     start)
  108.         start
  109.         ;;
  110.     stop)
  111.         stop
  112.         ;;
  113.     restart)
  114.         stop
  115.         sleep 3
  116.         start
  117.         ;;
  118.     *)
  119.         echo "Usage: $0 {start|stop|restart}"
  120.         RETVAL=1
  121. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement