Documentation ¶
Overview ¶
Package gofsutil provides many filesystem-specific functions, such as mount, format, etc., and is based largely on k8s.io/pkg/util/mount.
Index ¶
- Constants
- Variables
- func BindMount(ctx context.Context, source, target string, opts ...string) error
- func EvalSymlinks(ctx context.Context, symPath *string) error
- func FormatAndMount(ctx context.Context, source, target, fsType string, opts ...string) error
- func GetDiskFormat(ctx context.Context, disk string) (string, error)
- func MakeMountArgs(ctx context.Context, source, target, fsType string, opts ...string) []string
- func Mount(ctx context.Context, source, target, fsType string, opts ...string) error
- func RemoveDuplicates(a []string) []string
- func RemoveDuplicatesExponentialOrdered(a []string) []string
- func RemoveDuplicatesExponentialUnordered(a []string) []string
- func RemoveDuplicatesLinearOrdered(a []string) []string
- func Unmount(ctx context.Context, target string) error
- func ValidateDevice(ctx context.Context, source string) (string, error)
- type Entry
- type EntryScanFunc
- type FS
- func (fs *FS) BindMount(ctx context.Context, source, target string, options ...string) error
- func (fs *FS) FormatAndMount(ctx context.Context, source, target, fsType string, options ...string) error
- func (fs *FS) GetDevMounts(ctx context.Context, dev string) ([]Info, error)
- func (fs *FS) GetDiskFormat(ctx context.Context, disk string) (string, error)
- func (fs *FS) GetMounts(ctx context.Context) ([]Info, error)
- func (fs *FS) Mount(ctx context.Context, source, target, fsType string, options ...string) error
- func (fs *FS) Unmount(ctx context.Context, target string) error
- func (fs *FS) ValidateDevice(ctx context.Context, source string) (string, error)
- type Info
Constants ¶
const ProcMountsFields = 9
ProcMountsFields is fields per line in procMountsPath as per https://www.kernel.org/doc/Documentation/filesystems/proc.txt
Variables ¶
var ( // ErrNotImplemented is returned when a platform does not implement // the contextual function. ErrNotImplemented = errors.New("not implemented") )
Functions ¶
func BindMount ¶
BindMount behaves like Mount was called with a "bind" flag set in the options list.
func EvalSymlinks ¶
EvalSymlinks evaluates the provided path and updates it to remove any symlinks in its structure, replacing them with the actual path components.
func FormatAndMount ¶
FormatAndMount uses unix utils to format and mount the given disk.
func GetDiskFormat ¶
GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
func MakeMountArgs ¶
MakeMountArgs makes the arguments to the mount(8) command.
The argument list returned is built as follows:
mount [-t $fsType] [-o $options] [$source] $target
func Mount ¶
Mount mounts source to target as fstype with given options.
The parameters 'source' and 'fstype' must be empty strings in case they are not required, e.g. for remount, or for an auto filesystem type where the kernel handles fstype automatically.
The 'options' parameter is a list of options. Please see mount(8) for more information. If no options are required then please invoke Mount with an empty or nil argument.
func RemoveDuplicates ¶
RemoveDuplicates removes duplicate and empty values from the provided slice of strings and maintains the original order of the list.
This function defers to RemoveDuplicatesExponentialOrdered, a variation of RemoveDuplicatesExponentialUnordered as described in the article "Learning Golang By Benchmarking Set Implementations" by Karl Seguin at https://goo.gl/NTU36K. The variation preserves the slice's original order and has minimal to no impact on performance.
The function's O(n^2) complexity could be altered to O(n) (linear) by using a map[string]struct{} to track unique elements with zero memory allocation. However, as long as the input data is small, the current implementation is more performant both with respect to both CPU and memory. Given that this function is used to remove duplicate mount options, there should never be a sufficiently large enough dataset that a linear version of this function would be more performant the current implementation.
The function RemoveDuplicatesLinearOrdered is the linear version of this function. If the situation should ever change such that mount options number in the thousands then this function should defer to RemoveDuplicatesLinearOrdered instead of RemoveDuplicatesExponentialOrdered.
$ go test -run Bench -bench BenchmarkRemoveDuplicates -benchmem -v goos: darwin goarch: amd64 pkg: github.com/thecodeteam/gofsutil BenchmarkRemoveDuplicates_Exponential_Ordered___SmallData-8 20000000 121 ns/op 0 B/op 0 allocs/op BenchmarkRemoveDuplicates_Exponential_Unordered_SmallData-8 20000000 99.0 ns/op 0 B/op 0 allocs/op BenchmarkRemoveDuplicates_Linear______Ordered___SmallData-8 2000000 715 ns/op 288 B/op 1 allocs/op BenchmarkRemoveDuplicates_Exponential_Ordered___BigData-8 20000 84731 ns/op 0 B/op 0 allocs/op BenchmarkRemoveDuplicates_Exponential_Unordered_BigData-8 10000 156660 ns/op 0 B/op 0 allocs/op BenchmarkRemoveDuplicates_Linear______Ordered___BigData-8 50000 36971 ns/op 20512 B/op 2 allocs/op PASS ok github.com/thecodeteam/gofsutil 22.085s
func RemoveDuplicatesExponentialOrdered ¶
RemoveDuplicatesExponentialOrdered removes duplicate and empty values from the provided slice of strings using an exponentially complex algorithm and maintains the original order of the list.
func RemoveDuplicatesExponentialUnordered ¶
RemoveDuplicatesExponentialUnordered removes duplicate and empty values from the provided slice of strings using an exponentially complex algorithm and does not maintain the original order of the list.
func RemoveDuplicatesLinearOrdered ¶
RemoveDuplicatesLinearOrdered removes duplicate and empty values from the provided slice of strings using a linearly complex algorithm and maintains the original order of the list.
func ValidateDevice ¶ added in v0.1.2
ValidateDevice evalutes the specified path and determines whether or not it is a valid device. If true then the provided path is evaluated and returned as an absolute path without any symlinks. Otherwise an empty string is returned.
Types ¶
type Entry ¶
type Entry struct { // Root of the mount within the filesystem. Root string // MountPoint relative to the process's root MountPoint string // MountOpts are per-mount options. MountOpts []string // FSType is the name of filesystem of the form "type[.subtype]". FSType string // MountSource is filesystem specific information or "none" MountSource string }
Entry is a superset of Info and maps to the fields of a mount table entry:
(1) mount ID: unique identifier of the mount (may be reused after umount) (2) parent ID: ID of parent (or of self for the top of the mount tree) (3) major:minor: value of st_dev for files on filesystem (4) root: root of the mount within the filesystem (5) mount point: mount point relative to the process's root (6) mount options: per mount options (7) optional fields: zero or more fields of the form "tag[:value]" (8) separator: marks the end of the optional fields (9) filesystem type: name of filesystem of the form "type[.subtype]" (10) mount source: filesystem specific information or "none" (11) super options: per super block options
type EntryScanFunc ¶
type EntryScanFunc func( ctx context.Context, entry Entry, cache map[string]Entry) (Info, bool, error)
EntryScanFunc defines the signature of the function that is optionally provided to the functions in this package that scan the mount table. The mount entry table is ignored when this function returns a false value or error.
func DefaultEntryScanFunc ¶
func DefaultEntryScanFunc() EntryScanFunc
DefaultEntryScanFunc returns the default entry scan function.
type FS ¶
type FS struct { // ScanEntry is the function used to process mount table entries. ScanEntry EntryScanFunc }
FS provides many filesystem-specific functions, such as mount, format, etc.
func (*FS) BindMount ¶
BindMount behaves like Mount was called with a "bind" flag set in the options list.
func (*FS) FormatAndMount ¶
func (fs *FS) FormatAndMount( ctx context.Context, source, target, fsType string, options ...string) error
FormatAndMount uses unix utils to format and mount the given disk.
func (*FS) GetDevMounts ¶
GetDevMounts returns a slice of all mounts for the provided device.
func (*FS) GetDiskFormat ¶
GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
func (*FS) GetMounts ¶
GetMounts returns a slice of all the mounted filesystems.
* Linux hosts use mount_namespaces to obtain mount information.
Support for mount_namespaces was introduced to the Linux kernel in 2.2.26 (http://man7.org/linux/man-pages/man5/proc.5.html) on 2004/02/04. The kernel documents the contents of "/proc/<pid>/mountinfo" at https://www.kernel.org/doc/Documentation/filesystems/proc.txt.
- Darwin hosts parse the output of the "mount" command to obtain mount information.
func (*FS) Mount ¶
Mount mounts source to target as fstype with given options.
The parameters 'source' and 'fstype' must be empty strings in case they are not required, e.g. for remount, or for an auto filesystem type where the kernel handles fstype automatically.
The 'options' parameter is a list of options. Please see mount(8) for more information. If no options are required then please invoke Mount with an empty or nil argument.
func (*FS) ValidateDevice ¶ added in v0.1.1
ValidateDevice evalutes the specified path and determines whether or not it is a valid device. If true then the provided path is evaluated and returned as an absolute path without any symlinks. Otherwise an empty string is returned.
type Info ¶
type Info struct { // Device is the filesystem path of the device to which the filesystem is // mounted. Device string // Path is the filesystem path to which Device is mounted. Path string // Source may be set to one of two values: // // 1. If this is a bind mount created with "bindfs" then Source // is set to the filesystem path (absolute, no symlinks) // bind mounted to Path. // // 2. If this is any other type of mount then Source is set to // a concatenation of the mount source and the root of // the mount within the file system (fields 10 & 4 from // the section on /proc/<pid>/mountinfo at // https://www.kernel.org/doc/Documentation/filesystems/proc.txt). // // It is not possible to diffentiate a native bind mount from a // non-bind mount after the native bind mount has been created. // Therefore, while the Source field will be set to the filesystem // path bind mounted to Path for native bind mounts, the value of // the Source field can in no way be used to determine *if* a mount // is a bind mount. Source string // Type is the filesystem type. Type string // Opts are the mount options (https://linux.die.net/man/8/mount) // used to mount the filesystem. Opts []string }
Info describes a mounted filesystem.
Please note that all fields that represent filesystem paths must be absolute and not contain any symlinks.
func GetDevMounts ¶
GetDevMounts returns a slice of all mounts for the provided device.
func GetMounts ¶
GetMounts returns a slice of all the mounted filesystems.
* Linux hosts use mount_namespaces to obtain mount information.
Support for mount_namespaces was introduced to the Linux kernel in 2.2.26 (http://man7.org/linux/man-pages/man5/proc.5.html) on 2004/02/04. The kernel documents the contents of "/proc/<pid>/mountinfo" at https://www.kernel.org/doc/Documentation/filesystems/proc.txt.
- Darwin hosts parse the output of the "mount" command to obtain mount information.
func ReadProcMountsFrom ¶
func ReadProcMountsFrom( ctx context.Context, file io.Reader, quick bool, expectedFields int, scanEntry EntryScanFunc) ([]Info, uint32, error)
ReadProcMountsFrom parses the contents of a mount table file, typically "/proc/self/mountinfo".
From https://www.kernel.org/doc/Documentation/filesystems/proc.txt:
3.5 /proc/<pid>/mountinfo - Information about mounts --------------------------------------------------------
This file contains lines of the form:
36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)
(1) mount ID: unique identifier of the mount (may be reused after umount) (2) parent ID: ID of parent (or of self for the top of the mount tree) (3) major:minor: value of st_dev for files on filesystem (4) root: root of the mount within the filesystem (5) mount point: mount point relative to the process's root (6) mount options: per mount options (7) optional fields: zero or more fields of the form "tag[:value]" (8) separator: marks the end of the optional fields (9) filesystem type: name of filesystem of the form "type[.subtype]" (10) mount source: filesystem specific information or "none" (11) super options: per super block options
Parsers should ignore all unrecognised optional fields. Currently the possible optional fields are:
shared:X mount is shared in peer group X master:X mount is slave to peer group X propagate_from:X mount is slave and receives propagation from peer group X (*) unbindable mount is unbindable