Skip to content

Commit

Permalink
[Feature] Change ownership of volumes in user mode #75 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
GioF71 authored Oct 6, 2023
1 parent 17a955c commit 106510c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ENV PASSTHROUGH ""

ENV PUID ""
ENV PGID ""
ENV AUDIO_GID ""

ENV PARAMETER_PRIORITY ""
ENV LOG_COMMAND_LINE ""
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The Dockerfile and the included scripts have been tested on the following distro
- Manjaro Linux with Gnome (amd64)
- Raspberry Pi 3/4 (32 and 64 bit)
- Asus Tinkerboard with DietPi ([don't let that board run at a very low minimum frequency](https://github.com/GioF71/squeezelite-docker/blob/main/doc/asus-tinkerboard.md))
- OSMC on Raspberry Pi 4

As I test the Dockerfile on more platforms, I will update this list.

Expand Down Expand Up @@ -70,7 +71,7 @@ DEVICE_TYPE|speaker|Displayed device type: `computer`, `tablet`, `smartphone`, `
DEVICE||Audio device to use. Use `?` to list options if using `alsa`, `portaudio` or `rodio`. Enter the path to the output when using `pipe`. Defaults to the backend's default.
FORMAT|S16|Output format: `F64`, `F32`, `S32`, `S24`, `S24_3`, `S16`. Defaults to `S16`.
ENABLE_CACHE||`Y` or `y` to enable, uses corresponding volume.
ENABLE_SYSTEM_CACHE||`Y` or `y` to enable, uses corresponding volume.
ENABLE_SYSTEM_CACHE||`Y` or `y` to enable (recommended), uses corresponding volume (also recommeneded to use).
CACHE_SIZE_LIMIT||Limits the size of the cache for audio files. It's possible to use suffixes like `K`, `M` or `G`.
DISABLE_AUDIO_CACHE||`Y` or `y` to disable.
DISABLE_CREDENTIAL_CACHE||`Y` or `y` to disable.
Expand Down Expand Up @@ -98,8 +99,9 @@ VOLUME_RANGE||Range of the volume control (dB). Default for softvol: `60`. For t
AUTOPLAY||Autoplay similar songs when your music ends. `Y` or `y` to enable.
DISABLE_GAPLESS||Disables gapless playback by forcing the sink to close between tracks. `Y` or `y` to disable gapless mode.
PASSTHROUGH||Pass a raw stream to the output. Only works with the pipe and subprocess backends. `Y` or `y` to enable.
PUID|1000|For pulseaudio mode. Set the same as the current user id.
PGID|1000|For pulseaudio mode. Set the same as the current group id.
PUID||Set this value the the user which should run the application, defaults to `1000` if not set when using the `pulseaudio` backend
PGID||Set this value the the user which should run the application, defaults to `1000` if not set when using the `pulseaudio` backend
AUDIO_GID||Specifies the gid for the group `audio`, it is required if you want to use, e.g., the `alsa` backend in user mode. Refer to [this page](https://github.com/GioF71/squeezelite-docker/blob/main/doc/example-alsa-user-mode.md) from my squeezelite-docker repository for more details.
PARAMETER_PRIORITY||Where to look for a parameter first: `env` or `file`. For example, the `credentials.txt` file compared to `SPOTIFY_USERNAME` and `SPOTIFY_PASSWORD` environment variables. Defaults to `file`, meaning that each file is considered if it exists and if it contains the required values.
LOG_COMMAND_LINE||Set to `Y` or `y` to enable, `N` or `n` to disable. Defaults to `Y`.

Expand All @@ -108,9 +110,11 @@ LOG_COMMAND_LINE||Set to `Y` or `y` to enable, `N` or `n` to disable. Defaults
Volume|Description
:---|:---
/data/cache|Volume for cache, used by --cache (`ENABLE_CACHE`)
/data/system-cache|Volume for system-cache, used by --system-cache (`ENABLE_SYSTEM_CACHE`)
/data/system-cache|Volume for system-cache (recommended), used by --system-cache (`ENABLE_SYSTEM_CACHE`).
/user/config|Volume for user-provided configuration. Might contain a `credentials.txt` file.

Please not that the volume `/data/system-cache` will contain the encrypted credentials. Enabling the system cache and using a dedicated volume will help keeping players discoverable by the Spotify web app when you don't provide credentials to LibreSpot.

### Examples

#### Docker-compose
Expand Down Expand Up @@ -338,6 +342,7 @@ Just be careful to use the tag you have built.

Change Date|Major Changes
---|---
2023-10-06|Change ownership of volumes (see [#75](https://github.com/GioF71/librespot-docker/issues/75))
2023-09-05|Clean Dockerfile (see [#73](https://github.com/GioF71/librespot-docker/issues/73))
2023-06-23|Pass device name in quotes (see [#67](https://github.com/GioF71/librespot-docker/issues/67))
2023-06-23|Daily builds update `latest` images
Expand Down
88 changes: 58 additions & 30 deletions app/bin/run-librespot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ CMD_LINE="/usr/bin/librespot"
DEFAULT_UID=1000
DEFAULT_GID=1000

if [ -z "${PUID}" ]; then
if [[ "${BACKEND}" == "pulseaudio" && -z "${PUID}" ]]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
if [[ -z "${PGID}" ]]; then
PGID=$DEFAULT_GID;
echo "Also setting default value for PGID: ["$PGID"]"
fi
fi

if [ -z "${PGID}" ]; then
if [[ "${BACKEND}" == "pulseaudio" && -z "${PGID}" ]]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
Expand All @@ -34,32 +38,54 @@ GROUP_NAME=librespot-group

HOME_DIR=/home/$USER_NAME

### create home directory and ancillary directories
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
chown -R $PUID:$PGID $HOME_DIR
ls -la $HOME_DIR -d
ls -la $HOME_DIR
fi

### create group
if [ ! $(getent group $GROUP_NAME) ]; then
echo "group $GROUP_NAME does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
else
echo "group $GROUP_NAME already exists."
fi

### create user
if [ ! $(getent passwd $USER_NAME) ]; then
echo "user $USER_NAME does not exist, creating..."
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
usermod -a -G audio $USER_NAME
id $USER_NAME
echo "user $USER_NAME created."
else
echo "user $USER_NAME already exists."
# handle user mode
if [[ -n "${PUID}" && -n "${PUID}" ]]; then
echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
### create group if it does not exist
if [ ! $(getent group $PGID) ]; then
echo "Group with gid [$PGID] does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
echo "Group [$GROUP_NAME] with gid [$PGID] created."
else
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
fi
### create user if it does not exist
if [ ! $(getent passwd $PUID) ]; then
echo "User with uid [$PUID] does not exist, creating..."
useradd -g $PGID -u $PUID -M $USER_NAME
echo "User [$USER_NAME] with uid [$PUID] created."
else
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
echo "user with uid [$PUID] name [$USER_NAME] already exists."
HOME_DIR="/home/$USER_NAME"
fi
### create home directory
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
echo ". done."
fi
chown -R $PUID:$PGID $HOME_DIR
if [ -z "${AUDIO_GID}" ]; then
echo "WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
else
if [ $(getent group $AUDIO_GID) ]; then
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
else
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
groupadd -g $AUDIO_GID sq-audio
fi
echo "Alsa Mode - Adding $USER_NAME [$PUID] to gid [$AUDIO_GID]"
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
usermod -a -G $AUDIO_GRP $USER_NAME
echo "Alsa Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";
fi
# set ownership on volumes
chown -R $PUID:$PGID /data/cache
chown -R $PUID:$PGID /data/system-cache
chown -R $PUID:$PGID /user/config
fi

PULSE_CLIENT_CONF="/etc/pulse/client.conf"
Expand Down Expand Up @@ -239,9 +265,11 @@ if [[ -z "${LOG_COMMAND_LINE}" || "${LOG_COMMAND_LINE^^}" = "Y" ]]; then

fi

if [ "$BACKEND" = "pulseaudio" ]; then
if [[ "${BACKEND}" = "pulseaudio" || -n "${PUID}" ]]; then
echo "Running in user mode ..."
su - $USER_NAME -c "$CMD_LINE";
else
eval $CMD_LINE;
echo "Running as root ..."
eval $CMD_LINE;
fi

0 comments on commit 106510c

Please sign in to comment.