md

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package md provides a Go interface to Linux MD (software RAID) arrays via the mdadm(8) utility.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when the target array or member device does not
	// exist.
	ErrNotFound = errors.New("md: array not found")
	// ErrInUse is returned when the array or a member device is busy (mounted,
	// held open, or claimed by another device).
	ErrInUse = errors.New("md: resource in use")
	// ErrExists is returned when a create operation targets a device that is
	// already part of a RAID array, or an array name that already exists.
	ErrExists = errors.New("md: array or member already exists")
	// ErrInvalidArgument is returned when mdadm rejected the command line
	// (bad flags, wrong device count for the level, ...).
	ErrInvalidArgument = errors.New("md: invalid argument")
	// ErrResync is returned when mdadm refuses an operation while the array is
	// performing resync/recovery.
	ErrResync = errors.New("md: resync or recovery in progress")
	// ErrCommand is returned for any non-zero exit that does not match a more
	// specific sentinel.
	ErrCommand = errors.New("md: command failed")
)

Sentinel errors returned by the package. Callers should use errors.Is to dispatch on these rather than parsing the underlying mdadm output, which is human-oriented and may change between mdadm releases.

Functions

func ArrayStateForDevice

func ArrayStateForDevice(device string) (string, error)

ArrayStateForDevice returns the current array state for an MD device.

func DevicePath

func DevicePath(name string) string

DevicePath returns the stable by-id path for an MD array name.

func FindDeviceByMember

func FindDeviceByMember(member string) (string, error)

FindDeviceByMember returns the /dev/mdN node that contains member.

func InactiveArrays

func InactiveArrays() ([]string, error)

InactiveArrays returns assembled MD arrays whose sysfs state is inactive.

func IsSyncing

func IsSyncing(device string) (bool, error)

IsSyncing reports whether an MD device is currently doing sync work.

Types

type CreateOptions

type CreateOptions struct {
	// Level is the mdadm RAID level.
	Level int
	// Metadata is the mdadm metadata format (e.g. "1.0"); empty lets mdadm pick its default.
	Metadata string
	// RaidDevices is the number of active member slots.
	RaidDevices int
	// Devices are the member block devices.
	Devices []string
}

CreateOptions configures Create.

type Detail

type Detail struct {
	// Level is the observed MD level, e.g. raid1.
	Level string
	// RaidDevices is the active RAID device count.
	RaidDevices int
	// UUID is the stable MD array UUID.
	UUID string
	// Name is the metadata-stamped array name.
	Name string
	// DevName is the mdadm map name, if known.
	DevName string
	// Metadata is the MD metadata format/version.
	Metadata string
	// ReshapeActive reports mdadm's reshape-active flag when exported.
	ReshapeActive string
	// Members is the list of attached member devices.
	Members []string
	// MemberRoles maps member device path to mdadm's role value (number or spare).
	MemberRoles map[string]string
}

Detail is the parsed subset of mdadm --detail --export output.

type EventCallback

type EventCallback[T any] struct {
	// contains filtered or unexported fields
}

EventCallback is a thread-safe callback for handling events of type T.

func NewEventCallback

func NewEventCallback[T any](onEvent func(T)) *EventCallback[T]

NewEventCallback creates a new EventCallback for handling events of type T.

func (*EventCallback[T]) Emit

func (ec *EventCallback[T]) Emit(event T)

Emit calls the onEvent callback with the provided event of type T.

type ExecError

type ExecError struct {
	Sentinel error
	ExitCode int
	Stderr   []byte
}

ExecError carries the classified sentinel together with the raw exit code and stderr from a failed mdadm invocation. It is returned by (*MD).run when the command exits non-zero.

The structured fields let server-side handlers log the full diagnostic without surfacing the raw mdadm output to the API client - Error() only renders the sentinel for that reason.

func (*ExecError) Error

func (e *ExecError) Error() string

Error implements the error interface. Returns only the sentinel message so raw mdadm stderr is never leaked through err.Error().

func (*ExecError) Unwrap

func (e *ExecError) Unwrap() error

Unwrap returns the sentinel so errors.Is/As routes through it.

type ExtendOptions

type ExtendOptions struct {
	// Devices are member devices to add before growing.
	Devices []string
	// RaidDevices is the target active RAID device count.
	RaidDevices int
}

ExtendOptions configures Extend.

type MD

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

MD provides methods for managing MD (software RAID) arrays.

func New

func New(opts ...Option) (*MD, error)

New creates a new MD instance, resolving the mdadm binary.

func (*MD) Add

func (md *MD) Add(ctx context.Context, device string, members ...string) error

Add attaches member devices to an existing MD array.

func (*MD) ArrayStateForDevice

func (*MD) ArrayStateForDevice(device string) (string, error)

ArrayStateForDevice returns the current array state for an MD device.

func (*MD) Create

func (md *MD) Create(ctx context.Context, name string, opts CreateOptions) (string, error)

Create creates a new MD array and returns its /dev/mdN node.

func (*MD) Destroy

func (md *MD) Destroy(ctx context.Context, device string) error

Destroy stops an MD array and clears member superblocks.

func (*MD) DetailDevice

func (md *MD) DetailDevice(ctx context.Context, device string) (Detail, error)

DetailDevice returns parsed mdadm --detail --export information for an array.

func (*MD) Extend

func (md *MD) Extend(ctx context.Context, dev string, opts ExtendOptions) error

Extend adds member devices and grows the active RAID device count.

func (*MD) Fail

func (md *MD) Fail(ctx context.Context, device, member string) error

Fail marks a member device failed in an MD array.

func (*MD) FindDeviceByMember

func (*MD) FindDeviceByMember(member string) (string, error)

FindDeviceByMember returns the /dev/mdN node that contains member.

func (*MD) Grow

func (md *MD) Grow(ctx context.Context, device string, raidDevices int) error

Grow changes the active RAID device count for an MD array.

func (*MD) InactiveArrays

func (*MD) InactiveArrays() ([]string, error)

InactiveArrays returns assembled MD arrays whose sysfs state is inactive.

func (*MD) IsSyncing

func (*MD) IsSyncing(device string) (bool, error)

IsSyncing reports whether an MD device is currently doing sync work.

func (*MD) Monitor

func (md *MD) Monitor(ctx context.Context, onEvent func(string)) error

Monitor runs mdadm monitor and calls onEvent for each emitted event line.

onEvent receives only stdout event lines. Errors are not delivered through the callback: stderr is buffered and classified via the process exit, so the terminal "no array" condition surfaces once, as the (quiet) ErrNotFound return value, rather than also being logged as a warning per restart.

func (*MD) Remove

func (md *MD) Remove(ctx context.Context, device, member string) error

Remove detaches a member device from an MD array.

func (*MD) RunArray

func (md *MD) RunArray(ctx context.Context, device string) error

RunArray force-starts an assembled but inactive array.

func (*MD) Shrink

func (md *MD) Shrink(ctx context.Context, dev string, devices []string) error

Shrink removes member devices and reduces the active RAID device count.

func (*MD) Stop

func (md *MD) Stop(ctx context.Context, device string) error

Stop stops an MD array.

func (*MD) SyncActionForDevice

func (*MD) SyncActionForDevice(device string) (SyncAction, error)

SyncActionForDevice returns the current sync action for an MD device.

func (*MD) ZeroSuperblock

func (md *MD) ZeroSuperblock(ctx context.Context, members ...string) error

ZeroSuperblock clears MD metadata from member devices.

It wipes the on-disk regions where MD stores its superblock directly through the block device (covering every metadata version: 1.1 at the start, 1.2 at 4KiB, 1.0 and 0.90 at the device end), rather than shelling out to `mdadm --zero-superblock`. mdadm only clears a superblock it can still parse on the exact device given, so it is a silent no-op on a stale or corrupt superblock, or on a member that has detached from the array - exactly the leftovers that get auto-assembled into a phantom array holding the disk busy and breaking the next array built from it.

Callers must ensure the members are not part of an assembled array (stop it first); a busy device cannot be locked and wiped.

type Option

type Option func(*MD)

Option is a functional option for configuring the MD instance.

func WithMdadmPath

func WithMdadmPath(path string) Option

WithMdadmPath sets an explicit path to the mdadm binary.

type SyncAction

type SyncAction string

SyncAction is the current MD sync action reported by sysfs.

const (
	// SyncActionIdle means no sync operation is running.
	SyncActionIdle SyncAction = "idle"
	// SyncActionResync means a resync is running.
	SyncActionResync SyncAction = "resync"
	// SyncActionRecover means recovery is running.
	SyncActionRecover SyncAction = "recover"
	// SyncActionCheck means a consistency check is running.
	SyncActionCheck SyncAction = "check"
	// SyncActionRepair means a repair is running.
	SyncActionRepair SyncAction = "repair"
	// SyncActionReshape means a reshape is running.
	SyncActionReshape SyncAction = "reshape"
	// SyncActionFrozen means sync operations are frozen.
	SyncActionFrozen SyncAction = "frozen"
)

func SyncActionForDevice

func SyncActionForDevice(device string) (SyncAction, error)

SyncActionForDevice returns the current sync action for an MD device.

Jump to

Keyboard shortcuts

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