Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non writable directories gracefully #28 #29

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Just be careful to use the tag you have built.

Date|Major Changes
:---|:---
2024-06-15|Handle non writable mountpoints gracefully, see [#28](https://github.com/GioF71/minidlna-docker/issues/28)
2024-03-07|Add support for network_interface, see [#22](https://github.com/GioF71/minidlna-docker/issues/22)
2024-01-22|Add support for log_level, see [#14](https://github.com/GioF71/minidlna-docker/issues/14)
2024-01-16|Fixed run script, see [#12](https://github.com/GioF71/minidlna-docker/issues/12)
Expand Down
118 changes: 69 additions & 49 deletions app/bin/run-minidlna.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,53 @@ DEFAULT_GID=1000
current_user_id=$(id -u)
echo "Current user id is [$current_user_id]"

USE_USER_MODE=N
if [[ $current_user_id -eq 0 ]]; then
if [[ -n "${PUID}" ]] || ([[ "${USER_MODE^^}" == "Y" ]] || [[ "${USER_MODE^^}" = "YES" ]]); then
USE_USER_MODE=Y
if [ -z "${PUID}" ]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
fi
if [ -z "${PGID}" ]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
USER_NAME=minidlna-user
GROUP_NAME=minidlna-user
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
id $USER_NAME
echo "user $USER_NAME created."
else
echo "user $USER_NAME already exists."
fi
fi
echo "Setting user permissions on /log ..."
chown -R $USER_NAME:$GROUP_NAME /log
echo "Setting user permissions on /db ..."
chown -R $USER_NAME:$GROUP_NAME /db
echo "User permissions set."
fi

CONFIG_FILE=/tmp/minidlna.conf

echo "# MINIDLNA CONFIG" > $CONFIG_FILE
Expand Down Expand Up @@ -43,8 +90,28 @@ if [ -n "$MINIDLNA_MERGE_MEDIA_DIRS" ]; then
fi
fi

echo "db_dir=/db" >> $CONFIG_FILE
echo "log_dir=/log" >> $CONFIG_FILE
db_dir="/db"
if [ $current_user_id -ne 0 ]; then
if [ ! -w "$db_dir" ]; then
echo "Warning, user is [$current_user_id], directory for db [$db_dir] is not writable, using /tmp/db ..."
mkdir -p /tmp/db
db_dir=/tmp/db
else
echo "db_dir [$db_dir] is writable."
fi
fi
log_dir="/log"
if [ $current_user_id -ne 0 ]; then
if [ ! -w "$log_dir" ]; then
echo "Warning, user is [$current_user_id], directory for log [$log_dir] is not writable, using /tmp/log ..."
mkdir -p /tmp/log
log_dir=/tmp/log
else
echo "log_dir [$log_dir] is writable."
fi
fi
echo "db_dir=$db_dir" >> $CONFIG_FILE
echo "log_dir=$log_dir" >> $CONFIG_FILE

if [ -n "${MINIDLNA_LOG_LEVEL}" ]; then
echo "log_level=${MINIDLNA_LOG_LEVEL}" >> $CONFIG_FILE
Expand Down Expand Up @@ -100,53 +167,6 @@ if [ -n "${MINIDLNA_STRICT_DLNA}" ]; then
fi
fi

USE_USER_MODE=N
if [[ $current_user_id -eq 0 ]]; then
if [[ -n "${PUID}" ]] || ([[ "${USER_MODE^^}" == "Y" ]] || [[ "${USER_MODE^^}" = "YES" ]]); then
USE_USER_MODE=Y
if [ -z "${PUID}" ]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
fi
if [ -z "${PGID}" ]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
USER_NAME=minidlna-user
GROUP_NAME=minidlna-user
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
id $USER_NAME
echo "user $USER_NAME created."
else
echo "user $USER_NAME already exists."
fi
fi
echo "Setting user permissions on /log ..."
chown -R $USER_NAME:$GROUP_NAME /log
echo "Setting user permissions on /db ..."
chown -R $USER_NAME:$GROUP_NAME /db
echo "User permissions set."
fi

if [ -n ${MINIDLNA_FORCE_SORT_CRITERIA} ]; then
echo "force_sort_criteria=$MINIDLNA_FORCE_SORT_CRITERIA" >> $CONFIG_FILE
fi
Expand Down