mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-05-23 09:11:50 +02:00
Improve smb mount code
This commit is contained in:
@@ -2,32 +2,37 @@
|
|||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
# shellcheck disable=
|
# shellcheck disable=
|
||||||
|
|
||||||
#########################
|
####################
|
||||||
# MOUNT SMB SHARES v1.6 #
|
# MOUNT SMB SHARES #
|
||||||
#########################
|
####################
|
||||||
if bashio::config.has_value 'networkdisks'; then
|
if bashio::config.has_value 'networkdisks'; then
|
||||||
|
|
||||||
|
echo 'Mounting smb share(s)...'
|
||||||
|
|
||||||
# Define variables
|
# Define variables
|
||||||
MOREDISKS=$(bashio::config 'networkdisks')
|
MOREDISKS=$(bashio::config 'networkdisks')
|
||||||
CIFS_USERNAME=$(bashio::config 'cifsusername')
|
CIFS_USERNAME=$(bashio::config 'cifsusername')
|
||||||
CIFS_PASSWORD=$(bashio::config 'cifspassword')
|
CIFS_PASSWORD=$(bashio::config 'cifspassword')
|
||||||
|
|
||||||
SMBVERS=""
|
SMBVERS=""
|
||||||
|
SMBDEFAULT=""
|
||||||
SECVERS=""
|
SECVERS=""
|
||||||
|
|
||||||
# Mount CIFS Share if configured and if Protection Mode is active
|
# Clean data
|
||||||
echo 'Mounting smb share(s)...'
|
MOREDISKS=${MOREDISKS// \/\//,\/\/}
|
||||||
|
MOREDISKS=${MOREDISKS//, /,}
|
||||||
|
MOREDISKS=${MOREDISKS// /"\040"}
|
||||||
|
|
||||||
|
# Is domain set
|
||||||
if bashio::config.has_value 'cifsdomain'; then
|
if bashio::config.has_value 'cifsdomain'; then
|
||||||
echo "Using domain $(bashio::config 'cifsdomain')"
|
echo "... using domain $(bashio::config 'cifsdomain')"
|
||||||
DOMAIN=",domain=$(bashio::config 'cifsdomain')"
|
DOMAIN=",domain=$(bashio::config 'cifsdomain')"
|
||||||
else
|
else
|
||||||
DOMAIN=""
|
DOMAIN=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Mount using UID/GID values
|
# Is UID/GID set
|
||||||
if bashio::config.has_value 'PUID' && bashio::config.has_value 'PGID' && [ -z ${ROOTMOUNT+x} ]; then
|
if bashio::config.has_value 'PUID' && bashio::config.has_value 'PGID' && [ -z ${ROOTMOUNT+x} ]; then
|
||||||
echo "Using PUID $(bashio::config 'PUID') and PGID $(bashio::config 'PGID')"
|
echo "... using PUID $(bashio::config 'PUID') and PGID $(bashio::config 'PGID')"
|
||||||
PUID=",uid=$(bashio::config 'PUID')"
|
PUID=",uid=$(bashio::config 'PUID')"
|
||||||
PGID=",gid=$(bashio::config 'PGID')"
|
PGID=",gid=$(bashio::config 'PGID')"
|
||||||
else
|
else
|
||||||
@@ -35,17 +40,10 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
PGID=",gid=$(id -g)"
|
PGID=",gid=$(id -g)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clean data
|
|
||||||
MOREDISKS=${MOREDISKS// \/\//,\/\/}
|
|
||||||
MOREDISKS=${MOREDISKS//, /,}
|
|
||||||
MOREDISKS=${MOREDISKS// /"\040"}
|
|
||||||
|
|
||||||
# Mounting disks
|
# Mounting disks
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
for disk in ${MOREDISKS//,/ }; do # Separate comma separated values
|
for disk in ${MOREDISKS//,/ }; do # Separate comma separated values
|
||||||
|
|
||||||
echo "... mounting $disk"
|
|
||||||
|
|
||||||
# Clean name of network share
|
# Clean name of network share
|
||||||
# shellcheck disable=SC2116,SC2001
|
# shellcheck disable=SC2116,SC2001
|
||||||
disk=$(echo $disk | sed "s,/$,,") # Remove / at end of name
|
disk=$(echo $disk | sed "s,/$,,") # Remove / at end of name
|
||||||
@@ -54,14 +52,18 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
diskname="${diskname##*/}" # Get only last part of the name
|
diskname="${diskname##*/}" # Get only last part of the name
|
||||||
MOUNTED=false
|
MOUNTED=false
|
||||||
|
|
||||||
|
echo "... mounting $disk"
|
||||||
|
|
||||||
# Data validation
|
# Data validation
|
||||||
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 "The structure of your \"networkdisks\" option : \"$disk\" doesn't seem correct, please use a structure like //123.12.12.12/sharedfolder,//123.12.12.12/sharedfolder2. If you don't use it, you can simply remove the text, this will avoid this error message in the future."
|
bashio::log.fatal "The structure of your \"networkdisks\" option : \"$disk\" doesn't seem correct, please use a structure like //123.12.12.12/sharedfolder,//123.12.12.12/sharedfolder2. If you don't use it, you can simply remove the text, this will avoid this error message in the future."
|
||||||
break 2
|
break 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Does server exists
|
# Extract ip part of server for further manipulation
|
||||||
server="$(echo "$disk" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")"
|
server="$(echo "$disk" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")"
|
||||||
|
|
||||||
|
# Does server exists
|
||||||
if command -v "nc" &>/dev/null; then
|
if command -v "nc" &>/dev/null; then
|
||||||
# test if smb port is open
|
# test if smb port is open
|
||||||
nc -w 2 -z "$server" 445 2>/dev/null || \
|
nc -w 2 -z "$server" 445 2>/dev/null || \
|
||||||
@@ -70,11 +72,18 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
bashio::log.warning "Your server $server from $disk doesn't seem reachable, is it correct?"
|
bashio::log.warning "Your server $server from $disk doesn't seem reachable, is it correct?"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Try smbv1
|
||||||
|
if smbclient -t 2 -L "$server" -m NT1 -N &>/dev/null; then
|
||||||
|
echo "... only SMBv1 is supported, trying it"
|
||||||
|
SMBDEFAULT=",vers=1.0"
|
||||||
|
fi
|
||||||
|
|
||||||
# Prepare mount point
|
# Prepare mount point
|
||||||
mkdir -p /mnt/"$diskname"
|
mkdir -p /mnt/"$diskname"
|
||||||
chown -R root:root /mnt/"$diskname"
|
chown -R root:root /mnt/"$diskname"
|
||||||
|
|
||||||
# if Fail test different smb and sec versions
|
# if Fail test different smb and sec versions
|
||||||
|
echo "... looking for the optimal parameters for mounting"
|
||||||
if [ "$MOUNTED" = false ]; then
|
if [ "$MOUNTED" = false ]; then
|
||||||
|
|
||||||
# Test with domain, remove otherwise
|
# Test with domain, remove otherwise
|
||||||
@@ -91,11 +100,11 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
|
|
||||||
# Test with different SMB versions
|
# Test with different SMB versions
|
||||||
##################################
|
##################################
|
||||||
for SMBVERS in "" ",vers=3" ",vers=3.2" ",vers=3.0" ",vers=2.1" ",nodfs"; do
|
for SMBVERS in "$SMBDEFAULT" ",vers=3" ",vers=3.2" ",vers=3.0" ",vers=2.1" ",nodfs"; do
|
||||||
|
|
||||||
# Test with different security versions
|
# Test with different security versions
|
||||||
#######################################
|
#######################################
|
||||||
for SECVERS in "" ",sec=ntlm" ",sec=ntlmv2" ",sec=ntlmv2i" ",sec=ntlmssp" ",sec=ntlmsspi" ",sec=krb5i" ",sec=krb5"; do
|
for SECVERS in "" ",sec=ntlmv2" ",sec=ntlm" ",sec=ntlmv2i" ",sec=ntlmssp" ",sec=ntlmsspi" ",sec=krb5i" ",sec=krb5"; do
|
||||||
if [ "$MOUNTED" = false ]; then
|
if [ "$MOUNTED" = false ]; then
|
||||||
mount -t cifs -o "rw,file_mode=0775,dir_mode=0775,username=$CIFS_USERNAME,password=${CIFS_PASSWORD},nobrl$SMBVERS$SECVERS$PUIDPGID$CHARSET$DOMAINVAR" "$disk" /mnt/"$diskname" 2>ERRORCODE \
|
mount -t cifs -o "rw,file_mode=0775,dir_mode=0775,username=$CIFS_USERNAME,password=${CIFS_PASSWORD},nobrl$SMBVERS$SECVERS$PUIDPGID$CHARSET$DOMAINVAR" "$disk" /mnt/"$diskname" 2>ERRORCODE \
|
||||||
&& MOUNTED=true && MOUNTOPTIONS="$SMBVERS$SECVERS$PUIDPGID$CHARSET$DOMAINVAR" || MOUNTED=false
|
&& MOUNTED=true && MOUNTOPTIONS="$SMBVERS$SECVERS$PUIDPGID$CHARSET$DOMAINVAR" || MOUNTED=false
|
||||||
@@ -111,13 +120,6 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Try smbv1
|
|
||||||
if [ "$MOUNTED" = false ]; then
|
|
||||||
echo "... trying smbv1"
|
|
||||||
mount -t cifs -o "rw,file_mode=0775,dir_mode=0775,username=$CIFS_USERNAME,password=${CIFS_PASSWORD},vers=1.0$DOMAINVAR" "$disk" /mnt/"$diskname" \
|
|
||||||
&& MOUNTED=true && MOUNTOPTIONS="$SMBVERS,vers=1.0$DOMAINVAR" || MOUNTED=false
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Messages
|
# Messages
|
||||||
if [ "$MOUNTED" = true ] && mountpoint -q /mnt/"$diskname"; then
|
if [ "$MOUNTED" = true ] && mountpoint -q /mnt/"$diskname"; then
|
||||||
#Test write permissions
|
#Test write permissions
|
||||||
@@ -144,7 +146,7 @@ if bashio::config.has_value 'networkdisks'; then
|
|||||||
bashio::log.fatal "Here is some debugging info :"
|
bashio::log.fatal "Here is some debugging info :"
|
||||||
|
|
||||||
# Provide debugging info
|
# Provide debugging info
|
||||||
smbclient -L $disk -U "$CIFS_USERNAME%$CIFS_PASSWORD" || true
|
smbclient -t 5 -L $disk -U "$CIFS_USERNAME%$CIFS_PASSWORD"
|
||||||
|
|
||||||
# Error code
|
# Error code
|
||||||
mount -t cifs -o "rw,file_mode=0775,dir_mode=0775,username=$CIFS_USERNAME,password=${CIFS_PASSWORD},nobrl$DOMAINVAR" "$disk" /mnt/"$diskname" 2>ERRORCODE || MOUNTED=false
|
mount -t cifs -o "rw,file_mode=0775,dir_mode=0775,username=$CIFS_USERNAME,password=${CIFS_PASSWORD},nobrl$DOMAINVAR" "$disk" /mnt/"$diskname" 2>ERRORCODE || MOUNTED=false
|
||||||
|
|||||||
Reference in New Issue
Block a user