disk

package
v0.0.18 Latest Latest
Warning

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

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

Documentation

Index

Constants

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

EFI and Linux partition type GUIDs.

View Source
const LinuxLVMGUID = "E6D6D379-F507-44C2-A23C-238F2A3DF928"

LinuxLVMGUID is the GPT type GUID for Linux LVM partitions.

Variables

This section is empty.

Functions

func DetectNVMeControllers added in v0.0.8

func DetectNVMeControllers() ([]string, error)

DetectNVMeControllers lists NVMe controllers in /dev/.

func GenerateFstab added in v0.0.8

func GenerateFstab(layout *config.PartitionLayout, device string) string

GenerateFstab creates an fstab file for the partition layout.

func GenerateLVMFstab added in v0.0.8

func GenerateLVMFstab(lvm *config.LVMConfig) string

GenerateLVMFstab adds LVM volume fstab entries.

func PartitionDevicePath added in v0.0.8

func PartitionDevicePath(device string, num int) string

PartitionDevicePath returns the device path for a specific partition number.

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) ApplyLVMConfig added in v0.0.8

func (m *Manager) ApplyLVMConfig(ctx context.Context, device string, layout *config.PartitionLayout) error

ApplyLVMConfig creates PV, VG, and LVs according to the LVM config.

func (*Manager) ApplyNVMeNamespaceLayout added in v0.0.8

func (m *Manager) ApplyNVMeNamespaceLayout(ctx context.Context, cfgs []NVMeNamespaceConfig) (map[string][]string, error)

ApplyNVMeNamespaceLayout creates namespaces from config, using percentage-based sizing. Returns created NSIDs per controller in creation order.

func (*Manager) ApplyPartitionLayout added in v0.0.8

func (m *Manager) ApplyPartitionLayout(ctx context.Context, device string, layout *config.PartitionLayout) error

ApplyPartitionLayout creates partitions on a disk according to the layout. Only GPT partition tables are supported (via sgdisk).

func (*Manager) AttachNVMeNamespace added in v0.0.8

func (m *Manager) AttachNVMeNamespace(ctx context.Context, controller, nsid string) error

AttachNVMeNamespace attaches a namespace to controller 0.

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. Exit codes 0 (clean) and 1 (errors corrected) are acceptable. Exit code >= 4 indicates uncorrectable errors and returns an error.

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) CreateNVMeNamespace added in v0.0.8

func (m *Manager) CreateNVMeNamespace(ctx context.Context, controller string, sizeBlocks uint64, blockSize int) (string, error)

CreateNVMeNamespace creates a namespace with the given size in blocks and block size.

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. Removable media (USB drives, SD cards) are rejected unless BOOTY_ALLOW_REMOVABLE=true.

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) FindDiskBySerial added in v0.0.18

func (m *Manager) FindDiskBySerial(ctx context.Context, serial string, minSizeGB int) (string, error)

FindDiskBySerial finds a non-virtual target disk whose serial or stable identifier exactly matches serial. Removable media are rejected unless BOOTY_ALLOW_REMOVABLE=true.

func (*Manager) FindRootPartition

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

FindRootPartition finds the primary Linux filesystem partition. When multiple partitions share a Linux filesystem type, it prefers one with a "root" or "/" partition name, otherwise returns the last match (root is typically after /boot in the partition table).

func (*Manager) FormatNVMeNamespace added in v0.0.8

func (m *Manager) FormatNVMeNamespace(ctx context.Context, device string, blockSize, lbafIndex int) error

FormatNVMeNamespace performs a secure erase and reformat of a namespace. lbafIndex must be provided explicitly from device capabilities (e.g. nvme id-ns), because LBAF index ordering is vendor/device-specific.

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) MountPartitionReadOnly added in v0.0.11

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

MountPartitionReadOnly mounts a device read-only at the given mountpoint. It tries the same supported filesystem list as MountPartition.

func (*Manager) NVMeIdentifyController added in v0.0.8

func (m *Manager) NVMeIdentifyController(ctx context.Context, controller string) (map[string]string, error)

NVMeIdentifyController returns basic controller info via nvme id-ctrl.

func (*Manager) NVMeListNamespaces added in v0.0.8

func (m *Manager) NVMeListNamespaces(ctx context.Context, controller string) ([]string, error)

NVMeListNamespaces lists existing namespace IDs on a controller.

func (*Manager) NVMeResetNamespaces added in v0.0.8

func (m *Manager) NVMeResetNamespaces(ctx context.Context, controller string) error

NVMeResetNamespaces deletes all namespaces and recreates a single default namespace using the controller's full capacity at 512-byte block size. Used during deprovisioning to return the drive to a single-namespace factory state.

func (*Manager) NVMeSupportsMultiNS added in v0.0.8

func (m *Manager) NVMeSupportsMultiNS(ctx context.Context, controller string) (bool, error)

NVMeSupportsMultiNS checks whether the controller supports multiple namespaces. Returns false for consumer drives (nn == 1).

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) error

TeardownChrootBindMounts unmounts the standard chroot bind mounts and returns a joined error from the mandatory unmounts (/run, /sys, /proc, /dev). The efivarfs unmount is best-effort because efivarfs is only bind-mounted when /sys/firmware/efi/efivars exists on the host; failures there are logged at Debug level and never returned.

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.

func (*Manager) WipeDisk added in v0.0.13

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

WipeDisk clears partition-table and filesystem signatures on a single device. This is a quick erase (no data overwrite) suitable for pre-streaming cleanup.

func (*Manager) WipeFilesystemSignatures added in v0.0.18

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

WipeFilesystemSignatures clears filesystem signatures on an existing partition without touching the parent disk partition table.

type NVMeNamespace added in v0.0.8

type NVMeNamespace struct {
	Label     string `json:"label"`               // Human-readable label
	SizePct   int    `json:"sizePct"`             // Percentage of total capacity
	BlockSize int    `json:"blockSize,omitempty"` // 512 or 4096 (default: 512)
}

NVMeNamespace defines a single namespace to create.

type NVMeNamespaceConfig added in v0.0.8

type NVMeNamespaceConfig struct {
	Controller string          `json:"controller"` // e.g. "/dev/nvme0"
	Namespaces []NVMeNamespace `json:"namespaces"`
}

NVMeNamespaceConfig defines the desired namespace layout for an NVMe controller.

func ParseNVMeConfig added in v0.0.8

func ParseNVMeConfig(data string) ([]NVMeNamespaceConfig, error)

ParseNVMeConfig parses a JSON NVMe namespace configuration string.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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