Add optional root service install mode
The service installer now supports an explicit root-run mode via FILEMANAGER_SERVICE_RUN_AS_ROOT=1. In root mode it omits the User directive and enables recursive chmod-on-list via service environment settings so access repair works automatically on folder refresh.
@@ -19,9 +19,36 @@ KNOWN_PACKAGE_MANAGERS=(apt-get apt dnf yum pacman zypper apk)
19
19
# Resolve the user who owns the project directory (i.e. the user who cloned it,
20
20
# not root). Falls back to the SUDO_USER env var, then 'pi'.
21
21
RUN_AS="${SUDO_USER:-pi}"
22
SERVICE_RUN_AS_ROOT="${FILEMANAGER_SERVICE_RUN_AS_ROOT:-0}"
23
24
bool_is_true() {
25
case "${1,,}" in
26
1|true|yes|on)
27
return 0
28
;;
29
*)
30
return 1
31
;;
32
esac
33
}
34
35
SERVICE_USER_LINE="User=${RUN_AS}"
36
SERVICE_CAPABILITY_LINES=$'AmbientCapabilities=CAP_NET_BIND_SERVICE\nCapabilityBoundingSet=CAP_NET_BIND_SERVICE'
37
SERVICE_ENV_LINE=""
38
39
if bool_is_true "$SERVICE_RUN_AS_ROOT"; then
40
SERVICE_USER_LINE="# Running as root (User= omitted intentionally)"
41
SERVICE_CAPABILITY_LINES="# Root mode selected: privileged-port capability lines not required"
42
SERVICE_ENV_LINE="Environment=FILEMANAGER_LIST_ACCESS_CHMOD=1"
43
fi
22
44
23
45
echo "[install] Project directory : $SCRIPT_DIR"
24
echo "[install] Service will run as user: $RUN_AS"
46
if bool_is_true "$SERVICE_RUN_AS_ROOT"; then
47
echo "[install] Service will run as root (FILEMANAGER_SERVICE_RUN_AS_ROOT=1)."
48
echo "[install] Enabling recursive chmod-on-folder-list via FILEMANAGER_LIST_ACCESS_CHMOD=1"
49
else
50
echo "[install] Service will run as user: $RUN_AS"
51
fi
25
52
26
53
log_warn() {
27
54
echo "[install][warn] $*" >&2
@@ -141,14 +168,14 @@ After=network.target
141
168
142
169
[Service]
143
170
Type=simple
144
User=${RUN_AS}
171
${SERVICE_USER_LINE}
145
172
WorkingDirectory=${SCRIPT_DIR}
146
173
ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh
147
174
Restart=always
148
175
RestartSec=5
149
176
# Allow binding to privileged ports (< 1024) without running as root.
150
AmbientCapabilities=CAP_NET_BIND_SERVICE
151
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
177
${SERVICE_CAPABILITY_LINES}
178
${SERVICE_ENV_LINE}
152
179
153
180
[Install]
154
181
WantedBy=multi-user.target
@@ -19,9 +19,36 @@ KNOWN_PACKAGE_MANAGERS=(apt-get apt dnf yum pacman zypper apk)19
# Resolve the user who owns the project directory (i.e. the user who cloned it,19
# Resolve the user who owns the project directory (i.e. the user who cloned it,20
# not root). Falls back to the SUDO_USER env var, then 'pi'.20
# not root). Falls back to the SUDO_USER env var, then 'pi'.21
RUN_AS="${SUDO_USER:-pi}"21
RUN_AS="${SUDO_USER:-pi}"22
SERVICE_RUN_AS_ROOT="${FILEMANAGER_SERVICE_RUN_AS_ROOT:-0}"23
24
bool_is_true() {25
case "${1,,}" in26
1|true|yes|on)27
return 028
;;29
*)30
return 131
;;32
esac33
}34
35
SERVICE_USER_LINE="User=${RUN_AS}"36
SERVICE_CAPABILITY_LINES=$'AmbientCapabilities=CAP_NET_BIND_SERVICE\nCapabilityBoundingSet=CAP_NET_BIND_SERVICE'37
SERVICE_ENV_LINE=""38
39
if bool_is_true "$SERVICE_RUN_AS_ROOT"; then40
SERVICE_USER_LINE="# Running as root (User= omitted intentionally)"41
SERVICE_CAPABILITY_LINES="# Root mode selected: privileged-port capability lines not required"42
SERVICE_ENV_LINE="Environment=FILEMANAGER_LIST_ACCESS_CHMOD=1"43
fi22
44
23
echo "[install] Project directory : $SCRIPT_DIR"45
echo "[install] Project directory : $SCRIPT_DIR"24
echo "[install] Service will run as user: $RUN_AS"46
if bool_is_true "$SERVICE_RUN_AS_ROOT"; then47
echo "[install] Service will run as root (FILEMANAGER_SERVICE_RUN_AS_ROOT=1)."48
echo "[install] Enabling recursive chmod-on-folder-list via FILEMANAGER_LIST_ACCESS_CHMOD=1"49
else50
echo "[install] Service will run as user: $RUN_AS"51
fi25
52
26
log_warn() {53
log_warn() {27
echo "[install][warn] $*" >&254
echo "[install][warn] $*" >&2@@ -141,14 +168,14 @@ After=network.target141
168
142
[Service]169
[Service]143
Type=simple170
Type=simple144
User=${RUN_AS}171
${SERVICE_USER_LINE}145
WorkingDirectory=${SCRIPT_DIR}172
WorkingDirectory=${SCRIPT_DIR}146
ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh173
ExecStart=/bin/sh ${SCRIPT_DIR}/start.sh147
Restart=always174
Restart=always148
RestartSec=5175
RestartSec=5149
# Allow binding to privileged ports (< 1024) without running as root.176
# Allow binding to privileged ports (< 1024) without running as root.150
AmbientCapabilities=CAP_NET_BIND_SERVICE177
${SERVICE_CAPABILITY_LINES}151
CapabilityBoundingSet=CAP_NET_BIND_SERVICE178
${SERVICE_ENV_LINE}152
179
153
[Install]180
[Install]154
WantedBy=multi-user.target181
WantedBy=multi-user.target