mntinfo

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: Apache-2.0 Imports: 7 Imported by: 4

README

mntinfo

PkgGoDev GitHub build and test file descriptors Go Report Card Coverage

mntinfo is a minimalistic Linux-only Go package for discovering the currently mounted filesystems seen by processes. This package additionally supports discovering only those mounts matching a specific filesystem type.

Note: mount discovery is done using /proc/[PID]/mountinfo data from the proc filesystem – see also proc(5).

Installation

go get github.com/thediveo/go-mntinfo

Hacking It

  • to view the package documentation locally:
  • make shows the available make targets.

mntinfo is Copyright 2019-23 Harald Albrecht, and licensed under the Apache License, Version 2.0.

Requirements

Linux.

For a multi-platform solution please take a look at gopsutil instead.

Documentation

Overview

Package mntinfo provides information about the currently mounted filesystems on Linux (from the current mount namespace). This information is gathered from the proc filesystem, in particular, from /proc/self/mountinfo, or alternatively, from a specific PID (via /proc/[PID]/mountinfo).

Just to emphasize: NO /etc/fstab is used here (as it doesn't contain, for instance, information about bind mounts, and more).

Technical Details

For more background information about the mount information returned, please see also proc(5).

Alternatives

For a multi-platform solution, please take a look at the Go gopsutil/disk package instead.

Example

Shows all mounted filesystems with lots of details.

package main

import (
	"fmt"

	mntinfo "github.com/thediveo/go-mntinfo"
)

func main() {
	mounts := mntinfo.Mounts()
	for _, mount := range mounts {
		fmt.Printf("%v\n", mount)
	}
	// Output should be slightly similar to:
	//   {22 28 0 21 / /sys [rw nosuid nodev noexec relatime] map[shared:7] sysfs sysfs rw}
	//   {23 28 0 4 / /proc [rw nosuid nodev noexec relatime] map[shared:14] proc proc rw}
	//   {24 28 0 6 / /dev [rw nosuid relatime] map[shared:2] devtmpfs udev rw,size=8158448k,nr_inodes=2039612,mode=755}
	//   {25 24 0 22 / /dev/pts [rw nosuid noexec relatime] map[shared:3] devpts devpts rw,gid=5,mode=620,ptmxmode=000}
	//   {26 28 0 23 / /run [rw nosuid noexec relatime] map[shared:5] tmpfs tmpfs rw,size=1637744k,mode=755}
	//   {28 0 8 2 / / [rw relatime] map[shared:1] ext4 /dev/sda2 rw,errors=remount-ro,data=ordered}
	//   {29 22 0 7 / /sys/kernel/security [rw nosuid nodev noexec relatime] map[shared:8] securityfs securityfs rw}
	//   ...
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mountinfo

type Mountinfo struct {
	// unique ID for the mount, might be reused after umount(2).
	MountID int `json:"mountid"`
	// ID of the parent mount, or self for the root of a mount namespace's mount tree.
	ParentID int `json:"parentid"`
	// major ID for the st_dev for files on this filesystem.
	Major int `json:"major"`
	// minor ID for the st_dev for filed on this filesystem.
	Minor int `json:"minor"`
	// pathname of the directory in the filesystem which forms the root of this mount.
	Root string `json:"root"`
	// pathname of the mount point relative to root directory of the process.
	MountPoint string `json:"mountpoint"`
	// mount options specific to this mount.
	MountOptions []string `json:"mountoptions"`
	// optional fields "tag[:value]"; tags cannot be a single hyphen "-".
	Tags map[string]string `json:"tags"`
	// filesystem type in the form "type[.subtype]"
	FsType string `json:"fstype"`
	// filesystem-specific information or "none".
	Source string `json:"source"`
	// per-superblock options.
	SuperOptions string `json:"superoptions"`
}

Mountinfo stores per-mount information as discovered from the proc filesystem. For mountinfo details, please refer to the proc(5) man page, and there to /proc/[PID]/mountinfo in particular.

func Mounts

func Mounts() []Mountinfo

Mounts returns all mounts for the current process. If for some reason the mount information cannot be read then an empty slice is returned instead.

Example (SortedPaths)

Lists all mounted filesystems: where they are mounted and of which fs type they are. This list is sorted by mount path.

package main

import (
	"fmt"
	"sort"

	mntinfo "github.com/thediveo/go-mntinfo"
)

func main() {
	mounts := mntinfo.Mounts()
	sort.Slice(mounts, func(a, b int) bool {
		return mounts[a].MountPoint < mounts[b].MountPoint
	})
	for _, mount := range mounts {
		fmt.Printf("%s of type %s\n", mount.MountPoint, mount.FsType)
	}
}
Output:

Example (UniqueFstypes)

Lists the types of filesystems currently mounted, in alphabetical order. Most code in this example is needed for creating the final, sorted list of unique filesystem types.

package main

import (
	"fmt"
	"sort"
	"strings"

	mntinfo "github.com/thediveo/go-mntinfo"
)

func main() {
	mounts := mntinfo.Mounts()

	fstypes := map[string]bool{} // ...poor gopher's set emulation
	fstypeNames := []string{}    // gather all unique fstypes
	for _, mount := range mounts {
		if _, ok := fstypes[mount.FsType]; !ok {
			fstypes[mount.FsType] = true
			fstypeNames = append(fstypeNames, mount.FsType)
		}
	}
	sort.Strings(fstypeNames)
	fmt.Printf("currently mounted filesystem types: %s", strings.Join(fstypeNames, ", "))
	// currently mounted filesystem types: autofs, binfmt_misc, cgroup, cgroup2,
	// configfs, debugfs, ... vfat
}
Output:

func MountsOfPid

func MountsOfPid(pid int) []Mountinfo

MountsOfPid returns all mounts for either the current process (when pid specified as -1), or for another process identified by its PID. If the pid is invalid then an empty slice is returned instead.

func MountsOfType

func MountsOfType(pid int, fstype string) []Mountinfo

MountsOfType returns only those mounts for the current or another process (-1 or specific PID) matching the given fstype. Some fstypes are "ext4", "proc", "sysfs", "vfat", and many more. If the pid is invalid then an empty slice is returned instead.

Example

Lists only (bind-mounted) Linux namespaces. In Linux, namespaces are represented in the filesystem using the "nsfs" filesystem type. Tools like Docker or iproute2 (ip netns ...) bind-mount (network) namespaces in additional places outside the /proc filesystem in order to keep them open until the bind-mount is removed again, regardless of whether there are still any processes left using them.

package main

import (
	"fmt"

	mntinfo "github.com/thediveo/go-mntinfo"
)

func main() {
	mounts := mntinfo.MountsOfType(-1, "nsfs")
	for _, mount := range mounts {
		fmt.Printf("namespace %s at %s", mount.Root, mount.MountPoint)
	}
	// namespace net:[4026532757] at /run/docker/netns/4281a40c1612
}
Output:

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL