1
0

zram.init 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [ -e "$zram_dev" ] || {
  23. logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
  24. return 1
  25. }
  26. which mkswap >/dev/null || {
  27. logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
  28. return 1
  29. }
  30. which swapon >/dev/null || {
  31. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
  32. return 1
  33. }
  34. which swapoff >/dev/null || {
  35. logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
  36. return 1
  37. }
  38. }
  39. zram_dev()
  40. {
  41. local idx="$1"
  42. echo "/dev/zram${idx:-0}"
  43. }
  44. zram_reset()
  45. {
  46. local dev="$1"
  47. local message="$2"
  48. local proc_entry="/sys/block/$( basename "$dev" )/reset"
  49. logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
  50. echo "1" >"$proc_entry"
  51. }
  52. zram_getdev()
  53. {
  54. #get unallocated zram dev
  55. local zdev=$( zram_dev )
  56. if [ "$(mount | grep $zdev)" ]; then
  57. local idx=`cat /sys/class/zram-control/hot_add`
  58. zdev="$( zram_dev $idx )"
  59. fi
  60. echo $zdev
  61. }
  62. zram_comp_algo()
  63. {
  64. local dev="$1"
  65. local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )"
  66. if [ -z "$zram_comp_algo" ] || [ ! -e /sys/block/$( basename $dev )/comp_algorithm ]; then
  67. return 0
  68. fi
  69. if [ `grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm` -ne 0 ]; then
  70. logger -s -t zram_comp_algo -p daemon.debug "Set compression algorithm '$zram_comp_algo' for zram '$dev'"
  71. echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm"
  72. else
  73. logger -s -t zram_comp_algo -p daemon.debug "Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
  74. fi
  75. }
  76. zram_comp_streams()
  77. {
  78. local dev="$1"
  79. local logical_cpus=$( grep -ci "^processor" /proc/cpuinfo )
  80. [ $logical_cpus -gt 1 ] || return 1
  81. local zram_comp_streams="$( uci -q get system.@system[0].zram_comp_streams )"
  82. [ -n "$zram_comp_streams" ] && [ "$zram_comp_streams" -le "$logical_cpus" ] || zram_comp_streams=$logical_cpus
  83. if [ -e /sys/block/$( basename $dev )/max_comp_streams ]; then
  84. logger -s -t zram_comp_streams -p daemon.debug "Set max compression streams to '$zram_comp_streams' for zram '$dev'"
  85. echo $zram_comp_streams > /sys/block/$( basename $dev )/max_comp_streams
  86. fi
  87. }
  88. start()
  89. {
  90. local zram_size="$( zram_size )"
  91. local zram_dev
  92. if [ $( grep -cs zram /proc/swaps ) -ne 0 ]; then
  93. logger -s -t zram_start -p daemon.notice "[OK] zram swap is already mounted"
  94. return 1
  95. fi
  96. zram_dev="$( zram_getdev )"
  97. zram_applicable "$zram_dev" || return 1
  98. logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
  99. zram_reset "$zram_dev" "enforcing defaults"
  100. zram_comp_algo "$zram_dev"
  101. zram_comp_streams "$zram_dev"
  102. echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize"
  103. mkswap "$zram_dev"
  104. swapon "$zram_dev"
  105. }
  106. stop()
  107. {
  108. local zram_dev
  109. for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do {
  110. logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
  111. swapoff "$zram_dev" && zram_reset "$zram_dev" "claiming memory back"
  112. local dev_index="$( echo $zram_dev | grep -o "[0-9]*$" )"
  113. if [ $dev_index -ne 0 ]; then
  114. logger -s -t zram_stop -p daemon.debug "removing zram $zram_dev"
  115. echo $dev_index > /sys/class/zram-control/hot_remove
  116. fi
  117. } done
  118. }