disko

package module
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: Apache-2.0 Imports: 7 Imported by: 5

README ¶

📀 disko

Disk Operations API in Go

Actions Status Actions Status codecov Go Report Card

Documentation ¶

Overview ¶

Package disko - disk operations

Index ¶

Constants ¶

View Source
const ExtentSize = 4 * Mebibyte

ExtentSize is extent size for lvm

View Source
const Mebibyte uint64 = 1024 * 1024

Mebibyte defines 1MiB

Variables ¶

This section is empty.

Functions ¶

func GUIDToString ¶

func GUIDToString(bguid GUID) string

GUIDToString - turn a Guid into a string.

Types ¶

type AttachmentType ¶

type AttachmentType int

AttachmentType enumerates the type of device to which the disks are attached to in the system.

const (
	// UnknownAttach - indicates an unknown attachment.
	UnknownAttach AttachmentType = iota

	// RAID - indicates that the device is attached to RAID card
	RAID

	// SCSI - indicates device is attached to scsi, but not a RAID card.
	SCSI

	// ATA - indicates that the device is attached to ATA card
	ATA

	// PCIE - indicates that the device is attached to PCIE card
	PCIE

	// USB - indicates that the device is attached to USB bus
	USB

	// VIRTIO - indicates that the device is attached to virtio.
	VIRTIO

	// IDE - indicates that the device is attached to IDE.
	IDE

	// NBD - a network block device (/dev/nbd0)
	NBD

	// LOOP - a loop device (/dev/loop0)
	LOOP

	// XENBUS - xen blkfront
	XENBUS

	// FILESYSTEM - a file on a filesystem.
	FILESYSTEM
)

func StringToAttachmentType ¶

func StringToAttachmentType(atypeStr string) AttachmentType

StringToAttachmentType - Convert a string to an AttachmentType

func (AttachmentType) MarshalJSON ¶

func (t AttachmentType) MarshalJSON() ([]byte, error)

MarshalJSON - Custom to marshal as a string.

func (AttachmentType) String ¶

func (t AttachmentType) String() string

func (*AttachmentType) UnmarshalJSON ¶

func (t *AttachmentType) UnmarshalJSON(b []byte) error

UnmarshalJSON - reverse of the custom marshler

type Disk ¶

type Disk struct {
	// Name is the kernel name of the disk.
	Name string `json:"name"`

	// Path is the device path of the disk.
	Path string `json:"path"`

	// Size is the size of the disk in bytes.
	Size uint64 `json:"size"`

	// SectorSize is the sector size of the device, if its unknown or not
	// applicable it will return 0.
	SectorSize uint `json:"sectorSize"`

	// ReadOnly - cannot be written to.
	ReadOnly bool `json:"read-only"`

	// Type is the DiskType indicating the type of this disk. This value
	// can be used to determine if the disk is of a particular media type like
	// HDD, SSD or NVMe.
	Type DiskType `json:"type"`

	// Attachment is the type of storage card this disk is attached to.
	// For example: RAID, ATA or PCIE.
	Attachment AttachmentType `json:"attachment"`

	// Partitions is the set of partitions on this disk.
	Partitions PartitionSet `json:"partitions"`

	// TableType is the type of the table
	Table TableType `json:"table"`

	// Properties are a set of properties of this disk.
	Properties PropertySet `json:"properties"`

	// UdevInfo is the disk's udev information.
	UdevInfo UdevInfo `json:"udevInfo"`
}

Disk wraps the disk level operations. It provides basic information about the disk including name, device path, size etc.

func (Disk) Details ¶

func (d Disk) Details() string

Details returns the disk details as a table formatted string.

func (*Disk) FreeSpaces ¶

func (d *Disk) FreeSpaces() []FreeSpace

FreeSpaces returns a list of slots of free spaces on the disk. These slots can be used to create new partitions.

func (*Disk) FreeSpacesWithMin ¶

func (d *Disk) FreeSpacesWithMin(minSize uint64) []FreeSpace

FreeSpacesWithMin returns a list of freespaces that are minSize long or more.

func (Disk) String ¶

func (d Disk) String() string

type DiskFilter ¶

type DiskFilter func(Disk) bool

DiskFilter is filter function that returns true if the matching disk is accepted false otherwise.

type DiskSet ¶

type DiskSet map[string]Disk

DiskSet is a map of the kernel device name and the disk.

func (DiskSet) Details ¶

func (ds DiskSet) Details() string

Details prints the details of the disks in the disk set ina a tabular format.

type DiskType ¶

type DiskType int

DiskType enumerates supported disk types.

const (
	// HDD - hard disk drive
	HDD DiskType = iota

	// SSD - solid state disk
	SSD

	// NVME - Non-volatile memory express
	NVME

	// TYPEFILE - A file on disk, not a block device.
	TYPEFILE
)

func StringToDiskType ¶

func StringToDiskType(typeStr string) DiskType

StringToDiskType - convert a string to a disk type.

func (DiskType) MarshalJSON ¶

func (t DiskType) MarshalJSON() ([]byte, error)

MarshalJSON - Custom to marshal as a string.

func (DiskType) String ¶

func (t DiskType) String() string

func (*DiskType) UnmarshalJSON ¶

func (t *DiskType) UnmarshalJSON(b []byte) error

UnmarshalJSON - custom to read as string or int.

type FreeSpace ¶

type FreeSpace struct {
	Start uint64 `json:"start"`
	Last  uint64 `json:"last"`
}

FreeSpace indicates a free slot on the disk with a Start and Last offset, where a partition can be created.

func (*FreeSpace) Size ¶

func (f *FreeSpace) Size() uint64

Size returns the size of the free space, which is End - Start.

type GUID ¶

type GUID [16]byte

GUID - a 16 byte Globally Unique ID

func GenGUID ¶

func GenGUID() GUID

GenGUID - generate a random uuid and return it

func StringToGUID ¶

func StringToGUID(sguid string) (GUID, error)

StringToGUID - convert a string to a GUID

func (GUID) String ¶

func (g GUID) String() string

type LV ¶

type LV struct {
	// Name is the name of the logical volume.
	Name string `json:"name"`

	// UUID for the LV
	UUID string `json:"uuid"`

	// Path is the full path of the logical volume.
	Path string `json:"path"`

	// Size the size of the logical volume.
	Size uint64 `json:"size"`

	// Type is the type of logical volume.
	Type LVType `json:"type"`

	// The volume group that this logical volume is part of.
	VGName string `json:"vgname"`

	// Encrypted indicates if the logical volume is encrypted.
	Encrypted bool `json:"encrypted"`

	// DecryptedLVName is the name of the decrypted logical volume as set by
	// the CryptOpen call.
	DecryptedLVName string `json:"decryptedLVName"`

	// DecryptedLVPath is the full path of the decrypted logical volume. This
	// is set only for encrypted volumes, using the CryptFormat.
	DecryptedLVPath string `json:"decryptedLVPath"`
}

LV interface wraps the lvm logical volume information and operations. A logical volume partitions a volume group into a slice of capacity that can be used a block device to create a file system.

type LVFilter ¶

type LVFilter func(LV) bool

LVFilter is filter function that returns true if the matching lv is accepted false otherwise.

type LVSet ¶

type LVSet map[string]LV

LVSet is a map of LV names to the LV.

type LVType ¶

type LVType int

LVType defines the type of the logical volume.

const (
	// THICK indicates thickly provisioned logical volume.
	THICK LVType = iota

	// THIN indicates thinly provisioned logical volume.
	THIN

	// THINPOOL indicates a pool lv for other lvs
	THINPOOL

	// LVTypeUnknown - unknown type
	LVTypeUnknown
)

func (LVType) MarshalJSON ¶

func (t LVType) MarshalJSON() ([]byte, error)

MarshalJSON - serialize to json

func (LVType) String ¶

func (t LVType) String() string

func (*LVType) UnmarshalJSON ¶

func (t *LVType) UnmarshalJSON(b []byte) error

UnmarshalJSON - custom to read as strings or int

type PV ¶

type PV struct {
	// Name returns the name of the PV.
	Name string `json:"name"`

	// UUID for the PV
	UUID string `json:"uuid"`

	// Path returns the device path of the PV.
	Path string `json:"path"`

	// Size returns the size of the PV.
	Size uint64 `json:"size"`

	// The volume group this PV is part of ("" if none)
	VGName string `json:"vgname"`

	// FreeSize returns the free size of the PV.
	FreeSize uint64 `json:"freeSize"`
}

PV wraps a LVM physical volume. A lvm physical volume is the raw block device or other disk like devices that provide storage capacity.

type PVFilter ¶

type PVFilter func(PV) bool

PVFilter is filter function that returns true if the matching pv is accepted false otherwise.

type PVSet ¶

type PVSet map[string]PV

PVSet is a set of PVs indexed by their names.

type PartType ¶

type PartType GUID

PartType represents a GPT Partition GUID

func StringToPartType ¶

func StringToPartType(s string) (PartType, error)

StringToPartType - convert a string to a partition type.

func (PartType) String ¶

func (p PartType) String() string

type Partition ¶

type Partition struct {
	// Start is the offset in bytes of the start of this partition.
	Start uint64 `json:"start"`

	// Last is the last byte that is part of this partition.
	Last uint64 `json:"last"`

	// ID is the partition id.
	ID GUID `json:"id"`

	// Type is the partition type.
	Type PartType `json:"type"`

	// Name is the name of this partition.
	Name string `json:"name"`

	// Number is the number of this partition.
	Number uint `json:"number"`
}

Partition wraps the disk partition information.

func (Partition) MarshalJSON ¶

func (p Partition) MarshalJSON() ([]byte, error)

MarshalJSON - serialize to json

func (*Partition) Size ¶

func (p *Partition) Size() uint64

Size returns the size of the partition in bytes.

func (*Partition) UnmarshalJSON ¶

func (p *Partition) UnmarshalJSON(b []byte) error

UnmarshalJSON - unserialize from json

type PartitionSet ¶

type PartitionSet map[uint]Partition

PartitionSet is a map of partition number to the partition.

type Property ¶

type Property string

Property - a property of a disk such

const (
	// Ephemeral - A cloud ephemeral disk.
	Ephemeral Property = "EPHEMERAL"
)

type PropertySet ¶

type PropertySet map[Property]bool

PropertySet - a group of properties of a disk

func (PropertySet) MarshalJSON ¶

func (p PropertySet) MarshalJSON() ([]byte, error)

MarshalJSON - serialize to json

func (PropertySet) String ¶

func (p PropertySet) String() string

func (*PropertySet) UnmarshalJSON ¶

func (p *PropertySet) UnmarshalJSON(b []byte) error

UnmarshalJSON - json unserialize

type System ¶

type System interface {
	// ScanAllDisks scans the system for all available disks and returns a
	// set of disks that are accepted by the filter function. Use this function
	// if you dont know the device paths for the specific disks to be scanned.
	ScanAllDisks(filter DiskFilter) (DiskSet, error)

	// ScanDisks scans the system for disks identified by the specified paths
	// and returns a set of disks that are accepted by the filter function.
	ScanDisks(filter DiskFilter, paths ...string) (DiskSet, error)

	// ScanDisk scans the system for a single disk specified by the device path.
	ScanDisk(path string) (Disk, error)

	// CreatePartition creates a partition on the is disk with the specified
	// partition number, type and disk offsets.
	CreatePartition(Disk, Partition) error

	// CreatePartitions creates multiple partitions on disk.
	CreatePartitions(Disk, PartitionSet) error

	// UpdatePartition updates multiple existing partitions on a disk.
	UpdatePartition(Disk, Partition) error

	// UpdatePartitions updates multiple existing partitions on a disk.
	UpdatePartitions(Disk, PartitionSet) error

	// DeletePartition deletes the specified partition.
	DeletePartition(Disk, uint) error

	// Wipe wipes the disk to make it a clean disk. All partitions and data
	// on the disk will be lost.
	Wipe(Disk) error
}

System interface provides system level disk and lvm methods that are implemented by the specific system.

type TableType ¶

type TableType int

TableType enumerates the type of device to which the disks are attached to in the system.

const (
	// TableNone - no table type
	TableNone TableType = iota

	// MBR - a Master Boot Record style partition table.
	MBR

	// GPT - a Guid Partition Table style partition table
	GPT
)

func StringToTableType ¶

func StringToTableType(atypeStr string) TableType

StringToTableType - Convert a string to an TableType

func (TableType) MarshalJSON ¶

func (t TableType) MarshalJSON() ([]byte, error)

MarshalJSON - Custom to marshal as a string.

func (TableType) String ¶

func (t TableType) String() string

func (*TableType) UnmarshalJSON ¶

func (t *TableType) UnmarshalJSON(b []byte) error

UnmarshalJSON - reverse of the custom marshler

type UdevInfo ¶

type UdevInfo struct {
	// Name of the disk
	Name string `json:"name"`

	// SysPath is the system path of this device.
	SysPath string `json:"sysPath"`

	// Symlinks for the disk.
	Symlinks []string `json:"symLinks"`

	// Properties is udev information as a map of key, value pairs.
	Properties map[string]string `json:"properties"`
}

UdevInfo captures the udev information about a disk.

type VG ¶

type VG struct {
	// Name is the name of the volume group.
	Name string `json:"name"`

	// UUID for the VG
	UUID string `json:"uuid"`

	// Size is the current size of the volume group.
	Size uint64 `json:"size"`

	// Volumes is set of all the volumes in this volume group.
	Volumes LVSet `json:"volumes"`

	// FreeSpace is the amount free space left in the volume group.
	FreeSpace uint64 `json:"freeSpace"`

	// PVs is the set of PVs that belongs to this VG.
	PVs PVSet `json:"pvs"`
}

VG wraps a LVM volume group. A volume group combines one or more physical volumes into storage pools and provides a unified logical device with combined storage capacity of the underlying physical volumes.

type VGFilter ¶

type VGFilter func(VG) bool

VGFilter is filter function that returns true if the matching vg is accepted false otherwise.

type VGSet ¶

type VGSet map[string]VG

VGSet is set of volume groups indexed by their name.

func (VGSet) Details ¶

func (vgs VGSet) Details() string

Details returns a formatted string with the information of volume groups.

type VolumeManager ¶

type VolumeManager interface {
	// ScanPVs scans the system for all the PVs and returns the set of PVs that
	// are accepted by the filter function.
	ScanPVs(filter PVFilter) (PVSet, error)

	// ScanVGs scans the systems for all the VGs and returns the set of VGs that
	// are accepted by the filter function.
	ScanVGs(filter VGFilter) (VGSet, error)

	// CreatePV creates a PV with specified name.
	CreatePV(diskName string) (PV, error)

	// DeletePV deletes the specified PV.
	DeletePV(pv PV) error

	// HasPV returns true if the pv exists. This indicates that the device
	// already has an lvm pv header.
	HasPV(name string) bool

	// CreateVG creates a VG with specified name and adds the provided pvs to
	// this vg.
	CreateVG(name string, pvs ...PV) (VG, error)

	// ExtendVG extends the volument group storage capacity with the specified
	// PVs.
	ExtendVG(vgName string, pvs ...PV) error

	// Delete deletes this VG and all the LVs in the VG.
	RemoveVG(vgName string) error

	// HasVG returns true if the vg exists.
	HasVG(vgName string) bool

	// CryptFormat setups up encryption for this volume using the provided key.
	CryptFormat(vgName string, lvName string, key string) error

	// CryptOpen opens the encrypted logical volume for use using the provided
	// key.
	CryptOpen(vgName string, lvName string, decryptedName string, key string) error

	// CryptClose close the encrypted logical volume using the provided key.
	CryptClose(vgName string, lvName string, decryptedName string) error

	// CreateLV creates a LV with specified name, size and type.
	CreateLV(vgName string, name string, size uint64, lvType LVType) (LV, error)

	// RemoveLV removes this LV.
	RemoveLV(vgName string, lvName string) error

	// RenameLV renames this LV to newLvName.
	RenameLV(vgName string, lvName string, newLvName string) error

	// ExtendLV expands the LV to the requested new size.
	ExtendLV(vgName string, lvName string, newSize uint64) error

	// HasVG returns true if the lv exists.
	HasLV(vgName string, name string) bool
}

VolumeManager provides logical volume oprations that allows for creation and management of volume groups, physical volumes and logical volumes.

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

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