Skip to content

Commit 106510c

Browse files
authored
[Feature] Change ownership of volumes in user mode #75 (#76)
1 parent 17a955c commit 106510c

3 files changed

Lines changed: 68 additions & 34 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ ENV PASSTHROUGH ""
9292

9393
ENV PUID ""
9494
ENV PGID ""
95+
ENV AUDIO_GID ""
9596

9697
ENV PARAMETER_PRIORITY ""
9798
ENV LOG_COMMAND_LINE ""

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The Dockerfile and the included scripts have been tested on the following distro
3737
- Manjaro Linux with Gnome (amd64)
3838
- Raspberry Pi 3/4 (32 and 64 bit)
3939
- 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))
40+
- OSMC on Raspberry Pi 4
4041

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

@@ -70,7 +71,7 @@ DEVICE_TYPE|speaker|Displayed device type: `computer`, `tablet`, `smartphone`, `
7071
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.
7172
FORMAT|S16|Output format: `F64`, `F32`, `S32`, `S24`, `S24_3`, `S16`. Defaults to `S16`.
7273
ENABLE_CACHE||`Y` or `y` to enable, uses corresponding volume.
73-
ENABLE_SYSTEM_CACHE||`Y` or `y` to enable, uses corresponding volume.
74+
ENABLE_SYSTEM_CACHE||`Y` or `y` to enable (recommended), uses corresponding volume (also recommeneded to use).
7475
CACHE_SIZE_LIMIT||Limits the size of the cache for audio files. It's possible to use suffixes like `K`, `M` or `G`.
7576
DISABLE_AUDIO_CACHE||`Y` or `y` to disable.
7677
DISABLE_CREDENTIAL_CACHE||`Y` or `y` to disable.
@@ -98,8 +99,9 @@ VOLUME_RANGE||Range of the volume control (dB). Default for softvol: `60`. For t
9899
AUTOPLAY||Autoplay similar songs when your music ends. `Y` or `y` to enable.
99100
DISABLE_GAPLESS||Disables gapless playback by forcing the sink to close between tracks. `Y` or `y` to disable gapless mode.
100101
PASSTHROUGH||Pass a raw stream to the output. Only works with the pipe and subprocess backends. `Y` or `y` to enable.
101-
PUID|1000|For pulseaudio mode. Set the same as the current user id.
102-
PGID|1000|For pulseaudio mode. Set the same as the current group id.
102+
PUID||Set this value the the user which should run the application, defaults to `1000` if not set when using the `pulseaudio` backend
103+
PGID||Set this value the the user which should run the application, defaults to `1000` if not set when using the `pulseaudio` backend
104+
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.
103105
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.
104106
LOG_COMMAND_LINE||Set to `Y` or `y` to enable, `N` or `n` to disable. Defaults to `Y`.
105107

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

116+
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.
117+
114118
### Examples
115119

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

339343
Change Date|Major Changes
340344
---|---
345+
2023-10-06|Change ownership of volumes (see [#75](https://github.com/GioF71/librespot-docker/issues/75))
341346
2023-09-05|Clean Dockerfile (see [#73](https://github.com/GioF71/librespot-docker/issues/73))
342347
2023-06-23|Pass device name in quotes (see [#67](https://github.com/GioF71/librespot-docker/issues/67))
343348
2023-06-23|Daily builds update `latest` images

app/bin/run-librespot.sh

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ CMD_LINE="/usr/bin/librespot"
1919
DEFAULT_UID=1000
2020
DEFAULT_GID=1000
2121

22-
if [ -z "${PUID}" ]; then
22+
if [[ "${BACKEND}" == "pulseaudio" && -z "${PUID}" ]]; then
2323
PUID=$DEFAULT_UID;
2424
echo "Setting default value for PUID: ["$PUID"]"
25+
if [[ -z "${PGID}" ]]; then
26+
PGID=$DEFAULT_GID;
27+
echo "Also setting default value for PGID: ["$PGID"]"
28+
fi
2529
fi
2630

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

3539
HOME_DIR=/home/$USER_NAME
3640

37-
### create home directory and ancillary directories
38-
if [ ! -d "$HOME_DIR" ]; then
39-
echo "Home directory [$HOME_DIR] not found, creating."
40-
mkdir -p $HOME_DIR
41-
chown -R $PUID:$PGID $HOME_DIR
42-
ls -la $HOME_DIR -d
43-
ls -la $HOME_DIR
44-
fi
45-
46-
### create group
47-
if [ ! $(getent group $GROUP_NAME) ]; then
48-
echo "group $GROUP_NAME does not exist, creating..."
49-
groupadd -g $PGID $GROUP_NAME
50-
else
51-
echo "group $GROUP_NAME already exists."
52-
fi
53-
54-
### create user
55-
if [ ! $(getent passwd $USER_NAME) ]; then
56-
echo "user $USER_NAME does not exist, creating..."
57-
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
58-
usermod -a -G audio $USER_NAME
59-
id $USER_NAME
60-
echo "user $USER_NAME created."
61-
else
62-
echo "user $USER_NAME already exists."
41+
# handle user mode
42+
if [[ -n "${PUID}" && -n "${PUID}" ]]; then
43+
echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
44+
### create group if it does not exist
45+
if [ ! $(getent group $PGID) ]; then
46+
echo "Group with gid [$PGID] does not exist, creating..."
47+
groupadd -g $PGID $GROUP_NAME
48+
echo "Group [$GROUP_NAME] with gid [$PGID] created."
49+
else
50+
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
51+
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
52+
fi
53+
### create user if it does not exist
54+
if [ ! $(getent passwd $PUID) ]; then
55+
echo "User with uid [$PUID] does not exist, creating..."
56+
useradd -g $PGID -u $PUID -M $USER_NAME
57+
echo "User [$USER_NAME] with uid [$PUID] created."
58+
else
59+
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
60+
echo "user with uid [$PUID] name [$USER_NAME] already exists."
61+
HOME_DIR="/home/$USER_NAME"
62+
fi
63+
### create home directory
64+
if [ ! -d "$HOME_DIR" ]; then
65+
echo "Home directory [$HOME_DIR] not found, creating."
66+
mkdir -p $HOME_DIR
67+
echo ". done."
68+
fi
69+
chown -R $PUID:$PGID $HOME_DIR
70+
if [ -z "${AUDIO_GID}" ]; then
71+
echo "WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
72+
else
73+
if [ $(getent group $AUDIO_GID) ]; then
74+
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
75+
else
76+
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
77+
groupadd -g $AUDIO_GID sq-audio
78+
fi
79+
echo "Alsa Mode - Adding $USER_NAME [$PUID] to gid [$AUDIO_GID]"
80+
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
81+
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
82+
usermod -a -G $AUDIO_GRP $USER_NAME
83+
echo "Alsa Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";
84+
fi
85+
# set ownership on volumes
86+
chown -R $PUID:$PGID /data/cache
87+
chown -R $PUID:$PGID /data/system-cache
88+
chown -R $PUID:$PGID /user/config
6389
fi
6490

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

240266
fi
241267

242-
if [ "$BACKEND" = "pulseaudio" ]; then
268+
if [[ "${BACKEND}" = "pulseaudio" || -n "${PUID}" ]]; then
269+
echo "Running in user mode ..."
243270
su - $USER_NAME -c "$CMD_LINE";
244271
else
245-
eval $CMD_LINE;
272+
echo "Running as root ..."
273+
eval $CMD_LINE;
246274
fi
247275

0 commit comments

Comments
 (0)