disks

package
v0.0.0-...-b21f90c Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: GPL-3.0 Imports: 19 Imported by: 55

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlkIDDecodeLabel

func BlkIDDecodeLabel(in string) (string, error)

BlkIDDecodeLabel decodes a string such as a filesystem or partition label encoded by udev in BlkIDEncodeLabel for normal comparison, i.e. "BIOS\x20Boot" becomes "BIOS Boot"

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 CalculateLastUsableLBA

func CalculateLastUsableLBA(device string, diskSize uint64, sectorSize uint64) (uint64, error)

func CandidateByLabelPath

func CandidateByLabelPath(label string) (string, error)

CandidateByLabelPath searches for a filesystem with label matching "label". It tries first an exact match, otherwise it tries again by ignoring capitalization, but in that case it will return a match only if the filesystem is vfat. If found, it returns the path of the symlink in the by-label folder.

func CreateLinearMapperDevice

func CreateLinearMapperDevice(device, name, uuid string, offset, size uint64) (string, error)

CreateLinearMapperDevice creates a linear device mapping of the given device with the given offset and size.

The total size of underlying device must be offset+size, this is not validated by this code.

The mapper device node is returned.

func MockCalculateLastUsableLBA

func MockCalculateLastUsableLBA(value uint64, err error) (restore func())

func MockDeviceNameToDiskMapping

func MockDeviceNameToDiskMapping(mockedDisks map[string]*MockDiskMapping) (restore func())

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

func MockDevicePathToDiskMapping

func MockDevicePathToDiskMapping(mockedDisks map[string]*MockDiskMapping) (restore func())

MockDevicePathToDiskMapping will mock DiskFromDevicePath 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.

func MockPartitionDeviceNodeToDiskMapping

func MockPartitionDeviceNodeToDiskMapping(mockedDisks map[string]*MockDiskMapping) (restore func())

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

func MockUdevPropertiesForDevice

func MockUdevPropertiesForDevice(new func(string, string) (map[string]string, error)) (restore func())

func MountPointsForPartitionRoot

func MountPointsForPartitionRoot(p Partition, matchingMountOptions map[string]string) ([]string, error)

MountPointsForPartitionRoot returns all mounts from the mount table which are for the root directory of the specified partition and have matching mount options as specified. Options not specified in the map argument can have any value or be set or unset, but any option set in the map must match exactly. The order in which they are returned is the same order that they appear in the mount table.

func PartitionUUID

func PartitionUUID(node string) (string, error)

PartitionUUID returns the UUID of a given partition

func PartitionUUIDFromMountPoint

func PartitionUUIDFromMountPoint(mountpoint string, opts *Options) (string, error)

PartitionUUIDFromMountPoint returns the UUID of the partition which is a source of a given mount point.

func RegisterDeviceMapperBackResolver

func RegisterDeviceMapperBackResolver(name string, f func(dmUUID, dmName []byte) (dev string, ok bool))

RegisterDeviceMapperBackResolver takes a callback function which is used when the disks package through some of it's various methods to locate/create a disk needs to trace a device mapper node back to the original device node location such as /dev/mapper/something -> /dev/sda1 -> /dev/sda. The parameters the callback is provided are the device mapper UUID and name parameters from the kernel. If and only if the device mapper handler matches this device mapper node, the callback should return the source device node for the mapper device and true. If the handler does not match a provided device mapper, the function should return "ok" as false. The name of the handler is currently only used in tests.

func SectorSize

func SectorSize(devname string) (uint64, error)

Types

type Disk

type Disk interface {
	// FindMatchingPartitionWithFsLabel finds the partition with a matching
	// 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.
	FindMatchingPartitionWithFsLabel(string) (Partition, error)

	// FindMatchingPartitionWithPartLabel is like
	// FindMatchingPartitionWithFsLabel, 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.
	FindMatchingPartitionWithPartLabel(string) (Partition, error)

	// FindMatchingPartitionUUIDWithFsLabel is like
	// FindMatchingPartitionWithFsLabel, but returns specifically the
	// PartitionUUID. This method will be eliminated soon in favor of all
	// clients using FindMatchingPartitionWithFsLabel instead as it is more
	// generically useful.
	FindMatchingPartitionUUIDWithFsLabel(string) (string, error)

	// FindMatchingPartitionUUIDWithPartLabel is like
	// FindMatchingPartitionWithPartLabel, but returns specifically the
	// PartitionUUID. This method will be eliminated soon in favor of all
	// clients using FindMatchingPartitionWithPartLabel instead as it is more
	// generically useful.
	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

	// DiskID returns the partition table ID, which is either a hexadecimal
	// number for DOS disks, or a UUID for GPT disks.
	DiskID() string

	// Partitions returns all partitions found on a physical disk device. Note
	// that this method, and all others that require discovering partitions on
	// the disk, caches the partitions once first found and does not re-discover
	// partitions again later on if the disk is re-partitioned.
	Partitions() ([]Partition, error)

	// KernelDeviceNode returns the full device node path in /dev/ for the disk
	// such as /dev/mmcblk0 or /dev/vda.
	KernelDeviceNode() string

	// KernelDevicePath returns the full device path in /sys/devices for the
	// disk such as /sys/devices/pci0000:00/0000:00:03.0/virtio1/block/vda/.
	KernelDevicePath() string

	// Schema returns the schema for the disk, either DOS or GPT in lowercase.
	Schema() string

	// SectorSize returns the sector size for the disk in bytes, usually 512,
	// sometimes 4096, possibly even 8196 some day.
	// TODO: make this return a quantity.Size when that is doable without
	// importing gadget
	SectorSize() (uint64, error)

	// SizeInBytes returns the overall size of the disk in bytes. Not all of the
	// bytes may be usable for partitions, as some space on disks is reserved
	// for metadata such as the MBR on DOS disks or the GPT headers (and backup)
	// on GPT disks.
	// TODO: make this return a quantity.Size when that is doable without
	// importing gadget
	SizeInBytes() (uint64, error)

	// UsableSectorsEnd returns the exclusive end of usable sectors on the disk
	// where partitions may occupy and be created. Specifically, the end is not
	// itself usable, it is the region immediately after usable space; this sort
	// of measurement is used when partitioning disks to indicate where a given
	// partition ends.
	// The sector unit is the in the native size for the disk, either 512 or
	// 4096 bytes typically.
	// This measurement is distinct from the size of the disk, though for some
	// disks, this measurement may be the size of the disk in sectors - this is
	// the case for DOS disks, but not for GPT disks. GPT disks have a backup
	// header section at the end of the disk that is not usable for partitions.
	UsableSectorsEnd() (uint64, error)
}

Disk is a single physical disk device that contains partitions.

func AllPhysicalDisks

func AllPhysicalDisks() ([]Disk, error)

func DiskFromDeviceName

func DiskFromDeviceName(deviceName string) (Disk, error)

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

func DiskFromDevicePath

func DiskFromDevicePath(devicePath string) (Disk, error)

DiskFromDeviceName finds a matching Disk using the specified path in the kernel's sysfs, such as /sys/devices/pci0000:00/0000:00:04.0/virtio2/block/vdb.

func DiskFromMountPoint

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

DiskFromMountPoint finds a matching Disk for the specified mount point.

func DiskFromPartitionDeviceNode

func DiskFromPartitionDeviceNode(node string) (Disk, error)

DiskFromPartitionDeviceNode finds a matching Disk that the specified partition node resides on.

type GPTGUID

type GPTGUID [16]byte

type GPTHeader

type GPTHeader struct {
	Signature      [8]byte
	Revision       uint32
	HeaderSize     uint32
	HeaderCRC      uint32
	Reserved       uint32
	CurrentLBA     GPTLBA
	AlternateLBA   GPTLBA
	FirstUsableLBA GPTLBA
	LastUsableLBA  GPTLBA
	DiskGUID       [16]byte
	EntriesLBA     GPTLBA
	NEntries       uint32
	EntrySize      uint32
	EntriesCRC     uint32
}

https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_table_header_(LBA_1)

func LoadGPTHeader

func LoadGPTHeader(devfd *os.File, sectorSize uint64) (GPTHeader, error)

func ReadGPTHeader

func ReadGPTHeader(device string, sectorSize uint64) (GPTHeader, error)

type GPTLBA

type GPTLBA uint64

type MockDiskMapping

type MockDiskMapping struct {
	// TODO: should this be automatically determined if Structure has non-zero
	// len instead?
	DiskHasPartitions bool

	// Structure is the set of partitions or structures on the disk. These
	// partitions are used with Partitions() as well as
	// FindMatchingPartitionWith{Fs,Part}Label
	Structure []Partition

	// static variables for the disk that must be unique for different disks,
	// but note that there are potentially multiple DevNode values that could
	// map to a single disk, but it's not worth encoding that complexity here
	// by making DevNodes a list
	DevNum  string
	DevNode string
	DevPath string

	ID                  string
	DiskSchema          string
	SectorSizeBytes     uint64
	DiskUsableSectorEnd uint64
	DiskSizeInBytes     uint64
}

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) DiskID

func (d *MockDiskMapping) DiskID() string

func (*MockDiskMapping) FindMatchingPartitionUUIDWithFsLabel

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

func (*MockDiskMapping) FindMatchingPartitionUUIDWithPartLabel

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

func (*MockDiskMapping) FindMatchingPartitionWithFsLabel

func (d *MockDiskMapping) FindMatchingPartitionWithFsLabel(label string) (Partition, error)

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

func (*MockDiskMapping) FindMatchingPartitionWithPartLabel

func (d *MockDiskMapping) FindMatchingPartitionWithPartLabel(label string) (Partition, 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) KernelDeviceNode

func (d *MockDiskMapping) KernelDeviceNode() string

func (*MockDiskMapping) KernelDevicePath

func (d *MockDiskMapping) KernelDevicePath() string

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.

func (*MockDiskMapping) Partitions

func (d *MockDiskMapping) Partitions() ([]Partition, error)

func (*MockDiskMapping) Schema

func (d *MockDiskMapping) Schema() string

func (*MockDiskMapping) SectorSize

func (d *MockDiskMapping) SectorSize() (uint64, error)

func (*MockDiskMapping) SizeInBytes

func (d *MockDiskMapping) SizeInBytes() (uint64, error)

func (*MockDiskMapping) UsableSectorsEnd

func (d *MockDiskMapping) UsableSectorsEnd() (uint64, error)

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 Partition

type Partition struct {
	// FilesystemLabel is the encoded filesystem label, this should only be
	// compared with normal Go strings that are encoded with BlkIDEncodeLabel.
	FilesystemLabel string
	// FilesystemUUID is the encoded filesystem UUID, this should be compared
	// with normal Go strings that are encoded with BlkIDEncodeLabel.
	FilesystemUUID string
	// PartitionLabel is the encoded partition label, this should only be
	// compared with normal Go strings that are encoded with BlkIDEncodeLabel.
	PartitionLabel string
	// the partition UUID
	PartitionUUID string

	// Major is the major number for this partition.
	Major int
	// Minor is the minor number for this partition.
	Minor int
	// KernelDevicePath is the kernel device path for this device in /sys for
	// this partition.
	KernelDevicePath string
	// KernelDeviceNode is the kernel device node in /dev.
	KernelDeviceNode string
	// PartitionType is the type of structure, for example 0C in the case of a
	// vfat partition on a DOS disk, or 0FC63DAF-8483-4772-8E79-3D69D8477DE4,
	// which is ext4 on a GPT disk. This is always upper case.
	PartitionType string
	// FilesystemType is the type of filesystem i.e. ext4 or vfat, etc.
	FilesystemType string
	// DiskIndex is the index of the structure on the disk, where the first
	// partition/structure has index of 1.
	DiskIndex uint64
	// StartInBytes is the beginning of the partition/structure in bytes.
	StartInBytes uint64
	// SizeInBytes is the overall size of the partition/structure in bytes.
	SizeInBytes uint64
}

Partition represents a partition on a Disk device.

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