Skip to content

Mount a tmpfs if requested#635

Open
mz-pdm wants to merge 1 commit intocontainers:mainfrom
mz-pdm:pdm-tmpfs
Open

Mount a tmpfs if requested#635
mz-pdm wants to merge 1 commit intocontainers:mainfrom
mz-pdm:pdm-tmpfs

Conversation

@mz-pdm
Copy link
Copy Markdown
Collaborator

@mz-pdm mz-pdm commented Apr 14, 2026

Podman's --tmpfs command line option is currently ignored by libkrun. This means the host tmpfs, as arranged by crun, is auto-mounted as a virtiofs when first accessing the given directory. Having such a tmpfs adds some overhead when accessing it and reduces the VM isolation. Let's add a limited support for mount the given directory natively.

This is implemented by looking at `mounts' in /.krun_config.json and mounting the first matching tmpfs directory there, if any. "Matching" means it's a tmpfs entry and the directory is not yet mounted. The mount options are ignored.

We refuse to mount already mounted directories; this is because tmpfs mounts can be requested by Podman itself also for other directories, for example /dev, which libkrun already mounts itself. The mount check is implemented by looking at /proc/mounts; using stat/lstat would result in triggering the Podman's automount.

The mount directory must already exist, which is the case with Podman's --tmpfs.

This commit implements just the most basic easy part and is not fully compliant. Possible future improvements are:

  • Copy the files from the original directory to the tmpfs. This is requested by default from Podman (tmpcopyup option). It's not implemented right now because the copying is non-trivial and it's better to leave that for Rust implementation of init.

  • Create the mount directory if it doesn't exist.

  • Honour the other mount options.

  • Honour other mounts from the configuration.

Fixes: #515

@mz-pdm
Copy link
Copy Markdown
Collaborator Author

mz-pdm commented Apr 14, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for parsing and mounting tmpfs filesystems defined in a JSON configuration. It introduces helper functions for JSON navigation and mount point validation. Feedback suggests enhancing the JSON parser's robustness, eliminating redundant helper functions in favor of existing ones, and ensuring error output is sent to stderr.

Comment thread init/init.c
Comment thread init/init.c Outdated
Comment thread init/init.c Outdated
Comment thread init/init.c Outdated
Comment thread init/init.c Outdated
Comment thread init/init.c Outdated
@slp slp added the 1.x label Apr 14, 2026
@mz-pdm mz-pdm marked this pull request as draft April 14, 2026 11:37
@mz-pdm mz-pdm force-pushed the pdm-tmpfs branch 2 times, most recently from 9f4cf40 to 44df048 Compare April 21, 2026 13:53
@mz-pdm
Copy link
Copy Markdown
Collaborator Author

mz-pdm commented Apr 21, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces logic to parse and mount tmpfs volumes from a configuration file in the init process. The changes include new helper functions for JSON parsing and mount point verification. Several critical issues were identified during the review: the JSON token indexing logic in config_parse_mounts is incorrect and could lead to crashes, the sscanf approach for parsing /proc/mounts fails to handle escaped characters, and there is a memory leak for the config_tmpfs string. Additionally, it is recommended to add the MS_NODEV flag when mounting the tmpfs for better security.

Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c Outdated
Comment thread init/init.c
Comment on lines +1436 to +1459
config_parse_file(&config_argv, &config_workdir, &config_tmpfs,
config_file);

#if __FreeBSD__
if (config_file_mounted) {
unmount_config_iso();
}
#endif

if (config_tmpfs) {
if (mkdir(config_tmpfs, 0755) < 0 && errno != EEXIST) {
perror("mkdir for tmpfs");
exit(-1);
}

/* TODO: Honour mount flags from the config file. Most notably,
* tmpcopyup is set by Podman by default, requesting copying the files
* present in the original directory, e.g. from the image. */
if (mount("tmpfs", config_tmpfs, "tmpfs",
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount for tmpfs");
exit(-1);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The config_tmpfs string is allocated via malloc (inside config_parse_string called by config_parse_mounts) but is never freed in main. This results in a small memory leak.

References
  1. Ensure all dynamically allocated memory is properly freed to avoid leaks, especially in long-running processes or entry points.

Copy link
Copy Markdown
Collaborator Author

@mz-pdm mz-pdm Apr 21, 2026

Choose a reason for hiding this comment

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

True but it is consistent with handling the other config_* variables passed to config_parse_file. Maybe we should free all of them (in a separate change)?

@mz-pdm
Copy link
Copy Markdown
Collaborator Author

mz-pdm commented Apr 21, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for parsing and mounting tmpfs filesystems specified in the container configuration. It includes new utility functions for JSON token skipping and mount point verification via /proc/mounts. Review feedback highlights several areas for improvement: ensuring the 'mounts' JSON token is validated as an array, increasing buffer sizes for filesystem paths to avoid truncation, and refining the JSON parsing logic to handle complex values correctly using the new skip utility. There is also a suggestion to relax the validation of the 'source' field for tmpfs mounts to better align with OCI specifications.

Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c
@mz-pdm
Copy link
Copy Markdown
Collaborator Author

mz-pdm commented Apr 21, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces functionality to parse tmpfs mount specifications from a JSON configuration and perform the corresponding mount operations. It includes new logic for JSON token skipping and mount point detection via /proc/mounts. The review feedback suggests increasing buffer sizes for path handling to prevent truncation and improving the robustness of directory creation for nested paths.

Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c
Comment thread init/init.c Outdated
Podman's --tmpfs command line option is currently ignored by libkrun.
This means the host tmpfs, as arranged by crun, is auto-mounted as a
virtiofs when first accessing the given directory.  Having such a tmpfs
adds some overhead when accessing it and reduces the VM isolation.
Let's add a limited support for mount the given directory natively.

This is implemented by looking at `mounts' in /.krun_config.json and
mounting the first matching tmpfs directory there, if any.  "Matching"
means it's a tmpfs entry and the directory is not yet mounted.  The
mount options are ignored.

We refuse to mount already mounted directories; this is because tmpfs
mounts can be requested by Podman itself also for other directories, for
example /dev, which libkrun already mounts itself.  The mount check is
implemented by looking at /proc/mounts; using stat/lstat would result in
triggering the Podman's automount.

The mount directory must already exist, which is the case with Podman's
--tmpfs.

This commit implements just the most basic easy part and is not fully
compliant.  Possible future improvements are:

- Copy the files from the original directory to the tmpfs.  This is
  requested by default from Podman (tmpcopyup option).  It's not
  implemented right now because the copying is non-trivial and it's
  better to leave that for Rust implementation of init.

- Create the mount directory if it doesn't exist.

- Honour the other mount options.

- Honour other mounts from the configuration.

Fixes: containers#515

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
@mz-pdm mz-pdm marked this pull request as ready for review April 21, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an API so users can request /tmp to be a tmpfs

2 participants