-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzram-config
executable file
·139 lines (117 loc) · 2.74 KB
/
zram-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: enable swap with compressed ram block device backend
# Description: Adapted from script written by tradetaxfree and is found at http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/
# also ported from Ubuntu's upstart version of zram-config
### END INIT INFO
# Author: David Sobon <[email protected]>
DESC="zram-config"
NAME="zram-config"
# percentage of real memory to use for zram.
# note: pre-compressed amount. real memory usage may end up 30-60%
ZRAM_FACTOR=50
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# start
#
do_start() {
NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/')
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
MODPROBE_ARGS="zram_num_devices=${NRDEVICES}"
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
MODPROBE_ARGS="num_devices=${NRDEVICES}"
else
exit 1
fi
modprobe zram $MODPROBE_ARGS
if [ $? -gt 0 ]; then
log_failure_msg "Your Kernel needs to be compiled with ZRAM support."
exit 1
fi
# Calculate memory to use for zram.
totalmem=`free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
mem=$(((totalmem / ${NRDEVICES} * ${ZRAM_FACTOR} / 100) * 1024))
# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i - 1))
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
RETVAL=$?
if [ $RETVAL -gt 0 ]; then
log_failure_msg "/dev/zram# already in use!"
return $RETVAL
fi
mkswap /dev/zram${DEVNUMBER} >/dev/null
swapon -p 5 /dev/zram${DEVNUMBER} >/dev/null
done
return 0
}
#
# stop
#
do_stop() {
if DEVICES=$(grep zram /proc/swaps | awk '{print $1}'); then
for i in $DEVICES; do
swapoff $i >/dev/null
done
fi
rmmod -w zram 1>/dev/null 2>&1
RETVAL=$?
return $RETVAL
}
#
# do_status
#
function do_status () {
I=$(/sbin/swapon -s | grep zram | wc -l)
if [ $I -eq 0 ]; then
echo "$NAME is NOT running."
exit 3
fi
echo "$NAME is running."
exit 0
}
#
#
#
case "$1" in
start)
log_daemon_msg "Starting $DESC"
do_start
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC"
do_stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart|force-reload)
do_stop
sleep 1
do_start
;;
status)
do_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL