disk

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EFISystemPartitionGUID = "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
	LinuxFilesystemGUID    = "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
)

EFI and Linux partition type GUIDs.

Variables

This section is empty.

Functions

func PartitionNumber

func PartitionNumber(node, disk string) int

PartitionNumber extracts the partition number from a device node path. For example: /dev/sda1 -> 1, /dev/nvme0n1p2 -> 2.

Types

type Commander

type Commander = executil.Commander

Commander abstracts command execution for testing.

type ExecCommander

type ExecCommander = executil.ExecCommander

ExecCommander executes real system commands.

type Manager

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

Manager handles disk operations for provisioning.

func NewManager

func NewManager(cmd Commander) *Manager

NewManager creates a Manager with the given Commander (nil = ExecCommander).

func (*Manager) BindMount

func (m *Manager) BindMount(source, target string) error

BindMount performs a bind mount.

func (*Manager) CheckFilesystem

func (m *Manager) CheckFilesystem(ctx context.Context, device string) error

CheckFilesystem runs e2fsck on the device.

func (*Manager) ChrootRun

func (m *Manager) ChrootRun(ctx context.Context, root, command string) ([]byte, error)

ChrootRun executes a command in a chroot environment. When using a mock commander (tests), the command is routed through the commander. For real execution, the external chroot binary is used. If the chroot binary is not found (e.g., in minimal initramfs), falls back to syscall-based chroot.

func (*Manager) CreateRAIDArray

func (m *Manager) CreateRAIDArray(ctx context.Context, name string, level int, devices []string) error

CreateRAIDArray creates a software RAID array using mdadm. level is the RAID level (0, 1, 5, 6, 10). devices are the member disks.

func (*Manager) DetectDisk

func (m *Manager) DetectDisk(_ context.Context, minSizeGB int) (string, error)

DetectDisk finds the target disk for provisioning. Prefers NVMe, falls back to SATA/SAS.

func (*Manager) DisableLVM

func (m *Manager) DisableLVM(ctx context.Context) error

DisableLVM deactivates LVM volume groups before disk wipe.

func (*Manager) EnableLVM

func (m *Manager) EnableLVM(ctx context.Context) error

EnableLVM activates LVM volume groups.

func (*Manager) FindBootPartition

func (m *Manager) FindBootPartition(parts []Partition) (*Partition, error)

FindBootPartition finds the EFI System Partition.

func (*Manager) FindRootPartition

func (m *Manager) FindRootPartition(parts []Partition) (*Partition, error)

FindRootPartition finds the primary Linux filesystem partition.

func (*Manager) GrowPartition

func (m *Manager) GrowPartition(ctx context.Context, disk string, partNum int) error

GrowPartition grows a partition to fill available space using growpart.

func (*Manager) MountPartition

func (m *Manager) MountPartition(ctx context.Context, device, mountpoint string) error

MountPartition mounts a device at the given mountpoint. Tries all supported filesystem types in order: ext4, xfs, btrfs, ext3, ext2, vfat. Waits up to 10 seconds for the device node to appear (devtmpfs may lag after partprobe).

func (*Manager) ParsePartitions

func (m *Manager) ParsePartitions(ctx context.Context, disk string) ([]Partition, error)

ParsePartitions reads the partition table using sfdisk --json.

sfdisk may emit non-JSON warnings (e.g. "GPT PMBR size mismatch") on stderr which CombinedOutput merges before the JSON body. We strip everything before the first '{' so the JSON decoder sees clean input.

func (*Manager) PartProbe

func (m *Manager) PartProbe(ctx context.Context, disk string) error

PartProbe re-reads partition table and triggers device node creation.

Calls partprobe first; falls back to blockdev --rereadpt if partprobe fails. After re-reading, runs mdev -s (busybox mini-udev) to ensure partition device nodes are created — devtmpfs alone may not create them in minimal initramfs environments without udevd.

func (*Manager) ResizeFilesystem

func (m *Manager) ResizeFilesystem(ctx context.Context, device string) error

ResizeFilesystem resizes the filesystem on the given device. Supports ext2/3/4 (resize2fs), XFS (xfs_growfs), and btrfs.

func (*Manager) SecureEraseAllDisks

func (m *Manager) SecureEraseAllDisks(ctx context.Context) error

SecureEraseAllDisks performs hardware-level secure erase on all disks. For NVMe drives: uses nvme format (User Data Erase). For SATA/SAS drives: uses ATA SECURITY ERASE UNIT via hdparm. Falls back to quick erase (wipefs) if secure erase is not supported.

func (*Manager) SetupChrootBindMounts

func (m *Manager) SetupChrootBindMounts(root string) error

SetupChrootBindMounts creates standard bind mounts for chroot operations.

func (*Manager) StopRAIDArrays

func (m *Manager) StopRAIDArrays(ctx context.Context) error

StopRAIDArrays stops all RAID arrays via mdadm.

func (*Manager) TeardownChrootBindMounts

func (m *Manager) TeardownChrootBindMounts(root string)

TeardownChrootBindMounts unmounts standard bind mounts. Errors are logged to aid debugging of stale mount points.

func (*Manager) Unmount

func (m *Manager) Unmount(target string) error

Unmount unmounts a filesystem.

func (*Manager) WipeAllDisks

func (m *Manager) WipeAllDisks(ctx context.Context) error

WipeAllDisks runs wipefs on all block devices excluding loop and CD-ROM. This performs a quick erase: clears partition tables and filesystem signatures without overwriting data.

type Partition

type Partition struct {
	Node  string `json:"node"`
	Start int64  `json:"start"`
	Size  int64  `json:"size"`
	Type  string `json:"type"`
	Name  string `json:"name,omitempty"`
}

Partition represents a single disk partition from sfdisk output.

Jump to

Keyboard shortcuts

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