Documentation
¶
Overview ¶
Package md provides a Go interface to Linux MD (software RAID) arrays via the mdadm(8) utility.
Index ¶
- Variables
- func ArrayStateForDevice(device string) (string, error)
- func DevicePath(name string) string
- func FindDeviceByMember(member string) (string, error)
- func InactiveArrays() ([]string, error)
- func IsSyncing(device string) (bool, error)
- type CreateOptions
- type Detail
- type EventCallback
- type ExecError
- type ExtendOptions
- type MD
- func (md *MD) Add(ctx context.Context, device string, members ...string) error
- func (*MD) ArrayStateForDevice(device string) (string, error)
- func (md *MD) Create(ctx context.Context, name string, opts CreateOptions) (string, error)
- func (md *MD) Destroy(ctx context.Context, device string) error
- func (md *MD) DetailDevice(ctx context.Context, device string) (Detail, error)
- func (md *MD) Extend(ctx context.Context, dev string, opts ExtendOptions) error
- func (md *MD) Fail(ctx context.Context, device, member string) error
- func (*MD) FindDeviceByMember(member string) (string, error)
- func (md *MD) Grow(ctx context.Context, device string, raidDevices int) error
- func (*MD) InactiveArrays() ([]string, error)
- func (*MD) IsSyncing(device string) (bool, error)
- func (md *MD) Monitor(ctx context.Context, onEvent func(string)) error
- func (md *MD) Remove(ctx context.Context, device, member string) error
- func (md *MD) RunArray(ctx context.Context, device string) error
- func (md *MD) Shrink(ctx context.Context, dev string, devices []string) error
- func (md *MD) Stop(ctx context.Context, device string) error
- func (*MD) SyncActionForDevice(device string) (SyncAction, error)
- func (md *MD) ZeroSuperblock(ctx context.Context, members ...string) error
- type Option
- type SyncAction
Constants ¶
This section is empty.
Variables ¶
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 ¶
ArrayStateForDevice returns the current array state for an MD device.
func DevicePath ¶
DevicePath returns the stable by-id path for an MD array name.
func FindDeviceByMember ¶
FindDeviceByMember returns the /dev/mdN node that contains member.
func InactiveArrays ¶
InactiveArrays returns assembled MD arrays whose sysfs state is inactive.
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 ¶
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.
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 (*MD) ArrayStateForDevice ¶
ArrayStateForDevice returns the current array state for an MD device.
func (*MD) DetailDevice ¶
DetailDevice returns parsed mdadm --detail --export information for an array.
func (*MD) FindDeviceByMember ¶
FindDeviceByMember returns the /dev/mdN node that contains member.
func (*MD) InactiveArrays ¶
InactiveArrays returns assembled MD arrays whose sysfs state is inactive.
func (*MD) Monitor ¶
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) SyncActionForDevice ¶
func (*MD) SyncActionForDevice(device string) (SyncAction, error)
SyncActionForDevice returns the current sync action for an MD device.
func (*MD) ZeroSuperblock ¶
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 ¶
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.