-
Notifications
You must be signed in to change notification settings - Fork 1
/
zramcfg.sh
executable file
·150 lines (124 loc) · 4.11 KB
/
zramcfg.sh
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
140
141
142
143
144
145
146
147
148
149
150
#!/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
help() {
# This method displays the help
cat <<EOF
zramcfg - configure zram for semplice
USAGE: zramcfg [-r] [-f] [-q] [-h]
Arguments:
-r Removes current configuration
-q No output, except for errors
-h Displays this message
zramcfg reads the configuration from /etc/default/zramcfg, see that file
for details.
EOF
}
error() {
# This method displays an error
echo "E: $@" >&2
exit 1
}
# Parse arguments
while getopts ":hfrq" opt; do
case "$opt" in
h)
help
exit 0
;;
r)
REMOVE="y"
;;
q)
QUIET="y"
;;
\?)
error "Invalid option: -$OPTARG"
;;
esac
done
# Welcome to zramcfg!
[ "$UID" != "0" ] && error "You should be root to use zramcfg."
[ ! -e /etc/default/zramcfg ] && error "Configuration file /etc/default/zramcfg missing!"
# Read configuration file
. /etc/default/zramcfg
# Remove configuration files
[ -e /etc/udev/rules.d/10-zramcfg.rules ] && rm /etc/udev/rules.d/10-zramcfg.rules
[ -e /etc/modprobe.d/zramcfg.conf ] && rm /etc/modprobe.d/zramcfg.conf
[ -e /etc/modules-load.d/zramcfg.conf ] && rm /etc/modules-load.d/zramcfg.conf
#[ -e /etc/fstab.d/10-zramcfg ] && rm /etc/fstab.d/10-zramcfg
sed -i '/# added by zramcfg/d' /etc/fstab
if [ -n "$REMOVE" ]; then
[ -z "$QUIET" ] && echo "Successfully removed configuration."
exit 0
fi
# 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 ))
[ -z "$QUIET" ] && echo "Configuring $NUM_DEVICES devices of $SIZE MB size each."
SIZE=$((${SIZE}*1024*1024))
# Create the modprobe configuration
cat > /etc/modprobe.d/zramcfg.conf <<EOF
# modprobe configuration file generated by zramcfg.
# Configured to use $NUM_DEVICES devices of $SIZE MB size.
# Use zramcfg -r to remove configuration and disable zram.
options zram num_devices=$NUM_DEVICES
EOF
# Create the modules-load configuration
cat > /etc/modules-load.d/zramcfg.conf <<EOF
# modules-load configuration file generated by zramcfg.
# Configured to use $NUM_DEVICES devices of $SIZE MB size.
# Use zramcfg -r to remove configuration and disable zram.
zram
EOF
# Now it's time to generate the udev and fstab rules...
cat > /etc/udev/rules.d/10-zramcfg.rules <<EOF
# udev rules file generated by zramcfg.
# Configured to use $NUM_DEVICES devices of $SIZE MB size each.
# Use zramcfg -r to remove configuration and disable zram.
EOF
#cat > /etc/fstab.d/10-zramcfg <<EOF
## fstab rules file generated by zramcfg.
## Configured to use $NUM_DEVICES devices of $SIZE MB size.
## Use zramcfg -r to remove configuration and disable zram.
#EOF
for num in $(seq 0 $(($NUM_DEVICES - 1))); do
# Udev
cat >> /etc/udev/rules.d/10-zramcfg.rules <<EOF
KERNEL=="zram${num}", SUBSYSTEM=="block", DRIVER=="", ACTION=="add", ATTR{disksize}=="0", ATTR{disksize}="${SIZE}", RUN+="/sbin/mkswap \$env{DEVNAME}"
EOF
# fstab
cat >> /etc/fstab <<EOF
/dev/zram${num} swap swap pri=32767 0 0 # added by zramcfg
EOF
done
[ -z "$QUIET" ] && echo "Successfully configured. Reboot the system to apply the changes."
exit 0