Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active April 4, 2017 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou1okada/c22e32283c472490e214 to your computer and use it in GitHub Desktop.
Save kou1okada/c22e32283c472490e214 to your computer and use it in GitHub Desktop.
An initscript for zram.

An initscript for zram

Installation

# for CentOS 6.6
( cd /etc/init.d; export $(grep ^proxy= /etc/yum.conf)>/dev/null; curl -LO ${proxy:+-x $proxy} https://gist.github.com/kou1okada/c22e32283c472490e214/raw/zram && chmod +x zram )
chkconfig --add zram
chkconfig zram on

Configuration

/etc/init.d/zram loads below configuration files.

  • /etc/syscontrol/zram
  • /etc/default/zram

Example of configuration file:

# FACTOR=50 # Percentage to total memory for zram size (default: 50).

Operation

service zram {start|stop|restart|status}

License

References

#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: An initscript for zram.
# The official page of this script is below.
# https://gist.github.com/kou1okada/c22e32283c472490e214
# This script uses below as a recerence.
# https://github.com/mystilleef/FedoraZram
# https://gist.github.com/jdef/8544249
# https://launchpad.net/ubuntu/+source/zram-config
# https://extremeshok.com/5094/centos-6-x-rhce-6-redhat-6-zram-compression-compressed-swap-residing-in-ram-over-allocating-memory/
# This script is distributed under the GPLv2.
# https://www.gnu.org/licenses/gpl-2.0.html
### END INIT INFO
. /etc/init.d/functions
RETVAL=0
is_running () {
ls /sys/block/zram* >& /dev/null
}
start() {
local FACTOR num_devices memtotal mem modparms
if is_running; then
echo -n "; zram already configured, aborting"
return 1
fi
[ -f /etc/sysconfig/zram ] && . /etc/sysconfig/zram
: ${FACTOR:=50}
num_devices=$(grep -c processor /proc/cpuinfo | sed 's/^0$/1/')
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
modparms+=( "zram_num_devices=${num_devices}" )
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
modparams+=( "num_devices=${num_devices}" )
else
echo -n "; 'modinfo zram' dose not have parm of zram_num_devices or num_devices, aborting"
return 1
fi
modprobe zram "${modparams[@]}"
memtotal=$( free -b | awk '/^Mem:/ {print $2}' )
mem=$(( memtotal / num_devices * FACTOR / 100 ))
for i in $( seq 0 $(( num_devices - 1 )) ); do
grep -q lz4 "/sys/block/zram${i}/comp_algorithm" >&/dev/null \
&& echo lz4>"/sys/block/zram${i}/comp_algorithm"
echo $mem > "/sys/block/zram${i}/disksize"
mkswap "/dev/zram${i}" > /dev/null
swapon -p 5 "/dev/zram${i}"
done
}
stop() {
local i
for i in $(awk '$1 ~ "^/dev/zram[0-9]+$" {print $1}' /proc/swaps); do
swapoff "${i}"
done
if grep -q "^zram " /proc/modules; then
sleep 1
rmmod zram
fi
}
status() {
local i compr orig ratio
is_running || return 0
for i in /sys/block/zram*; do
compr=$(< "${i}/compr_data_size" )
orig=$(< "${i}/orig_data_size" )
ratio=0
if (( 0 < $compr )); then
ratio=$(awk "BEGIN{print ${orig}*100/${compr}; exit0}")
fi
echo -e "/dev/${i##*/}:\t${ratio}% (${orig} -> ${compr})"
done
}
case "$1" in
start)
action "Starting zram" start
;;
stop)
action "Stopping zram" stop
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment