SHOW:
|
|
- or go back to the newest paste.
1 | - | #!/bin/sh |
1 | + | #!/bin/bash |
2 | ### BEGIN INIT INFO | |
3 | # Provides: zram | |
4 | # Required-Start: | |
5 | # Required-Stop: | |
6 | # Default-Start: 2 3 4 5 | |
7 | # Default-Stop: 0 1 6 | |
8 | # Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM) | |
9 | # Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram | |
10 | # Included as part of antix-goodies package by anticapitalista <antiX@operamail.com> | |
11 | # This script was written by tradetaxfree and is found at http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/ | |
12 | # Copy this script (as root) from /usr/local/bin to /etc/init.d and then #update-rc.d zram defaults | |
13 | # After booting verify the module is loaded with: lsmod | grep zram | |
14 | ### END INIT INFO | |
15 | ||
16 | start() { | |
17 | # get the number of CPUs | |
18 | num_cpus=$(grep -c processor /proc/cpuinfo) | |
19 | # if something goes wrong, assume we have 1 | |
20 | [ "$num_cpus" != 0 ] || num_cpus=1 | |
21 | ||
22 | # set decremented number of CPUs | |
23 | last_cpu=$((num_cpus - 1)) | |
24 | ||
25 | #default Factor % = 90 change this value here or create /etc/default/zram | |
26 | FACTOR=90 | |
27 | #& put the above single line in /etc/default/zram with the value you want | |
28 | [ -f /etc/default/zram ] && source /etc/default/zram || true | |
29 | factor=$FACTOR # percentage | |
30 | ||
31 | # get the amount of memory in the machine | |
32 | memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') | |
33 | mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024)) | |
34 | ||
35 | # load dependency modules | |
36 | # kernels 3.4 onwards | |
37 | ||
38 | modprobe zram num_devices=$num_cpus | |
39 | ||
40 | if [ $? -gt 0 ]; then | |
41 | # kernels 3.1 - 3.3 | |
42 | #modprobe zram zram_num_devices=$num_cpus | |
43 | #else | |
44 | echo -e "Your Kernel needs to be compiled with ZRAM support:" \ | |
45 | "\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \ | |
46 | "\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)" \ | |
47 | "\n\nThe Liquorix Kernel (http://liquorix.net) has ZRAM support built in." | |
48 | exit 1 | |
49 | fi | |
50 | echo "zram devices probed successfully" | |
51 | ||
52 | # initialize the devices | |
53 | for i in $(seq 0 $last_cpu); do | |
54 | echo $mem_by_cpu > /sys/block/zram$i/disksize | |
55 | # Creating swap filesystems | |
56 | mkswap /dev/zram$i | |
57 | # Switch the swaps on | |
58 | swapon -p 100 /dev/zram$i | |
59 | done | |
60 | } | |
61 | ||
62 | stop() { | |
63 | # get the number of CPUs | |
64 | num_cpus=$(grep -c processor /proc/cpuinfo) | |
65 | ||
66 | # set decremented number of CPUs | |
67 | last_cpu=$((num_cpus - 1)) | |
68 | ||
69 | # Switching off swap | |
70 | for i in $(seq 0 $last_cpu); do | |
71 | if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then | |
72 | swapoff /dev/zram$i | |
73 | sleep 1 | |
74 | fi | |
75 | done | |
76 | ||
77 | sleep 1 | |
78 | rmmod zram | |
79 | } | |
80 | ||
81 | case "$1" in | |
82 | start) | |
83 | start | |
84 | ;; | |
85 | stop) | |
86 | stop | |
87 | ;; | |
88 | restart) | |
89 | stop | |
90 | sleep 3 | |
91 | start | |
92 | ;; | |
93 | *) | |
94 | echo "Usage: $0 {start|stop|restart}" | |
95 | RETVAL=1 | |
96 | esac | |
97 | exit $RETVAL |