#!/bin/bash
#set -x
# default factor of 1/3 of RAM
factor=3
[ -f /etc/zram.conf ] && source /etc/zram.conf || true
factor=$FACTOR

# Calculate ZRAM size
mem_total=$(free -w |grep "^Mem" |awk '{printf("%d",$2)}')
zram_size=$((${mem_total} / ${factor} /1024))

# zram in recent kernels is multitreaded so we don't need to balance across CPUs
modprobe -q zram num_devices=1

# Create ZRAM with first device, lz4 algorithm
zramdev=$(zramctl --find --algorithm lz4 --size ${zram_size}MB 2>&1)
# The above returns the device. Use it below
mkswap $zramdev
swapon $zramdev

# Investigate this and swapiness
# echo 0 > /proc/sys/vm/page-cluster

echo -e "\nActivated ZRAM swap device of ${zram_size} MB\n"
