1
0

zram.init 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh /etc/rc.common
  2. START=15
  3. ram_size()
  4. {
  5. local line
  6. while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
  7. }
  8. zram_size() # in megabytes
  9. {
  10. local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
  11. local ram_size="$( ram_size )"
  12. if [ -z "$zram_size" ]; then
  13. # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
  14. echo $(( $ram_size / 2048 ))
  15. else
  16. echo "$zram_size"
  17. fi
  18. }
  19. zram_applicable()
  20. {
  21. local zram_dev="$1"
  22. grep -sq ^"$zram_dev " /proc/swaps && {
  23. logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active"
  24. return 1
  25. }
  26. [ -e "$zram_dev" ] || {
  27. logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
  28. return 1
  29. }
  30. which mkswap >/dev/null || {
  31. logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
  32. return 1
  33. }
  34. which swapon >/dev/null || {
  35. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
  36. return 1
  37. }
  38. which swapoff >/dev/null || {
  39. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
  40. return 1
  41. }
  42. }
  43. zram_dev()
  44. {
  45. local core="$1"
  46. echo "/dev/zram${core:-0}"
  47. }
  48. zram_reset()
  49. {
  50. local dev="$1"
  51. local message="$2"
  52. local proc_entry="/sys/block/$( basename "$dev" )/reset"
  53. logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
  54. echo "1" >"$proc_entry"
  55. }
  56. list_cpu_idx()
  57. {
  58. # Offset by 1 if /dev/zram0 is in use by /tmp
  59. if [ "$(mount | grep /dev/zram0)" ]; then
  60. local line i=1
  61. # Hot-add new ZRAM device (if necessary)
  62. if [ ! -b /dev/zram1 ]; then
  63. cat /sys/class/zram-control/hot_add
  64. fi
  65. else
  66. local line i=0
  67. fi
  68. while read line; do {
  69. case "$line" in
  70. [Pp]rocessor*)
  71. echo $i
  72. i=$(( $i + 1 ))
  73. ;;
  74. esac
  75. } done <"/proc/cpuinfo"
  76. }
  77. start()
  78. {
  79. # http://shmilyxbq-compcache.googlecode.com/hg/README
  80. # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
  81. local zram_size="$( zram_size )"
  82. local zram_dev core
  83. for core in $( list_cpu_idx ); do {
  84. zram_dev="$( zram_dev "$core" )"
  85. zram_applicable "$zram_dev" || return 1
  86. logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
  87. zram_reset "$zram_dev" "enforcing defaults"
  88. echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
  89. mkswap "$zram_dev"
  90. swapon "$zram_dev"
  91. } done
  92. }
  93. stop()
  94. {
  95. local zram_dev proc_entry
  96. for core in $( list_cpu_idx ); do {
  97. zram_dev="$( zram_dev "$core" )"
  98. proc_entry="/sys/block/$( basename "$zram_dev" )/reset"
  99. grep -sq ^"$zram_dev " /proc/swaps && {
  100. logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
  101. swapoff "$zram_dev"
  102. }
  103. zram_reset "$zram_dev" "claiming memory back"
  104. } done
  105. }