-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserver.sh
executable file
·322 lines (277 loc) · 9.07 KB
/
reserver.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
# Server space reserver.
# https://github.com/ceiphr/reserver/
# MIT License
# Copyright (c) 2022 Ari Birnbaum.
#
# A *very mild* contingency plan for servers that run out of space.
# Based on this great article by Brian Schrader:
# https://brianschrader.com/archive/why-all-my-servers-have-an-8gb-empty-file/
# Default Configuration
FILE_NAME=".reservation" # The file that will be used to store the reservation.
DIRECTORY=$HOME # The directory where the reservation is stored.
RESERVATION_SIZE=5 # The amount of space, in gigabytes, to reserve.
MAX_UNALLOCATED_SPACE_PERCENT=50 # The maximum percentage of unallocated space that can be reserved.
CONFIG_FILE="$HOME/.reserver.conf" # The configuration file.
LOCK_FILE="/tmp/reserver.lock" # The lock file. Prevent multiple reservations at once.
DEBUG=0 # Set to 1 to enable debug mode.
# Don't edit below this line.
# ----------------------------------------------------------------------------------------------------------------------
VERSION=v1.0.0
# For echo -e color support.
TXT_DEFAULT='\033[0m'
TXT_GREY='\033[2m'
TXT_GREEN='\033[0;32m'
TXT_RED='\033[0;31m'
TXT_BOLD='\033[1m'
TXT_UNBOLD_DIM='\033[0;2m'
# https://no-color.org/
if [[ -n "${NO_COLOR}" ]]; then
TXT_DEFAULT='\033[0m'
TXT_GREY='\033[0m'
TXT_GREEN='\033[0m'
TXT_RED='\033[0m'
fi
# In case the user quits the program in the critical section, we can
# simply unlock the section before exiting.
#
# If they interrupt the script as it's writing to the reservation file,
# the worst that could happen is that the reservation file is only partially
# full. The script will still function correctly.
handle_sigint() {
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit 1
}
trap 'handle_sigint' INT
# Check the default configuration is valid.
if [ $MAX_UNALLOCATED_SPACE_PERCENT -gt 100 ]; then
echo -e "${TXT_RED}MAX_UNALLOCATED_SPACE_PERCENT must be less than 100. Exiting.${TXT_DEFAULT}" >&2
exit 1
fi
# Check if the DEBUG environment variable is set.
if [[ -n "${RESERVER_DEBUG}" ]]; then
DEBUG=1
fi
if [ $DEBUG -eq 1 ]; then
echo -e "Debug mode ${TXT_GREEN}enabled${TXT_DEFAULT}. Reserver will not reserve space."
fi
help() {
echo -e "$(
cat <<-EOF
Usage: $(basename "$0") [options]
${TXT_GREY}
This script will reserve space on a server,
so, you can delete it later.
Specifically, when your server runs out of space, you
can remove this reservation and the server will hopefully
have enough space to function again while you try to fix
whatever caused the issue.
By default, this script will create a ${TXT_BOLD}5GB${TXT_UNBOLD_DIM} reservation file
in your home directory.
${TXT_DEFAULT}
Options:
-r: Remove reservation.
-s: The amount of space, in ${TXT_BOLD}gigabytes${TXT_DEFAULT}, to reserve.
${TXT_GREY}Default is ${TXT_BOLD}$RESERVATION_SIZE${TXT_UNBOLD_DIM}.
Max is ${TXT_BOLD}$MAX_UNALLOCATED_SPACE_PERCENT%${TXT_UNBOLD_DIM} of the system's unallocated space.${TXT_DEFAULT}
-f: The file name to use for the reservation.
${TXT_GREY}Default is '$FILE_NAME'.${TXT_DEFAULT}
-d: The directory to use for the reservation.
${TXT_GREY}Default is the user's home directory.${TXT_DEFAULT}
-h: Print this help message.
EOF
)"
}
# Tiago Lopo: https://stackoverflow.com/a/29436423/9264137
# This is used to ask the user for confirmation.
function yes_or_no {
while true; do
read -r -p "$* [y/N]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*)
echo "Aborted"
return 1
;;
esac
done
}
# Mikkel: https://unix.stackexchange.com/a/206216/378978
# These two functions read and write to the config file.
# This way, if the user sets a custom location and or name
# for the reservation, we can remember it and remove
# it when asked.
read_config() {
typeset -A config
config=(
[file_name]=$FILE_NAME
[directory]=$DIRECTORY
)
while read -r line; do
if echo "$line" | grep -F = &>/dev/null; then
varname=$(echo "$line" | cut -d '=' -f 1)
config[$varname]=$(echo "$line" | cut -d '=' -f 2-)
fi
done <"$CONFIG_FILE"
FILE_NAME=${config[file_name]}
DIRECTORY=${config[directory]}
}
write_config() {
# In case the path is relative, make it absolute.
absolute_dir=$(realpath "$DIRECTORY")
echo "file_name=$FILE_NAME" >"$CONFIG_FILE"
echo "directory=$absolute_dir" >>"$CONFIG_FILE"
}
# Remove the reservation file if it exists.
ret=0
remove() {
# Config file remembers name and location of last reservation.
# Once a reservation is removed, the config file is no longer needed.
if [ -f "$CONFIG_FILE" ]; then
read_config
rm "$CONFIG_FILE"
fi
if [ -f "$DIRECTORY/$FILE_NAME" ]; then
rm "$DIRECTORY/$FILE_NAME"
echo -e "${TXT_GREEN}Reservation removed. Good luck!${TXT_DEFAULT}"
ret=0
else
echo -e "${TXT_RED}No reservation file found. Exiting.${TXT_DEFAULT}" >&2
ret=1
fi
return $ret
}
# Safety checks
BASH_VERSION=5
if ((BASH_VERSINFO[0] < BASH_VERSION)); then
echo -e "${TXT_RED}This script was tested on GNU bash, version ${BASH_VERSION}."
echo -e "You're using an older version (v${BASH_VERSINFO[0]}).${TXT_DEFAULT}"
message="Would you still like to continue?"
yes_or_no "$message" || exit 1
fi
if [ "$EUID" -eq 0 ]; then
echo -e "${TXT_RED}This script isn't supposed to be run as root.${TXT_DEFAULT}"
message="Would you still like to continue?"
yes_or_no "$message" || exit 1
exit
fi
# Parse command line arguments.
configured=0
while getopts ':f:hd:s:rv' OPTION; do
case "$OPTION" in
s)
RESERVATION_SIZE="$OPTARG"
echo -e "Reservation size set to ${TXT_BOLD}${RESERVATION_SIZE}GB${TXT_DEFAULT}."
;;
f)
FILE_NAME="$OPTARG"
configured=1
;;
d)
DIRECTORY="$OPTARG"
configured=1
;;
r)
remove
exit $ret
;;
v)
echo "$VERSION"
exit 0
;;
h)
help
exit 0
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ $configured -eq 1 ] && [ -f "$DIRECTORY/$FILE_NAME" ]; then
echo -e "${TXT_RED}Configured file already exists. Will not overwrite. Exiting.${TXT_DEFAULT}" >&2
exit 1
fi
# Acquire lock.
if [ -f "$LOCK_FILE" ]; then
echo -e "${TXT_RED}A reservation is already in process. Exiting.${TXT_DEFAULT}" >&2
exit 1
else
touch "$LOCK_FILE"
fi
# Load config file if it exists.
if [ -f "$CONFIG_FILE" ]; then
read_config
fi
if [ ! -d "$DIRECTORY" ] || [ ! -w "$DIRECTORY" ] ||
[ "$DIRECTORY/$FILE_NAME" = "$CONFIG_FILE" ] ||
[ "$DIRECTORY/$FILE_NAME" = "$LOCK_FILE" ]; then
echo -e "${TXT_RED}Directory is not usable or does not exist. Exiting.${TXT_DEFAULT}" >&2
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit 1
fi
# If the user configured a file name or directory, remember that,
# so, it's not necessary to ask again when removing the reservation.
if [ $configured -eq 1 ] && [ ! -f "$CONFIG_FILE" ]; then
touch "$CONFIG_FILE"
write_config
fi
# Reservation was already made.
if [ -f "$DIRECTORY/$FILE_NAME" ]; then
message="Reservation file already exists. Delete?"
yes_or_no "$message" && remove
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit $ret
fi
if [ "$RESERVATION_SIZE" -le 0 ]; then
echo -e "${TXT_RED}Reservation size is invalid. Exiting.${TXT_DEFAULT}" >&2
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit 1
fi
echo -e "Reserving ${TXT_BOLD}${RESERVATION_SIZE}GB${TXT_DEFAULT} of space..."
# Get the total unallocated space on the system.
TOTAL_UNALLOCATED_SPACE=$(df -BG / | grep -v "Filesystem" | awk '{print $4}' | tail -n 1)
if [ -z "$TOTAL_UNALLOCATED_SPACE" ]; then
echo -e "${TXT_RED}Unable to determine total unused disk space. Exiting.${TXT_DEFAULT}" >&2
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit 1
fi
# Determine if the reservation is too large.
ALLOWED_UNALLOCATED_SPACE=$((${TOTAL_UNALLOCATED_SPACE::-1} * MAX_UNALLOCATED_SPACE_PERCENT / 100))
if [ "$ALLOWED_UNALLOCATED_SPACE" -lt "$RESERVATION_SIZE" ]; then
echo -e "${TXT_RED}Not enough unused disk space. Exiting.${TXT_DEFAULT}" >&2
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
exit 1
fi
# Create reservation file.
CALCULATED_RESERVATION_SIZE=$((RESERVATION_SIZE * 1024))
if [ $DEBUG -eq 1 ]; then
touch "$DIRECTORY/$FILE_NAME"
elif ! command -v fallocate &>/dev/null; then
# Fallocate is not available. We're on a legacy system. Using dd instead.
dd if=/dev/zero of="$DIRECTORY/$FILE_NAME" bs=1M count=$CALCULATED_RESERVATION_SIZE
else
fallocate -l ${CALCULATED_RESERVATION_SIZE}M "$DIRECTORY/$FILE_NAME"
fi
# Release lock.
if [ -f "$LOCK_FILE" ]; then
rm "$LOCK_FILE"
fi
echo -e "${TXT_GREEN}Reservation complete!${TXT_DEFAULT}"