-
Notifications
You must be signed in to change notification settings - Fork 45
/
nfs_mount.sh
executable file
·294 lines (230 loc) · 8.06 KB
/
nfs_mount.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
#!/usr/bin/env bash
# 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/>.
# Copyright 2022 Oliver "RealLarry" Jaksch
# You can download the latest version of this script from:
# https://github.com/MiSTer-devel/Scripts_MiSTer
# Version 1.1.1 - 2024-02-17 - Update Github link
# Version 1.1 - 2022-12-25 - Cosmetics
# Version 1.0 - 2021-12-29 - First commit
#========= USER OPTIONS =========
# You can edit these user options or make an ini file with the same
# name as the script, i.e. nfs_mount.ini, containing the same options.
# Your NFS Server, i.e. your NAS name or it's IP address.
SERVER=""
# The path to mount from your NFS server, for example "/storage/games"
SERVER_PATH=""
# The number of seconds to wait before considering the server unreachable
SERVER_TIMEOUT="60"
# Wake up the server from above by using WOL (Wake On LAN)
WOL="no"
MAC="FFFFFFFFFFFF"
SERVER_MAC="00:11:22:33:44:55"
# Optional additional mount options.
MOUNT_OPTIONS="noatime"
# "yes" for automounting NFS shares at boot time;
# it will create start/kill scripts in /etc/network/if-up.d and /etc/network/if-down.d.
MOUNT_AT_BOOT="yes"
#=========NO USER-SERVICEABLE PARTS BELOW THIS LINE=====
#=========FUNCTION LIBRARY==============================
# Are we running as root?
function as_root() {
if [ "$(id -u)" != "0" ]; then
/usr/bin/logger "This script must be run as root. Please run again with sudo or as root user. Exiting."
exit 1
fi
}
# Check for existing NFS mounts
function no_existing_nfs() {
if mount | grep -q nfs; then
/usr/bin/logger "Found mounted NFS filesystems. Aborting."
exit 1
fi
}
# Load script configuration from an INI file.
# ..which isn't really an INI file but just a list of Bash vars
function load_ini() {
local SCRIPT_PATH="$(realpath "$0")"
local INI_FILE=${SCRIPT_PATH%.*}.ini
if [ -e "$INI_FILE" ]; then
eval "$(cat $INI_FILE | tr -d '\r')"
fi
}
# Check if we have an IPv4 address on any of the interfaces that is
# not a local loopback (127.0.0.0/8) or link-local (169.254.0.0/16) adddress.
function has_ip_address() {
local start_time=$(date +%s)
while [[ $(($(date +%s) - $start_time)) -lt 30 ]]; do
ip addr show | grep 'inet ' | grep -vE '127.|169.254.' >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
return 0
fi
sleep 1
done
/usr/bin/logger "Failed to obtain an IP address within the time limit. Exiting."
exit 1
}
# Check if the script's configuration is minimally viable
function viable_config() {
if [ -z "$SERVER" ] || [ -z "$SERVER_PATH" ]; then
/usr/bin/logger "SERVER and SERVER_PATH must be set. Exiting."
exit 1
fi
}
# Wake-up the NFS server using WOL
function wake_up_nfs() {
if [ "${WOL}" == "yes" ]; then
for REP in {1..16}; do
MAC+=$(echo ${SERVER_MAC})
done
echo -n "${MAC}" | xxd -r -u -p | socat - UDP-DATAGRAM:255.255.255.255:9,broadcast
fi
}
# Wait for the NFS server to be up
function wait_for_nfs() {
local PORTS=(2049 111)
local START=$(date +%s)
local ELAPSED=0
while [ $ELAPSED -lt $SERVER_TIMEOUT ]; do
for PORT in "${PORTS[@]}"; do
if nc -z "$SERVER" "$PORT" >/dev/null 2>&1; then
/usr/bin/logger "NFS server $SERVER is up."
return 0
fi
done
sleep 1
ELAPSED=$(($(date +%s) - $START))
done
/usr/bin/logger "Timeout while waiting for NFS server $SERVER."
exit 1
}
# Install the mount-at-boot scripts
function install_mount_at_boot() {
# Enable strict error checking
set -euo pipefail
# Check if MOUNT_AT_BOOT is set to "yes"
if [ "${MOUNT_AT_BOOT:-}" != "yes" ]; then
return 0
fi
# Remount root filesystem read-write if it's currently read-only
if mount | grep -q "on / .*[(,]ro[,$]"; then
mount -o remount,rw /
readonly ROOTFS_REMOUNTED="yes"
fi
# Set up scripts to run on network interface up/down events
SCRIPT_PATH="$(realpath "$0")"
SCRIPT_NAME="$(basename "${SCRIPT_PATH%.*}")"
NET_UP_SCRIPT="/etc/network/if-up.d/${SCRIPT_NAME}"
NET_DOWN_SCRIPT="/etc/network/if-down.d/${SCRIPT_NAME}"
# Ensure directories for scripts exist and have appropriate permissions
mkdir -p /etc/network/if-up.d /etc/network/if-down.d
chmod 755 /etc/network /etc/network/if-up.d /etc/network/if-down.d
# Create network interface up script
cat >"${NET_UP_SCRIPT}" <<EOF
#!/bin/bash
${SCRIPT_PATH} &
EOF
chmod 755 "${NET_UP_SCRIPT}"
# Create network interface down script
cat >"${NET_DOWN_SCRIPT}" <<EOF
#!/bin/bash
umount -a -t nfs4
EOF
chmod 755 "${NET_DOWN_SCRIPT}"
# Remount root filesystem read-only if it was remounted earlier
if [ "${ROOTFS_REMOUNTED:-}" == "yes" ]; then
mount -o remount,ro /
fi
return 0
}
# Remove the mount-at-boot script
function remove_mount_at_boot() {
RO_ROOT="no"
if [ "${MOUNT_AT_BOOT}" != "yes" ]; then
local ORIGINAL_SCRIPT_PATH="$0"
local NET_UP_SCRIPT="/etc/network/if-up.d/$(basename ${ORIGINAL_SCRIPT_PATH%.*})"
local NET_DOWN_SCRIPT="/etc/network/if-down.d/$(basename ${ORIGINAL_SCRIPT_PATH%.*})"
# We need to write to the root filesystem so remount it
# read-write if it's currently read-only.
if mount | grep -q "on / .*[(,]ro[,$]"; then
RO_ROOT="yes"
mount / -o remount,rw
fi
if [ -f "${NET_UP_SCRIPT}" ]; then
rm "${NET_UP_SCRIPT}"
fi
if [ -f "${NET_DOWN_SCRIPT}" ]; then
rm "${NET_DOWN_SCRIPT}"
fi
sync
# If we remounted the rootfs because it was read-only, we now
# undo our remount action and revert the mount to how we found it.
if [ "${RO_ROOT}" == "yes" ]; then
mount / -o remount,ro
fi
return 0
fi
}
# Perform the actual mount operation supporting only NFSv4 for now
function mount_nfs() {
set -e
local ORIGINAL_SCRIPT_PATH="$0"
local SCRIPT_NAME="${ORIGINAL_SCRIPT_PATH##*/}"
SCRIPT_NAME="${SCRIPT_NAME%.*}"
if ! mkdir -p "/tmp/${SCRIPT_NAME}" >/dev/null 2>&1; then
/usr/bin/logger "Error: failed to create directory /tmp/${SCRIPT_NAME}"
exit 1
fi
if ! /usr/bin/busybox mount -t nfs4 "${SERVER}:${SERVER_PATH}" "/tmp/${SCRIPT_NAME}" -o "${MOUNT_OPTIONS}"; then
/usr/bin/logger "Error: failed to mount NFS share ${SERVER}:${SERVER_PATH} to /tmp/${SCRIPT_NAME}"
exit 1
fi
find "/tmp/${SCRIPT_NAME}" -mindepth 1 -maxdepth 1 -type d | while read -r LDIR; do
LDIR="${LDIR##*/}"
if [ -d "/media/fat/${LDIR}" ] && ! mount | grep -qx "/media/fat/${LDIR}"; then
if ! mount -o bind "/tmp/${SCRIPT_NAME}/${LDIR}" "/media/fat/${LDIR}"; then
/usr/bin/logger "Error: failed to mount directory /tmp/${SCRIPT_NAME}/${LDIR} to /media/fat/${LDIR}"
exit 1
fi
fi
done
}
#=========BUSINESS LOGIC================================
#
# This part just calls the functions we define above
# in a sequence. To keep things excruciatingly easy
# to follow, any and all config checks are done *inside*
# the functions themselves.
#
# Each of these steps will exit if things are not OK.
#
#=======================================================
# Ensure we are the root user
as_root
# Load configuration from the .ini file if we have one.
load_ini
# Only cause changes if the configuration is viable..
viable_config
# ..and our network seems to be working.
has_ip_address
# We wake up the NFS-server if needed
wake_up_nfs
# ..and give it time to actually get dressed.
wait_for_nfs
# Install/update the scripts to run at every reboot
install_mount_at_boot
# ..or remove them if that's what the user wants.
remove_mount_at_boot
# Only ensure we haven't already got NFS filesystems present
no_existing_nfs
# Finally, we mount the NFS filesystem and be done with it.
mount_nfs
exit 0