disk

package
v1.9.4 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 21 Imported by: 37

Documentation

Overview

Package disk provides utilities for working directly with a disk

Most of the provided functions are intelligent wrappers around implementations of github.com/diskfs/go-diskfs/partition and github.com/diskfs/go-diskfs/filesystem

Index

Constants

This section is empty.

Variables

View Source
var ErrReReadDeferred = errors.New("partition table written to disk but kernel re-read deferred (device busy); reboot to apply")

ErrReReadDeferred is returned (wrapped) by ReReadPartitionTable when the on-disk partition table was written successfully but the kernel could not be made to re-read it because the disk is busy (a partition is mounted/held — the common case when repartitioning the disk you are booted from). The new table is committed on disk; a reboot makes the kernel pick it up. Callers can detect this with errors.Is and reboot-to-apply instead of treating it as a failure.

Functions

This section is empty.

Types

type DeviceType added in v1.8.0

type DeviceType int
const (
	DeviceTypeUnknown DeviceType = iota
	DeviceTypeFile
	DeviceTypeBlockDevice
)

func DetermineDeviceType added in v1.8.0

func DetermineDeviceType(f iofs.File) (DeviceType, error)

type Disk

type Disk struct {
	Backend           backend.Storage
	Size              int64
	LogicalBlocksize  int64
	PhysicalBlocksize int64
	Table             partition.Table
	DefaultBlocks     bool
}

Disk is a reference to a single disk block device or image that has been Create() or Open()

func (*Disk) Close added in v1.4.1

func (d *Disk) Close() error

Close the disk. Once successfully closed, it can no longer be used.

func (*Disk) CreateFilesystem

func (d *Disk) CreateFilesystem(spec FilesystemSpec) (filesystem.FileSystem, error)

CreateFilesystem creates a filesystem on a disk image, the equivalent of mkfs.

Required:

  • desired partition number, or 0 to create the filesystem on the entire block device or disk image,
  • the filesystem type from github.com/diskfs/go-diskfs/filesystem

Optional:

  • volume label for those filesystems that support it; under Linux this shows in '/dev/disks/by-label/<label>'

if successful, returns a filesystem-implementing structure for the given filesystem type

returns error if there was an error creating the filesystem, or the partition table is invalid and did not request the entire disk.

func (*Disk) GetFilesystem

func (d *Disk) GetFilesystem(partIndex int) (filesystem.FileSystem, error)

GetFilesystem gets the filesystem that already exists on a disk image

pass the desired partition number, or 0 to create the filesystem on the entire block device / disk image,

if successful, returns a filesystem-implementing structure for the given filesystem type

returns error if there was an error reading the filesystem, or the partition table is invalid and did not request the entire disk.

func (*Disk) GetPartition added in v1.8.0

func (d *Disk) GetPartition(partIndex int) (part.Partition, error)

func (*Disk) GetPartitionTable

func (d *Disk) GetPartitionTable() (partition.Table, error)

GetPartitionTable retrieves a PartitionTable for a Disk

If the table is able to be retrieved from the disk, it is saved in the instance.

returns an error if the Disk is invalid or does not exist, or the partition table is unknown

func (*Disk) Partition

func (d *Disk) Partition(table partition.Table) error

Partition applies a partition.Table implementation to a Disk

The Table can have zero, one or more Partitions, each of which is unique to its implementation. E.g. MBR partitions in mbr.Table look different from GPT partitions in gpt.Table

Actual writing of the table is delegated to the individual implementation

func (*Disk) ReReadPartitionTable added in v1.1.0

func (d *Disk) ReReadPartitionTable() error

ReReadPartitionTable makes the kernel pick up the on-disk partition table.

It first tries a whole-disk re-read via the BLKRRPART ioctl. That is all-or-nothing and fails with EBUSY whenever the kernel or udev is transiently holding any partition -- which is common immediately after a partition-table write, because the write triggers a udev re-probe -- or when a partition is mounted. When BLKRRPART fails, it falls back to per-partition BLKPG reconciliation (the same mechanism partx/parted use): it adds, removes, and re-creates only the entries whose geometry changed, via BLKPG ioctls that do not require a whole-disk exclusive re-read. This keeps the library free of any dependency on external tools such as partx. The fallback is Linux-only; on other platforms it reports that the re-read could not be completed.

func (*Disk) ReadPartitionContents

func (d *Disk) ReadPartitionContents(partIndex int, writer io.Writer) (int64, error)

ReadPartitionContents reads the contents of a partition to an io.Writer

if successful, returns the number of bytes read

returns an error if there was an error reading from the disk, writing to the writer, the table is invalid, or the partition is invalid

func (*Disk) WritePartitionContents

func (d *Disk) WritePartitionContents(partIndex int, reader io.Reader) (int64, error)

WritePartitionContents writes the contents of an io.Reader to a given partition

if successful, returns the number of bytes written

returns an error if there was an error writing to the disk, reading from the reader, the table is invalid, or the partition is invalid

type FilesystemSpec

type FilesystemSpec struct {
	Partition   int
	FSType      filesystem.Type
	VolumeLabel string
	WorkDir     string
	// The filesystem will be created in a reproducible manner following SOURCE_DATE_EPOCH guidelines.
	Reproducible bool
}

FilesystemSpec represents the specification of a filesystem to be created

type InvalidPartitionError added in v1.8.0

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

func NewInvalidPartitionError added in v1.8.0

func NewInvalidPartitionError(requested int) *InvalidPartitionError

func (*InvalidPartitionError) Error added in v1.8.0

func (e *InvalidPartitionError) Error() string

type MaxPartitionsExceededError added in v1.8.0

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

func NewMaxPartitionsExceededError added in v1.8.0

func NewMaxPartitionsExceededError(requested, maxPart int) *MaxPartitionsExceededError

func (*MaxPartitionsExceededError) Error added in v1.8.0

type NoPartitionTableError added in v1.8.0

type NoPartitionTableError struct{}

func (*NoPartitionTableError) Error added in v1.8.0

func (e *NoPartitionTableError) Error() string

type Type

type Type int

Type represents the type of disk this is

const (
	// File is a file-based disk image
	File Type = iota
	// Device is an OS-managed block device
	Device
)

type UnknownFilesystemError added in v1.8.0

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

func NewUnknownFilesystemError added in v1.8.0

func NewUnknownFilesystemError(partition int) *UnknownFilesystemError

func (*UnknownFilesystemError) Error added in v1.8.0

func (e *UnknownFilesystemError) Error() string

Jump to

Keyboard shortcuts

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