Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
- ### END INIT INFO
- start() {
- # 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
- last_cpu=$((num_cpus - 1))
- FACTOR=40
- #& put the above single line in /etc/default/zram with the value you want
- [ -f /etc/default/zram ] && source /etc/default/zram || true
- factor=$FACTOR # percentage
- # get the amount of memory in the machine
- memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
- mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
- # load dependency modules
- echo $num_cpus
- if ! modprobe zram zram_num_devices=$num_cpus
- then
- echo -e "Your Kernel needs to be compiled with ZRAM support:" \
- "\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \
- "\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)"
- exit 1
- fi
- echo "zram devices probed successfully"
- # initialize the devices
- for i in $(seq 0 $last_cpu); do
- echo $mem_by_cpu > /sys/block/zram$i/disksize
- # Creating swap filesystems
- mkswap /dev/zram$i
- # Switch the swaps on
- swapon -p 100 /dev/zram$i
- done
- }
- stop() {
- # get the number of CPUs
- num_cpus=$(grep -c processor /proc/cpuinfo)
- # set decremented number of CPUs
- last_cpu=$((num_cpus - 1))
- # Switching off swap
- for i in $(seq 0 $last_cpu); do
- if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
- swapoff /dev/zram$i
- sleep 1
- fi
- done
- sleep 1
- rmmod zram
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- stop
- sleep 3
- start
- ;;
- *)
- echo "Usage: $0 {start|stop|restart}"
- RETVAL=1
- esac
- exit $RETVAL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement