disk

package
v0.9.0-alpha.20 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package disk provides utilities for disk operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDeviceTypeFunc added in v0.5.0

func DefaultDeviceTypeFunc(dt string) bool

DefaultDeviceTypeFunc returns true for common block device types. This function is used by the disk component to filter which devices to monitor.

Supported device types:

  • "disk": Physical disks (HDDs, SSDs, NVMe)
  • "part": Disk partitions
  • "lvm": Logical Volume Manager volumes
  • "raid*": Software RAID devices (raid0, raid1, raid5, raid10, etc.)
  • "md*": MD (multiple device) RAID arrays (md, md0, md127, etc.)

RAID/MD support was added to fix https://github.com/leptonai/gpud/issues/1107 where RAID devices containing the root filesystem were being filtered out, causing false "no block device found" warnings.

func DefaultExt4FsTypeFunc added in v0.5.0

func DefaultExt4FsTypeFunc(fsType string) bool

func DefaultFsTypeFunc added in v0.5.0

func DefaultFsTypeFunc(fsType string) bool

func DefaultMatchFuncDeviceType

func DefaultMatchFuncDeviceType(deviceType string) bool

func DefaultMountPointFunc added in v0.6.0

func DefaultMountPointFunc(mountPoint string) bool

func DefaultNFSFsTypeFunc added in v0.5.0

func DefaultNFSFsTypeFunc(fsType string) bool

func FindFsTypeAndDeviceByMinorNumber added in v0.3.8

func FindFsTypeAndDeviceByMinorNumber(minor int) (string, string, error)

FindFsTypeAndDeviceByMinorNumber retrieves the filesystem type and device name for a given minor number. If not found, it returns empty strings.

func FindMntTargetDevice

func FindMntTargetDevice(dir string) (string, string, error)

FindMntTargetDevice returns the device name and file system type of the mount target. Implements "findmnt --target [DIRECTORY]". It returns an empty string and no error if the target is not found.

Types

type BlockDevice

type BlockDevice struct {
	Name             string        `json:"name,omitempty"`
	ParentDeviceName string        `json:"parent_device_name,omitempty"`
	Type             string        `json:"type,omitempty"`
	Size             CustomUint64  `json:"size,omitempty"`
	Rota             CustomBool    `json:"rota,omitempty"`
	Serial           string        `json:"serial,omitempty"`
	WWN              string        `json:"wwn,omitempty"`
	Vendor           string        `json:"vendor,omitempty"`
	Model            string        `json:"model,omitempty"`
	Rev              string        `json:"rev,omitempty"`
	MountPoint       string        `json:"mountpoint,omitempty"`
	FSType           string        `json:"fstype,omitempty"`
	FSUsed           CustomUint64  `json:"fsused,omitempty"`
	PartUUID         string        `json:"partuuid,omitempty"`
	PKName           string        `json:"-"`
	Children         []BlockDevice `json:"children,omitempty"`
}

BlockDevice represents output of lsblk command for a device

type BlockDevices

type BlockDevices []BlockDevice

func GetBlockDevicesWithLsblk added in v0.5.0

func GetBlockDevicesWithLsblk(ctx context.Context, opts ...OpOption) (BlockDevices, error)

GetBlockDevicesWithLsblk run "lsblk" command for device and construct BlockDevice struct based on output Receives device path. If device is empty string, info about all devices will be collected Returns slice of BlockDevice structs or error if something went wrong

func (BlockDevices) Flatten added in v0.5.0

func (blks BlockDevices) Flatten() FlattenedBlockDevices

func (BlockDevices) GetTotalBytes

func (blks BlockDevices) GetTotalBytes() uint64

Returns the total bytes of all block devices.

func (BlockDevices) RenderTable

func (blks BlockDevices) RenderTable(wr io.Writer)

type CustomBool

type CustomBool struct {
	Bool bool
}

CustomBool to handle Rota lsblk output - true/false or "1"/"0"

func (CustomBool) MarshalJSON

func (cb CustomBool) MarshalJSON() ([]byte, error)

MarshalJSON customizes rota marshaling

func (*CustomBool) UnmarshalJSON

func (cb *CustomBool) UnmarshalJSON(data []byte) error

UnmarshalJSON customizes string rota unmarshaling

type CustomUint64 added in v0.5.0

type CustomUint64 struct {
	Uint64 uint64
}

CustomUint64 to handle Size lsblk output - 8001563222016 or "8001563222016"

func (*CustomUint64) MarshalJSON added in v0.5.0

func (ci *CustomUint64) MarshalJSON() ([]byte, error)

MarshalJSON customizes size marshaling

func (*CustomUint64) UnmarshalJSON added in v0.5.0

func (ci *CustomUint64) UnmarshalJSON(data []byte) error

UnmarshalJSON customizes string size unmarshaling "63.9M" should be parsed to 63.9 million (63900000)

type DeviceUsage added in v0.5.0

type DeviceUsage struct {
	DeviceName string `json:"device_name,omitempty"`
	MountPoint string `json:"mountpoint,omitempty"`

	TotalBytes uint64 `json:"total_bytes"`
	FreeBytes  uint64 `json:"free_bytes"`
	UsedBytes  uint64 `json:"used_bytes"`
}

DeviceUsage is derived from the output of "lsblk" command, and the size and usage information based on its mount point

type DeviceUsages added in v0.5.0

type DeviceUsages []DeviceUsage

func (DeviceUsages) RenderTable added in v0.5.0

func (devs DeviceUsages) RenderTable(wr io.Writer)

type FindMntOutput added in v0.3.6

type FindMntOutput struct {
	// The input mount target.
	Target string `json:"target"`

	Filesystems []FoundMnt `json:"filesystems"`
}

Represents the output of the command "findmnt --target /var/lib/kubelet --json --df". ref. https://man7.org/linux/man-pages/man8/findmnt.8.html

func FindMnt added in v0.3.6

func FindMnt(ctx context.Context, target string) (*FindMntOutput, error)

Runs "findmnt --target [TARGET] --json --df" and parses the output.

func ParseFindMntOutput added in v0.3.6

func ParseFindMntOutput(output string) (*FindMntOutput, error)

type FlattenedBlockDevice added in v0.5.0

type FlattenedBlockDevice struct {
	Name       string `json:"name,omitempty"`
	Type       string `json:"type,omitempty"`
	Size       uint64 `json:"size,omitempty"`
	Rota       bool   `json:"rota,omitempty"`
	Serial     string `json:"serial,omitempty"`
	WWN        string `json:"wwn,omitempty"`
	Vendor     string `json:"vendor,omitempty"`
	Model      string `json:"model,omitempty"`
	Rev        string `json:"rev,omitempty"`
	MountPoint string `json:"mountpoint,omitempty"`
	FSType     string `json:"fstype,omitempty"`
	FSUsed     uint64 `json:"fsused,omitempty"`
	PartUUID   string `json:"partuuid,omitempty"`

	Parents  []string `json:"parents,omitempty"`
	Children []string `json:"children,omitempty"`
}

FlattenedBlockDevice represents the flattened output of lsblk command for a device, meaning the child device is included as its own entry in the device list.

type FlattenedBlockDevices added in v0.5.0

type FlattenedBlockDevices []FlattenedBlockDevice

func (FlattenedBlockDevices) GetDeviceUsages added in v0.5.0

func (blks FlattenedBlockDevices) GetDeviceUsages(parts Partitions) DeviceUsages

func (FlattenedBlockDevices) RenderTable added in v0.5.0

func (blks FlattenedBlockDevices) RenderTable(wr io.Writer)

type FoundMnt added in v0.3.6

type FoundMnt struct {
	// Regardless of the input mount target, this is where the target is mounted.
	MountedPoint string `json:"mounted_point"`

	// The filesystem may use more block devices.
	// This is why findmnt provides  SOURCE and SOURCES (pl.) columns.
	// ref. https://man7.org/linux/man-pages/man8/findmnt.8.html
	Sources []string `json:"sources"`

	Fstype string `json:"fstype"`

	SizeHumanized string `json:"size_humanized"`
	SizeBytes     uint64 `json:"size_bytes"`

	UsedHumanized string `json:"used_humanized"`
	UsedBytes     uint64 `json:"used_bytes"`

	AvailableHumanized string `json:"available_humanized"`
	AvailableBytes     uint64 `json:"available_bytes"`

	UsedPercentHumanized string  `json:"used_percent_humanized"`
	UsedPercent          float64 `json:"used_percent"`
}

type MatchFunc

type MatchFunc func(fs string) bool

type Op

type Op struct {
	// contains filtered or unexported fields
}

type OpOption

type OpOption func(*Op)

func WithDeviceType

func WithDeviceType(matchFunc MatchFunc) OpOption

func WithFstype

func WithFstype(matchFunc MatchFunc) OpOption

func WithMountPoint added in v0.6.0

func WithMountPoint(matchFunc MatchFunc) OpOption

WithMountPoint is used to filter out devices. This is useful for filtering out devices that are not mounted, such as swap devices.

func WithSkipUsage added in v0.5.0

func WithSkipUsage() OpOption

func WithStatTimeout added in v0.6.0

func WithStatTimeout(timeout time.Duration) OpOption

type Partition

type Partition struct {
	Device string `json:"device"`

	Fstype     string `json:"fstype"`
	MountPoint string `json:"mount_point"`
	Mounted    bool   `json:"mounted"`
	// StatTimedOut is true if the stat operation timed out.
	StatTimedOut bool `json:"stat_timed_out"`

	Usage *Usage `json:"usage"`
}

type Partitions

type Partitions []Partition

func GetPartitions

func GetPartitions(ctx context.Context, opts ...OpOption) (Partitions, error)

func (Partitions) GetMountedTotalBytes

func (parts Partitions) GetMountedTotalBytes() uint64

Returns the total bytes of all mounted partitions.

func (Partitions) RenderTable

func (parts Partitions) RenderTable(wr io.Writer)

type Usage

type Usage struct {
	TotalBytes uint64 `json:"total_bytes"`
	FreeBytes  uint64 `json:"free_bytes"`
	UsedBytes  uint64 `json:"used_bytes"`
}

func GetUsage

func GetUsage(ctx context.Context, mountPoint string) (*Usage, error)

Jump to

Keyboard shortcuts

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