This repo contains some examples using 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 contextfsconfig( 5.1 - May 2019 ) - Configures the configuration contextfsmount( 5.1 - May 2019 ) - Makes a detached mountof the configuration context, returns a dirfdfspick( 5.1 - May 2019 ) - Allows to reconfigure a mountpoint, returns a configuration contextopen_tree( 4.18 - August 2018 ) - Pick or clone mount object and attach to fd, returns a dirfdmove_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:
-
No glibc implementation - Syscalls are called directly, with wrappers present on
common.h
listmount- Alternative to mountinfostatmount- Alternative to mountinfomount_setattr( 5.12 )open_tree_attr- Atomically create a detached mount tree and set mount options on itfsinfo
Run the following script to make the directories ready for the examples. It will compile everything prepare the necessary directories
/make.shThe 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
This example simply creates a tmpfs of size 50MB. It does it in 5 steps:
- Create the fs context for a
tmpfsfilesystem - 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
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 withfsopen()) - 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
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/
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/)