Last active
June 19, 2019 19:30
-
-
Save jserviceorg/f1e379c26c9a8506bb7872f9094fe316 to your computer and use it in GitHub Desktop.
Install zRAM on Debian(8)
This file contains 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
#!/bin/bash | |
# | |
not_root() { | |
echo "ERROR: You have to be root to execute this script" | |
exit 1 | |
} | |
zram_exists() { | |
echo "ERROR: /etc/init.d/zram already exists" | |
exit 1 | |
} | |
# Check if user is root | |
[ $EUID != 0 ] && not_root | |
# Check if zram file already exists | |
[ -f /etc/init.d/zram ] && zram_exists | |
# Check if insserv is installed | |
if [ $(dpkg-query -W -f='${Status}' insserv 2>/dev/null | grep -c "ok installed") -eq 0 ]; | |
then | |
apt update && apt install insserv; | |
fi | |
cat >/etc/init.d/zram <<EOL | |
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: zram | |
# Required-Start: $local_fs | |
# Required-Stop: $local_fs | |
# Default-Start: S | |
# Default-Stop: 0 1 6 | |
# Short-Description: Use compressed RAM as in-memory swap | |
# Description: Use compressed RAM as in-memory swap | |
### END INIT INFO | |
# Author: Antonio Galea <antonio.galea@gmail.com> | |
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization | |
# modified by Juergen Gotteswinter - use zramctl to create devices with lz4 compression and install insserv if required | |
FRACTION=75 | |
MEMORY=\`perl -ne'/^MemTotal:\s+(\d+)/ && print \$1*1024;' < /proc/meminfo\` | |
CPUS=\`grep -c processor /proc/cpuinfo\` | |
SIZE=\$(( MEMORY * FRACTION / 100 / CPUS )) | |
case "\$1" in | |
"start") | |
param=\`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '\` | |
modprobe zram \$param=\$CPUS | |
for n in \`seq \$CPUS\`; do | |
i=\$((n - 1)) | |
# echo \$SIZE > /sys/block/zram\$i/disksize | |
echo $SIZE | |
zramctl -a lz4 -t $CPUS -s \$SIZE /dev/zram\$i | |
mkswap /dev/zram\$i | |
swapon /dev/zram\$i -p 10 | |
done | |
;; | |
"stop") | |
for n in \`seq \$CPUS\`; do | |
i=$((n - 1)) | |
swapoff /dev/zram\$i && echo "disabled disk \$n of \$CPUS" & | |
done | |
wait | |
sleep .5 | |
modprobe -r zram | |
;; | |
*) | |
echo "Usage: \`basename \$0\` (start | stop)" | |
exit 1 | |
;; | |
esac | |
EOL | |
chmod +x /etc/init.d/zram | |
insserv zram | |
/etc/init.d/zram start | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment