As an old-school technocrat, I prefer doing things at as low a level as possible. I therefore created a small script to enable or change the size of my zram swap file.
#!/bin/bash
# Based on https://github.com/ric96/zram
# ver: 30 dec 2018
# Installed in /usr/local/bin/my-zram.bash
# And made executable " chmod a+x /usr/local/bin/my-zram.bash "
# I use an /etc/crontab entry of:
# " @reboot root /usr/local/bin/my-zram.bash 256M ; # [ optionally use w/ "'size'M" for Mega ]
# Or for command-line: " sudo /usr/local/bin/my-zram.bash 256M "
# Note that you may want to change the '256M' parameter I am using.
logger -- $0 $$ zram $1 Start
## If $1 exists
test -n $1 && \
{
ZRAMSIZE=$1
DOLONE=$1
}
## And yes, invalid option input will create interesting, but apparently not hazardous results.
## If no $1, fallback and calculate a reasonable (to me) size.
test -z $1 && \
{
totalmem=`free | grep -e "^Mem:" | awk '{print $2}'`
mem=$(( ($totalmem) * 1024 / 2 ))
#echo $mem > /sys/block/zram0/disksize
ZRAMSIZE=$mem
DOLONE=NULL
logger -- $0 $$ Using totalmem $totalmem \* 1024 /2 = $mem ZRAMSIZE=$ZRAMSIZE
}
## Do we already have a /dev/zram0 ?? if so, swapoff and reset it.
test -b /dev/zram0 && swapoff -v /dev/zram0
test -b /dev/zram0 && echo 1 > /sys/block/zram0/reset
## If /dev/zram0 does NOT exist, but the 'zram' kernel module IS loaded, then remove it.
test -b /dev/zram0 || ( lsmod|grep -q zram && rmmod -v zram )
## (Re)Install the 'zram' kernel module. FYI: It doesn't hurt to 'reinstall'...
modprobe -v zram num_devices=1
## Build the zram swapfile.
echo $ZRAMSIZE > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -v -p 5 /dev/zram0
logger -- $0 $$ zram Done. ZRAMSIZE=$ZRAMSIZE \$1=$DOLONE
# The_End
Enjoy! It works for me. And you may want to review, understand, and edit to suit your situation. :-)
And be aware that if you run this script from cron, as written, it produces output which will (should) be mailed to you ( or root ).
sudo echo
. You need to doecho 128M > sudo tee /sys/block/zram0/disksize
to have permission to gain permission to overwrite the file in /sys. – ketil Jan 11 '17 at 10:35