Files
bookorbit/server/entrypoint.sh
T
neon 3f32d8e828 fix(docker): fail fast on CPUs that lack SSE4.2 support (#118)
On old hardware (e.g. AMD Turion/Phenom, Intel Core 2), sharp's musl binary
crashes with SIGILL during NestJS module init. Combined with bufferLogs: true
and restart: unless-stopped, this produces a silent infinite restart loop -
only the migration success message is ever visible.

Add check_cpu_sse42 to entrypoint.sh: greps /proc/cpuinfo for sse4_2, exits
with a clear error if absent, no-op when /proc/cpuinfo is unavailable. Also
add a System Requirements section to README documenting the SSE4.2 minimum.

Closes #116
2026-05-27 14:27:12 -06:00

129 lines
3.7 KiB
Bash

#!/bin/sh
set -e
log() {
echo "BookOrbit startup: $*" >&2
}
ensure_dir() {
dir="$1"
if ! mkdir -p "$dir"; then
log "cannot create $dir. Grant the host directory write access, or set PUID/PGID to a user that can write there."
exit 1
fi
}
is_managed_data_path() {
case "$1" in
/data | /data/* | /tmp | /tmp/*) return 0 ;;
*) return 1 ;;
esac
}
fix_owner() {
dir="$1"
if [ "${BOOKORBIT_FIX_PERMISSIONS:-true}" != "true" ]; then
return 0
fi
if ! is_managed_data_path "$dir"; then
log "skipping ownership fix for $dir because it is outside /data."
return 0
fi
if ! chown -R "$APP_UID:$APP_GID" "$dir"; then
log "could not update ownership for $dir. Startup will continue only if UID:GID $APP_UID:$APP_GID can already write there."
fi
}
check_writable_as_user() {
dir="$1"
if ! su-exec "$APP_UID:$APP_GID" sh -c 'touch "$1/.bookorbit-permission-test" && rm -f "$1/.bookorbit-permission-test"' sh "$dir"; then
log "$dir is not writable by UID:GID $APP_UID:$APP_GID."
log "Set PUID/PGID to a NAS user with write access, or fix the host folder ownership/ACLs."
exit 1
fi
}
check_writable_current_user() {
dir="$1"
if ! touch "$dir/.bookorbit-permission-test" || ! rm -f "$dir/.bookorbit-permission-test"; then
log "$dir is not writable by the current container user $(id -u):$(id -g)."
log "Remove the compose user override, set PUID/PGID, or fix the host folder ownership/ACLs."
exit 1
fi
}
check_cpu_sse42() {
if [ ! -f /proc/cpuinfo ]; then
return 0
fi
if ! grep -q 'sse4_2' /proc/cpuinfo; then
log "CPU does not support SSE4.2 instructions."
log "The image processing library (sharp) requires a CPU with SSE4.2 support."
log "Minimum supported CPUs: Intel Core (Nehalem, 2008 or newer) or AMD (Bulldozer, 2011 or newer)."
log "Incompatible CPUs include: Intel Core 2 series, AMD Phenom, AMD Turion, and other pre-2011 AMD chips."
log "Run 'docker stop <container-name>' to stop this container, then upgrade your hardware."
exit 1
fi
}
if [ -z "$DATABASE_URL" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/${POSTGRES_DB}"
fi
export APP_DATA_PATH="${APP_DATA_PATH:-/data}"
book_bucket_path="$APP_DATA_PATH/book-bucket"
export HOME="${HOME:-/tmp}"
APP_UID="${PUID:-1000}"
APP_GID="${PGID:-1000}"
NODE_MAX_OLD_SPACE_SIZE="${NODE_MAX_OLD_SPACE_SIZE:-2048}"
case "$APP_UID" in
'' | *[!0-9]*)
log "PUID must be a numeric UID, got '$APP_UID'."
exit 1
;;
esac
case "$APP_GID" in
'' | *[!0-9]*)
log "PGID must be a numeric GID, got '$APP_GID'."
exit 1
;;
esac
case "$NODE_MAX_OLD_SPACE_SIZE" in
'' | *[!0-9]*)
log "NODE_MAX_OLD_SPACE_SIZE must be a numeric MB value, got '$NODE_MAX_OLD_SPACE_SIZE'."
exit 1
;;
esac
if [ "$NODE_MAX_OLD_SPACE_SIZE" -lt 1 ]; then
log "NODE_MAX_OLD_SPACE_SIZE must be greater than 0."
exit 1
fi
export NODE_MAX_OLD_SPACE_SIZE
check_cpu_sse42
ensure_dir "$APP_DATA_PATH"
ensure_dir "$APP_DATA_PATH/covers"
ensure_dir "$book_bucket_path"
if [ "$(id -u)" = "0" ]; then
fix_owner "$APP_DATA_PATH"
fix_owner "$book_bucket_path"
check_writable_as_user "$APP_DATA_PATH"
check_writable_as_user "$APP_DATA_PATH/covers"
check_writable_as_user "$book_bucket_path"
exec su-exec "$APP_UID:$APP_GID" sh -c 'node dist/scripts/migrate.js && exec node --max-old-space-size="$NODE_MAX_OLD_SPACE_SIZE" dist/main.js'
fi
check_writable_current_user "$APP_DATA_PATH"
check_writable_current_user "$APP_DATA_PATH/covers"
check_writable_current_user "$book_bucket_path"
node dist/scripts/migrate.js
exec node --max-old-space-size="$NODE_MAX_OLD_SPACE_SIZE" dist/main.js