Make ZRAM_PERCENTAGE and ZRAM_MAX_DEVICES configurable

This commit is contained in:
Thomas Kaiser 2018-09-06 10:04:37 +02:00 committed by GitHub
parent 46340a242f
commit 4804c91114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,7 @@
# and script configuration
. /usr/lib/armbian/armbian-common
# ZRAM_PERCENTAGE and ZRAM_MAX_DEVICES can be defined in /etc/default/armbian-zram-config
[ -f /etc/default/armbian-zram-config ] && . /etc/default/armbian-zram-config
activate_zram() {
@ -25,17 +25,18 @@ activate_zram() {
dpkg -l | grep -q 'zram-config' && return
[[ "$ENABLED" != "true" ]] && return
# Load zram module with n instances (one per CPU core, 4 are the maximum)
# Load zram module with n instances (one per CPU core, $ZRAM_MAX_DEVICES are the max)
zram_max_devs=${ZRAM_MAX_DEVICES:=4}
cpu_cores=$(grep -c '^processor' /proc/cpuinfo | sed 's/^0$/1/')
[[ ${cpu_cores} -gt 4 ]] && zram_devices=4 || zram_devices=${cpu_cores}
[[ ${cpu_cores} -gt ${zram_max_devs} ]] && zram_devices=${zram_max_devs} || zram_devices=${cpu_cores}
module_args="$(modinfo zram | awk -F" " '/num_devices/ {print $2}' | cut -f1 -d:)"
[[ -n ${module_args} ]] && modprobe zram ${module_args}=$(( ${zram_devices} + 1 )) || return
# Use half of the real memory by default --> 1/${ram_divisor}
ram_divisor=2
# Use 50% of real memory by default
zram_percent=${ZRAM_PERCENTAGE:=50}
mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
memory_total=$(awk '{printf("%d",$2*1024)}' <<<${mem_info})
mem_per_zram_device=$(( ${memory_total} / ${zram_devices} / ${ram_divisor} ))
mem_per_zram_device=$(( ${memory_total} / ${zram_devices} * ${zram_percent} / 100 ))
for (( i=1; i<=zram_devices; i++ )); do
[[ -f /sys/block/zram${i}/comp_algorithm ]] && echo lz4 >/sys/block/zram${i}/comp_algorithm 2>/dev/null
@ -43,7 +44,7 @@ activate_zram() {
mkswap /dev/zram${i}
swapon -p 5 /dev/zram${i}
done
echo -e "\n\n\n### Activated ${zram_devices} zram swap devices with $(( ${mem_per_zram_device} / 1048576 )) MB each\n" >>${Log}
echo -e "### Activated ${zram_devices} zram swap devices with $(( ${mem_per_zram_device} / 1048576 )) MB each\n" >>${Log}
# Dedicate SIZE MB (uncompressed) /dev/zram0 device for log2ram and choose most efficient
# compression scheme available -- https://patchwork.kernel.org/patch/9918897/