Skip to content
Draft
4 changes: 4 additions & 0 deletions samba/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 12.7.0

- Add Windows automatic discovery support with WSDD

## 12.6.1

- Support spaces in configurable username
Expand Down
2 changes: 1 addition & 1 deletion samba/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ENV LANG C.UTF-8

# Setup base
RUN \
apk add --no-cache samba \
apk add --no-cache samba wsdd \
&& mkdir -p /var/lib/samba \
&& touch \
/etc/samba/lmhosts \
Expand Down
2 changes: 1 addition & 1 deletion samba/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
version: 12.6.1
version: 12.7.0
slug: samba
name: Samba share
description: Expose Home Assistant folders with SMB/CIFS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/with-contenv bash
# vim: ft=bash
# shellcheck shell=bash

# Populate the provided array variable with enabled network interfaces.
get_enabled_interfaces() {
local -n _out_interfaces=$1
local interface
local interface_enabled

_out_interfaces=()
for interface in $(bashio::network.interfaces); do
interface_enabled=$(bashio::network.enabled "${interface}")
if bashio::var.true "${interface_enabled}"; then
_out_interfaces+=("${interface}")
fi
done
}
9 changes: 3 additions & 6 deletions samba/rootfs/etc/s6-overlay/s6-rc.d/init-smbd/run
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ declare samba_username
declare -a interfaces=()
export HOSTNAME

source /etc/s6-overlay/s6-rc.d/init-smbd/network-interfaces.sh

# Check Login data
if ! bashio::config.has_value 'username' || ! bashio::config.has_value 'password'; then
bashio::exit.nok "Setting a username and password is required!"
Expand All @@ -27,12 +29,7 @@ fi
bashio::log.info "Hostname: ${HOSTNAME}"

# Get supported interfaces
for interface in $(bashio::network.interfaces); do
interface_enabled=$(bashio::network.enabled "${interface}")
if bashio::var.true "${interface_enabled}"; then
interfaces+=("${interface}")
fi
done
get_enabled_interfaces interfaces
if [ ${#interfaces[@]} -eq 0 ]; then
bashio::exit.nok 'No supported interfaces found to bind on.'
fi
Expand Down
1 change: 1 addition & 0 deletions samba/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/wsdd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

29 changes: 29 additions & 0 deletions samba/rootfs/etc/s6-overlay/s6-rc.d/wsdd/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Keep the addon running when the optional wsdd service fails
# ==============================================================================

# shellcheck disable=SC2155
readonly exit_code_container=$(</run/s6-linux-init-container-results/exitcode)
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
readonly service="wsdd"

bashio::log.info \
"Service ${service} exited with code ${exit_code_service}" \
"(by signal ${exit_code_signal})"

if [[ "${exit_code_service}" -eq 256 && "${exit_code_signal}" -eq 15 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
exec /run/s6/basedir/bin/halt
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part feels a bit overengineered to me, but I am fine with it. I mostly meant we should not shutdown the container on regular failure, but what coderabbit suggested was a bit more elaborate 😅

elif [[ "${exit_code_service}" -eq 256 ]]; then
bashio::log.warning \
"Optional service ${service} exited by signal ${exit_code_signal}; continuing without Windows discovery."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this...

elif [[ "${exit_code_service}" -ne 0 ]]; then
bashio::log.warning \
"Optional service ${service} failed; continuing without Windows discovery."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and this case s6 will simply restart the service again. If we want it to remain stopped we need to exit the finish script with exit code 125 (permanent failure). And I'd make use of that here, since that is what the warning suggests is what we want to happen.

fi
38 changes: 38 additions & 0 deletions samba/rootfs/etc/s6-overlay/s6-rc.d/wsdd/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start wsdd service
# ==============================================================================
declare hostname
declare workgroup
declare -a wsdd_interfaces=()
declare -a interfaces=()

source /etc/s6-overlay/s6-rc.d/init-smbd/network-interfaces.sh

# Match Samba's advertised server name.
hostname=$(bashio::info.hostname)
if bashio::var.is_empty "${hostname}"; then
bashio::log.warning "Can't read hostname, using default."
hostname="hassio"
fi
workgroup=$(bashio::config 'workgroup')

get_enabled_interfaces interfaces

if [ ${#interfaces[@]} -eq 0 ]; then
bashio::log.warning 'WSDD disabled: no supported interfaces found to bind on.'
exec tail -f /dev/null
fi

# wsdd expects each interface to be preceded by "-i".
for interface in "${interfaces[@]}"; do
wsdd_interfaces+=("-i" "${interface}")
done

bashio::log.info \
"Starting the WSDD daemon on interfaces: $(printf '%s ' "${interfaces[@]}")" \
"for ${workgroup}/${hostname}"

exec wsdd "${wsdd_interfaces[@]}" -n "${hostname}" -w "${workgroup}"
1 change: 1 addition & 0 deletions samba/rootfs/etc/s6-overlay/s6-rc.d/wsdd/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Loading