1 #!/bin/sh /etc/rc.common
9 while read line
; do case "$line" in MemTotal
:*) set $line; echo "$2"; break ;; esac; done </proc
/meminfo
12 zram_size
() # in megabytes
14 local zram_size
="$( uci -q get system.@system[0].zram_size_mb )"
15 local ram_size
="$( ram_size )"
17 if [ -z "$zram_size" ]; then
18 # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
19 echo $
(( ram_size
/ 2048 ))
29 grep -sq ^
"$zram_dev " /proc
/swaps
&& {
30 logger
-s -t zram_applicable
-p daemon.notice
"[OK] '$zram_dev' already active"
34 [ -e "$zram_dev" ] ||
{
35 logger
-s -t zram_applicable
-p daemon.crit
"[ERROR] device '$zram_dev' not found"
39 which mkswap
>/dev
/null ||
{
40 logger
-s -t zram_applicable
-p daemon.err
"[ERROR] 'mkswap' not installed"
44 which swapon
>/dev
/null ||
{
45 logger
-s -t zram_applicable
-p daemon.err
"[ERROR] 'swapon' not installed"
49 which swapoff
>/dev
/null ||
{
50 logger
-s -t zram_applicable
-p daemon.err
"[ERROR] 'swapoff' not installed"
59 echo "/dev/zram${core:-0}"
66 local proc_entry
="/sys/block/$( basename "$dev" )/reset"
68 logger
-s -t zram_reset
-p daemon.debug
"$message via $proc_entry"
69 echo "1" >"$proc_entry"
75 local zram_comp_algo
="$( uci -q get system.@system[0].zram_comp_algo )"
77 if [ -z "$zram_comp_algo" ] ||
[ ! -e /sys
/block
/$
( basename $dev )/comp_algorithm
]; then
81 if [ `grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm` -ne 0 ]; then
82 logger
-s -t zram_comp_algo
-p daemon.debug
"Set compression algorithm '$zram_comp_algo' for zram '$dev'"
83 echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm"
85 logger
-s -t zram_comp_algo
-p daemon.debug
"Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
91 # Offset by 1 if /dev/zram0 is in use by /tmp
92 if mount |
grep -q /dev
/zram0
; then
94 # Hot-add new ZRAM device (if necessary)
95 if [ ! -b /dev
/zram1
]; then
96 cat /sys
/class
/zram-control
/hot_add
102 while read line
; do {
109 } done <"/proc/cpuinfo"
114 # http://shmilyxbq-compcache.googlecode.com/hg/README
115 # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
117 local zram_size
="$( zram_size )"
120 for core
in $
( list_cpu_idx
); do {
121 zram_dev
="$( zram_dev "$core" )"
122 zram_applicable
"$zram_dev" ||
return 1
124 logger
-s -t zram_start
-p daemon.debug
"activating '$zram_dev' for swapping ($zram_size MegaBytes)"
126 zram_reset
"$zram_dev" "enforcing defaults"
127 echo $
(( zram_size
* 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize"
128 zram_comp_algo
"$zram_dev"
138 for zram_dev
in $
( grep zram
/proc
/swaps |
awk '{print $1}' ); do {
139 logger
-s -t zram_stop
-p daemon.debug
"deactivate swap $zram_dev"
140 swapoff
"$zram_dev" && zram_reset
"$zram_dev" "claiming memory back"
141 local dev_index
="$( echo $zram_dev | grep -o "[0-9]*$
" )"
142 if [ $dev_index -ne 0 ]; then
143 logger
-s -t zram_stop
-p daemon.debug
"removing zram $zram_dev"
144 echo $dev_index > /sys
/class
/zram-control
/hot_remove