Updates moby/sys mountinfo package to v0.6.0

Update to moby/sys/mountinfo package that contains MountedFast
function. The function uses OpenAt2 call for newer kernels
to determine if a mount-point is present or not.
This commit is contained in:
Manu Gupta 2022-03-19 16:09:21 -07:00
parent 475f7af1c1
commit f9abf7e7ac
11 changed files with 92 additions and 59 deletions

3
go.mod
View File

@ -56,6 +56,7 @@ require (
github.com/lithammer/dedent v1.1.0 github.com/lithammer/dedent v1.1.0
github.com/lpabon/godbc v0.1.1 // indirect github.com/lpabon/godbc v0.1.1 // indirect
github.com/moby/ipvs v1.0.1 github.com/moby/ipvs v1.0.1
github.com/moby/sys/mountinfo v0.6.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb // indirect github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb // indirect
github.com/mrunalp/fileutils v0.5.0 github.com/mrunalp/fileutils v0.5.0
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
@ -317,7 +318,7 @@ replace (
github.com/mitchellh/mapstructure => github.com/mitchellh/mapstructure v1.4.1 github.com/mitchellh/mapstructure => github.com/mitchellh/mapstructure v1.4.1
github.com/moby/ipvs => github.com/moby/ipvs v1.0.1 github.com/moby/ipvs => github.com/moby/ipvs v1.0.1
github.com/moby/spdystream => github.com/moby/spdystream v0.2.0 github.com/moby/spdystream => github.com/moby/spdystream v0.2.0
github.com/moby/sys/mountinfo => github.com/moby/sys/mountinfo v0.4.1 github.com/moby/sys/mountinfo => github.com/moby/sys/mountinfo v0.6.0
github.com/moby/term => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 github.com/moby/term => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/modern-go/concurrent => github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd github.com/modern-go/concurrent => github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/modern-go/reflect2 => github.com/modern-go/reflect2 v1.0.2 github.com/modern-go/reflect2 => github.com/modern-go/reflect2 v1.0.2

4
go.sum
View File

@ -308,8 +308,8 @@ github.com/moby/ipvs v1.0.1 h1:aoZ7fhLTXgDbzVrAnvV+XbKOU8kOET7B3+xULDF/1o0=
github.com/moby/ipvs v1.0.1/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ= github.com/moby/ipvs v1.0.1/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/moby/sys/mountinfo v0.4.1 h1:1O+1cHA1aujwEwwVMa2Xm2l+gIpUHyd3+D+d7LZh1kM= github.com/moby/sys/mountinfo v0.6.0 h1:gUDhXQx58YNrpHlK4nSL+7y2pxFZkUcXqzFDKWdC0Oo=
github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= github.com/moby/sys/mountinfo v0.6.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc= github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw= github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=

View File

@ -1,5 +1,5 @@
module github.com/moby/sys/mountinfo module github.com/moby/sys/mountinfo
go 1.14 go 1.16
require golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 require golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359

View File

@ -1,2 +1,2 @@
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 h1:2B5p2L5IfGiD7+b9BOoRMC6DgObAVZV+Fsp050NqXik=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -7,6 +7,34 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
// MountedFast is a method of detecting a mount point without reading
// mountinfo from procfs. A caller can only trust the result if no error
// and sure == true are returned. Otherwise, other methods (e.g. parsing
// /proc/mounts) have to be used. If unsure, use Mounted instead (which
// uses MountedFast, but falls back to parsing mountinfo if needed).
//
// If a non-existent path is specified, an appropriate error is returned.
// In case the caller is not interested in this particular error, it should
// be handled separately using e.g. errors.Is(err, os.ErrNotExist).
//
// This function is only available on Linux. When available (since kernel
// v5.6), openat2(2) syscall is used to reliably detect all mounts. Otherwise,
// the implementation falls back to using stat(2), which can reliably detect
// normal (but not bind) mounts.
func MountedFast(path string) (mounted, sure bool, err error) {
// Root is always mounted.
if path == string(os.PathSeparator) {
return true, true, nil
}
path, err = normalizePath(path)
if err != nil {
return false, false, err
}
mounted, sure, err = mountedFast(path)
return
}
// mountedByOpenat2 is a method of detecting a mount that works for all kinds // mountedByOpenat2 is a method of detecting a mount that works for all kinds
// of mounts (incl. bind mounts), but requires a recent (v5.6+) linux kernel. // of mounts (incl. bind mounts), but requires a recent (v5.6+) linux kernel.
func mountedByOpenat2(path string) (bool, error) { func mountedByOpenat2(path string) (bool, error) {
@ -16,9 +44,6 @@ func mountedByOpenat2(path string) (bool, error) {
Flags: unix.O_PATH | unix.O_CLOEXEC, Flags: unix.O_PATH | unix.O_CLOEXEC,
}) })
if err != nil { if err != nil {
if err == unix.ENOENT { // not a mount
return false, nil
}
return false, &os.PathError{Op: "openat2", Path: dir, Err: err} return false, &os.PathError{Op: "openat2", Path: dir, Err: err}
} }
fd, err := unix.Openat2(dirfd, last, &unix.OpenHow{ fd, err := unix.Openat2(dirfd, last, &unix.OpenHow{
@ -26,33 +51,51 @@ func mountedByOpenat2(path string) (bool, error) {
Resolve: unix.RESOLVE_NO_XDEV, Resolve: unix.RESOLVE_NO_XDEV,
}) })
_ = unix.Close(dirfd) _ = unix.Close(dirfd)
switch err { switch err { //nolint:errorlint // unix errors are bare
case nil: // definitely not a mount case nil: // definitely not a mount
_ = unix.Close(fd) _ = unix.Close(fd)
return false, nil return false, nil
case unix.EXDEV: // definitely a mount case unix.EXDEV: // definitely a mount
return true, nil return true, nil
case unix.ENOENT: // not a mount
return false, nil
} }
// not sure // not sure
return false, &os.PathError{Op: "openat2", Path: path, Err: err} return false, &os.PathError{Op: "openat2", Path: path, Err: err}
} }
func mounted(path string) (bool, error) { // mountedFast is similar to MountedFast, except it expects a normalized path.
// Try a fast path, using openat2() with RESOLVE_NO_XDEV. func mountedFast(path string) (mounted, sure bool, err error) {
mounted, err := mountedByOpenat2(path) // Root is always mounted.
if err == nil { if path == string(os.PathSeparator) {
return mounted, nil return true, true, nil
} }
// Try a fast path, using openat2() with RESOLVE_NO_XDEV.
mounted, err = mountedByOpenat2(path)
if err == nil {
return mounted, true, nil
}
// Another fast path: compare st.st_dev fields. // Another fast path: compare st.st_dev fields.
mounted, err = mountedByStat(path) mounted, err = mountedByStat(path)
// This does not work for bind mounts, so false negative // This does not work for bind mounts, so false negative
// is possible, therefore only trust if return is true. // is possible, therefore only trust if return is true.
if mounted && err == nil { if mounted && err == nil {
return true, true, nil
}
return
}
func mounted(path string) (bool, error) {
path, err := normalizePath(path)
if err != nil {
return false, err
}
mounted, sure, err := mountedFast(path)
if sure && err == nil {
return mounted, nil return mounted, nil
} }
// Fallback to parsing mountinfo // Fallback to parsing mountinfo.
return mountedByMountinfo(path) return mountedByMountinfo(path)
} }

View File

@ -1,9 +1,9 @@
// +build linux freebsd,cgo openbsd,cgo //go:build linux || (freebsd && cgo) || (openbsd && cgo) || (darwin && cgo)
// +build linux freebsd,cgo openbsd,cgo darwin,cgo
package mountinfo package mountinfo
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -15,10 +15,6 @@ func mountedByStat(path string) (bool, error) {
var st unix.Stat_t var st unix.Stat_t
if err := unix.Lstat(path, &st); err != nil { if err := unix.Lstat(path, &st); err != nil {
if err == unix.ENOENT {
// Treat ENOENT as "not mounted".
return false, nil
}
return false, &os.PathError{Op: "stat", Path: path, Err: err} return false, &os.PathError{Op: "stat", Path: path, Err: err}
} }
dev := st.Dev dev := st.Dev
@ -49,14 +45,6 @@ func normalizePath(path string) (realPath string, err error) {
} }
func mountedByMountinfo(path string) (bool, error) { func mountedByMountinfo(path string) (bool, error) {
path, err := normalizePath(path)
if err != nil {
if errors.Is(err, unix.ENOENT) {
// treat ENOENT as "not mounted"
return false, nil
}
return false, err
}
entries, err := GetMounts(SingleEntryFilter(path)) entries, err := GetMounts(SingleEntryFilter(path))
if err != nil { if err != nil {
return false, err return false, err

View File

@ -10,11 +10,12 @@ func GetMounts(f FilterFunc) ([]*Info, error) {
return parseMountTable(f) return parseMountTable(f)
} }
// Mounted determines if a specified path is a mount point. // Mounted determines if a specified path is a mount point. In case of any
// error, false (and an error) is returned.
// //
// The argument must be an absolute path, with all symlinks resolved, and clean. // If a non-existent path is specified, an appropriate error is returned.
// One way to ensure it is to process the path using filepath.Abs followed by // In case the caller is not interested in this particular error, it should
// filepath.EvalSymlinks before calling this function. // be handled separately using e.g. errors.Is(err, os.ErrNotExist).
func Mounted(path string) (bool, error) { func Mounted(path string) (bool, error) {
// root is always mounted // root is always mounted
if path == string(os.PathSeparator) { if path == string(os.PathSeparator) {

View File

@ -1,4 +1,5 @@
// +build freebsd,cgo openbsd,cgo //go:build (freebsd && cgo) || (openbsd && cgo) || (darwin && cgo)
// +build freebsd,cgo openbsd,cgo darwin,cgo
package mountinfo package mountinfo
@ -21,7 +22,7 @@ func parseMountTable(filter FilterFunc) ([]*Info, error) {
count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT)) count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT))
if count == 0 { if count == 0 {
return nil, fmt.Errorf("Failed to call getmntinfo") return nil, fmt.Errorf("failed to call getmntinfo")
} }
var entries []C.struct_statfs var entries []C.struct_statfs
@ -55,6 +56,10 @@ func parseMountTable(filter FilterFunc) ([]*Info, error) {
} }
func mounted(path string) (bool, error) { func mounted(path string) (bool, error) {
path, err := normalizePath(path)
if err != nil {
return false, err
}
// Fast path: compare st.st_dev fields. // Fast path: compare st.st_dev fields.
// This should always work for FreeBSD and OpenBSD. // This should always work for FreeBSD and OpenBSD.
mounted, err := mountedByStat(path) mounted, err := mountedByStat(path)

View File

@ -52,7 +52,7 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
numFields := len(fields) numFields := len(fields)
if numFields < 10 { if numFields < 10 {
// should be at least 10 fields // should be at least 10 fields
return nil, fmt.Errorf("Parsing '%s' failed: not enough fields (%d)", text, numFields) return nil, fmt.Errorf("parsing '%s' failed: not enough fields (%d)", text, numFields)
} }
// separator field // separator field
@ -67,7 +67,7 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
for fields[sepIdx] != "-" { for fields[sepIdx] != "-" {
sepIdx-- sepIdx--
if sepIdx == 5 { if sepIdx == 5 {
return nil, fmt.Errorf("Parsing '%s' failed: missing - separator", text) return nil, fmt.Errorf("parsing '%s' failed: missing - separator", text)
} }
} }
@ -75,46 +75,39 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
p.Mountpoint, err = unescape(fields[4]) p.Mountpoint, err = unescape(fields[4])
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing '%s' failed: mount point: %w", fields[4], err) return nil, fmt.Errorf("parsing '%s' failed: mount point: %w", fields[4], err)
} }
p.FSType, err = unescape(fields[sepIdx+1]) p.FSType, err = unescape(fields[sepIdx+1])
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing '%s' failed: fstype: %w", fields[sepIdx+1], err) return nil, fmt.Errorf("parsing '%s' failed: fstype: %w", fields[sepIdx+1], err)
} }
p.Source, err = unescape(fields[sepIdx+2]) p.Source, err = unescape(fields[sepIdx+2])
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing '%s' failed: source: %w", fields[sepIdx+2], err) return nil, fmt.Errorf("parsing '%s' failed: source: %w", fields[sepIdx+2], err)
} }
p.VFSOptions = fields[sepIdx+3] p.VFSOptions = fields[sepIdx+3]
// ignore any numbers parsing errors, as there should not be any // ignore any numbers parsing errors, as there should not be any
p.ID, _ = strconv.Atoi(fields[0]) p.ID, _ = strconv.Atoi(fields[0])
p.Parent, _ = strconv.Atoi(fields[1]) p.Parent, _ = strconv.Atoi(fields[1])
mm := strings.Split(fields[2], ":") mm := strings.SplitN(fields[2], ":", 3)
if len(mm) != 2 { if len(mm) != 2 {
return nil, fmt.Errorf("Parsing '%s' failed: unexpected minor:major pair %s", text, mm) return nil, fmt.Errorf("parsing '%s' failed: unexpected major:minor pair %s", text, mm)
} }
p.Major, _ = strconv.Atoi(mm[0]) p.Major, _ = strconv.Atoi(mm[0])
p.Minor, _ = strconv.Atoi(mm[1]) p.Minor, _ = strconv.Atoi(mm[1])
p.Root, err = unescape(fields[3]) p.Root, err = unescape(fields[3])
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing '%s' failed: root: %w", fields[3], err) return nil, fmt.Errorf("parsing '%s' failed: root: %w", fields[3], err)
} }
p.Options = fields[5] p.Options = fields[5]
// zero or more optional fields // zero or more optional fields
switch { p.Optional = strings.Join(fields[6:sepIdx], " ")
case sepIdx == 6:
// zero, do nothing
case sepIdx == 7:
p.Optional = fields[6]
default:
p.Optional = strings.Join(fields[6:sepIdx-1], " ")
}
// Run the filter after parsing all of the fields. // Run the filter after parsing all fields.
var skip, stop bool var skip, stop bool
if filter != nil { if filter != nil {
skip, stop = filter(p) skip, stop = filter(p)

View File

@ -1,4 +1,5 @@
// +build !windows,!linux,!freebsd,!openbsd freebsd,!cgo openbsd,!cgo //go:build (!windows && !linux && !freebsd && !openbsd && !darwin) || (freebsd && !cgo) || (openbsd && !cgo) || (darwin && !cgo)
// +build !windows,!linux,!freebsd,!openbsd,!darwin freebsd,!cgo openbsd,!cgo darwin,!cgo
package mountinfo package mountinfo

5
vendor/modules.txt vendored
View File

@ -520,7 +520,8 @@ github.com/moby/ipvs
# github.com/moby/spdystream v0.2.0 => github.com/moby/spdystream v0.2.0 # github.com/moby/spdystream v0.2.0 => github.com/moby/spdystream v0.2.0
github.com/moby/spdystream github.com/moby/spdystream
github.com/moby/spdystream/spdy github.com/moby/spdystream/spdy
# github.com/moby/sys/mountinfo v0.4.1 => github.com/moby/sys/mountinfo v0.4.1 # github.com/moby/sys/mountinfo v0.6.0 => github.com/moby/sys/mountinfo v0.6.0
## explicit
github.com/moby/sys/mountinfo github.com/moby/sys/mountinfo
# github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 # github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/moby/term github.com/moby/term
@ -2588,7 +2589,7 @@ sigs.k8s.io/yaml
# github.com/mitchellh/mapstructure => github.com/mitchellh/mapstructure v1.4.1 # github.com/mitchellh/mapstructure => github.com/mitchellh/mapstructure v1.4.1
# github.com/moby/ipvs => github.com/moby/ipvs v1.0.1 # github.com/moby/ipvs => github.com/moby/ipvs v1.0.1
# github.com/moby/spdystream => github.com/moby/spdystream v0.2.0 # github.com/moby/spdystream => github.com/moby/spdystream v0.2.0
# github.com/moby/sys/mountinfo => github.com/moby/sys/mountinfo v0.4.1 # github.com/moby/sys/mountinfo => github.com/moby/sys/mountinfo v0.6.0
# github.com/moby/term => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 # github.com/moby/term => github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
# github.com/modern-go/concurrent => github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd # github.com/modern-go/concurrent => github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
# github.com/modern-go/reflect2 => github.com/modern-go/reflect2 v1.0.2 # github.com/modern-go/reflect2 => github.com/modern-go/reflect2 v1.0.2