Wait for IP, log messages to Syslog instead of echoing to console.

This commit is contained in:
Bas v.d. Wiel 2023-03-12 15:24:46 +01:00
parent 38bf4a4313
commit 5952f9ebb7
1 changed files with 18 additions and 15 deletions

View File

@ -56,7 +56,7 @@ MOUNT_AT_BOOT="yes"
function as_root() {
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root. Please run again with sudo or as root user. Exiting."
/usr/bin/logger "This script must be run as root. Please run again with sudo or as root user. Exiting."
exit 1
fi
}
@ -65,7 +65,7 @@ function as_root() {
function no_existing_nfs() {
if mount | grep -q nfs; then
echo "Found mounted NFS filesystems. Aborting."
/usr/bin/logger "Found mounted NFS filesystems. Aborting."
exit 1
fi
}
@ -84,20 +84,23 @@ function load_ini() {
# not a local loopback (127.0.0.0/8) or link-local (169.254.0.0/16) adddress.
function has_ip_address() {
ip addr show | grep 'inet ' | grep -vE '127.|169.254.' >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
return 0
else
echo "Network connectivity is probably not OK. Exiting."
exit 1
fi
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
echo "SERVER and SERVER_PATH must be set. Exiting."
/usr/bin/logger "SERVER and SERVER_PATH must be set. Exiting."
exit 1
fi
}
@ -123,7 +126,7 @@ function wait_for_nfs() {
while [ $ELAPSED -lt $SERVER_TIMEOUT ]; do
for PORT in "${PORTS[@]}"; do
if nc -z "$SERVER" "$PORT" >/dev/null 2>&1; then
echo "NFS server $SERVER is up."
/usr/bin/logger "NFS server $SERVER is up."
return 0
fi
done
@ -132,7 +135,7 @@ function wait_for_nfs() {
ELAPSED=$(($(date +%s) - $START))
done
echo "Timeout while waiting for NFS server $SERVER."
/usr/bin/logger "Timeout while waiting for NFS server $SERVER."
exit 1
}
@ -228,18 +231,18 @@ function mount_nfs() {
local SCRIPT_NAME="${ORIGINAL_SCRIPT_PATH##*/}"
SCRIPT_NAME="${SCRIPT_NAME%.*}"
if ! mkdir -p "/tmp/${SCRIPT_NAME}" >/dev/null 2>&1; then
echo "Error: failed to create directory /tmp/${SCRIPT_NAME}"
/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
echo "Error: failed to mount NFS share ${SERVER}:${SERVER_PATH} to /tmp/${SCRIPT_NAME}"
/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 -q "/media/fat/${LDIR}"; then
if ! mount -o bind "/tmp/${SCRIPT_NAME}/${LDIR}" "/media/fat/${LDIR}"; then
echo "Error: failed to mount directory /tmp/${SCRIPT_NAME}/${LDIR} to /media/fat/${LDIR}"
/usr/bin/logger "Error: failed to mount directory /tmp/${SCRIPT_NAME}/${LDIR} to /media/fat/${LDIR}"
exit 1
fi
fi