Skip to content

Examples with the fsmount and friends syscalls for a demonstration

Notifications You must be signed in to change notification settings

kovagsm/fsmount-syscalls

Repository files navigation

This repo contains some examples using the new mount API

The new mount API

The new mount API was created to address the current limitations of mount():

  • Can't pass too many options, only 4096 bytes
  • The information of what went wrong during the mount process isn't ideal
  • Decouple the complexity of the mount syscall
  • Allowing programs to have a better understanding which step of the mount is failing

Everything resolves around a "filesystem context":

  • fsopen ( 5.1 - May 2019 ) - Create a filesystem configuration context
  • fsconfig ( 5.1 - May 2019 ) - Configures the configuration context
  • fsmount ( 5.1 - May 2019 ) - Makes a detached mountof the configuration context, returns a dirfd
  • fspick ( 5.1 - May 2019 ) - Allows to reconfigure a mountpoint, returns a configuration context
  • open_tree ( 4.18 - August 2018 ) - Pick or clone mount object and attach to fd, returns a dirfd
  • move_mount ( 4.18 - August 2018 ) - Move a mount or attach an unattached mount

Documentation is scarce!

There isn't much documentation available on these syscalls, and some of the information is contradictory

Manpages that were scrapped from the mailing lists can be found here:

https://github.com/brauner/man-pages-md/tree/main

Interesting articles:

Other new interesting syscalls

  • listmount - Alternative to mountinfo
  • statmount - Alternative to mountinfo
  • mount_setattr ( 5.12 )
  • open_tree_attr - Atomically create a detached mount tree and set mount options on it
  • fsinfo

Compiling

Run the following script to make the directories ready for the examples. It will compile everything prepare the necessary directories

/make.sh

Examples

The examples pause at each step so you have the opportunity to explore around /proc/ The following files/directories can be of interest:

  • /proc/<pid>/fd/
  • /proc/<pid>/fdinfo/
  • /proc/<pid>/mountinfo

Example 1

This example simply creates a tmpfs of size 50MB. It does it in 5 steps:

  • Create the fs context for a tmpfs filesystem
  • Configure it to have size of 50M
  • "Commits" the configuration with FSCONFIG_CMD_CREATE - Superblock is created but not mounted yet
  • Mounts - Mount id is created by the kernel here
  • Move mounts and makes the filesystem visible on <current_dir>/tmpfs

Example 2

This example does the same of a remount, resizes the previous mount to 200MB in the following steps:

  • Uses fspick() to get a handle to the mount configuration fd (of the same type as the one obtained with fsopen())
  • Uses fsconfig() to set the size to 200M
  • Commits the configuration with FSCONFIG_CMD_RECONFIGURE

Notice that there was no need to call fsmount the size gets changed when it's comitte.d

Example 3

This example performs a bind mount from bind-source/ to bind-target/ with the following steps:

  • Gets an fd handle for the source directory
  • Performs a move_mount to bind-target/

Example 4

This was inspired from runc init, it creates an overlayfs with the a hello-world binary on the lowerdir then uses fsmount() to create a detached mount thats not visible from the root tree, and then uses the fsmount fd handle with openat() to open the hello-world binary from the detacled mount.

Openat returns a file descriptor, and this fd is used to execute hello-world without needing to attach the mountpoint, by manually running (/proc/<pid>/fd/)

About

Examples with the fsmount and friends syscalls for a demonstration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors