This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

zramctrl Arch Linux Raspberry Pi

By: a guest on May 19th, 2013  |  syntax: Bash  |  size: 1.12 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/sh
  2.  
  3. start() {
  4.   cpus=$(ls -ld /sys/devices/system/cpu/cpu[0-9]* 2>/dev/null| grep -cEw 'cpu[0-9]+')
  5.  
  6.   exec awk -v cpus=$cpus '
  7.  FILENAME == "/proc/cpuinfo" && $1 == "processor" {
  8.    cpucount++
  9.    next
  10.  }
  11.  
  12.  FILENAME == "/proc/meminfo" && $1 == "MemTotal:" {
  13.    mem_total = (0 + $2) * 1024
  14.    next
  15.  }
  16.  
  17.  END {
  18.    if (cpucount == 0){
  19.      if (cpus == 0) {
  20.        cpus = 1;
  21.        print "Warning no CPUs detected, defaulting to 1 CPU"
  22.      }
  23.      cpucount = cpus
  24.    }
  25.    mem_per_cpu = sprintf("%d", mem_total / cpucount)
  26.  
  27.    system("modprobe zram num_devices=" cpucount)
  28.  
  29.    for (i = 0; i < cpucount; i++) {
  30.      print mem_per_cpu > "/sys/block/zram" i "/disksize"
  31.      system("mkswap /dev/zram" i " -L zram" i)
  32.      swapdevs = swapdevs " /dev/zram" i
  33.    }
  34.  
  35.    system("swapon -p 100" swapdevs)
  36.  }
  37.  ' /proc/cpuinfo /proc/meminfo
  38. }
  39.  
  40. stop() {
  41.   exec awk '
  42.  FNR > 1 && $1 ~ /^\/dev\/zram[0-9]+$/ {
  43.    activeswaps = activeswaps " " $1
  44.  }
  45.  
  46.  END {
  47.    system("swapoff" activeswaps)
  48.    system("rmmod zram")
  49.  }
  50.  ' /proc/swaps
  51. }
  52.  
  53. case $1 in
  54.   start|stop) "$1" ;;
  55. esac
clone this paste RAW Paste Data