mount

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: BSD-3-Clause Imports: 9 Imported by: 342

Documentation

Overview

Package mount implements mounting, moving, and unmounting file systems.

Index

Constants

View Source
const (
	MS_RDONLY   = unix.MS_RDONLY
	MS_BIND     = unix.MS_BIND
	MS_LAZYTIME = unix.MS_LAZYTIME
	MS_NOEXEC   = unix.MS_NOEXEC
	MS_NOSUID   = unix.MS_NOSUID
	MS_NOUSER   = unix.MS_NOUSER
	MS_RELATIME = unix.MS_RELATIME
	MS_SYNC     = unix.MS_SYNC
	MS_NOATIME  = unix.MS_NOATIME

	ReadOnly = unix.MS_RDONLY | unix.MS_NOATIME
)

Most commonly used mount flags.

View Source
const (
	MNT_FORCE  = unix.MNT_FORCE
	MNT_DETACH = unix.MNT_DETACH
)

Unmount flags.

Variables

View Source
var (
	EXT2     = []byte{0x53, 0xef}
	EXT3     = []byte{0x53, 0xef}
	EXT4     = []byte{0x53, 0xef}
	ISOFS    = []byte{1, 'C', 'D', '0', '0', '1'}
	SQUASHFS = []byte{'h', 's', 'q', 's'}
	XFS      = []byte{'X', 'F', 'S', 'B'}
	// There's no fixed magic number for the different FAT varieties
	// Usually they start with 0xEB but it's not mandatory.
	// Therefore we just list a few examples that we have seen in the wild.
	MSDOS = []byte{0xeb, 0x3c}
	VFAT  = []byte{0xeb, 0x58}
	// QEMU virtual VFAT
	VVFAT = []byte{0xeb, 0x3e}

	AAFS        = []byte{0x5a, 0x3c, 0x69, 0xf0}
	ADFS        = []byte{0xad, 0xf5}
	AFFS        = []byte{0xad, 0xff}
	AFS         = []byte{0x53, 0x46, 0x41, 0x4F}
	BDEVFS      = []byte{0x62, 0x64, 0x65, 0x76}
	BINDERFS    = []byte{0x6c, 0x6f, 0x6f, 0x70}
	BINFMTFS    = []byte{0x42, 0x49, 0x4e, 0x4d}
	BPF         = []byte{0xca, 0xfe, 0x4a, 0x11}
	BTRFS       = []byte{'_', 'B', 'H', 'R', 'f', 'S', '_', 'M'}
	CGROUP      = []byte{0x27, 0xe0, 0xeb}
	CGROUP2     = []byte{0x63, 0x67, 0x72, 0x70}
	CODA        = []byte{0x73, 0x75, 0x72, 0x45}
	CRAMFS      = []byte{0x28, 0xcd, 0x3d, 0x45}
	CRAMFSOther = []byte{0x45, 0x3d, 0xcd, 0x28}
	DAXFS       = []byte{0x64, 0x64, 0x61, 0x78}
	DEBUGFS     = []byte{0x64, 0x62, 0x67, 0x20}
	DEVPTS      = []byte{0x1c, 0xd1}
	ECRYPTFS    = []byte{0xf1, 0x5f}
	EFIVARFS    = []byte{0xde, 0x5e, 0x81, 0xe4}
	EFS         = []byte{0x41, 0x4A, 0x53}
	// EXFAT seems to be a samsung file system.
	// EXFAT       = []byte{0x53, 0xef}
	F2FS      = []byte{0xF2, 0xF5, 0x20, 0x10}
	FUSE      = []byte{0x65, 0x73, 0x55, 0x46}
	FUTEXFS   = []byte{0xBA, 0xD1, 0xDE, 0xA}
	HOSTFS    = []byte{0x00, 0xc0, 0xff, 0xee}
	HPFS      = []byte{0xf9, 0x95, 0xe8, 0x49}
	HUGETLBFS = []byte{0x95, 0x84, 0x58, 0xf6}
	JFFS2     = []byte{0x72, 0xb6}
	JFS       = []byte{0x31, 0x53, 0x46, 0x4a}
	MTD       = []byte{0x11, 0x30, 0x78, 0x54}
	NFS       = []byte{0x69, 0x69}
	NILFS     = []byte{0x34, 0x34}
	NSFS      = []byte{0x6e, 0x73, 0x66, 0x73}
	// From docs, not tested.
	NTFS       = []byte{0xeb, 0x52, 0x90, 'N', 'T', 'F', 'S', ' ', ' ', ' ', ' '}
	OCFS2      = []byte{0x74, 0x61, 0x63, 0x6f}
	OPENPROM   = []byte{0x9f, 0xa1}
	OVERLAYFS  = []byte{0x79, 0x4c, 0x76, 0x30}
	PIPEFS     = []byte{0x50, 0x49, 0x50, 0x45}
	PROC       = []byte{0x9f, 0xa0}
	PSTOREFS   = []byte{0x61, 0x65, 0x67, 0x6C}
	QNX4       = []byte{0x00, 0x2f}
	QNX6       = []byte{0x68, 0x19, 0x11, 0x22}
	RAMFS      = []byte{0x85, 0x84, 0x58, 0xf6}
	RDTGROUP   = []byte{0x76, 0x55, 0x82, 1}
	ROMFS      = []byte{0x72, 0x75}
	SECURITYFS = []byte{0x73, 0x63, 0x66, 0x73}
	SELINUX    = []byte{0xf9, 0x7c, 0xff, 0x8c}
	SMACK      = []byte{0x43, 0x41, 0x5d, 0x53}
	SMB        = []byte{0x51, 0x7B}
	SOCKFS     = []byte{0x53, 0x4F, 0x43, 0x4B}
	SYSFS      = []byte{0x62, 0x65, 0x65, 0x72}
	TMPFS      = []byte{0x01, 0x02, 0x19, 0x94}
	TRACEFS    = []byte{0x74, 0x72, 0x61, 0x63}
	UBIFS      = []byte{0x24, 0x05, 0x19, 0x05}
	UDF        = []byte{0x15, 0x01, 0x33, 0x46}
	USBDEVICE  = []byte{0x9f, 0xa2}
	V9FS       = []byte{0x01, 0x02, 0x19, 0x97}
	XENFS      = []byte{0xab, 0xba, 0x19, 0x74}
	ZONEFS     = []byte{0x5a, 0x4f, 0x46, 0x53}
	ZSMALLOC   = []byte{0x58, 0x29, 0x58, 0x29}
)

These are inferred magic numbers from documents and partitions. Ones known to work are first, followed by a gap, followed by not tested ones. Please preserve this pattern.

Functions

func FSFromBlock

func FSFromBlock(n string) (fs string, flags uintptr, err error)

FSFromBlock determines the file system type of a block device. It returns a string and an error. The error can be for an IO operation, an unknown magic number, or a magic with an unsupported file system. There is still a question here about whether this ought to act like a map and return a bool, not an error, since there are so many bogus block devices and we don't care about most of them.

func FindFileSystem

func FindFileSystem(fstype string) error

FindFileSystem returns nil if a file system is available for use.

It rereads /proc/filesystems each time as the supported file systems can change as modules are added and removed.

func FindMagics

func FindMagics(blk []byte) []magic

FindMagics finds all the magics matching a magic number.

func GetBlockFilesystems

func GetBlockFilesystems() (fstypes []string, err error)

GetBlockFilesystems returns the supported file systems for block devices.

func IsTmpRamfs

func IsTmpRamfs(path string) (bool, error)

IsTmpRamfs tells if the file path given is under a tmpfs or ramfs.

func MoveMount

func MoveMount(oldPath string, newPath string) error

MoveMount moves a mount from oldPath to newPath.

This function is just a wrapper around the MOUNT syscall with the MOVE flag supplied.

func SameFilesystem

func SameFilesystem(path1, path2 string) (bool, error)

SameFilesystem returns true if both paths reside in the same filesystem. This is achieved by comparing Stat_t.Dev, which contains the fs device's major/minor numbers.

func SwitchRoot

func SwitchRoot(newRootDir string, init string) error

SwitchRoot makes newRootDir the new root directory of the system.

To be exact, it makes newRootDir the new root directory of the calling process's mount namespace.

It moves special mounts (dev, proc, sys, run) to the new directory, then does a chroot, moves the root mount to the new directory and finally DELETES EVERYTHING in the old root and execs the given init.

func Unmount

func Unmount(path string, force, lazy bool) error

Unmount detaches any file system mounted at path.

force forces an unmount regardless of currently open or otherwise used files within the file system to be unmounted.

lazy disallows future uses of any files below path -- i.e. it hides the file system mounted at path, but the file system itself is still active and any currently open files can continue to be used. When all references to files from this file system are gone, the file system will actually be unmounted.

Types

type MountPoint

type MountPoint struct {
	Path   string
	Device string
	FSType string
	Flags  uintptr
	Data   string
}

MountPoint represents a mounted file system.

func Mount

func Mount(dev, path, fsType, data string, flags uintptr, opts ...func() error) (*MountPoint, error)

Mount attaches the fsType file system at path.

dev is the device to mount (this is often the path of a block device, name of a file, or a placeholder string). data usually contains arguments for the specific file system.

opts is usually empty, but if you want, e.g., to pre-create the mountpoint, you can call Mount with a mkdirall, e.g. mount.Mount("none", dst, fstype, "", 0,

func() error { return os.MkdirAll(dst, 0o666)})

func TryMount

func TryMount(device, path, data string, flags uintptr, opts ...func() error) (*MountPoint, error)

TryMount tries to mount a device on the given mountpoint, trying in order the supported block device file systems on the system.

func (*MountPoint) String

func (mp *MountPoint) String() string

String implements fmt.Stringer.

func (*MountPoint) Unmount

func (mp *MountPoint) Unmount(flags uintptr) error

Unmount unmounts a file system that was previously mounted.

type Mounter

type Mounter interface {
	// DevName returns the name of the device.
	DevName() string
	// Mount attaches the device at path.
	Mount(path string, flags uintptr, opts ...func() error) (*MountPoint, error)
}

Mounter is a device that can be attached at a file system path.

type Pool

type Pool struct {
	// List of items mounted by this pool.
	MountPoints []*MountPoint
	// contains filtered or unexported fields
}

Pool keeps track of multiple MountPoint.

func (*Pool) Add

func (p *Pool) Add(m ...*MountPoint)

Add adds MountPoints to the pool.

func (*Pool) Mount

func (p *Pool) Mount(mounter Mounter, flags uintptr) (*MountPoint, error)

Mount mounts a file system using Mounter and returns the MountPoint. If the device has already been mounted, it is not mounted again.

Note the pool is keyed on Mounter.DevName() alone meaning DevName is used to determine whether it has already been mounted.

func (*Pool) UnmountAll

func (p *Pool) UnmountAll(flags uintptr) error

UnmountAll umounts all the mountpoints from the pool. This makes a best-effort attempt to unmount everything and cleanup temporary directories. If this function fails, it can be re-tried.

Directories

Path Synopsis
Package block finds, mounts, and modifies block devices on Linux systems.
Package block finds, mounts, and modifies block devices on Linux systems.
Package gpt implements reading and writing of GUID Partition tables.
Package gpt implements reading and writing of GUID Partition tables.
Package loop provides an interface to interacting with Linux loop devices.
Package loop provides an interface to interacting with Linux loop devices.
Chips are made by vendors, and an individual vendor is defined by a 1 to 8 byte vendor id stored in the chip.
Chips are made by vendors, and an individual vendor is defined by a 1 to 8 byte vendor id stored in the chip.
Package scuzz supports direct access to SCSI or SATA devices.
Package scuzz supports direct access to SCSI or SATA devices.

Jump to

Keyboard shortcuts

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