LibrePortal/scripts/storage/storage_move.sh
librelad 26e98698d8 feat(storage): per-app placement, app move, and the READONLY marker
Phase 2 and 4 of docs/roadmap/storage-locations.md. Apps can now be
placed on a location and moved between them.

CFG_<APP>_STORAGE lands in all 37 app templates, holding a location NAME
rather than a path: names survive a migrate to a host with different
disks, paths do not. The 11 infrastructure apps that other apps reach by
literal path (traefik, prometheus, grafana, adguard, gluetun, crowdsec,
headscale, dashy, pihole, unbound, wireguard) are pinned. libreportal
itself never gets the key — it is pinned structurally by webuiDir.

Pinning needed no second config key. "Pinned" is not a fact about a value,
it is a statement about whether the field may be edited, so it goes in the
comment beside **ADVANCED** and **DEV** as **READONLY**, and the field
factory renders those disabled. That marker earns its keep beyond this
feature: derived fields already warned in prose that editing them does
nothing (crowdsec.config:72) next to a perfectly editable input.

storage_app_config.sh keeps the comment honest — it carries the resolved
path for hand-recovery and regenerates the dropdown from the registry, but
only writes when something actually changed, since the app .config is
user-editable and lives in the container-owned tree.

app move stops the app (a live copy of a running Postgres is a corrupt
copy), snapshots it, copies, verifies, and only then removes the source.
The copy runs in libreportal-ownership because it must: app data holds
rootless sub-UID files the manager can neither read nor recreate.
Verified against two real ext4 filesystems that a cross-device move
preserves uid 231141 and the payload, and that the source survives every
refusal path — unregistered destination, the WebUI app, a traversal in the
app name, and an occupied destination.

Task titles registered in both tables, with a specific rule so a move
renders as "Nextcloud - Move to bigdisk" rather than the generic fallback
dropping the destination. lp-task-names could not be run to confirm — it
borrows the WebUI container's node and no containers are running.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-24 20:37:35 +01:00

134 lines
5.4 KiB
Bash

#!/bin/bash
# Move an installed app's data to a different storage location.
#
# The ordering here is the whole design. Data must be quiescent before it is
# copied (a live copy of a running Postgres is a corrupt copy), the source must
# survive until the destination is verified, and the config must not claim the
# new location until the data is actually there.
#
# 1. resolve + refuse the obvious (same location, not installed, unavailable)
# 2. space check with headroom
# 3. compose down
# 4. pre-move snapshot (default on)
# 5. copy as root — app data holds rootless sub-UID files the manager can
# neither read nor recreate
# 6. verify, THEN remove the source. Never mv semantics that can half-delete
# 7. repoint config + index, bring back up, health-check
#
# Rollback: before step 6 the source is untouched, so failure just restarts the
# app where it was. After step 6 the destination is complete, so failure leaves
# it there and the config is repointed anyway.
storageMoveApp()
{
local app="$1"
local want="$2"
local opts="${3:-}"
if [[ -z "$app" || -z "$want" ]]; then
isError "Usage: storageMoveApp <app> <location> [--no-pre-backup]"
return 1
fi
isHeader "Move $app to storage location '$want'"
# ---- 1. resolve ---------------------------------------------------------
local src_dir dest_root dest_dir
if ! src_dir=$(appDir "$app"); then
isError "$app is on a storage location that is not mounted — mount it first."
return 1
fi
if [[ ! -d "$src_dir" ]]; then
isError "$app is not installed ($src_dir does not exist)."
return 1
fi
if ! dest_root=$(storageLocationPath "$want"); then
isError "No such storage location: $want"
isNotice "Known locations: $(storageList 2>/dev/null | awk 'NR>2{print $2}' | paste -sd, -)"
return 1
fi
if ! storageRootAvailable "$dest_root"; then
isError "Storage location '$want' ($dest_root) is not mounted."
return 1
fi
dest_dir="${dest_root%/}/$app"
if [[ "${src_dir%/}" == "${dest_dir%/}" ]]; then
isNotice "$app is already on '$want' — nothing to do."
return 0
fi
if [[ -e "$dest_dir" ]]; then
isError "Refusing to move — '$dest_dir' already exists."
return 1
fi
# A pinned app must not move: other apps reach it by literal path.
local pin_key="CFG_${app^^}_STORAGE"
if runCfgOp grep -qE "^${pin_key}=.*\*\*READONLY\*\*" "$src_dir/$app.config" 2>/dev/null; then
isError "$app is pinned to the primary location — other apps reference it by path."
return 1
fi
# ---- 2. space, with headroom -------------------------------------------
local need_kb avail_kb
need_kb=$(runFileOp du -sk "$src_dir" 2>/dev/null | awk '{print $1}')
avail_kb=$(df -Pk "$dest_root" 2>/dev/null | awk 'NR==2 {print $4}')
if [[ -n "$need_kb" && -n "$avail_kb" ]]; then
local need_hdr=$(( need_kb + need_kb / 10 ))
if (( avail_kb < need_hdr )); then
isError "Not enough space on '$want': need ~$((need_hdr / 1024)) MiB (10% headroom), have $((avail_kb / 1024)) MiB."
return 1
fi
isNotice "Moving $((need_kb / 1024)) MiB; $((avail_kb / 1024)) MiB free at the destination."
if (( need_kb > 5242880 )); then
isNotice "This is a large app — the copy can take a while and $app stays down until it finishes."
fi
fi
# ---- 3. stop ------------------------------------------------------------
isNotice "Stopping $app — data must be quiescent before it is copied."
dockerComposeDown "$app" || true
# ---- 4. pre-move snapshot ----------------------------------------------
if [[ "$opts" != *--no-pre-backup* ]] && declare -f migratePreBackupDestination >/dev/null 2>&1; then
local bidx
bidx=$(resticEnabledLocations 2>/dev/null | head -1)
if [[ -n "$bidx" ]]; then
isNotice "Taking a safety snapshot before moving."
migratePreBackupDestination "$app" "$bidx" || \
isNotice "Pre-move snapshot failed — continuing (the source is not deleted until the copy verifies)."
else
isNotice "No backup location enabled — skipping the safety snapshot."
fi
fi
# ---- 5/6. copy, verify, then remove the source --------------------------
isNotice "Copying $src_dir -> $dest_dir"
if ! runOwnership app-move "$app" "$dest_root"; then
isError "Copy failed — $app has not been moved. Restarting it where it was."
dockerComposeUp "$app" || true
return 1
fi
# ---- 7. repoint and restart --------------------------------------------
updateConfigOption "$pin_key" "$(storageLocationName "$dest_root")" "$dest_dir/$app.config" >/dev/null 2>&1 || true
storageIndexSet "$app" "$dest_root"
storageCacheReset
local now
now=$(appDir "$app")
if [[ "${now%/}" != "${dest_dir%/}" ]]; then
isError "After the move $app resolves to '$now', not '$dest_dir'. Not starting it — investigate before running anything else."
return 1
fi
declare -f storageSyncAppComment >/dev/null 2>&1 && storageSyncAppComment "$app"
isNotice "Starting $app on its new location."
dockerComposeUpdateAndStartApp "$app" || true
dockerComposeUp "$app" || true
isSuccessful "$app now lives at $dest_dir"
return 0
}