Skip to content

Instantly share code, notes, and snippets.

@rizalp rizalp/init-zram-swapping
Last active Aug 27, 2019

Embed
What would you like to do?
/usr/bin/init-zram-swapping custom config
#!/bin/sh
# load dependency modules
# NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/')
NRDEVICES=1
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
MODPROBE_ARGS="zram_num_devices=${NRDEVICES}"
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
MODPROBE_ARGS="num_devices=${NRDEVICES}"
else
exit 1
fi
modprobe zram $MODPROBE_ARGS
# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
mem=$(((totalmem * 2 / ${NRDEVICES}) * 1024))
# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i - 1))
echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
mkswap /dev/zram${DEVNUMBER}
swapon -p 100 /dev/zram${DEVNUMBER}
done
@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Jan 4, 2019

Found after sudo apt install zram-config and seeing the contents of /etc/systemd/system/multi-user.target.wants/zram-config.service

@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Jan 13, 2019

/usr/bin/init-zram-swapping

@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Jan 18, 2019

In /etc/sysctl.conf, append:

vm.swappiness = 80

sudo sysctl -p

@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Aug 26, 2019

Better use zswap, as it will work in conjunction with swap devices https://www.addictivetips.com/ubuntu-linux-tips/enable-zswap-on-linux/

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=40 zswap.zpool=z3fold"

sudo update-grub

@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Aug 26, 2019

Need to add

sudo su
echo lz4 >> /etc/initramfs-tools/modules
echo lz4_compress >> /etc/initramfs-tools/modules
echo z3fold >> /etc/initramfs-tools/modules
update-initramfs -u
@rizalp

This comment has been minimized.

Copy link
Owner Author

commented Aug 27, 2019

verify

cat /sys/module/zswap/parameters/enabled
dmesg | grep -i zswap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.