From 2d2e2dd396d9dd41c69520a8e900789c97c49ff1 Mon Sep 17 00:00:00 2001 From: Giovanni Fulco Date: Mon, 18 Dec 2023 16:06:45 +0100 Subject: [PATCH] [Enhancement] Create and store random player mac address #247 (#248) --- Dockerfile | 2 ++ README.md | 8 +++++++- app/bin/cmd-line-builder.sh | 31 +++++++++++++++++++++++++++++++ app/bin/run-squeezelite-alsa.sh | 18 +++++++++++++++--- app/bin/run-squeezelite-pulse.sh | 12 +++++++++++- doc/release.md | 1 + 6 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 79a1428..abb9cae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -121,6 +121,8 @@ RUN chmod +x /app/bin/*.sh VOLUME '/app/assets/additional-presets.conf' VOLUME '/app/assets/binaries' +VOLUME '/config' + COPY README.md /app/doc/ COPY doc/* /app/doc/ COPY app/assets/builtin-presets.conf /app/assets/ diff --git a/README.md b/README.md index f4aa29f..5b35aa6 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ SQUEEZELITE_PRIORITY|-p|Please refer to the squeezelite's man page for `-p`. SQUEEZELITE_DELAY|-D|Set it to maybe something like `500,dop` if your DAC supports DoP. Defaults to `500` SQUEEZELITE_NAME|-n|Name of the SqueezeLite player. SQUEEZELITE_MODEL_NAME|-M|Name of the SqueezeLite model name. -SQUEEZELITE_MAC_ADDRESS|-m|Mac Address of the SqueezeLite player. The format must be colon-delimited hexadecimal, for example: `ab:cd:ef:12:34:56`. +SQUEEZELITE_MAC_ADDRESS|-m|Mac Address of the SqueezeLite player. The format must be colon-delimited hexadecimal, for example: `ab:cd:ef:12:34:56`. See note below. SQUEEZELITE_TIMEOUT|-C|Device timeout in seconds, defaults to `2` SQUEEZELITE_SERVER_PORT|-s|Server and port of the server, for example: `squeezebox-server.local:3483` or `192.168.1.10:3483`. Do not specify the variable if you want to use the auto discovery feature. If you don't specify this variable, you will probably need to use host network mode. See the examples for some hints. The port can be omitted if not different from the default `3483`. So other possible valid values are `squeezebox-server.local` or `192.168.1.10`. SQUEEZELITE_RATES|-r|From squeezelite's man page for `-r`: Specify sample rates supported by the output device; this is required if the output device is switched off when squeezelite is started. The format is either a single maximum sample rate, a range of sample rates in the format `-`, or a comma-separated list of available rates. Delay is an optional time to wait when switching sample rates between tracks, in milliseconds. Switch back to the author of this repository: it is recommended to specify sample rates that are effectively supported by your audio device. @@ -125,12 +125,18 @@ It is possible to add and additional preset configuration file using the volume Possible values for variables `SQUEEZELITE_LOG_CATEGORY_*` are `info`, `debug` or `sdebug`. +### Automatic MAC address creation + +If you don't provide a value to `SQUEEZELITE_MAC_ADDRESS`, a random mac address will be generated and stored (if possible) under `/config/mac-address.txt`, so it will be reloaded on next restart. +Use a persistent volume in order to preserve the functionality in the event of container recreation (such as when you update to a newer image). + ## Volumes Volume|Description :---|:--- /app/assets/additional-presets.conf|Additional preset file /app/assets/binaries|Custom binaries should be placed here +/config|Container configuration will be stored here ### Additional preset file diff --git a/app/bin/cmd-line-builder.sh b/app/bin/cmd-line-builder.sh index 4101dcf..7ce9fd3 100644 --- a/app/bin/cmd-line-builder.sh +++ b/app/bin/cmd-line-builder.sh @@ -150,4 +150,35 @@ function cmdline-rates() { echo "Variable SQUEEZELITE_RATES specified: ${SQUEEZELITE_RATES}" CMD_LINE="$CMD_LINE -r "$(quote_if_needed "${SQUEEZELITE_RATES}") fi +} + +function handle_mac_address() { + if [[ -z "${SQUEEZELITE_MAC_ADDRESS}" ]]; then + echo "No mac address, specified, try loading ..." + if [ -f "/config/mac-address.txt" ]; then + echo "Found file with mac address ..." + mac=`cat /config/mac-address.txt` + echo "Mac address is ${mac}" + SQUEEZELITE_MAC_ADDRESS="${mac}" + else + echo "Mac address not found, generating ..." + for i in $(seq 1 6); do + portion=$[RANDOM%10]$[RANDOM%10] + if [ $i -eq 1 ]; then + mac="${portion}" + else + mac="${mac}:${portion}" + fi + done + echo "Generated mac address is [$mac]" + SQUEEZELITE_MAC_ADDRESS="${mac}" + # write if possible + if [ -w /config ]; then + echo "${mac}" > /config/mac-address.txt + echo "Written generated [${mac}] mac address." + else + echo "Config directory is not writable" + fi + fi + fi } \ No newline at end of file diff --git a/app/bin/run-squeezelite-alsa.sh b/app/bin/run-squeezelite-alsa.sh index e415008..f427fb0 100644 --- a/app/bin/run-squeezelite-alsa.sh +++ b/app/bin/run-squeezelite-alsa.sh @@ -165,7 +165,6 @@ cmdline-server-port cmdline-player-name cmdline-model-name cmdline-timeout -cmdline-mac-address cmdline-audio-device cmdline-mixer-device cmdline-delay @@ -244,8 +243,17 @@ USER_NAME=$DEFAULT_USER_NAME GROUP_NAME=$DEFAULT_GROUP_NAME HOME_DIR=$DEFAULT_HOME_DIR +current_user_id=$(id -u) +echo "Current user id is [$current_user_id], requested USER_MODE=[${USER_MODE}]" +actual_user_mode=0 +if [[ $curret_user_id -eq 0 ]] && [[ "${USER_MODE^^}" == "YES" || "${USER_MODE^^}" == "Y" ]]; then + actual_user_mode=1 +fi + +echo "Resulting actual_user_mode: [$actual_user_mode]" + ## User mode support -if [[ "${USER_MODE^^}" == "YES" || "${USER_MODE^^}" == "Y" ]]; then +if [[ $actual_user_mode -eq 1 ]]; then USE_USER_MODE="Y" echo "User mode enabled" echo "Creating user ..."; @@ -303,9 +311,13 @@ else echo "User mode disabled" fi +handle_mac_address +cmdline-mac-address + echo "Command Line: ["$CMD_LINE"]" -if [[ ${USE_USER_MODE} == "Y" ]]; then +if [[ $actual_user_mode -eq 1 ]]; then + chown -R $USER_NAME:$GROUP_NAME /config su - $USER_NAME -c "$CMD_LINE" else eval $CMD_LINE diff --git a/app/bin/run-squeezelite-pulse.sh b/app/bin/run-squeezelite-pulse.sh index 249f35f..b674aae 100644 --- a/app/bin/run-squeezelite-pulse.sh +++ b/app/bin/run-squeezelite-pulse.sh @@ -2,6 +2,13 @@ echo "Running in pulse mode" +current_user_id=$(id -u) +echo "Current user id is [$current_user_id], requested USER_MODE=[${USER_MODE}]" +if [[ $current_user_id -ne 0 ]]; then + echo "Cannot run in Pulse Mode using docker user mode, you need to run as root and specify PUID/PGID" + exit 1 +fi + DEFAULT_UID=1000 DEFAULT_GID=1000 @@ -87,7 +94,6 @@ cmdline-server-port cmdline-player-name cmdline-model-name cmdline-timeout -cmdline-mac-address cmdline-audio-device cmdline-params cmdline-codecs @@ -108,7 +114,11 @@ source logging.sh add_log_categories +handle_mac_address +cmdline-mac-address + echo "Command Line: ["$CMD_LINE"]" +chown -R $USER_NAME:$GROUP_NAME /config su - $USER_NAME -c "$CMD_LINE" diff --git a/doc/release.md b/doc/release.md index 02eaad9..2da5062 100644 --- a/doc/release.md +++ b/doc/release.md @@ -10,6 +10,7 @@ Older build might be dropped in order to save space on docker-hub and incur in l Date|Type|Description :---|:---|:--- +2023-12-18|Improvement|Autogen mac address (see [#247](https://github.com/GioF71/squeezelite-docker/issues/247)) 2023-12-18|Improvement|Arguments in quotes (see [#245](https://github.com/GioF71/squeezelite-docker/issues/245)) 2023-12-17|Improvement|Support output device `-o` also with PulseAudio (see [#243](https://github.com/GioF71/squeezelite-docker/issues/243)) 2023-09-23|Improvement|Add presets (Topping D10s/D10 Balanced), updated Yulong D200 (see [#239](https://github.com/GioF71/squeezelite-docker/issues/239))