Skip to content

fix(volumectl): protect move-volume from backups - #1426

Merged
DavidePrincipi merged 4 commits into
mainfrom
bug-8259
Dec 2, 2026
Merged

DavidePrincipi merged 4 commits into
mainfrom
bug-8259

Conversation

@DavidePrincipi

@DavidePrincipi DavidePrincipi commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

QA of volumectl move-volume found a failure in the "busy application" test: when a backup of the application was running, the final step of the move stopped the application anyway, interrupted the backup and moved the volume even without --force. The backup runs outside the application agent, so the existing busy check did not see it. Also, while the application was stopped, any command run as the application user (for example a backup retry, or a metrics collection, through runagent) started the whole application again, in the middle of the move. NethServer/dev#8259

Now the move refuses to start while a backup or an application update is running, unless --force is given. During the move, backups and updates are postponed, and the application stays stopped until the move ends, whatever command runs meanwhile. If the move is interrupted by a closed terminal (for example a dropped SSH connection) or by SIGTERM, it is rolled back like with Ctrl-C. Outside a move nothing changes, except that backups and metrics no longer run for an application whose user session is stopped.

  • The move takes the lock used by backups and application updates, and keeps it until the end, also with --force after it interrupts the running backup.
  • A temporary service unit, volumectl-rundir-*, holds the application session during the move: a new systemd drop-in, [email protected]/40-hold.conf, skips any start of the application user manager while that unit is active. Podman now always runs through runagent, and the move is refused if the drop-in is not loaded.
  • volumectl handles SIGHUP and SIGTERM like Ctrl-C: the original volume is restored and the application is started again.
  • The node run-backup and refresh-module-metrics commands skip an application whose user session is stopped: its backup is retried later, its metrics keep their last value.

Known limits: the code still has a few small race windows, on purpose, to keep the change simple. For example, a command run as the application user exactly while the move stops or starts the application, or a signal received at the very edges of the rollback. In the worst case the move fails and is rolled back, or the downtime is a few seconds longer: the volume data is not at risk.

How to test

Use a node with an additional disk and a rootless application with a data volume, as in the QA steps of NethServer/dev#8259, on Rocky Linux 9 and on Debian 13.

  • Start a backup of the application, then run the move without --sync: the command refuses and the backup completes. Run it again with --force: the backup is interrupted, the move completes, and a backup started during the move is retried later.
  • Run the move while the node runs backups or metrics collections: the application starts only once, at the end of the move (journalctl shows any earlier start attempt as "skipped, unmet condition check").
  • Close the SSH connection, or send SIGTERM to volumectl, while it copies the last changes: the volume stays at its original location and the application runs again.
  • Repeat the other QA steps (move to the additional disk and back, Ctrl-C, error messages): the behavior is unchanged.

Generated by Claude Code:claude-opus-5-5 with nethserver-pr

The finalize step of move-volume checked only the processes of the
module agent service. A module backup is started by the node agent, so
it was not detected: finalize stopped the user session, the backup was
killed and the volume was moved without --force.

Take the module-backup lock, shared with module-backup and the
update-module scripts, and hold it until the move ends. If the lock is
busy, refuse unless --force is given: then the lock holder is killed
with the user session, and the lock is taken right after. A backup
that starts during the move exits at once and the node run-backup
command retries it later.

Assisted-by: Claude Code:claude-sonnet-5-5
Assisted-by: Claude Code:claude-opus-5-5
@DavidePrincipi DavidePrincipi self-assigned this Nov 20, 2026
@DavidePrincipi
DavidePrincipi requested review from gsanchietti and removed request for gsanchietti November 20, 2026 18:22
@DavidePrincipi
DavidePrincipi marked this pull request as draft November 23, 2026 16:26
During the finalize step of move-volume, a PAM session of the module
user, like the one opened by runagent -m for a backup or for metrics,
started the user manager again while the volume was being moved.

Hold the user session with a transient unit for the whole finalize
step. A new [email protected] drop-in skips the start of the user manager,
and of [email protected], while the RuntimeDirectory of that unit
exists under /run/nethserver/user-hold. It works with any session
class and systemd version; the hold ends when the unit stops, or at
reboot.

The same unit keeps the runtime directory of the user, by requiring
[email protected], instead of recreating it by hand.

As the hold covers any session of the module user, Podman now runs
through runagent also while the session is stopped, instead of
runuser -u. The move is refused if systemd has not loaded the drop-in.

Assisted-by: Claude Code:claude-opus-5-5
Only Ctrl-C rolled back an interrupted finalize step. A closed
terminal, like a dropped SSH connection, or SIGTERM killed volumectl
at once: the application stayed stopped, held by the transient unit,
and the volume could be left half moved.

Handle SIGHUP and SIGTERM like SIGINT, and ignore write errors on the
closed terminal, so the original volume is restored and the
application is started again.

Assisted-by: Claude Code:claude-opus-5-5
run-backup and refresh-module-metrics run a command of each module
with runagent -m. If the user session of a rootless module is stopped,
for example while volumectl move-volume moves its data, the command
runs without the user manager and the module may be half moved.

Check the user session first. The backup of that module is retried
later, like a module whose backup is already running. Its metrics keep
their last output.

Assisted-by: Claude Code:claude-opus-5-5
@DavidePrincipi
DavidePrincipi marked this pull request as ready for review December 1, 2026 16:08
Comment on lines +313 to +318
lock_path = self.home / ".config/state/.module-backup.lock"
fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC, 0o644)
# module-backup runs as the module user: it must be able to
# open the file if we create it
os.fchown(fd, self.uid, self.gid)
self.backup_lock_fd = fd

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lock_backup() runs as root, but the lock file lives under the module home, which the module user controls. If the module replaces .module-backup.lock (or .config/state) with a symlink to /etc/shadow, root opens the target and fchown() gives it to the module UID: privilege escalation from module to root.

Open the file as the module user instead: a symlink then only reaches files the module user can already write, the file is created with the right owner, and fchown() is not needed. O_NONBLOCK avoids hanging on a FIFO. Not tested on a node yet.

Suggested change
lock_path = self.home / ".config/state/.module-backup.lock"
fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC, 0o644)
# module-backup runs as the module user: it must be able to
# open the file if we create it
os.fchown(fd, self.uid, self.gid)
self.backup_lock_fd = fd
lock_path = self.home / ".config/state/.module-backup.lock"
# Open the file as the module user: its home is not trusted.
# The file is created with the right owner, for module-backup
groups = os.getgroups()
os.setgroups([])
os.setegid(self.gid)
os.seteuid(self.uid)
try:
fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_NONBLOCK | os.O_CLOEXEC, 0o644)
finally:
os.seteuid(0)
os.setegid(0)
os.setgroups(groups)
self.backup_lock_fd = fd

Comment on lines +326 to +339
"""Stop the user manager of the module, and keep its runtime
directory: rootless Podman runs without the user manager. A
transient unit requires [email protected], otherwise the
directory is removed with the user manager on some distros.
While the unit is active, its own runtime directory keeps the
user manager stopped, see [email protected]/40-hold.conf: a PAM
session of the module user, like the one opened by runagent
for a backup, would start it again."""
_log(f"stopping the user session of {self.mid}")
# Stop the leftover of an interrupted run, if any
subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL)
subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}",
"--service-type=oneshot", "--property=RemainAfterExit=yes",
f"--property=Requires=user-runtime-dir@{self.uid}.service",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Requires=, cleanup_runtime() restarting user-runtime-dir@UID also restarts the holder unit. Its RuntimeDirectory /run/nethserver/user-hold/UID is removed and recreated, so for a moment 40-hold.conf does not block user@UID. A runagent -m MODULE ... (PAM session) in that window starts the user manager, and the module services can write to the volume while it is moved.

Wants= does not propagate the restart, and still keeps user-runtime-dir@ needed (StopWhenUnneeded=), so /run/user/UID stays. Checked with user-level units that copy this setup: with Requires= the hold directory is gone right after the restart, with Wants= it is untouched; in both cases the runtime-dir unit stops when the holder stops. Not tested on a node yet.

Suggested change
"""Stop the user manager of the module, and keep its runtime
directory: rootless Podman runs without the user manager. A
transient unit requires [email protected], otherwise the
directory is removed with the user manager on some distros.
While the unit is active, its own runtime directory keeps the
user manager stopped, see [email protected]/40-hold.conf: a PAM
session of the module user, like the one opened by runagent
for a backup, would start it again."""
_log(f"stopping the user session of {self.mid}")
# Stop the leftover of an interrupted run, if any
subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL)
subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}",
"--service-type=oneshot", "--property=RemainAfterExit=yes",
f"--property=Requires=user-runtime-dir@{self.uid}.service",
"""Stop the user manager of the module, and keep its runtime
directory: rootless Podman runs without the user manager. A
transient unit wants [email protected], otherwise the
directory is removed with the user manager on some distros.
Wants=, not Requires=: a restart of user-runtime-dir@ must not
restart the unit, or its hold directory disappears for a moment.
While the unit is active, its own runtime directory keeps the
user manager stopped, see [email protected]/40-hold.conf: a PAM
session of the module user, like the one opened by runagent
for a backup, would start it again."""
_log(f"stopping the user session of {self.mid}")
# Stop the leftover of an interrupted run, if any
subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL)
subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}",
"--service-type=oneshot", "--property=RemainAfterExit=yes",
f"--property=Wants=user-runtime-dir@{self.uid}.service",

@gsanchietti gsanchietti left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it looks good, IMO the suggestion about the lock file is could be good, but feel free to ignore both agent commands

@DavidePrincipi
DavidePrincipi merged commit 596597a into main Dec 2, 2026
4 checks passed
@DavidePrincipi
DavidePrincipi deleted the bug-8259 branch December 2, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants