LibrePortal/scripts/storage/storage_app_config.sh
librelad 56cd6e7fa4 storage: choose which drive an app installs onto
The resolver already supported per-app placement — CFG_<APP>_STORAGE names a
location and appDir sends data, compose and config there — and 37 of 39 app
templates ship the field. What was missing was choosing AT INSTALL TIME. The
only routes were editing a config by hand before installing, or installing onto
the default disk and then `app move`ing it, which copies the data twice.

    libreportal app install <app> --storage=<location>

and the App Center's existing storage dropdown, which travels inside
config_variables. Both resolve to one answer in storageChoiceFor, so there is a
single code path.

Ordering is the whole difficulty, and getting it wrong is quiet. installApp
copies the app template into appDir(), sources it, and later applies the form
overrides. The choice has to be live before the copy (or the directory is
created on the wrong disk), written into the config before the source (or the
template's "default" wins and every later appDir in that process returns the
primary root), and folded into config_variables (or the override pass writes
"default" back). Miss any one and the directory and its config disagree — which
resolves correctly only until something sources the config.

Refuses an unknown or unmounted location, an existing directory, and an app
whose template marks the field **READONLY** (fixed to the primary root because
other apps reach it by literal path — storageMoveApp already refuses to move
those, and installing one elsewhere is the same violation from the other end).

Three shipped bugs found making this work:

  * updateConfigOption chose its write helper by comparing the path against
    $containers_dir — the PRIMARY root only — so an app on any other registered
    location took the manager branch and `sed -i` failed with exactly the
    permission error the comment above that code describes. `app move` writes
    the new location with `|| true`, so it reported a successful move while
    leaving the config naming the old disk.
  * storageLocationName resolved a location's name only from an in-scope
    CFG_STORAGE_LOC_<id>_NAME, falling back to the bare id. That name is the
    value CFG_<APP>_STORAGE is set to, so the generated dropdown offered
    "location-1" as both label and value — a choice that does not resolve. Read
    it from the location's config when the variable is not in scope.
  * storageSyncAllAppComments was written for "the regen path" and never wired
    into one. Every CFG_<APP>_STORAGE option list was frozen at install time, so
    adding a drive did not make it selectable anywhere. Called from the storage
    generator now, which runs exactly when those lists go stale — and extended
    to app TEMPLATES, since an app not installed yet is precisely the one whose
    install form needs to show which drives exist.

Verified on a live install with three locations: linkding and authelia on disk1,
ipinfo on disk2, fourteen on the default root, each config naming its own drive.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 07:22:11 +01:00

160 lines
5.8 KiB
Bash

#!/bin/bash
# Keeping CFG_<APP>_STORAGE's comment truthful.
#
# The value is a location NAME, because names survive a migrate to a host whose
# disks are laid out differently and paths do not. But a name alone is useless to
# someone reading the config file at 2am with no tooling, so the RESOLVED PATH
# rides in the field's comment:
#
# CFG_NEXTCLOUD_STORAGE=bigdisk # Storage Location - Currently at /mnt/bigdisk/apps/nextcloud [default:Primary|bigdisk:Big disk]
#
# Two rules keep that from becoming a liability:
#
# 1. It is a BREADCRUMB, never a source of truth. Nothing reads it to decide
# anything — appDir resolves by discovery. A stale one is cosmetic.
# 2. It is written ONLY when it changes. The app .config is user-editable and
# lives in the container-owned tree, so an unconditional rewrite on every
# regen is both file churn and a runFileOp per app per pass. Compare first.
# Rebuild the "[default:Primary|<name>:<label>|…]" option list from the registry,
# so a dropdown reflects locations added since the app was installed.
_storageOptionList()
{
local out="default:Primary ($(primaryRoot))"
local id state path name_var name
while IFS=$'\t' read -r id state path; do
[[ -z "$id" ]] && continue
# One resolver, so the label and the value it writes cannot disagree.
name=$(storageLocationName "$path" 2>/dev/null)
[[ -n "$name" ]] || name="$id"
# An unavailable location stays selectable and says so, rather than
# vanishing from the list and looking like it was deleted.
if [[ "$state" == "ok" ]]; then
out+="|${name}:${name} (${path})"
else
out+="|${name}:${name} (${path}) — not mounted"
fi
done < <(runStorage verify 2>/dev/null)
printf '%s' "$out"
}
# Rewrite one app's CFG_<APP>_STORAGE comment. No-op when nothing changed.
storageSyncAppComment()
{
local app="$1"
[[ -n "$app" ]] || return 0
local dir cfg key up
up="${app^^}"
key="CFG_${up}_STORAGE"
dir=$(appDir "$app") || return 0 # unavailable: leave the file alone
cfg="$dir/$app.config"
[[ -f "$cfg" ]] || return 0
local line
line=$(runCfgOp grep -m1 -E "^${key}=" "$cfg" 2>/dev/null) || return 0
[[ -n "$line" ]] || return 0
# Preserve the value and the READONLY marker; regenerate the rest.
local value="${line#*=}"
value="${value%%#*}"
value="${value//$'\r'/}"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
value="${value%\"}"; value="${value#\"}"
local readonly_marker=""
[[ "$line" == *'**READONLY**'* ]] && readonly_marker=" **READONLY**"
local desc
if [[ -n "$readonly_marker" ]]; then
desc="Storage Location - Fixed: other apps reference $app by path. Currently at $dir"
else
desc="Storage Location - Currently at $dir [$(_storageOptionList)]"
fi
local want="${key}=${value} # ${desc}${readonly_marker}"
[[ "$line" == "$want" ]] && return 0 # unchanged: no write, no churn
local escaped_key="${key}"
local tmp; tmp=$(mktemp) || return 1
runCfgOp cat "$cfg" 2>/dev/null > "$tmp" || { rm -f "$tmp"; return 1; }
awk -v k="^${escaped_key}=" -v repl="$want" '
$0 ~ k && !done { print repl; done=1; next }
{ print }
' "$tmp" | runCfgOp tee "$cfg" >/dev/null
rm -f "$tmp"
return 0
}
# The same option list, but written into an app TEMPLATE.
#
# The App Center's install form reads an uninstalled app's fields from its
# template under install_containers_dir, and templates ship
# "[default:Primary]" — so the storage dropdown offered exactly one choice and
# the drives you could actually install onto were invisible until after the app
# was installed, which is the wrong way round. The value is preserved; only the
# option list is regenerated.
storageSyncTemplateComment()
{
local app="$1"
[[ -n "$app" ]] || return 0
local cfg="${install_containers_dir%/}/$app/$app.config"
[[ -f "$cfg" ]] || return 0
local key="CFG_${app^^}_STORAGE" line
line=$(runInstallOp grep -m1 -E "^${key}=" "$cfg" 2>/dev/null) || return 0
[[ -n "$line" ]] || return 0
# Pinned to the primary root on purpose — nothing to offer.
[[ "$line" == *'**READONLY**'* ]] && return 0
local value="${line#*=}"
value="${value%%#*}"
value="${value//$'\r'/}"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
value="${value%\"}"; value="${value#\"}"
local want="${key}=${value} # Storage Location - Which disk holds this app's data [$(_storageOptionList)]"
[[ "$line" == "$want" ]] && return 0
local tmp; tmp=$(mktemp) || return 1
runInstallOp cat "$cfg" 2>/dev/null > "$tmp" || { rm -f "$tmp"; return 1; }
awk -v k="^${key}=" -v repl="$want" '
$0 ~ k && !done { print repl; done=1; next }
{ print }
' "$tmp" | runInstallWrite "$cfg" >/dev/null
rm -f "$tmp"
return 0
}
# Every app template, so the install form offers the real drives.
storageSyncAllTemplateComments()
{
local d app
[[ -d "${install_containers_dir%/}" ]] || return 0
for d in "${install_containers_dir%/}"/*/; do
app=$(basename "$d")
[[ "$app" == "libreportal" || "$app" == "libreportal_catalog" ]] && continue
storageSyncTemplateComment "$app"
done
return 0
}
# Sync every installed app. Called from the regen path; cheap because each app
# short-circuits unless its resolved path or the option list actually changed.
storageSyncAllAppComments()
{
local app
while IFS= read -r app; do
[[ -z "$app" || "$app" == "libreportal" ]] && continue
storageSyncAppComment "$app"
done < <(storageApps)
# Templates too: an app not installed yet is exactly the one whose install
# form needs to show which drives exist.
storageSyncAllTemplateComments
return 0
}