Repository navigation
Conversation
This is a temporary replace until Listmount and Statmount are merged into golang.org/x/sys. Note that current x/sys requires Go 1.26, so go mod tidy bumped the go directive. Signed-off-by: Kir Kolyshkin <[email protected]>
Add an internal implementation of getting mount information using listmount(2) and statmount(2), converting the results to Info in a way that it is identical to what is parsed from /proc/self/mountinfo (this mimics show_mountinfo from the kernel's fs/proc_namespace.c). Since some statmount fields (fs subtype, sb source) are only available since Linux 6.13, statmount is only considered supported if it returns all the needed information. Add UniqueID and ParentUniqueID fields to Info. These are only filled in when statmount is used. Add a test comparing the results with mountinfo parsing, and a benchmark. Note that for getting all mounts, statmount is slower than parsing mountinfo (47.7 vs 27.3 µs for 33 mounts), so it is not used by GetMounts. The intended use is for targeted queries. Signed-off-by: Kir Kolyshkin <[email protected]>
Many users need to find the mount a given path belongs to (and its
properties, e.g. propagation or source). Currently this is done by
GetMounts(ParentsFilter(path)) and choosing an entry with the longest
mount point (moby, containerd, cri-o, nri, buildx etc.), or by
SingleEntryFilter (which returns the bottommost entry when several
mounts are stacked on the same mount point).
Add MountOf which does it faster and more correctly:
- on Linux 6.13+, statx(STATX_MNT_ID_UNIQUE) + statmount;
- on Linux 5.8+, statx(STATX_MNT_ID) + mountinfo search by mount ID;
- otherwise (and on BSDs), ParentsFilter + longest mount point,
choosing the topmost one if several mounts are stacked.
With statmount, the cost of MountOf does not depend on the number of
mounts, while the mountinfo-based approaches are O(n) and are further
slowed down by concurrent mounts/unmounts (as on a busy container host).
Benchmark results (ns/op, Linux 7.2, 4 CPUs; with 1034 mounts, the path
is on the last one, like a rootfs of a just started container):
34 mounts 1034 mounts 1034 mounts +
4x mount/umount loop
MountOf (statmount) 1660 1914 2812
statx + mount ID 25456 704318 941112
ParentsFilter 29406 717812 977935
Signed-off-by: Kir Kolyshkin <[email protected]>
Many users parse the Optional field by hand to figure out mount propagation (moby, cri-o, nri, k8sd, rootlesskit, u-root), sometimes incorrectly (e.g. strings.Contains(Optional, "shared:1") also matches "shared:12"). Add PeerGroup, Master, PropagateFrom and Unbindable methods to Info. These do not allocate. Together with MountOf, this gives a simple way to find out the propagation of a mount a given path belongs to. Add a unit test, and a test for real mounts (requires root). Signed-off-by: Kir Kolyshkin <[email protected]>
Many users need to find all mounts on and under a given path, usually
to unmount them (containerd, buildah, nerdbox, werf, snapshotters).
Currently this is done by GetMounts(PrefixFilter(path)) (which requires
a normalized path), or by a custom filter.
Add Submounts which returns the same result as
GetMounts(PrefixFilter(path)), except path does not have to be
normalized. On Linux 6.13+, if path is a mount point, it uses statx,
listmount and statmount, and avoids reading mountinfo. Otherwise
(including the case when path is not a mount point, as then the
subtree to look at might be as large as the whole mount table), it
falls back to using PrefixFilter.
Benchmark results (ns/op, Linux 7.2, 4 CPUs; the path is a mount point
with 10 submounts, like a container rootfs):
1044 mounts 1044 mounts +
4x mount/umount loop
Submounts 22876 32059
PrefixFilter 631660 782542
Signed-off-by: Kir Kolyshkin <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: waiting for
Listmount/Statmountin golang.org/x/sys, see https://go-review.googlesource.com/c/sys/+/863921. Until then, the first commit adds areplaceto a local x/sys checkout, so CI fails. Once the CL is merged, that commit will be replaced by an x/sys bump (which requires Go 1.26).This adds a new API for the most common use of this package (besides
Mounted): finding the mount a given path belongs to, and its properties (such as propagation). Today, users do it byGetMounts(ParentsFilter(path))plus choosing the longest mount point (moby, containerd, cri-o, nri, buildx), or bySingleEntryFilter(which returns the bottom entry when several mounts are stacked), and then parseOptionalby hand.MountOf(path)returns the mount the path belongs to (the topmost one, if mounts are stacked):statx(STATX_MNT_ID_UNIQUE)+statmount(2), without reading mountinfo;statx(STATX_MNT_ID)+ mountinfo lookup by mount ID;ParentsFilter+ longest mount point.InfogetsUniqueIDandParentUniqueIDfields (filled in when statmount is used).InfogetsPeerGroup,Master,PropagateFromandUnbindablemethods to parseOptional.Submounts(path)(usinglistmount(2)on a subtree), for users which unmount everything under a path (containerd, buildah, snapshotters).statmount(2)results toInfoso that it is identical to what is parsed from mountinfo; this is tested against mountinfo for all mounts.GetMountsis intentionally not switched to listmount/statmount: for a full table, it is 2–2.7x slower than parsing mountinfo.Benchmark (ns/op, Linux 7.2, 4 CPUs; with 1034 mounts, the path is on the last one, like a rootfs of a just started container):
MountOf(statmount)ParentsFilterRelated: #281 (use statx in
Mounted).