diff --git a/Dockerfile b/Dockerfile index 9656d1a..88eb4f6 100755 --- a/Dockerfile +++ b/Dockerfile @@ -24,9 +24,24 @@ RUN apk add --no-cache runit \ && touch /external/avahi/not-mounted \ && echo done +########################### +# DYNAMIC VOLUMES STUFF +########################### +RUN mkdir -p /dynamic-volumes +#RUN apk add --no-cache webhook moreutils yq +RUN apk add --no-cache webhook moreutils py3-pip +RUN pip install crudini --break-system-packages +########################### + VOLUME ["/shares"] -EXPOSE 137/udp 139 445 +# udp port 137: Netbios Name Service +# udp port 138: Netbios Datagram Service +# tcp port 139: SMB for Windows NT or older +# tcp port 445: SMB for Windows 2000 or newer +# tcp port 9000: Webhook + +EXPOSE 137/udp 138/udp 139 445 9000 COPY . /container/ diff --git a/README.md b/README.md index cfebe93..eb9d181 100755 --- a/README.md +++ b/README.md @@ -320,3 +320,33 @@ Note: _This `wsdd2` service seems to need `CAP_NET_ADMIN` as a capability. (more cap_add: - CAP_NET_ADMIN ``` + +## Dynamic Volumes + +Added support to add dynamic volumes to the samba configuration. A directory mapped to volume '/dynamic-volumes' is monitored for files and directories added. Directories are automatically mounted and shared. Added this functionality to share automounted USB drives. There are 2 types of files monitored: + +* __\.share__ + * Format: INI file + * Adds content to samba config. Should only contain samba shares (check smb.conf for format) + +* __directory.template__ + * Format: INI file + * Default configuration for directories added to '/dynamic-volumes' + * Values added to the [global] section are added to all directories. If a custom config is needed for a directory just add values to a section with the same name as the directory (values in [global] will be added as well). + +Environment variables added: + +* __SAMBA\_DYNAMIC\_VOLUMES__ + * _optional_ + * default: disabled + * flag to enable dynamic volume monitoring. Valid values: enabled|true|yes +* __SAMBA\_DYNAMIC\_WEBHOOK\_PORT__ + * _optional_ + * default: 9000 + * webhook used to manually refresh dynamic volumes, address is http://\/hooks/refresh + +* __SAMBA\_DYNAMIC\_INTERVAL__ + * _optional_ + * default: 1 (0 to disable monitoring) + * Monitoring interval for checking changes to the dynamic volume directory (minutes). If monitoring is disabled the only way to refresh volumes is by calling the webhook. + diff --git a/config/runit/crond/run b/config/runit/crond/run new file mode 100755 index 0000000..e85c534 --- /dev/null +++ b/config/runit/crond/run @@ -0,0 +1,3 @@ +#!/bin/sh -x +sleep 14 +exec crond -f diff --git a/config/runit/webhook/run b/config/runit/webhook/run new file mode 100755 index 0000000..1c96060 --- /dev/null +++ b/config/runit/webhook/run @@ -0,0 +1,5 @@ +#!/bin/sh -x + +sleep 14 + +exec webhook -hooks /container/config/webhook/hooks.json -port $SAMBA_DYNAMIC_WEBHOOK_PORT diff --git a/config/webhook/hooks.json b/config/webhook/hooks.json new file mode 100755 index 0000000..4cf8b38 --- /dev/null +++ b/config/webhook/hooks.json @@ -0,0 +1,8 @@ +[ + { + "id": "refresh", + "execute-command": "/container/scripts/register-dynamic.sh", + "command-working-directory": "/container/scripts", + "include-command-output-in-response": true + } +] diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index ccacf7d..f2ce562 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -149,7 +149,7 @@ if [ ! -f "$INITALIZED" ]; then echo -e "$ACCOUNT_PASSWORD\n$ACCOUNT_PASSWORD" | passwd "$ACCOUNT_NAME" echo -e "$ACCOUNT_PASSWORD\n$ACCOUNT_PASSWORD" | smbpasswd "$ACCOUNT_NAME" fi - + smbpasswd -e "$ACCOUNT_NAME" done @@ -300,7 +300,7 @@ if [ ! -f "$INITALIZED" ]; then echo ">> EXTERNAL AVAHI: list of services" ls -l /external/avahi/*.service fi - + echo "" echo ">> SAMBA: check smb.conf file using 'testparm -s'" echo "############################### START ####################################" @@ -320,6 +320,50 @@ else echo ">> CONTAINER: already initialized - direct start of samba" fi +################################################## +# START - DYNAMIC VOLUMES +################################################## + +if [ -z "$SAMBA_DYNAMIC_WEBHOOK_PORT" ]; then + SAMBA_DYNAMIC_WEBHOOK_PORT=9000 +fi + +case "$SAMBA_DYNAMIC_INTERVAL" in + "") + # interval not set, check every minute + SAMBA_DYNAMIC_INTERVAL=1 + ;; + *[!0-9]*) + # interval not a positive number, check every minute + SAMBA_DYNAMIC_INTERVAL=1 + ;; +esac + +LABEL="minutes" +if [ "$SAMBA_DYNAMIC_INTERVAL" -lt 2 ]; then + LABEL="minute" +fi + +if echo "$SAMBA_DYNAMIC_VOLUMES" | grep -i -E "^(enabled|yes|y|true)$" > /dev/null; then + echo ">> DYNAMIC-VOLUMES: Enabled" + # register static shares defined in docker compose file + /container/scripts/register-static.sh + # check interval value + if [ "$SAMBA_DYNAMIC_INTERVAL" -eq 0 ]; then + echo ">> DYNAMIC-VOLUMES: Monitoring disabled, use webhook to reload config" + else + echo ">> DYNAMIC-VOLUMES: Monitoring enabled, checking every $SAMBA_DYNAMIC_INTERVAL $LABEL" + # set cron job to monitor /dynamic-volumes directory for shares + echo "*/$SAMBA_DYNAMIC_INTERVAL * * * * /container/scripts/register-dynamic.sh" >> /etc/crontabs/root + fi +else + echo ">> DYNAMIC-VOLUMES: Disabled" +fi + +################################################## +# END - DYNAMIC VOLUMES +################################################## + ## # CMD ## diff --git a/scripts/ini-functions-crudini.include b/scripts/ini-functions-crudini.include new file mode 100755 index 0000000..05f71f2 --- /dev/null +++ b/scripts/ini-functions-crudini.include @@ -0,0 +1,165 @@ +#!/bin/sh + +############################ +# FILENAME FUNCTIONS +############################ + +CRUDINI_OPTIONS="--ini-options=ignoreindent" + +filename(){ + local FILENAME=$(basename "$1") + echo "${FILENAME%.*}" +} + +extname(){ + local FILENAME=$(basename "$1") + local EXTNAME="${FILENAME##*.}" + # + if [ "$FILENAME" = "$EXTNAME" ]; then + echo "" + else + echo "$EXTNAME" + fi +} + +############################ +# INI FUNCTIONS +############################ + +ini_valid(){ + local SOURCE="$1" + local SECTIONS="" + local RESULT="" + # save sections to file + if ! crudini --get $CRUDINI_OPTIONS "$SOURCE" > /tmp/sections.txt 2> /dev/null; then + RESULT="error" + else + # check sections + while IFS= read -r CURR ; do + if [ -n "$RESULT" ]; then + : # HAS ERROR + elif ! crudini --get $CRUDINI_OPTIONS "$SOURCE" "$CURR" > /dev/null 2>&1; then + RESULT="error" + fi + done < /tmp/sections.txt + fi + # + if [ -n "$RESULT" ]; then + return 1 + else + return 0 + fi +} + +ini_list_sections(){ + local SOURCE="$1" + crudini --get $CRUDINI_OPTIONS "$SOURCE" 2> /dev/null +} + +ini_has_section(){ + local SOURCE="$1" + local SECTION="$2" + # + if crudini --get $CRUDINI_OPTIONS "$SOURCE" | grep "$SECTION" > /dev/null 2>&1; then + return 0 + else + return 1 + fi +} + +ini_print_section(){ + local SOURCE="$1" + local SECTION="$2" + + crudini --get $CRUDINI_OPTIONS --format=ini "$SOURCE" "$SECTION" | tail -n +2 +} + +ini_del_section(){ + local SOURCE="$1" + local SECTION="$2" + crudini --del $CRUDINI_OPTIONS "$SOURCE" "$SECTION" +} + +ini_clear_section(){ + local SOURCE="$1" + local SECTION="$2" + # check if section exists + if ! crudini --get $CRUDINI_OPTIONS "$SOURCE" | grep "$SECTION" > /dev/null 2>&1; then + return 1 + fi + # delete all keys + ini_list_keys "$SOURCE" "$SECTION" | while IFS= read -r KEY ; do + ini_del_key "$SOURCE" "$SECTION" "$KEY" + done +} + +ini_list_keys(){ + local SOURCE="$1" + local SECTION="$2" + if crudini --get $CRUDINI_OPTIONS "$SOURCE" | grep "$SECTION" > /dev/null 2>&1; then + crudini --get $CRUDINI_OPTIONS "$SOURCE" "$SECTION" + fi +} + +ini_get_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local DEF="$4" + # + if ! crudini --get $CRUDINI_OPTIONS "$SOURCE" "$SECTION" "$KEY" 2> /dev/null; then + echo "$DEF" + fi +} + +ini_is_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local TEST_VALUE="$4" + local CURR_VALUE=$(crudini --get $CRUDINI_OPTIONS "$SOURCE" "$SECTION" "$KEY" 2> /dev/null) + # + if [ "$TEST_VALUE" = "$CURR_VALUE" ]; then + return 0 + else + return 1 + fi +} + +ini_set_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + # + crudini --set $CRUDINI_OPTIONS "$SOURCE" "$SECTION" "$KEY" "$VALUE" +} + +ini_del_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + # + crudini --del $CRUDINI_OPTIONS "$SOURCE" "$SECTION" "$KEY" +} + +ini_copy_section(){ + local FROM_FILE="$1" + local FROM_SECTION="$2" + local TO_FILE="$3" + local TO_SECTION="$4" + local EXCLUDE="$5" + # + if [ -f "$FROM_FILE" ] && [ -z "$EXCLUDE" ]; then + ini_list_keys "$FROM_FILE" "$FROM_SECTION" | while IFS= read -r KEY ; do + VALUE=$(ini_get_key "$FROM_FILE" "$FROM_SECTION" "$KEY") + ini_set_key "$TO_FILE" "$TO_SECTION" "$KEY" "$VALUE" + done + elif [ -f "$FROM_FILE" ]; then + ini_list_keys "$FROM_FILE" "$FROM_SECTION" | grep -v -E "$EXCLUDE" | while IFS= read -r KEY ; do + VALUE=$(ini_get_key "$FROM_FILE" "$FROM_SECTION" "$KEY") + ini_set_key "$TO_FILE" "$TO_SECTION" "$KEY" "$VALUE" + done + fi +} diff --git a/scripts/ini-functions-yq.include b/scripts/ini-functions-yq.include new file mode 100755 index 0000000..41fc0e3 --- /dev/null +++ b/scripts/ini-functions-yq.include @@ -0,0 +1,142 @@ +#!/bin/sh + +# yq has a problem when INI keys have a colon in the name +# when they fix it I'll replace crudini with yq + +############################ +# FILENAME FUNCTIONS +############################ + +INI_PARSER="yq --input-format ini --output-format ini" + +filename(){ + local FILENAME=$(basename "$1") + echo "${FILENAME%.*}" +} + +extname(){ + local FILENAME=$(basename "$1") + local EXTNAME="${FILENAME##*.}" + # + if [ "$FILENAME" = "$EXTNAME" ]; then + echo "" + else + echo "$EXTNAME" + fi +} + +############################ +# INI FUNCTIONS +############################ + +ini_valid(){ + local SOURCE="$1" + # save sections to file + if ! $INI_PARSER "." "$SOURCE" > /dev/null 2>&1; then + return 1 + else + return 0 + fi +} + +ini_list_sections(){ + local SOURCE="$1" + $INI_PARSER ".[] | key" "$SOURCE" 2> /dev/null +} + +ini_has_section(){ + local SOURCE="$1" + local SECTION="$2" + # + if $INI_PARSER ". | has(\"$SECTION\")" "$SOURCE" | grep "true" > /dev/null 2>&1; then + return 0 + else + return 1 + fi +} + +ini_print_section(){ + local SOURCE="$1" + local SECTION="$2" + # + $INI_PARSER ".[\"$SECTION\"]" "$SOURCE" +} + +ini_del_section(){ + local SOURCE="$1" + local SECTION="$2" + # + $INI_PARSER -i "del(.[\"$SECTION\"])" "$SOURCE" +} + +ini_list_keys(){ + local SOURCE="$1" + local SECTION="$2" + # + $INI_PARSER ".[\"$SECTION\"].[] | key" "$SOURCE" +} + +ini_get_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local DEF="$4" + local VALUE=$($INI_PARSER ".[\"$SECTION\"].[\"$KEY\"]" "$SOURCE") + if [ "$VALUE" = "null" ]; then + echo "$DEF" + else + echo "$VALUE" + fi +} + +ini_is_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local TEST_VALUE="$4" + local CURR_VALUE=$($INI_PARSER ".[\"$SECTION\"].[\"$KEY\"]" "$SOURCE") + # + if [ "$TEST_VALUE" = "$CURR_VALUE" ]; then + return 0 + else + return 1 + fi +} + +ini_set_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + # + $INI_PARSER -i ".[\"$SECTION\"].[\"$KEY\"]=\"$VALUE\"" "$SOURCE" +} + +ini_del_key(){ + local SOURCE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + # + $INI_PARSER -i "del(.[\"$SECTION\"].[\"$KEY\"])" "$SOURCE" +} + +ini_copy_section(){ + local FROM_FILE="$1" + local FROM_SECTION="$2" + local TO_FILE="$3" + local TO_SECTION="$4" + local EXCLUDE="$5" + # + if [ -f "$FROM_FILE" ] && [ -z "$EXCLUDE" ]; then + ini_list_keys "$FROM_FILE" "$FROM_SECTION" | while IFS= read -r KEY ; do + VALUE=$(ini_get_key "$FROM_FILE" "$FROM_SECTION" "$KEY") + ini_set_key "$TO_FILE" "$TO_SECTION" "$KEY" "$VALUE" + done + elif [ -f "$FROM_FILE" ]; then + ini_list_keys "$FROM_FILE" "$FROM_SECTION" | grep -v -E "$EXCLUDE" | while IFS= read -r KEY ; do + VALUE=$(ini_get_key "$FROM_FILE" "$FROM_SECTION" "$KEY") + ini_set_key "$TO_FILE" "$TO_SECTION" "$KEY" "$VALUE" + done + fi +} diff --git a/scripts/register-dynamic.sh b/scripts/register-dynamic.sh new file mode 100755 index 0000000..3e4f730 --- /dev/null +++ b/scripts/register-dynamic.sh @@ -0,0 +1,212 @@ +#!/bin/sh + +. "/container/scripts/ini-functions-crudini.include" + +register_action(){ + local SECTION="$1" + local ACTION="$2" + ini_set_key "$VOLUME_REGISTRY" "global" "$SECTION" "$ACTION" + # + if [ "$ACTION" = "add" ] || [ "$ACTION" = "del" ]; then + ini_set_key "$VOLUME_REGISTRY" "global" "changed" "true" + fi +} + +register_isaction(){ + local SECTION="$1" + local ACTION="$2" + if ini_is_key "$VOLUME_REGISTRY" "global" "$SECTION" "$ACTION"; then + return 0 + else + return 1 + fi +} + +register_ischanged(){ + if ini_is_key "$VOLUME_REGISTRY" "global" "changed" "true"; then + return 0 + else + return 1 + fi +} + +register_directory(){ + local S_SOURCE="$1" + local S_DIR=$(dirname "$S_SOURCE") + local S_NAME=$(filename "$S_SOURCE") + local S_TEMPLATE="$S_DIR/directory.template" + local S_USR="nobody" + local S_GRP="nogroup" + local S_WRITABLE="no" + local CURR_CSUM=$(cat "$S_TEMPLATE" 2> /dev/null | md5sum | cut -f 1 -d " ") + # check if valid name + if echo "$S_NAME" | grep -E "global|home|printers" > /dev/null; then + # reserved section name + return 1 + elif ini_is_key "$VOLUME_REGISTRY" "$S_NAME" "type" "static"; then + # already registered as static + return 1 + elif ini_is_key "$VOLUME_REGISTRY" "$S_NAME" "checksum" "$CURR_CSUM"; then + # registered and same checksum, nothing changed + register_action "$S_NAME" "none" + return 0 + fi + # get directory information + local S_SOURCE_STAT=$(stat -L "$S_SOURCE" -c "%A %G %U") + S_USR=$(echo "$S_SOURCE_STAT" | cut -f 2 -d " ") + S_GRP=$(echo "$S_SOURCE_STAT" | cut -f 3 -d " ") + if echo "$S_SOURCE_STAT" | grep -E "^........w." > /dev/null; then + S_WRITABLE="yes" + elif echo "$S_SOURCE_STAT" | grep -E "^.....w...." > /dev/null && [ "$S_GRP" = "$SAMBA_DYNAMIC_GROUP" ]; then + S_WRITABLE="yes" + elif [ "$S_USR" = "$SAMBA_DYNAMIC_USER" ]; then + S_WRITABLE="yes" + fi + # remove existing + ini_del_section "$SAMBA_CONFIG" "$S_NAME" + # add to registry + ini_set_key "$VOLUME_REGISTRY" "$S_NAME" "type" "dynamic" + ini_set_key "$VOLUME_REGISTRY" "$S_NAME" "checksum" "$CURR_CSUM" + # add to samba + ini_set_key "$SAMBA_CONFIG" "$S_NAME" "path" "$S_SOURCE" + ini_set_key "$SAMBA_CONFIG" "$S_NAME" "comment" "Share for $S_SOURCE in %L" + ini_set_key "$SAMBA_CONFIG" "$S_NAME" "writeable" "$S_WRITABLE" + # if template exists add its keys + echo ">> DYNAMIC-VOLUMES: Register directory [$S_NAME]" + if [ ! -f "$S_TEMPLATE" ]; then + : # no template found + elif ! ini_valid "$S_TEMPLATE"; then + # file not a valid INI file + echo " ERROR: Template file $(basename $S_TEMPLATE) is malformed." + else + # copy default keys (no section) + ini_copy_section "$S_TEMPLATE" "global" "$SAMBA_CONFIG" "$S_NAME" "path|comment|writeable|readable" + # copy keys for name if found + if ini_has_section "$S_TEMPLATE" "$S_NAME"; then + ini_copy_section "$S_TEMPLATE" "$S_NAME" "$SAMBA_CONFIG" "$S_NAME" "path|comment|writeable|readable" + fi + fi + # registered, update state + register_action "$S_NAME" "add" + # print section + ini_print_section "$SAMBA_CONFIG" "$S_NAME" | while IFS= read -r LINE; do + echo " $LINE" + done + # + return 0 +} + +register_file(){ + local S_SOURCE="$1" + local S_DIR=$(dirname "$S_SOURCE") + local S_NAME=$(filename "$S_SOURCE") + local S_EXT=$(extname "$S_SOURCE") + local S_USR="nobody" + local S_GRP="nogroup" + local S_WRITABLE="no" + local CURR_CSUM=$(cat "$S_SOURCE" 2> /dev/null | md5sum | cut -f 1 -d " ") + # check if valid INI file + if ! ini_valid "$S_SOURCE"; then + # file not a valid INI file + echo ">> DYNAMIC-VOLUMES: Register directories from $(basename $S_SOURCE)" + echo " ERROR: Share file $(basename $S_SOURCE) is malformed." + return 1 + fi + # read all sections in share file and add to samba + ini_list_sections "$S_SOURCE" | while IFS= read -r S_SECTION; do + local S_TYPE=$(ini_get_key "$VOLUME_REGISTRY" "$S_SECTION" "type") + local S_PATH=$(ini_get_key "$S_SOURCE" "$S_SECTION" "path") + local S_CSUM=$(ini_get_key "$VOLUME_REGISTRY" "$S_SECTION" "checksum") + local S_FS=$(df -P "$S_PATH" 2> /dev/null | tail -n 1 | awk '{print $1}') + # check if valid name + if echo "$S_SECTION" | grep -E "global|home|printers" > /dev/null; then + echo ">> DYNAMIC-VOLUMES: Register directory [$S_SECTION] from $(basename $S_SOURCE)" + echo " ERROR: [$S_SECTION] is a reserved name" + elif ini_is_key "$VOLUME_REGISTRY" "$S_SECTION" "type" "static"; then + echo ">> DYNAMIC-VOLUMES: Register directory [$S_SECTION] from $(basename $S_SOURCE)" + echo " ERROR: [$S_SECTION] already registered as static" + elif ini_is_key "$VOLUME_REGISTRY" "$S_SECTION" "checksum" "$CURR_CSUM"; then + # checksum same, no change, add to registered directories + register_action "$S_SECTION" "none" + elif echo "$S_FS" | grep -E "^\/$|^overlay$|^shm$" > /dev/null; then + echo ">> DYNAMIC-VOLUMES: Register directory [$S_SECTION] from $(basename $S_SOURCE)" + echo " ERROR: [$S_SECTION] points to an internal container path" + else + echo ">> DYNAMIC-VOLUMES: Register directory [$S_SECTION] from $(basename $S_SOURCE)" + # remove existing + ini_del_section "$SAMBA_CONFIG" "$S_SECTION" + # add to registry + ini_set_key "$VOLUME_REGISTRY" "$S_SECTION" "type" "dynamic" + ini_set_key "$VOLUME_REGISTRY" "$S_SECTION" "checksum" "$CURR_CSUM" + # add to samba + ini_copy_section "$S_SOURCE" "$S_SECTION" "$SAMBA_CONFIG" "$S_SECTION" "" + # registered, update state + register_action "$S_SECTION" "add" + # print section + ini_print_section "$SAMBA_CONFIG" "$S_SECTION" | while IFS= read -r LINE; do + echo " $LINE" + done + fi + done + return 0 +} + +############################ +# MAIN +############################ + +# variables + +VOLUME_DIR="/dynamic-volumes" +VOLUME_REGISTRY="/tmp/volumes.ini" +SAMBA_CONFIG="/etc/samba/smb.conf" + +# clear volumes state + +ini_clear_section "$VOLUME_REGISTRY" "global" + +# register dynamic volumes + +for S_NAME in $(find "$VOLUME_DIR" -mindepth 1 -maxdepth 1 -type f -name "*.share" -o -type d); do + if [ -d "$S_NAME" ]; then + register_directory "$S_NAME" + else + register_file "$S_NAME" + fi +done + +# check removed + +ini_list_sections "$SAMBA_CONFIG" | while IFS= read -r S_NAME; do + # check if valid name + if echo "$S_NAME" | grep -E "global|home|printers" > /dev/null; then + # reserved section name + continue + elif ini_is_key "$VOLUME_REGISTRY" "$S_NAME" "type" "static"; then + # volume is static + continue + elif register_isaction "$S_NAME" "add"; then + # volume added + continue + elif register_isaction "$S_NAME" "none"; then + # volume not changed + continue + else + echo ">> DYNAMIC-VOLUMES: Unregister directory $S_NAME" + ini_del_section "$VOLUME_REGISTRY" "$S_NAME" + ini_del_section "$SAMBA_CONFIG" "$S_NAME" + register_action "$S_NAME" "del" + fi +done + +# reload samba if needed + +if ! register_ischanged; then + : # do nothing, smb.conf not changed +elif ! which smbcontrol > /dev/null; then + echo ">> DYNAMIC-VOLUMES: Samba configuration changed, smbcontrol missing!!" +elif ! smbcontrol all reload-config; then + echo ">> DYNAMIC-VOLUMES: Samba configuration changed, error reloading!!" +else + echo ">> DYNAMIC-VOLUMES: Samba configuration changed, reload" +fi diff --git a/scripts/register-static.sh b/scripts/register-static.sh new file mode 100755 index 0000000..bc1b803 --- /dev/null +++ b/scripts/register-static.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +. "/container/scripts/ini-functions-crudini.include" + +############################ +# MAIN +############################ + +# variables + +VOLUME_REGISTRY="/tmp/volumes.ini" +SAMBA_CONFIG="/etc/samba/smb.conf" + +# create empty volume registry + +echo "[global]" > "$VOLUME_REGISTRY" + +# register static volumes, get from environment values + +for VAR_NAME in $(env | grep '^SAMBA_VOLUME_CONFIG_' | cut -d= -f1); do + eval "VAR_VALUE=\${$VAR_NAME}" + S_NAME=$(echo "$VAR_VALUE" | sed 's/^[[:space:]]*\[\(.*\)\].*/\1/') + # check if valid name + if echo "$S_NAME" | grep -E "global|home|printers" > /dev/null; then + # reserved section name + continue + else + echo ">> DYNAMIC-VOLUMES: Register static directory [$S_NAME]" + ini_set_key "$VOLUME_REGISTRY" "$S_NAME" "type" "static" + fi +done