-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzramcfg-fallback
86 lines (72 loc) · 2.03 KB
/
zramcfg-fallback
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
#!/bin/bash
#
# zramcfg - configure zram for semplice
# Copyright (C) 2013 Eugenio "g7" Paolantonio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -e
error() {
# This method displays an error
echo "E: $@" >&2
exit 1
}
# Parse arguments
while getopts "s" opt; do
case "$opt" in
s)
STOP="y"
;;
\?)
error "Invalid option: -$OPTARG"
;;
esac
done
[ ! -e /etc/default/zramcfg ] && error "Configuration file /etc/default/zramcfg missing!"
# Read configuration file
. /etc/default/zramcfg
# Parse NUM_DEVICES
if [ "$NUM_DEVICES" == "cpu" ]; then
# Should look at the cpu to get the number of cores...
NUM_DEVICES="`grep -c processor /proc/cpuinfo`"
[ "$NUM_DEVICES" == "0" ] && NUM_DEVICES="1"
fi
# Parse SIZE
if [[ "$SIZE" == *"%"* ]]; then
# It's a percentage
__memory="`grep MemTotal /proc/meminfo | awk '{ print $2 }'`"
# 8000 : 100 = x : 25
SIZE=$(($__memory * ${SIZE//"%"/} / 100 / 1024))
fi
# Now divide it equally...
SIZE=$(($SIZE / $NUM_DEVICES ))
# Modprobe
[ -z "$STOP" ] && modprobe zram num_devices=$NUM_DEVICES
for num in $(seq 0 $(($NUM_DEVICES - 1))); do
if [ -z "$STOP" ]; then
# Set disksize
echo $((${SIZE}*1024*1024)) > /sys/block/zram${num}/disksize
# Make swap
/sbin/mkswap /dev/zram${num}
# Mount
/sbin/swapon /dev/zram${num} -p 32767
else
# Shutdown swap
/sbin/swapoff /dev/zram${num}
# Reset
echo 1 > /sys/block/zram${num}/reset
fi
done
# Modprobe -r if stop
[ -n "$STOP" ] && modprobe -r zram