disks

package
v0.0.0-...-c7fba9c Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlkIDEncodeLabel

func BlkIDEncodeLabel(in string) string

BlkIDEncodeLabel encodes a name for use as a partition or filesystem label symlink by udev. The result matches the output of blkid_encode_string() from libblkid.

func MockDeviceNameDisksToPartitionMapping

func MockDeviceNameDisksToPartitionMapping(mockedMountPoints map[string]*MockDiskMapping) (restore func())

MockDeviceNameDisksToPartitionMapping will mock DiskFromDeviceName such that the provided map of device names to mock disks is used instead of the actual implementation using udev.

func MockMountPointDisksToPartitionMapping

func MockMountPointDisksToPartitionMapping(mockedMountPoints map[Mountpoint]*MockDiskMapping) (restore func())

MockMountPointDisksToPartitionMapping will mock DiskFromMountPoint such that the specified mapping is returned/used. Specifically, keys in the provided map are mountpoints, and the values for those keys are the disks that will be returned from DiskFromMountPoint or used internally in MountPointIsFromDisk.

Types

type Disk

type Disk interface {
	// FindMatchingPartitionUUIDWithFsLabel finds the partition uuid for a
	// partition matching the specified filesystem label on the disk. Note that
	// for non-ascii labels like "Some label", the label will be encoded using
	// \x<hex> for potentially non-safe characters like in "Some\x20Label".
	// If the filesystem label was not found on the disk, and no other errors
	// were encountered, a PartitionNotFoundError will be returned.
	FindMatchingPartitionUUIDWithFsLabel(string) (string, error)

	// FindMatchingPartitionUUIDWithPartLabel is like
	// FindMatchingPartitionUUIDWithFsLabel, but searches for a partition that
	// has a matching partition label instead of the filesystem label. The same
	// encoding scheme is performed on the label as in that function.
	FindMatchingPartitionUUIDWithPartLabel(string) (string, error)

	// MountPointIsFromDisk returns whether the specified mountpoint corresponds
	// to a partition on the disk. Note that this only considers partitions
	// and mountpoints found when the disk was identified with
	// DiskFromMountPoint.
	// TODO: make this function return what a Disk of where the mount point
	//       is actually from if it is not from the same disk for better
	//       error reporting
	MountPointIsFromDisk(string, *Options) (bool, error)

	// Dev returns the string "major:minor" number for the disk device.
	Dev() string

	// HasPartitions returns whether the disk has partitions or not. A physical
	// disk will have partitions, but a mapper device will just be a volume that
	// does not have partitions for example.
	HasPartitions() bool
}

Disk is a single physical disk device that contains partitions.

func DiskFromDeviceName

func DiskFromDeviceName(deviceName string) (Disk, error)

DiskFromDeviceName finds a matching Disk using the specified name, such as vda, or mmcblk0, etc.

func DiskFromMountPoint

func DiskFromMountPoint(mountpoint string, opts *Options) (Disk, error)

DiskFromMountPoint finds a matching Disk for the specified mount point.

type MockDiskMapping

type MockDiskMapping struct {
	// FilesystemLabelToPartUUID is a mapping of the udev encoded filesystem
	// labels to the expected partition uuids.
	FilesystemLabelToPartUUID map[string]string
	// PartitionLabelToPartUUID is a mapping of the udev encoded partition
	// labels to the expected partition uuids.
	PartitionLabelToPartUUID map[string]string
	DiskHasPartitions        bool
	DevNum                   string
}

MockDiskMapping is an implementation of Disk for mocking purposes, it is exported so that other packages can easily mock a specific disk layout without needing to mock the mount setup, sysfs, or udev commands just to test high level logic. DevNum must be a unique string per unique mocked disk, if only one disk is being mocked it can be left empty.

func (*MockDiskMapping) Dev

func (d *MockDiskMapping) Dev() string

Dev returns a unique representation of the mock disk that is suitable for comparing two mock disks to see if they are the same. Part of the Disk interface.

func (*MockDiskMapping) FindMatchingPartitionUUIDWithFsLabel

func (d *MockDiskMapping) FindMatchingPartitionUUIDWithFsLabel(label string) (string, error)

FindMatchingPartitionUUIDWithFsLabel returns a matching PartitionUUID for the specified filesystem label if it exists. Part of the Disk interface.

func (*MockDiskMapping) FindMatchingPartitionUUIDWithPartLabel

func (d *MockDiskMapping) FindMatchingPartitionUUIDWithPartLabel(label string) (string, error)

FindMatchingPartitionUUIDWithPartLabel returns a matching PartitionUUID for the specified filesystem label if it exists. Part of the Disk interface.

func (*MockDiskMapping) HasPartitions

func (d *MockDiskMapping) HasPartitions() bool

HasPartitions returns if the mock disk has partitions or not. Part of the Disk interface.

func (*MockDiskMapping) MountPointIsFromDisk

func (d *MockDiskMapping) MountPointIsFromDisk(mountpoint string, opts *Options) (bool, error)

MountPointIsFromDisk returns if the disk that the specified mount point comes from is the same disk as the object. Part of the Disk interface.

type Mountpoint

type Mountpoint struct {
	Mountpoint        string
	IsDecryptedDevice bool
}

Mountpoint is a combination of a mountpoint location and whether that mountpoint is a decrypted device. It is only used in identifying mount points with MountPointIsFromDisk and DiskFromMountPoint with MockMountPointDisksToPartitionMapping.

type Options

type Options struct {
	// IsDecryptedDevice indicates that the mountpoint is referring to a
	// decrypted device.
	IsDecryptedDevice bool
}

Options is a set of options used when querying information about partition and disk devices.

type PartitionNotFoundError

type PartitionNotFoundError struct {
	SearchType  string
	SearchQuery string
}

PartitionNotFoundError is an error where a partition matching the SearchType was not found. SearchType can be either "partition-label" or "filesystem-label" to indicate searching by the partition label or the filesystem label on a given disk. SearchQuery is the specific query parameter attempted to be used.

func (PartitionNotFoundError) Error

func (e PartitionNotFoundError) Error() string

Jump to

Keyboard shortcuts

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