|
#!/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 |