419221b1a3ac173672333e5c6bba0ad83588f45e
[openwrt/openwrt.git] / package / system / zram-swap / files / zram.init
1 #!/bin/sh /etc/rc.common
2
3 START=15
4
5 ram_size()
6 {
7 local line
8
9 while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
10 }
11
12 zram_size() # in megabytes
13 {
14 local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
15 local ram_size="$( ram_size )"
16
17 if [ -z "$zram_size" ]; then
18 # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
19 echo $(( ram_size / 2048 ))
20 else
21 echo "$zram_size"
22 fi
23 }
24
25 zram_applicable()
26 {
27 local zram_dev="$1"
28
29 grep -sq ^"$zram_dev " /proc/swaps && {
30 logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active"
31 return 1
32 }
33
34 [ -e "$zram_dev" ] || {
35 logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
36 return 1
37 }
38
39 which mkswap >/dev/null || {
40 logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
41 return 1
42 }
43
44 which swapon >/dev/null || {
45 logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
46 return 1
47 }
48
49 which swapoff >/dev/null || {
50 logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
51 return 1
52 }
53 }
54
55 zram_dev()
56 {
57 local core="$1"
58
59 echo "/dev/zram${core:-0}"
60 }
61
62 zram_reset()
63 {
64 local dev="$1"
65 local message="$2"
66 local proc_entry="/sys/block/$( basename "$dev" )/reset"
67
68 logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
69 echo "1" >"$proc_entry"
70 }
71
72 zram_comp_algo()
73 {
74 local dev="$1"
75 local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )"
76
77 if [ -z "$zram_comp_algo" ] || [ ! -e /sys/block/$( basename $dev )/comp_algorithm ]; then
78 return 0
79 fi
80
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"
84 else
85 logger -s -t zram_comp_algo -p daemon.debug "Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
86 fi
87 }
88
89 list_cpu_idx()
90 {
91 # Offset by 1 if /dev/zram0 is in use by /tmp
92 if mount | grep -q /dev/zram0; then
93 local line i=1
94 # Hot-add new ZRAM device (if necessary)
95 if [ ! -b /dev/zram1 ]; then
96 cat /sys/class/zram-control/hot_add
97 fi
98 else
99 local line i=0
100 fi
101
102 while read line; do {
103 case "$line" in
104 [Pp]rocessor*)
105 echo $i
106 i=$(( i + 1 ))
107 ;;
108 esac
109 } done <"/proc/cpuinfo"
110 }
111
112 start()
113 {
114 # http://shmilyxbq-compcache.googlecode.com/hg/README
115 # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
116
117 local zram_size="$( zram_size )"
118 local zram_dev core
119
120 for core in $( list_cpu_idx ); do {
121 zram_dev="$( zram_dev "$core" )"
122 zram_applicable "$zram_dev" || return 1
123
124 logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
125
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"
129 mkswap "$zram_dev"
130 swapon "$zram_dev"
131 } done
132 }
133
134 stop()
135 {
136 local zram_dev proc_entry
137
138 for core in $( list_cpu_idx ); do {
139 zram_dev="$( zram_dev "$core" )"
140 proc_entry="/sys/block/$( basename "$zram_dev" )/reset"
141
142 grep -sq ^"$zram_dev " /proc/swaps && {
143 logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
144 swapoff "$zram_dev"
145 }
146
147 zram_reset "$zram_dev" "claiming memory back"
148 } done
149 }
150