Update 00-smb_mounts.sh

This commit is contained in:
Alexandre
2026-02-14 13:34:40 +01:00
committed by GitHub
parent 8a7d70d9c4
commit 005bd9e17c

View File

@@ -27,12 +27,10 @@ test_mount() {
ERROR_MOUNT=false ERROR_MOUNT=false
mountpoint="/mnt/$diskname" mountpoint="/mnt/$diskname"
# Not mounted → not an error for this function
if ! mountpoint -q "$mountpoint"; then if ! mountpoint -q "$mountpoint"; then
return 0 return 0
fi fi
# ---- Write test function ----
_test_write() { _test_write() {
local testfile="$mountpoint/.writetest_$$" local testfile="$mountpoint/.writetest_$$"
if : >"$testfile" 2>/dev/null; then if : >"$testfile" 2>/dev/null; then
@@ -44,13 +42,11 @@ test_mount() {
fi fi
} }
# First write test
if _test_write 2>/dev/null; then if _test_write 2>/dev/null; then
MOUNTED=true MOUNTED=true
return 0 return 0
fi fi
# ---- CIFS fallback: retry with noserverino ----
if [[ "$FSTYPE" == "cifs" && "$MOUNTOPTIONS" != *"noserverino"* ]]; then if [[ "$FSTYPE" == "cifs" && "$MOUNTOPTIONS" != *"noserverino"* ]]; then
echo "... retrying mount with noserverino" echo "... retrying mount with noserverino"
MOUNTOPTIONS="${MOUNTOPTIONS},noserverino" MOUNTOPTIONS="${MOUNTOPTIONS},noserverino"
@@ -64,7 +60,6 @@ test_mount() {
fi fi
fi fi
# ---- CIFS fallback: retry with noperm (common Samba permission quirk) ----
if [[ "$FSTYPE" == "cifs" && "$MOUNTOPTIONS" != *"noperm"* ]]; then if [[ "$FSTYPE" == "cifs" && "$MOUNTOPTIONS" != *"noperm"* ]]; then
echo "... retrying mount with noperm" echo "... retrying mount with noperm"
MOUNTOPTIONS="${MOUNTOPTIONS},noperm" MOUNTOPTIONS="${MOUNTOPTIONS},noperm"
@@ -78,7 +73,6 @@ test_mount() {
fi fi
fi fi
# ---- Final: mounted but not writable ----
MOUNTED="readonly" MOUNTED="readonly"
return 0 return 0
} }
@@ -102,6 +96,7 @@ mount_drive() {
retry_cifs_with_vers_ladder_on_dialect_failure() { retry_cifs_with_vers_ladder_on_dialect_failure() {
[[ "${FSTYPE:-}" == "cifs" ]] || return 0 [[ "${FSTYPE:-}" == "cifs" ]] || return 0
[[ "${MOUNTED:-false}" == "false" ]] || return 0 [[ "${MOUNTED:-false}" == "false" ]] || return 0
[[ "${CIFS_LADDER_ATTEMPTED:-false}" == "false" ]] || return 0
local err mountpoint local err mountpoint
mountpoint="/mnt/$diskname" mountpoint="/mnt/$diskname"
@@ -115,17 +110,20 @@ retry_cifs_with_vers_ladder_on_dialect_failure() {
return 0 return 0
fi fi
CIFS_LADDER_ATTEMPTED=true
bashio::log.warning "...... CIFS negotiation/dialect failure: trying SMB dialect ladder (3.x -> 2.x)." bashio::log.warning "...... CIFS negotiation/dialect failure: trying SMB dialect ladder (3.x -> 2.x)."
local base_opts try_opts vers vopt sectry local base_opts try_opts vers vopt sectry
base_opts="$MOUNTOPTIONS" base_opts="$MOUNTOPTIONS"
base_opts="$(echo "$base_opts" | sed -E 's/,vers=[^,]+//g; s/,sec=[^,]+//g')" base_opts="$(echo "$base_opts" | sed -E 's/,vers=[^,]+//g; s/,sec=[^,]+//g')"
local -a opt_variants=("" ",nounix" ",noserverino" ",nounix,noserverino") local -a opt_variants=("" ",nounix" ",noserverino" ",nounix,noserverino")
local -a sec_variants=("" ",sec=ntlmssp" ",sec=ntlmv2") local -a sec_variants=("" ",sec=ntlmssp" ",sec=ntlmv2")
local -a vers_variants=("3.1.1" "3.02" "3.0" "2.1" "2.0")
for vopt in "${opt_variants[@]}"; do for vopt in "${opt_variants[@]}"; do
for vers in "3.1.1" "3.02" "3.0" "2.1" "2.0"; do for vers in "${vers_variants[@]}"; do
for sectry in "${sec_variants[@]}"; do for sectry in "${sec_variants[@]}"; do
[[ "$MOUNTED" == "false" ]] || break [[ "$MOUNTED" == "false" ]] || break
umount "$mountpoint" 2>/dev/null || true umount "$mountpoint" 2>/dev/null || true
@@ -145,8 +143,9 @@ retry_cifs_with_vers_ladder_on_dialect_failure() {
base_opts="${base_opts//,nobrl/}" base_opts="${base_opts//,nobrl/}"
base_opts="$(echo "$base_opts" | sed -E 's/,iocharset=[^,]+//g')" base_opts="$(echo "$base_opts" | sed -E 's/,iocharset=[^,]+//g')"
local -a vers_variants2=("2.1" "2.0")
for vopt in "${opt_variants[@]}"; do for vopt in "${opt_variants[@]}"; do
for vers in "2.1" "2.0"; do for vers in "${vers_variants2[@]}"; do
for sectry in "${sec_variants[@]}"; do for sectry in "${sec_variants[@]}"; do
[[ "$MOUNTED" == "false" ]] || break [[ "$MOUNTED" == "false" ]] || break
umount "$mountpoint" 2>/dev/null || true umount "$mountpoint" 2>/dev/null || true
@@ -157,6 +156,18 @@ retry_cifs_with_vers_ladder_on_dialect_failure() {
done done
[[ "$MOUNTED" == "false" ]] || break [[ "$MOUNTED" == "false" ]] || break
done done
if [[ "$MOUNTED" == "false" ]]; then
for vopt in "${opt_variants[@]}"; do
for vers in "${vers_variants2[@]}"; do
[[ "$MOUNTED" == "false" ]] || break
umount "$mountpoint" 2>/dev/null || true
try_opts="${base_opts}${vopt},vers=${vers},sec=ntlmssp"
mount_drive "$try_opts"
done
[[ "$MOUNTED" == "false" ]] || break
done
fi
fi fi
return 0 return 0
@@ -178,18 +189,10 @@ if bashio::config.has_value 'networkdisks'; then
SECVERS="" SECVERS=""
CHARSET=",iocharset=utf8" CHARSET=",iocharset=utf8"
# ----------------------------
# Normalize / clean networkdisks
# ----------------------------
# Normalize Windows CRLF and multiline entries (HA UI/YAML can introduce these)
MOREDISKS="${MOREDISKS//$'\r'/}" MOREDISKS="${MOREDISKS//$'\r'/}"
MOREDISKS="${MOREDISKS//$'\n'/,}" MOREDISKS="${MOREDISKS//$'\n'/,}"
# Clean data (keeps NFS entries intact): normalize "comma with spaces" to plain commas
MOREDISKS="$(echo "$MOREDISKS" | sed -E 's/[[:space:]]*,[[:space:]]*/,/g; s/^[[:space:]]+//; s/[[:space:]]+$//')" MOREDISKS="$(echo "$MOREDISKS" | sed -E 's/[[:space:]]*,[[:space:]]*/,/g; s/^[[:space:]]+//; s/[[:space:]]+$//')"
# CIFS domain/workgroup
DOMAINCLIENT="" DOMAINCLIENT=""
CIFSDOMAIN="" CIFSDOMAIN=""
if bashio::config.has_value 'cifsdomain'; then if bashio::config.has_value 'cifsdomain'; then
@@ -198,7 +201,6 @@ if bashio::config.has_value 'networkdisks'; then
DOMAINCLIENT="--workgroup=$CIFSDOMAIN" DOMAINCLIENT="--workgroup=$CIFSDOMAIN"
fi fi
# UID/GID for CIFS mapping
PUID=",uid=$(id -u)" PUID=",uid=$(id -u)"
PGID=",gid=$(id -g)" PGID=",gid=$(id -g)"
if bashio::config.has_value 'PUID' && bashio::config.has_value 'PGID'; then if bashio::config.has_value 'PUID' && bashio::config.has_value 'PGID'; then
@@ -207,14 +209,12 @@ if bashio::config.has_value 'networkdisks'; then
PGID=",gid=$(bashio::config 'PGID')" PGID=",gid=$(bashio::config 'PGID')"
fi fi
# Split strictly on commas (no word-splitting/globbing)
IFS=',' read -r -a DISK_LIST <<< "$MOREDISKS" IFS=',' read -r -a DISK_LIST <<< "$MOREDISKS"
for disk in "${DISK_LIST[@]}"; do for disk in "${DISK_LIST[@]}"; do
CRED_FILE="" CRED_FILE=""
cleanup_cred cleanup_cred
# Per-item trim + safety cleanup
disk="${disk//$'\r'/}" disk="${disk//$'\r'/}"
disk="$(echo "$disk" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//')" disk="$(echo "$disk" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//')"
[[ -z "$disk" ]] && continue [[ -z "$disk" ]] && continue
@@ -222,7 +222,6 @@ if bashio::config.has_value 'networkdisks'; then
disk="$(echo "$disk" | sed 's,/$,,')" disk="$(echo "$disk" | sed 's,/$,,')"
disk="${disk//"\040"/ }" disk="${disk//"\040"/ }"
# Detect FS type
FSTYPE="cifs" FSTYPE="cifs"
if [[ "$disk" =~ ^nfs:// ]]; then if [[ "$disk" =~ ^nfs:// ]]; then
FSTYPE="nfs" FSTYPE="nfs"
@@ -231,7 +230,6 @@ if bashio::config.has_value 'networkdisks'; then
FSTYPE="nfs" FSTYPE="nfs"
fi fi
# Server for reachability checks
if [[ "$FSTYPE" == "cifs" ]]; then if [[ "$FSTYPE" == "cifs" ]]; then
server="$(echo "$disk" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | head -n 1)" server="$(echo "$disk" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | head -n 1)"
else else
@@ -246,6 +244,7 @@ if bashio::config.has_value 'networkdisks'; then
: >"$ERRORCODE_FILE" || true : >"$ERRORCODE_FILE" || true
MOUNTED=false MOUNTED=false
CIFS_LADDER_ATTEMPTED=false
SMBVERS_FORCE="" SMBVERS_FORCE=""
SECVERS_FORCE="" SECVERS_FORCE=""
SMBVERS="" SMBVERS=""
@@ -253,7 +252,6 @@ if bashio::config.has_value 'networkdisks'; then
echo "... mounting ($FSTYPE) $disk" echo "... mounting ($FSTYPE) $disk"
# Validation
if [[ "$FSTYPE" == "cifs" ]]; then if [[ "$FSTYPE" == "cifs" ]]; then
if [[ ! "$disk" =~ ^//[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.+ ]]; then if [[ ! "$disk" =~ ^//[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.+ ]]; then
bashio::log.fatal "...... invalid CIFS path \"$disk\". Use //123.12.12.12/sharedfolder,//123.12.12.12/sharedfolder2" bashio::log.fatal "...... invalid CIFS path \"$disk\". Use //123.12.12.12/sharedfolder,//123.12.12.12/sharedfolder2"
@@ -271,7 +269,6 @@ if bashio::config.has_value 'networkdisks'; then
mkdir -p "/mnt/$diskname" mkdir -p "/mnt/$diskname"
chown root:root "/mnt/$diskname" chown root:root "/mnt/$diskname"
# CIFS: credentials file (avoids commas/special chars in password)
if [[ "$FSTYPE" == "cifs" ]]; then if [[ "$FSTYPE" == "cifs" ]]; then
CRED_FILE="$(mktemp /tmp/cifs-cred.XXXXXX)" CRED_FILE="$(mktemp /tmp/cifs-cred.XXXXXX)"
chmod 600 "$CRED_FILE" chmod 600 "$CRED_FILE"
@@ -284,7 +281,6 @@ if bashio::config.has_value 'networkdisks'; then
} >"$CRED_FILE" } >"$CRED_FILE"
fi fi
# First mount attempt (no vers pinned yet; we will correct on negotiation/dialect failures via ladder)
if [[ "$FSTYPE" == "cifs" ]]; then if [[ "$FSTYPE" == "cifs" ]]; then
mount_drive "rw,file_mode=0775,dir_mode=0775,credentials=${CRED_FILE},nobrl,mfsymlinks${SMBVERS}${SECVERS}${PUID}${PGID}${CHARSET}" mount_drive "rw,file_mode=0775,dir_mode=0775,credentials=${CRED_FILE},nobrl,mfsymlinks${SMBVERS}${SECVERS}${PUID}${PGID}${CHARSET}"
if [[ "$MOUNTED" == "false" ]]; then if [[ "$MOUNTED" == "false" ]]; then
@@ -294,10 +290,8 @@ if bashio::config.has_value 'networkdisks'; then
mount_drive "rw,nfsvers=4.2,proto=tcp,hard,timeo=600,retrans=2" mount_drive "rw,nfsvers=4.2,proto=tcp,hard,timeo=600,retrans=2"
fi fi
# Deeper analysis if failed
if [[ "$MOUNTED" == "false" ]]; then if [[ "$MOUNTED" == "false" ]]; then
if [[ "$FSTYPE" == "cifs" ]]; then if [[ "$FSTYPE" == "cifs" ]]; then
# SMB port check
if command -v nmap >/dev/null 2>&1; then if command -v nmap >/dev/null 2>&1; then
output="$(nmap -F "$server" -T5 -oG - 2>/dev/null || true)" output="$(nmap -F "$server" -T5 -oG - 2>/dev/null || true)"
if ! echo "$output" | grep -q "445/open"; then if ! echo "$output" | grep -q "445/open"; then
@@ -315,7 +309,6 @@ if bashio::config.has_value 'networkdisks'; then
bashio::log.warning "...... nmap not available; skipping SMB port reachability test" bashio::log.warning "...... nmap not available; skipping SMB port reachability test"
fi fi
# Credentials test (use SERVER, not share path)
if command -v smbclient >/dev/null 2>&1; then if command -v smbclient >/dev/null 2>&1; then
OUTPUT="$(smbclient -t 2 -L "$server" -U "$USERNAME%$PASSWORD" -c "exit" $DOMAINCLIENT 2>&1 || true)" OUTPUT="$(smbclient -t 2 -L "$server" -U "$USERNAME%$PASSWORD" -c "exit" $DOMAINCLIENT 2>&1 || true)"
@@ -338,7 +331,6 @@ if bashio::config.has_value 'networkdisks'; then
bashio::log.warning "...... smbclient not available; skipping SMB credential test" bashio::log.warning "...... smbclient not available; skipping SMB credential test"
fi fi
# SMB version detect (best effort)
SMBRAW="" SMBRAW=""
if command -v nmap >/dev/null 2>&1; then if command -v nmap >/dev/null 2>&1; then
SMBRAW="$( SMBRAW="$(
@@ -367,18 +359,15 @@ if bashio::config.has_value 'networkdisks'; then
SECVERS=",sec=ntlm" SECVERS=",sec=ntlm"
SMBVERS=",vers=1.0" SMBVERS=",vers=1.0"
else else
# IMPORTANT: deterministic fallback (so we don't depend on kernel defaults)
echo "...... SMB version : couldn't detect, falling back to SMB3->SMB2 ladder on negotiation/dialect failure" echo "...... SMB version : couldn't detect, falling back to SMB3->SMB2 ladder on negotiation/dialect failure"
SMBVERS=",vers=3.1.1" SMBVERS=",vers=3.1.1"
fi fi
# Apply forced SMBv1 if needed
if [[ -n "$SMBVERS_FORCE" ]]; then if [[ -n "$SMBVERS_FORCE" ]]; then
[[ -z "$SMBVERS" ]] && SMBVERS="$SMBVERS_FORCE" [[ -z "$SMBVERS" ]] && SMBVERS="$SMBVERS_FORCE"
[[ -z "$SECVERS" ]] && SECVERS="$SECVERS_FORCE" [[ -z "$SECVERS" ]] && SECVERS="$SECVERS_FORCE"
fi fi
# Try security modes (keeping detected/forced SMBVERS)
SECVERS_BASE="$SECVERS" SECVERS_BASE="$SECVERS"
for SECTRY in "$SECVERS_BASE" ",sec=ntlmv2" ",sec=ntlmssp" ",sec=ntlmsspi" ",sec=krb5i" ",sec=krb5" ",sec=ntlm" ",sec=ntlmv2i"; do for SECTRY in "$SECVERS_BASE" ",sec=ntlmv2" ",sec=ntlmssp" ",sec=ntlmsspi" ",sec=krb5i" ",sec=krb5" ",sec=ntlm" ",sec=ntlmv2i"; do
if [[ "$MOUNTED" == "false" ]]; then if [[ "$MOUNTED" == "false" ]]; then
@@ -386,13 +375,11 @@ if bashio::config.has_value 'networkdisks'; then
fi fi
done done
# If still negotiation/dialect failure, step down SMB3 -> SMB2
if [[ "$MOUNTED" == "false" ]]; then if [[ "$MOUNTED" == "false" ]]; then
retry_cifs_with_vers_ladder_on_dialect_failure retry_cifs_with_vers_ladder_on_dialect_failure
fi fi
else else
# NFS ports check and fallback versions
if command -v nmap >/dev/null 2>&1; then if command -v nmap >/dev/null 2>&1; then
output="$(nmap -F "$server" -T5 -oG - 2>/dev/null || true)" output="$(nmap -F "$server" -T5 -oG - 2>/dev/null || true)"
if ! echo "$output" | grep -Eq '(2049|111)/open'; then if ! echo "$output" | grep -Eq '(2049|111)/open'; then
@@ -410,7 +397,6 @@ if bashio::config.has_value 'networkdisks'; then
fi fi
fi fi
# Finalization / messages
if [[ "$MOUNTED" == "true" ]]; then if [[ "$MOUNTED" == "true" ]]; then
bashio::log.info "...... $disk successfully mounted to /mnt/$diskname with options ${MOUNTOPTIONS/$PASSWORD/XXXXXXXXXX}" bashio::log.info "...... $disk successfully mounted to /mnt/$diskname with options ${MOUNTOPTIONS/$PASSWORD/XXXXXXXXXX}"
rm -f "$ERRORCODE_FILE" 2>/dev/null || true rm -f "$ERRORCODE_FILE" 2>/dev/null || true
@@ -439,7 +425,6 @@ if bashio::config.has_value 'networkdisks'; then
bashio::log.warning "smbclient not available; cannot print SMB debugging info" bashio::log.warning "smbclient not available; cannot print SMB debugging info"
fi fi
# last-ditch: minimal CIFS options (still uses credentials file)
mount_drive "rw,credentials=${CRED_FILE}${PUID}${PGID}" mount_drive "rw,credentials=${CRED_FILE}${PUID}${PGID}"
if [[ "$MOUNTED" == "false" ]]; then if [[ "$MOUNTED" == "false" ]]; then
retry_cifs_with_vers_ladder_on_dialect_failure retry_cifs_with_vers_ladder_on_dialect_failure