gpt

package
v0.0.0-...-8d377ce Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package gpt implements reading and writing GUID Partition Tables as specified in the UEFI Specification. It only implements up to 128 partitions per table (same as most other implementations) as more would require a dynamic table size, significantly complicating the code for little gain.

Index

Constants

View Source
const (
	// AttrRequiredPartition indicates that this partition is required for the
	// platform to function. Mostly used by vendors to mark things like recovery
	// partitions.
	AttrRequiredPartition = 1 << 0
	// AttrNoBlockIOProto indicates that EFI firmware must not provide an EFI
	// block device (EFI_BLOCK_IO_PROTOCOL) for this partition.
	AttrNoBlockIOProto = 1 << 1
	// AttrLegacyBIOSBootable indicates to special-purpose software outside of
	// UEFI that this partition can be booted using a traditional PC BIOS.
	// Don't use this unless you know that you need it specifically.
	AttrLegacyBIOSBootable = 1 << 2
)

Variables

View Source
var (
	PartitionTypeEFISystem = uuid.MustParse("C12A7328-F81F-11D2-BA4B-00A0C93EC93B")
)

Functions

func Overhead

func Overhead(blockSize int64) int64

Overhead returns the number of blocks the GPT partitioning itself consumes, i.e. aren't usable for user data.

Types

type AddOption

type AddOption func(*addOptions)

AddOption is a bitset controlling various

func WithAlignment

func WithAlignment(alignmenet int64) AddOption

WithAlignment allows aligning the partition start block to a non-default value. By default, these are aligned to 1MiB. Only use this flag if you are certain you need it, it can cause quite severe performance degradation under certain conditions.

func WithKeepEmptyEntries

func WithKeepEmptyEntries() AddOption

WithKeepEmptyEntries does not fill up empty entries which are followed by filled ones. It always appends the partition after the last used entry. Without this flag, the partition is placed in the first empty entry.

func WithPreferEnd

func WithPreferEnd() AddOption

WithPreferEnd tries to put the partition as close to the end as possible instead of as close to the start.

type Attribute

type Attribute uint64

Attribute is a bitfield of attributes set on a partition. Bits 0 to 47 are reserved for UEFI specification use and all current assignments are in the following const block. Bits 48 to 64 are available for per-Type use by the organization controlling the partition Type.

func (Attribute) PerTypeAttrs

func (a Attribute) PerTypeAttrs() uint32

PerTypeAttrs returns the top 24 bits which are reserved for custom per-Type attributes. The top 8 bits of the returned uint32 are always 0.

func (*Attribute) SetPerTypeAttrs

func (a *Attribute) SetPerTypeAttrs(v uint32)

SetPerTypeAttrs sets the top 24 bits which are reserved for custom per-Type attributes. It does not touch the lower attributes which are specified by the UEFI specification. The top 8 bits of v are silently discarded.

type Partition

type Partition struct {
	// Name of the partition, will be truncated if it expands to more than 36
	// UTF-16 code points. Not all systems can display non-BMP code points.
	Name string
	// Type is the type of Table partition, can either be one of the predefined
	// constants by the UEFI specification or a custom type identifier.
	// Note that the all-zero UUID denotes an empty partition slot, so this
	// MUST be set to something, otherwise it is not treated as a partition.
	Type uuid.UUID
	// ID is a unique identifier for this specific partition. It should be
	// changed when cloning the partition.
	ID uuid.UUID
	// The first logical block of the partition (inclusive)
	FirstBlock uint64
	// The last logical block of the partition (inclusive)
	LastBlock uint64
	// Bitset of attributes of this partition.
	Attributes Attribute

	*blockdev.Section
}

func (*Partition) IsUnused

func (p *Partition) IsUnused() bool

IsUnused checks if the partition is unused, i.e. it is nil or its type is the null UUID.

func (*Partition) SizeBlocks

func (p *Partition) SizeBlocks() uint64

SizeBlocks returns the size of the partition in blocks

type Table

type Table struct {
	// ID is the unique identifier of this specific disk / GPT.
	// If this is left uninitialized/all-zeroes a new random ID is automatically
	// generated when writing.
	ID uuid.UUID

	// Data put at the start of the very first block. Gets loaded and executed
	// by a legacy BIOS bootloader. This can be used to make GPT-partitioned
	// disks bootable by legacy systems or display a nice error message.
	// Maximum length is 440 bytes, if that is exceeded Write returns an error.
	// Should be left empty if the device is not bootable and/or compatibility
	// with BIOS booting is not required. Only useful on x86 systems.
	BootCode []byte

	// Partitions contains the list of partitions in this table. This is
	// artificially limited to 128 partitions. Holes in the partition list are
	// represented as nil values. Call IsUnused before checking any other
	// properties of the partition.
	Partitions []*Partition
	// contains filtered or unexported fields
}

func New

func New(b blockdev.BlockDev) (*Table, error)

New returns an empty table on the given block device. It does not read any existing GPT on the disk (use Read for that), nor does it write anything until Write is called.

func Read

func Read(r blockdev.BlockDev) (*Table, error)

Read reads a Table from a block device.

func (*Table) AddPartition

func (g *Table) AddPartition(p *Partition, size int64, options ...AddOption) error

AddPartition takes a pointer to a partition and adds it, placing it into the first (or last using WithPreferEnd) continuous free space which fits it. It writes the placement information (FirstBlock, LastBlock) back to p. By default, AddPartition aligns FirstBlock to 1MiB boundaries, but this can be overridden using WithAlignment.

func (*Table) FirstUsableBlock

func (g *Table) FirstUsableBlock() int64

FirstUsableBlock returns the first usable (i.e. a partition can start there) block.

func (*Table) GetFreeSpaces

func (g *Table) GetFreeSpaces() ([][2]int64, bool, error)

GetFreeSpaces returns a slice of tuples, each containing a half-closed interval of logical blocks not occupied by the GPT itself or any partition. The returned intervals are always in ascending order as well as non-overlapping. It also returns if it detected any overlaps between partitions or partitions and the GPT. It returns an error if and only if any partition has its FirstBlock before the LastBlock or exceeds the amount of blocks on the block device.

Note that the most common use cases for this function are covered by AddPartition, you're encouraged to use it instead.

func (*Table) LastUsableBlock

func (g *Table) LastUsableBlock() int64

LastUsableBlock returns the last usable (i.e. a partition can end there) block. This block is inclusive.

func (*Table) Write

func (gpt *Table) Write() error

Write writes the two GPTs, first the alternate, then the primary to the block device. If gpt.ID or any of the partition IDs are the all-zero UUID, new random ones are generated and written back. If the output is supposed to be reproducible, generate the UUIDs beforehand.

Jump to

Keyboard shortcuts

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