Files
Pulse/docker-entrypoint.sh
courtmanr@gmail.com 1f0f933243 Harden updater disk/download handling and warn on ephemeral /data
2026-11-20 telemetry showed 9.4% of in-app updates failing (394/4,182 in 30d), led by disk_space (113) and download (87), alongside 11.5-18.5% of Docker installs never activating (often from ephemeral /data mounts lost on recreate).

Prune stale pulse-update-* temp dirs and retained backups before allocating update staging space, choose the temp root with the most free space (keeping persistent backups on dataDir when space suffices), fall back to an alternate temp root for extraction, reclaim the downloaded archive immediately after extraction, retry mid-stream download disconnects and truncated transfers with partial-file cleanup, and warn at Docker startup when PULSE_DATA_DIR is not on a mounted volume.
2026-11-20 22:46:01 +00:00

170 lines
4.5 KiB
Bash
Executable File

#!/bin/sh
set -e
# Default UID/GID if not provided
PUID=${PUID:-1000}
PGID=${PGID:-1000}
PULSE_IMMUTABLE_OWNERSHIP_PATHS=${PULSE_IMMUTABLE_OWNERSHIP_PATHS:-}
is_immutable_ownership_path() {
target="$1"
[ -n "$target" ] || return 1
case ":$PULSE_IMMUTABLE_OWNERSHIP_PATHS:" in
*:"$target":*)
return 0
;;
esac
return 1
}
path_owner() {
stat -c '%u:%g' "$1" 2>/dev/null || stat -f '%u:%g' "$1" 2>/dev/null || echo ""
}
chown_tree_skipping_immutable_paths() {
owner="$1"
root="$2"
if [ ! -e "$root" ]; then
return 0
fi
if [ -z "$PULSE_IMMUTABLE_OWNERSHIP_PATHS" ]; then
chown -R "$owner" "$root"
return 0
fi
find "$root" -mindepth 1 | while IFS= read -r path; do
if is_immutable_ownership_path "$path"; then
echo "Skipping immutable ownership path: $path"
continue
fi
chown "$owner" "$path"
done
chown "$owner" "$root"
}
chown_tree_if_owner_mismatch() {
owner="$1"
uid="$2"
gid="$3"
root="$4"
if [ ! -e "$root" ]; then
return 0
fi
current_owner=$(path_owner "$root")
if [ "$current_owner" = "$uid:$gid" ]; then
return 0
fi
chown -R "$owner" "$root"
}
is_mountpoint_in_mountinfo() {
target="$1"
mountinfo_path="${2:-/proc/self/mountinfo}"
[ -n "$target" ] || return 1
[ -r "$mountinfo_path" ] || return 1
while [ "$target" != "/" ] && [ "${target%/}" != "$target" ]; do
target="${target%/}"
done
check="$target"
while [ -n "$check" ] && [ "$check" != "/" ]; do
while IFS=' ' read -r _ _ _ _ mount_point _; do
if [ "$mount_point" = "$check" ]; then
return 0
fi
done < "$mountinfo_path"
next="${check%/*}"
[ -n "$next" ] || next="/"
if [ "$next" = "$check" ]; then
break
fi
check="$next"
done
return 1
}
path_device_id() {
stat -c '%d' "$1" 2>/dev/null || stat -f '%d' "$1" 2>/dev/null || echo ""
}
warn_if_ephemeral_data_dir() {
target="${1:-/data}"
mountinfo_path="${2:-/proc/self/mountinfo}"
case "${PULSE_SUPPRESS_EPHEMERAL_DATA_WARNING:-}" in
1|true|TRUE|yes|YES)
return 0
;;
esac
if [ ! -e "$target" ]; then
return 0
fi
if [ -r "$mountinfo_path" ]; then
if is_mountpoint_in_mountinfo "$target" "$mountinfo_path"; then
return 0
fi
else
target_dev=$(path_device_id "$target")
root_dev=$(path_device_id "/")
if [ -z "$target_dev" ] || [ -z "$root_dev" ] || [ "$target_dev" != "$root_dev" ]; then
return 0
fi
fi
echo "WARNING: $target is not mounted on a persistent volume. Pulse configuration, authentication, and history will be lost when this container is recreated. Mount a persistent volume (for example: -v pulse-data:/data) or set PULSE_SUPPRESS_EPHEMERAL_DATA_WARNING=true to silence this warning." >&2
}
# Only adjust permissions if running as root
warn_if_ephemeral_data_dir "${PULSE_DATA_DIR:-/data}"
if [ "$(id -u)" = "0" ]; then
echo "Starting with UID: $PUID, GID: $PGID"
# If PUID is 0 (root), don't create a new user, just run as root
if [ "$PUID" = "0" ]; then
echo "Running as root user"
chown_tree_if_owner_mismatch root:root 0 0 /data
chown_tree_skipping_immutable_paths root:root /etc/pulse
exec "$@"
fi
# Check if we need to modify the user/group
current_uid=$(id -u pulse 2>/dev/null || echo "")
current_gid=$(getent group pulse 2>/dev/null | cut -d: -f3 || echo "")
# If user/group don't match, recreate them
if [ "$current_uid" != "$PUID" ] || [ "$current_gid" != "$PGID" ]; then
# Remove existing user and group
deluser pulse 2>/dev/null || true
delgroup pulse 2>/dev/null || true
# Create new group and user with desired IDs
addgroup -g "$PGID" pulse
adduser -D -u "$PUID" -G pulse pulse
fi
# Only mutable data paths are recursively fixed at startup. Immutable image
# paths such as /app and /opt/pulse are owned during the image build; chowning
# them here causes overlayfs copy-up for every tenant container.
chown_tree_if_owner_mismatch pulse:pulse "$PUID" "$PGID" /data
chown_tree_skipping_immutable_paths pulse:pulse /etc/pulse
# Switch to pulse user
exec su-exec pulse "$@"
else
# Not running as root, just exec the command
exec "$@"
fi