2013-01-06 16:21:29 +00:00
|
|
|
#!/bin/sh /etc/rc.common
|
|
|
|
|
|
|
|
START=15
|
|
|
|
|
|
|
|
ram_size()
|
|
|
|
{
|
|
|
|
local line
|
|
|
|
|
|
|
|
while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
|
|
|
|
}
|
|
|
|
|
|
|
|
zram_size() # in megabytes
|
|
|
|
{
|
|
|
|
local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
|
|
|
|
local ram_size="$( ram_size )"
|
|
|
|
|
|
|
|
if [ -z "$zram_size" ]; then
|
|
|
|
# e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
|
2018-08-07 09:16:34 +00:00
|
|
|
echo $(( ram_size / 2048 ))
|
2013-01-06 16:21:29 +00:00
|
|
|
else
|
|
|
|
echo "$zram_size"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
zram_applicable()
|
|
|
|
{
|
|
|
|
local zram_dev="$1"
|
|
|
|
|
|
|
|
[ -e "$zram_dev" ] || {
|
|
|
|
logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
which mkswap >/dev/null || {
|
|
|
|
logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
which swapon >/dev/null || {
|
|
|
|
logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
which swapoff >/dev/null || {
|
|
|
|
logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zram_dev()
|
|
|
|
{
|
2018-08-01 21:50:00 +00:00
|
|
|
local idx="$1"
|
|
|
|
echo "/dev/zram${idx:-0}"
|
2013-01-06 16:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zram_reset()
|
|
|
|
{
|
|
|
|
local dev="$1"
|
|
|
|
local message="$2"
|
|
|
|
local proc_entry="/sys/block/$( basename "$dev" )/reset"
|
|
|
|
|
|
|
|
logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
|
|
|
|
echo "1" >"$proc_entry"
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:50:00 +00:00
|
|
|
zram_getdev()
|
|
|
|
{
|
|
|
|
#get unallocated zram dev
|
|
|
|
local zdev=$( zram_dev )
|
|
|
|
|
|
|
|
if [ "$(mount | grep $zdev)" ]; then
|
|
|
|
local idx=`cat /sys/class/zram-control/hot_add`
|
|
|
|
zdev="$( zram_dev $idx )"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $zdev
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:15:44 +00:00
|
|
|
zram_comp_algo()
|
|
|
|
{
|
|
|
|
local dev="$1"
|
|
|
|
local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )"
|
|
|
|
|
|
|
|
if [ -z "$zram_comp_algo" ] || [ ! -e /sys/block/$( basename $dev )/comp_algorithm ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ `grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm` -ne 0 ]; then
|
|
|
|
logger -s -t zram_comp_algo -p daemon.debug "Set compression algorithm '$zram_comp_algo' for zram '$dev'"
|
|
|
|
echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm"
|
|
|
|
else
|
|
|
|
logger -s -t zram_comp_algo -p daemon.debug "Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-01-06 16:21:29 +00:00
|
|
|
start()
|
|
|
|
{
|
|
|
|
local zram_size="$( zram_size )"
|
2018-08-01 21:50:00 +00:00
|
|
|
local zram_dev
|
2013-01-06 16:21:29 +00:00
|
|
|
|
2018-08-01 21:50:00 +00:00
|
|
|
if [ $( grep -cs zram /proc/swaps ) -ne 0 ]; then
|
|
|
|
logger -s -t zram_start -p daemon.notice "[OK] zram swap is already mounted"
|
|
|
|
return 1
|
|
|
|
fi
|
2013-01-06 16:21:29 +00:00
|
|
|
|
2018-08-01 21:50:00 +00:00
|
|
|
zram_dev="$( zram_getdev )"
|
|
|
|
zram_applicable "$zram_dev" || return 1
|
2013-01-06 16:21:29 +00:00
|
|
|
|
2018-08-01 21:50:00 +00:00
|
|
|
logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
|
|
|
|
|
|
|
|
zram_reset "$zram_dev" "enforcing defaults"
|
|
|
|
zram_comp_algo "$zram_dev"
|
|
|
|
echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize"
|
|
|
|
mkswap "$zram_dev"
|
|
|
|
swapon "$zram_dev"
|
2013-01-06 16:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stop()
|
|
|
|
{
|
2018-08-01 21:18:45 +00:00
|
|
|
local zram_dev
|
|
|
|
|
|
|
|
for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do {
|
|
|
|
logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
|
|
|
|
swapoff "$zram_dev" && zram_reset "$zram_dev" "claiming memory back"
|
|
|
|
local dev_index="$( echo $zram_dev | grep -o "[0-9]*$" )"
|
|
|
|
if [ $dev_index -ne 0 ]; then
|
|
|
|
logger -s -t zram_stop -p daemon.debug "removing zram $zram_dev"
|
|
|
|
echo $dev_index > /sys/class/zram-control/hot_remove
|
|
|
|
fi
|
2013-01-06 16:21:29 +00:00
|
|
|
} done
|
|
|
|
}
|
|
|
|
|