gpt

package
v0.1.1-0...-42241d0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package gpt provides a library for working with GPT partitions.

Index

Constants

View Source
const (
	// MagicEFIPart is the magic string in the GPT header signature field.
	MagicEFIPart = "EFI PART"
	// HeaderSize is the GUID partition table header size in bytes.
	HeaderSize = 92
)

Variables

View Source
var (
	// ErrPartitionTableDoesNotExist indicates that the partition table does not exist.
	ErrPartitionTableDoesNotExist = errors.New("does not exist")
	// ErrHeaderCRCMismatch indicates that the header CRC does not match what is on disk.
	ErrHeaderCRCMismatch = errors.New("header CRC mismatch")
	// ErrEntriesCRCMismatch indicates that the partitions array CRC does not match what is on disk.
	ErrEntriesCRCMismatch = errors.New("entries CRC mismatch")
)

Functions

This section is empty.

Types

type GPT

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

GPT represents the GUID Partition Table.

func New

func New(f *os.File, setters ...Option) (g *GPT, err error)

New creates an in-memory partition table.

func Open

func Open(f *os.File) (g *GPT, err error)

Open attempts to open a partition table on f.

func (*GPT) Add

func (g *GPT) Add(size uint64, setters ...PartitionOption) (*Partition, error)

Add adds a partition to the end of the list.

func (*GPT) Delete

func (g *GPT) Delete(p *Partition) error

Delete deletes a partition.

func (*GPT) Header

func (g *GPT) Header() *Header

Header returns the partition table header.

func (*GPT) InsertAt

func (g *GPT) InsertAt(idx int, size uint64, setters ...PartitionOption) (*Partition, error)

InsertAt inserts partition before the partition at the position idx.

If idx == 0, it inserts new partition as the first partition, etc., idx == 1 as the second, etc.

func (*GPT) Partitions

func (g *GPT) Partitions() *Partitions

Partitions returns the partition table partitions.

func (*GPT) Read

func (g *GPT) Read() (err error)

Read reads the partition table on disk and updates the in-memory representation.

func (*GPT) Repair

func (g *GPT) Repair() error

Repair repairs the partition table.

func (*GPT) Resize

func (g *GPT) Resize(part *Partition) (bool, error)

Resize resizes a partition. TODO(andrewrynhard): Verify that we can indeed grow this partition safely.

func (*GPT) Write

func (g *GPT) Write() (err error)
type Header struct {
	*lba.Buffer
	*lba.LBA

	Signature                string
	Revision                 uint32
	Size                     uint32
	Checksum                 uint32
	CurrentLBA               uint64
	BackupLBA                uint64
	FirstUsableLBA           uint64
	LastUsableLBA            uint64
	GUUID                    uuid.UUID
	EntriesLBA               uint64
	NumberOfPartitionEntries uint32
	PartitionEntrySize       uint32
	PartitionEntriesChecksum uint32
}

Header represents the GPT header.

nolint: maligned

func (*Header) DeserializeBackupLBA

func (h *Header) DeserializeBackupLBA() (err error)

DeserializeBackupLBA deserializes the backup LBA (location of the other header copy).

func (*Header) DeserializeCRC

func (h *Header) DeserializeCRC() (err error)

DeserializeCRC deserializes the CRC32 of header (offset +0 up to header size) in little endian, with this field zeroed during calculation.

func (*Header) DeserializeCurrentLBA

func (h *Header) DeserializeCurrentLBA() (err error)

DeserializeCurrentLBA deserializes the current LBA (location of this header copy).

func (*Header) DeserializeFirstUsableLBA

func (h *Header) DeserializeFirstUsableLBA() (err error)

DeserializeFirstUsableLBA deserializes the first usable LBA for partitions (primary partition table last LBA + 1).

func (*Header) DeserializeGUUID

func (h *Header) DeserializeGUUID() (err error)

DeserializeGUUID deserializes the disk GUID in mixed endian.

func (*Header) DeserializeLastUsableLBA

func (h *Header) DeserializeLastUsableLBA() (err error)

DeserializeLastUsableLBA deserializes the last usable LBA (secondary partition table first LBA − 1).

func (*Header) DeserializeNumberOfPartitionEntries

func (h *Header) DeserializeNumberOfPartitionEntries() (err error)

DeserializeNumberOfPartitionEntries deserializes the number of partition entries in array.

func (*Header) DeserializePartitionEntriesCRC

func (h *Header) DeserializePartitionEntriesCRC() (err error)

DeserializePartitionEntriesCRC deserializes the CRC32 of partition entries array in little endian.

func (*Header) DeserializePartitionEntrySize

func (h *Header) DeserializePartitionEntrySize() (err error)

DeserializePartitionEntrySize deserializes the size of a single partition entry (usually 80h or 128).

func (*Header) DeserializeRevision

func (h *Header) DeserializeRevision() (err error)

DeserializeRevision deserializes the revision (for GPT version 1.0 (through at least UEFI version 2.7 (May 2017)), the value is 00h 00h 01h 00h).

func (*Header) DeserializeSignature

func (h *Header) DeserializeSignature() (err error)

DeserializeSignature desirializes the signature ("EFI PART", 45h 46h 49h 20h 50h 41h 52h 54h or 0x5452415020494645ULL on little-endian machines).

func (*Header) DeserializeSize

func (h *Header) DeserializeSize() (err error)

DeserializeSize deserializes the header size in little endian (in bytes, usually 5Ch 00h 00h 00h or 92 bytes).

func (*Header) DeserializeStartingLBA

func (h *Header) DeserializeStartingLBA() (err error)

DeserializeStartingLBA deserializes the starting LBA of array of partition entries (always 2 in primary copy).

func (*Header) Primary

func (h *Header) Primary() (b []byte, err error)

Primary returns the serialized primary header.

func (*Header) Secondary

func (h *Header) Secondary() (b []byte, err error)

Secondary returns the serialized secondary header.

func (*Header) SerializeBackupLBA

func (h *Header) SerializeBackupLBA() (err error)

SerializeBackupLBA serializes the backup LBA (location of the other header copy).

func (*Header) SerializeCRC

func (h *Header) SerializeCRC() (err error)

SerializeCRC serializes the CRC32 of header (offset +0 up to header size) in little endian, with this field zeroed during calculation.

func (*Header) SerializeCurrentLBA

func (h *Header) SerializeCurrentLBA() (err error)

SerializeCurrentLBA serializes the current LBA (location of this header copy).

func (*Header) SerializeFirstUsableLBA

func (h *Header) SerializeFirstUsableLBA() (err error)

SerializeFirstUsableLBA serializes the first usable LBA for partitions (primary partition table last LBA + 1).

func (*Header) SerializeGUUID

func (h *Header) SerializeGUUID() (err error)

SerializeGUUID serializes the disk GUID in mixed endian.

func (*Header) SerializeLastUsableLBA

func (h *Header) SerializeLastUsableLBA() (err error)

SerializeLastUsableLBA serializes the last usable LBA (secondary partition table first LBA − 1).

func (*Header) SerializeNumberOfPartitionEntries

func (h *Header) SerializeNumberOfPartitionEntries() (err error)

SerializeNumberOfPartitionEntries serializes the number of partition entries in array.

func (*Header) SerializePartitionEntriesCRC

func (h *Header) SerializePartitionEntriesCRC() (err error)

SerializePartitionEntriesCRC serializes the CRC32 of partition entries array in little endian.

func (*Header) SerializePartitionEntrySize

func (h *Header) SerializePartitionEntrySize() (err error)

SerializePartitionEntrySize serializes the size of a single partition entry (usually 80h or 128).

func (*Header) SerializeRevision

func (h *Header) SerializeRevision() (err error)

SerializeRevision serializes the revision (for GPT version 1.0 (through at least UEFI version 2.7 (May 2017)), the value is 00h 00h 01h 00h).

func (*Header) SerializeSignature

func (h *Header) SerializeSignature() (err error)

SerializeSignature serializes the signature ("EFI PART", 45h 46h 49h 20h 50h 41h 52h 54h or 0x5452415020494645ULL on little-endian machines).

func (*Header) SerializeSize

func (h *Header) SerializeSize() (err error)

SerializeSize serializes the header size in little endian (in bytes, usually 5Ch 00h 00h 00h or 92 bytes).

func (*Header) SerializeStartingLBA

func (h *Header) SerializeStartingLBA() (err error)

SerializeStartingLBA serializes the starting LBA of array of partition entries (always 2 in primary copy).

type Option

type Option func(*Options) error

Option is the functional option func.

func WithPartitionEntriesStartLBA

func WithPartitionEntriesStartLBA(o uint64) Option

WithPartitionEntriesStartLBA sets the LBA to be used for specifying which LBA should be used for the start of the partition entries.

type Options

type Options struct {
	PartitionEntriesStartLBA uint64
}

Options is the functional options struct.

func NewDefaultOptions

func NewDefaultOptions(setters ...Option) (*Options, error)

NewDefaultOptions initializes an `Options` struct with default values.

type Partition

type Partition struct {
	Type       uuid.UUID
	ID         uuid.UUID
	FirstLBA   uint64
	LastLBA    uint64
	Attributes uint64
	Name       string

	Number int32
	// contains filtered or unexported fields
}

Partition represents a GPT partition.

func (*Partition) DeserializeAttributes

func (p *Partition) DeserializeAttributes(buf *lba.Buffer) (err error)

DeserializeAttributes deserializes the attribute flags (e.g. bit 60 denotes read-only).

func (*Partition) DeserializeFirstLBA

func (p *Partition) DeserializeFirstLBA(buf *lba.Buffer) (err error)

DeserializeFirstLBA deserializes the first LBA (little endian).

func (*Partition) DeserializeID

func (p *Partition) DeserializeID(buf *lba.Buffer) (err error)

DeserializeID deserializes the unique partition GUID (mixed endian).

func (*Partition) DeserializeLastLBA

func (p *Partition) DeserializeLastLBA(buf *lba.Buffer) (err error)

DeserializeLastLBA deserializes the last LBA (inclusive, usually odd).

func (*Partition) DeserializeName

func (p *Partition) DeserializeName(buf *lba.Buffer) (err error)

DeserializeName deserializes partition name (36 UTF-16LE code units).

func (*Partition) DeserializeType

func (p *Partition) DeserializeType(buf *lba.Buffer) (err error)

DeserializeType deserializes the partition type GUID (mixed endian).

func (*Partition) Filesystem

func (p *Partition) Filesystem() (string, error)

Filesystem returns partition filesystem type.

func (*Partition) Length

func (p *Partition) Length() uint64

Length returns the partition's length in LBA.

func (*Partition) Path

func (p *Partition) Path() (string, error)

Path returns partition path.

func (*Partition) SerializeAttributes

func (p *Partition) SerializeAttributes(buf *lba.Buffer) (err error)

SerializeAttributes serializes the attribute flags (e.g. bit 60 denotes read-only).

func (*Partition) SerializeFirstLBA

func (p *Partition) SerializeFirstLBA(buf *lba.Buffer) (err error)

SerializeFirstLBA serializes the first LBA (little endian).

func (*Partition) SerializeID

func (p *Partition) SerializeID(buf *lba.Buffer) (err error)

SerializeID serializes the unique partition GUID (mixed endian).

func (*Partition) SerializeLastLBA

func (p *Partition) SerializeLastLBA(buf *lba.Buffer) (err error)

SerializeLastLBA serializes the last LBA (inclusive, usually odd).

func (*Partition) SerializeName

func (p *Partition) SerializeName(buf *lba.Buffer) (err error)

SerializeName serializes the partition name (36 UTF-16LE code units).

func (*Partition) SerializeType

func (p *Partition) SerializeType(buf *lba.Buffer) (err error)

SerializeType serializes the partition type GUID (mixed endian).

type PartitionOption

type PartitionOption func(*PartitionOptions)

PartitionOption is the functional option func.

func WithLegacyBIOSBootableAttribute

func WithLegacyBIOSBootableAttribute(o bool) PartitionOption

WithLegacyBIOSBootableAttribute marks the partition as bootable.

func WithMaximumSize

func WithMaximumSize(o bool) PartitionOption

WithMaximumSize indicates if the partition should be created with the maximum size possible.

func WithPartitionName

func WithPartitionName(o string) PartitionOption

WithPartitionName sets the partition name.

func WithPartitionType

func WithPartitionType(id string) PartitionOption

WithPartitionType sets the partition type.

type PartitionOptions

type PartitionOptions struct {
	Type        uuid.UUID
	Name        string
	MaximumSize bool
	Attibutes   uint64
}

PartitionOptions represent the options available for partitions.

func NewDefaultPartitionOptions

func NewDefaultPartitionOptions(setters ...PartitionOption) *PartitionOptions

NewDefaultPartitionOptions initializes a Options struct with default values.

type Partitions

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

Partitions represents the GPT partitions array.

func (*Partitions) FindByName

func (p *Partitions) FindByName(name string) *Partition

FindByName finds partition by label.

func (*Partitions) Items

func (p *Partitions) Items() []*Partition

Items returns the partitions.

Jump to

Keyboard shortcuts

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