Skip to content

scripts: add criu-move-mount and runc-action-add-mounts for restore-time bind mounts - #2

Merged
wangixt merged 2 commits into
mainfrom
cursor/-bc-3043cd3f-2fe7-4550-a5df-e38221726e78-ef34
Mar 25, 2026
Merged

scripts: add criu-move-mount and runc-action-add-mounts for restore-time bind mounts#2
wangixt merged 2 commits into
mainfrom
cursor/-bc-3043cd3f-2fe7-4550-a5df-e38221726e78-ef34

Conversation

@wangixt

@wangixt wangixt commented Mar 21, 2026

Copy link
Copy Markdown
Owner

What

Add support for adding new bind mounts into a container's mount namespace during CRIU restore, using the Linux 5.2+ new mount API (open_tree(OPEN_TREE_CLONE) + move_mount()).

How

Two new files are introduced in scripts/:

criu-move-mount.c

A C program that handles the complete workflow:

  • Phase filtering: only acts during the pre-resume CRIU action phase (mount tree is restored, processes not yet resumed)
  • Mount rule parsing: reads the CRIU_ADD_MOUNTS environment variable containing semicolon-separated mount rules
  • Cross-namespace mounts: for each rule, performs:
    1. open_tree(src, OPEN_TREE_CLONE) in the host mount namespace to clone the source mount
    2. setns(container_mntns) to switch to the container's mount namespace
    3. move_mount(treefd -> dst) to attach the cloned mount inside the container
    4. Optional remount(MS_RDONLY) for read-only mounts
  • Multiple mounts: handles multiple rules in a single invocation by saving the host mount namespace fd and switching back before each new mount
  • Debug logging: logs every step to /tmp/criu-move-mount.log by default (configurable via CRIU_MOVE_MOUNT_LOG, set to "" to disable)
  • Error tolerance: individual mount failures are logged but do not cause a non-zero exit, matching the original shell script's || true behaviour

runc-action-add-mounts.sh

A thin wrapper script designed to be passed to CRIU as --action-script. It locates and execs the criu-move-mount binary. All logic resides in the C program.

Mount rule format

Rules are specified exclusively via the CRIU_ADD_MOUNTS environment variable:

CRIU_ADD_MOUNTS="src=/host/path,dst=/container/path[,options=rbind:ro];..."

Example:

CRIU_ADD_MOUNTS="src=/home/b,dst=/home/b,options=rbind:rw;src=/data,dst=/data,options=rbind:ro"

Debugging

Check /tmp/criu-move-mount.log (or the path set via CRIU_MOVE_MOUNT_LOG) for detailed logs including:

  • Which CRIU action phase was invoked
  • The parsed PID and mount rules
  • Each mount attempt with success/failure and errno details

Usage

# Compile
gcc -O2 -o criu-move-mount criu-move-mount.c

# Restore with additional mounts
CRIU_ADD_MOUNTS="src=/home/b,dst=/home/b,options=rbind:rw" \
  criu restore --action-script /path/to/runc-action-add-mounts.sh
Open in Web Open in Cursor 

cursoragent and others added 2 commits March 21, 2026 09:24
…ime bind mounts

Add two new files for adding bind mounts into a container's mount
namespace during CRIU restore:

- criu-move-mount.c: C program that handles the complete workflow:
  * Phase filtering (only acts during pre-resume)
  * Reads mount rules from CRIU_ADD_MOUNTS environment variable
  * Parses semicolon-separated mount rules with comma-separated fields
  * Performs cross-namespace bind mounts using open_tree(OPEN_TREE_CLONE)
    + setns() + move_mount() (Linux 5.2+ new mount API)
  * Supports recursive bind (rbind) and read-only (ro) options
  * Handles multiple mount rules in a single invocation by switching
    between host and container mount namespaces

- runc-action-add-mounts.sh: Thin wrapper script that locates and
  exec's the criu-move-mount binary. Designed to be passed to CRIU as
  --action-script.

Mount rules are specified exclusively via the CRIU_ADD_MOUNTS
environment variable using this format:
  src=/host/path,dst=/container/path[,options=rbind:ro]
Multiple rules are separated by semicolons.

Co-authored-by: wangixt <wangixt@users.noreply.github.com>
Two changes to help diagnose failures:

1. Add file-based debug logging (default: /tmp/criu-move-mount.log).
   Every step (phase check, env read, open_tree, setns, move_mount)
   is logged with errno details.  The log path can be overridden
   via CRIU_MOVE_MOUNT_LOG or disabled by setting it to empty.

2. Fix exit code: individual mount failures no longer cause a non-zero
   exit.  This matches the original shell script's '|| true' behaviour
   and prevents a single bad rule from aborting the entire CRIU restore.

Co-authored-by: wangixt <wangixt@users.noreply.github.com>
@wangixt

wangixt commented Mar 25, 2026

Copy link
Copy Markdown
Owner Author

add mount

@wangixt
wangixt marked this pull request as ready for review March 25, 2026 07:34
@wangixt
wangixt merged commit 9f72ec7 into main Mar 25, 2026
0 of 24 checks passed
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