This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dphys-swapfile
executable file
·221 lines (153 loc) · 6.65 KB
/
dphys-swapfile
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#! /bin/sh
# /sbin/dphys-swapfile - automatically set up an swapfile
# author Neil Franklin, last modification 2010.05.05
# This script is copyright ETH Zuerich Physics Departement,
# use under either BSD or GPL license
# this script is intended to be run as root user, usually while booting
### ------ configuration for this site
# --- CONF_* various site dependant user config variables
# where we want the swapfile to be, this is the default
CONF_SWAPFILE=/var/swap
# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=
# set size to computed value, this times RAM size, dynamically adapts,
# guarantees that there is enough swap without wasting disk space on excess
CONF_SWAPFACTOR=2
# restrict size (computed and absolute!) to maximally this limit
# can be set to empty for no limit, but beware of filled partitions!
# this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
# but is also sensible on 64bit to prevent filling /var or even / partition
CONF_MAXSWAP=2048
### ------ actual implementation from here on
# no user settings any more below this point
set -e
# sanitise this place, else some commands may fail
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
# what we are
NAME=dphys-swapfile
PNAME=dphys-swapfile
# check user config file, let user override settings
# swap file place/filename and size
if [ -f /etc/"${PNAME}" ] ; then
. /etc/"${PNAME}"
fi
case "$1" in
setup)
# (re-)size/-generate, fast if no memory size change
if [ "${CONF_SWAPSIZE}" = "" ] ; then
# no absolute size given, so automatically compute optimal size
echo -n "computing size, "
# this seems to be the nearest to physical RAM size, lacks about 60k
# but it actually then fails from AMD64 kernel 2.6.32 onwards
#KCORESIZE="`ls -l /proc/kcore | awk '{ print $5 }'`"
## make MBytes which rounded down will be exactly 1 too few, so add 1
#MEMSIZE="`echo "${KCORESIZE} 1048576 / 1 + p q" | dc`"
# so second attempt at finding out physical RAM size, lacks about 10M
# see how long this variant stays usable :-)
MEMTOTAL="`grep '^MemTotal:' /proc/meminfo | awk '{ print $2 }'`"
# make MBytes which rounded down will be about 10 too few, so add 10
MEMSIZE="`echo "${MEMTOTAL} 1024 / 10 + p q" | dc`"
# compute desired swap size, as factor * RAM
CONF_SWAPSIZE="`echo "${MEMSIZE} ${CONF_SWAPFACTOR} * p q" | dc`"
# remove any fractional MBytes
CONF_SWAPSIZE="`echo "${CONF_SWAPSIZE}" | cut -f 1 -d '.'`"
fi
# announce end resulting config
echo -n "want ${CONF_SWAPFILE}=${CONF_SWAPSIZE}MByte"
if [ "${CONF_MAXSWAP}" != "" ] ; then
# check for swap size limit and restrict to it
if [ "${CONF_SWAPSIZE}" -gt "${CONF_MAXSWAP}" ] ; then
echo -n ", restricting to config limit: ${CONF_MAXSWAP}MBytes"
CONF_SWAPSIZE="${CONF_MAXSWAP}"
fi
fi
# we will be later starting, and in between possible deleting/rebuilding
# so deactivate any already running swapfile, to avoid errors
"$0" swapoff
# compare existing swapfile (if one exists) to see if it needs replacing
if [ -f "${CONF_SWAPFILE}" ] ; then
echo -n ", checking existing"
# we need bytes for comparing with existing swap file
SWAPBYTES="`echo "${CONF_SWAPSIZE} 1048576 * p q" | dc`"
FILEBYTES="`ls -l "${CONF_SWAPFILE}" | awk '{ print $5 }'`"
# wrong size, get rid of existing swapfile, after remake
if [ "${FILEBYTES}" != "${SWAPBYTES}" ] ; then
echo -n ": deleting wrong size file (${FILEBYTES})"
# deactivate and delete existing file, before remaking for new size
"$0" uninstall
else
echo -n ": keeping it"
fi
fi
# if no swapfile (or possibly old one got deleted) make one
if [ ! -f "${CONF_SWAPFILE}" ] ; then
echo -n ", generating swapfile ..."
# first deleting existing mount lines, if any there (same code as above)
grep -v "^${CONF_SWAPFILE}" /etc/fstab > /etc/.fstab
mv /etc/.fstab /etc/fstab
dd if=/dev/zero of="${CONF_SWAPFILE}" bs=1048576 \
count="${CONF_SWAPSIZE}" 2> /dev/null
mkswap "${CONF_SWAPFILE}" > /dev/null
# ensure that only root can read possibly critical stuff going in here
chmod 600 "${CONF_SWAPFILE}"
# do not mount swapfile via fstab, because swapfile may only
# be created after partitions are all mounted, not here yet
# so just add warning comment line that swapfile is not in fstab
# and because of this will get mounted by this script
# get rid of possibly already existing comment about
# swapfile mounted by this script, to avoid duplicate comments
grep -v "a swapfile is not" /etc/fstab > /etc/.fstab
grep -v "${NAME}" /etc/.fstab > /etc/fstab
# add new comment about this
echo "# a swapfile is not a swap partition, no line here" >> /etc/fstab
echo "# use ${NAME} swap[on|off] for that" >> /etc/fstab
# and inform the user what we did
echo -n " of ${CONF_SWAPSIZE}MBytes"
fi
echo
;;
install)
# synonym for setup, in case someone types this
"$0" setup
;;
swapon)
# as there can be no swapon in /etc/fstab, do it from here
# this is due to no possible insertion of code (at least in Debian)
# between mounting of /var (where swap file most likely resides)
# and executing swapon, where the file already needs to be existing
if [ -f "${CONF_SWAPFILE}" ] ; then
swapon "${CONF_SWAPFILE}" 2>&1 > /dev/null
else
echo "$0: ERROR: swap file ${CONF_SWAPFILE} missing!" \
"you need to first run $0 setup to generate one"
fi
;;
swapoff)
# as there can also be no swapoff in /etc/fstab, do it from here
# first test if swap is even active, else error from swapoff
if [ "`swapon -s | grep "${CONF_SWAPFILE}" | \
cut -f 1 -d ' '`" != "" ] ; then
swapoff "${CONF_SWAPFILE}" 2>&1 > /dev/null
fi
;;
uninstall)
# note: there is no install), as setup) can run from any blank system
# it auto-installs as side effect of recomputing and checking size
# deactivate before deleting
"$0" swapoff
if [ -f "${CONF_SWAPFILE}" ] ; then
# reclaim the file space
rm "${CONF_SWAPFILE}"
fi
# and get rid of now superfluous comment about swapfile mounting
grep -v "a swapfile is not" /etc/fstab > /etc/.fstab
grep -v "${NAME}" /etc/.fstab > /etc/fstab
;;
*)
echo "Usage: $0 {setup|swapon|swapoff|uninstall}"
exit 1
;;
esac
exit 0