forked from rwbaumg/admin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount-tmpfs.sh
executable file
·194 lines (155 loc) · 3.87 KB
/
mount-tmpfs.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
#!/bin/bash
# Mount temporary storage at the specified mountpoint
# Default size
SIZE_MB=100
exit_script()
{
# Default exit code is 1
local exit_code=1
local re
re='^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
if echo "$1" | grep -qE "$re"; then
exit_code=$1
shift
fi
re='[[:alnum:]]'
if echo "$@" | grep -iqE "$re"; then
if [ "$exit_code" -eq 0 ]; then
echo >&2 "INFO: $*"
else
echo "ERROR: $*" 1>&2
fi
fi
# Print 'aborting' string if exit code is not 0
[ "$exit_code" -ne 0 ] && echo >&2 "Aborting script..."
exit "$exit_code"
}
usage()
{
# Prints out usage and exit.
sed -e "s/^ //" -e "s|SCRIPT_NAME|$(basename "$0")|" -e "s|DEFAULT_SIZE|${SIZE_MB}|" << EOF
USAGE
Mount temporary storage to the specified location.
SYNTAX
SCRIPT_NAME [OPTIONS] ARGUMENTS
ARGUMENTS
mountpoint The location to mount temporary storage to.
OPTIONS
-s, --size <mb> The desired size (in megabytes).
Defaults to DEFAULT_SIZEMB.
-r, --random Create and use a random mountpoint folder.
-v, --verbose Make the script more verbose.
-h, --help Prints this usage.
EOF
exit_script "$@"
}
test_arg()
{
# Used to validate user input
local arg="$1"
local argv="$2"
if [ -z "$argv" ]; then
if echo "$arg" | grep -qE '^-'; then
usage "Null argument supplied for option $arg"
fi
fi
if echo "$argv" | grep -qE '^-'; then
usage "Argument for option $arg cannot start with '-'"
fi
}
test_path()
{
# test directory argument
local arg="$1"
test_arg "$arg"
if [ ! -d "$arg" ]; then
usage "Specified directory does not exist."
fi
if find "$arg" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
usage "The directory '$(readlink -m "$arg")' is not empty."
fi
}
test_number_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
re='^[0-9]+$'
if ! [[ "$argv" =~ $re ]]; then
usage "Value is not a valid number: '$argv'."
fi
if ! [ "$argv" -gt 0 ]; then
usage "The specified value is less than the minimum (1)."
fi
}
MOUNTPOINT=""
VERBOSITY=0
# process arguments
#[ $# -gt 0 ] || usage
while [ $# -gt 0 ]; do
case "$1" in
-s|--size)
test_number_arg "$1" "$2"
shift
SIZE_MB=$1
shift
;;
-r|--random)
if [ -n "${MOUNTPOINT}" ]; then
usage "Cannot specify a mount-point while using --random."
fi
MOUNTPOINT="$(mktemp -d --tmpdir tmpfs.$$.XXXXXXXXXX)"
shift
;;
-v|--verbose)
((VERBOSITY++))
shift
;;
-h|--help)
usage
;;
*)
if [ -n "${MOUNTPOINT}" ]; then
usage
fi
test_path "$1"
MOUNTPOINT=$(readlink -m "$1")
shift
;;
esac
done
if [ -z "${MOUNTPOINT}" ]; then
usage "No mountpoint was specified."
fi
# check if superuser
if [[ $EUID -ne 0 ]]; then
echo >&2 "This script must be run as root."
exit 1
fi
FREE_RAM=$(awk '/MemFree/ { printf "%i\n", $2/1024 }' /proc/meminfo)
SAFE_SIZE=$((FREE_RAM-(FREE_RAM/10)))
if [ $VERBOSITY -gt 0 ]; then
echo "Mountpoint : ${MOUNTPOINT}"
echo "RAM size : ${SIZE_MB}m"
echo "Free RAM : ${FREE_RAM}m"
echo "Usable RAM : ${SAFE_SIZE}m"
echo "Verbosity : ${VERBOSITY}"
fi
#if [ ${FREE_RAM} -lt 500 ]; then
# exit_script 1 "Not enough available memory (free memory: ${FREE_RAM}m)."
#fi
if [ "${SIZE_MB}" -gt ${SAFE_SIZE} ]; then
usage "The requested size (${SIZE_MB}m) is too large for the available memory (${SAFE_SIZE}m)."
fi
echo "Mounting ${SIZE_MB}m of RAM to ${MOUNTPOINT} ..."
MOUNT_EXTRA_ARGS=""
if [ $VERBOSITY -gt 0 ]; then
MOUNT_EXTRA_ARGS="-v"
fi
if ! mount ${MOUNT_EXTRA_ARGS} -t tmpfs tmpfs "${MOUNTPOINT}" -o size="${SIZE_MB}m"; then
exit_script 1 "Failed to mount tmpfs storage."
fi
exit_script 0