Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/sh
- #
- # zramswap - Script to setup zramswap.
- # based on atftpd and zramswap-enabler.
- #
- ### BEGIN INIT INFO
- # Provides: atftpd
- # Required-Start: $syslog $network $remote_fs
- # Required-Stop: $syslog $network $remote_fs
- # Should-Start: $local_fs
- # Should-Stop: $local_fs
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: Setup zramswap
- # Description: Setup zramswap, to compress memory.
- ### END INIT INFO
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- NAME=zramswap
- DESC="zram swapping"
- OPTIONS=""
- set -e
- # get the number of CPUs
- num_cpus=$(grep -c processor /proc/cpuinfo)
- # if something goes wrong, assume we have 1
- [ "$num_cpus" != 0 ] || num_cpus=1
- # set decremented number of CPUs
- decr_num_cpus=$((num_cpus - 1))
- # get the amount of memory in the machine
- mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
- mem_total=$((mem_total_kb * 1024))
- case "$1" in
- start)
- echo -n "Starting $DESC: "
- # load dependency modules
- modprobe zram zram_num_devices=$num_cpus
- # initialize the devices
- for i in $(seq 0 $decr_num_cpus); do
- echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
- done
- # Creating swap filesystems
- for i in $(seq 0 $decr_num_cpus); do
- mkswap /dev/zram$i
- done
- # Switch the swaps on
- for i in $(seq 0 $decr_num_cpus); do
- swapon -p 100 /dev/zram$i
- done
- echo "$NAME."
- ;;
- stop)
- echo -n "Stopping $DESC: "
- # Switching off swap
- for i in $(seq 0 $decr_num_cpus); do
- if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
- swapoff /dev/zram$i
- fi
- done
- rmmod zram
- echo "$NAME."
- ;;
- restart|reload|force-reload)
- /etc/init.d/zramswap stop
- sleep 1
- /etc/init.d/zramswap start
- ;;
- *)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment