ansible-role-zram/files/usr/local/sbin/zram-swap.sh
2018-10-08 08:36:03 +02:00

77 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
#
# zram-config Start zram-config
#
# chkconfig: 2345 08 92
# description: Starts, stops and saves zram-config
#
#
### BEGIN INIT INFO
# Provides: zram-config
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop zram-config
# Description: Start, stop and save zram-config
### END INIT INFO
# http://askubuntu.com/questions/130374/ramdisk-compressed-writeable-no-swap
# https://kernel.org/doc/Documentation/blockdev/zram.txt
#NRDEVICES=$[$(grep ^physical\ id /proc/cpuinfo | sort -u | wc -l)*$(grep ^core\ id /proc/cpuinfo | sort -u | wc -l)]
#if [ $NRDEVICES -eq 0 ]; then
# NRDEVICES=$(grep -c '^processor\s*:' /proc/cpuinfo)
#fi
#NRDEVICES=1
case "$1" in
start)
if [ $(grep -c /dev/zram /proc/swaps) -gt 1 ]; then
echo "Already running" >&2
exit 2
fi
if [ $(lsmod | grep -c zram) -eq 0 ]; then
modprobe zram
ZRAMID=0
else
ZRAMID=$(cat /sys/class/zram-control/hot_add)
fi
MEM=$(free -k | grep -e "^Mem:" | awk '{printf("%d\n",$2/1.2)}')
# echo 1 > /sys/block/zram${ZRAMID}/reset
if [ -e /sys/block/zram${ZRAMID}/comp_algorithm ]; then
if [ $(grep -c lz4 /sys/block/zram${ZRAMID}/comp_algorithm) -eq 1 ]; then
echo lz4 > /sys/block/zram${ZRAMID}/comp_algorithm
fi
fi
if [ -e /sys/block/zram${ZRAMID}/mem_limit ]; then
echo $[${MEM}*2]k > /sys/block/zram${ZRAMID}/disksize
echo ${MEM}k > /sys/block/zram${ZRAMID}/mem_limit
else
echo ${MEM}k > /sys/block/zram${ZRAMID}/disksize
fi
mkswap /dev/zram${ZRAMID}
swapon -p 10 /dev/zram${ZRAMID}
;;
stop)
# Suppression zram
# echo X > /sys/class/zram-control/hot_remove ou X=0 pour zram0
for ZRAMID in $(grep /dev/zram /proc/swaps | awk '{print $1}' | cut -c10-); do
swapoff /dev/zram${ZRAMID}
echo 1 > /sys/block/zram${ZRAMID}/reset
echo ${ZRAMID} > /sys/class/zram-control/hot_remove
done
modprobe -r zram
;;
esac
#mke2fs -q -m 0 -b 4096 -O sparse_super -L zram /dev/zram0
#mount -o relatime,noexec,nosuid /dev/zram0 /mnt/zram
#chmod 1777 /mnt/zram/
# cat /sys/block/zram0/mem_used_total
# https://www.google.fr/search?q=sysem+to+ram+pivot_root&ie=utf-8&oe=utf-8&rls=org.mozilla:fr:unofficial&client=seamonkey-a&gws_rd=cr&ei=ThHLVs3WA4H9acbUtNAH#q=system+to+ram+pivot_root
#http://dreamlayers.blogspot.fr/2012/10/running-linux-from-ram.html
#http://unix.stackexchange.com/questions/126217/when-would-you-use-pivot-root-over-switch-root
#https://wiki.gentoo.org/wiki/Zram