Last active
July 19, 2018 12:44
-
-
Save thomsh/048dc7aeabeffafe62ccd7499f7c6322 to your computer and use it in GitHub Desktop.
Setup zram as swap quick and dirty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
zramctl=$(command -v zramctl) | |
if [[ -z "$zramctl" ]];then | |
echo "Command zramctl not found" 1>&2 | |
exit 2 | |
fi | |
if swapon -s |grep '^/dev/zram' > /dev/null ; then | |
echo "zram already setup as swap" | |
exit 0 | |
fi | |
set -eux -o pipefail | |
modprobe zram | |
nproc=$(nproc) | |
size=$(grep '^MemTotal:' /proc/meminfo |awk '{print $2/1024/8}') | |
# default use lz4; with max available thread and 1/8 total ram | |
# so, zram device can use 1/8 of our ram with incompressible data | |
# + a small overhead | |
zdevice=$($zramctl --find --size ${size%.*}M -a lz4 -t $nproc) | |
if [[ ! -b "${zdevice}" ]];then | |
sleep 2 ## add a pause if device is not available | |
fi | |
mkswap --label zram4swap ${zdevice} | |
swapon --priority 100 ${zdevice} | |
# restore default swapiness value | |
sysctl -w vm.swappiness=60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment