diskgen

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package diskgen builds synthetic partition-table images for tests and fuzz seeds. Every builder produces a fully valid structure by default; defect injection is the caller's job, via the Mutate hook or by editing the result.

Index

Constants

View Source
const (
	GUIDLinuxFS     = "0fc63daf-8483-4772-8e79-3d69d8477de4"
	GUIDLinuxSwap   = "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"
	GUIDEFISystem   = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"
	GUIDAppleHFS    = "48465300-0000-11aa-aa11-00306543ecac"
	GUIDMSReserved  = "e3c9e316-0b5c-4db8-817d-f92df00215ae"
	GUIDMSBasicData = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"
)

Well-known GPT type GUIDs, in the textual form libtable reports.

View Source
const DefaultSize = 8 << 20

DefaultSize is the image size used when an Opts leaves Size at zero.

Variables

This section is empty.

Functions

func APM

func APM(o APMOpts) []byte

APM builds an Apple Partition Map.

func BSD

func BSD(o BSDOpts) []byte

BSD builds a disk carrying a BSD disklabel.

func GPT

func GPT(o GPTOpts) []byte

GPT builds a GPT disk with a protective or hybrid MBR.

func GUIDBytes

func GUIDBytes(s string) []byte

GUIDBytes encodes a GUID string into the mixed-endian on-disk layout GPT uses: the first three groups are little-endian, the last two big-endian.

func MBR

func MBR(o MBROpts) []byte

MBR builds a disk with a single primary MBR.

func MBRExtended

func MBRExtended(o MBRExtendedOpts) []byte

MBRExtended builds an MBR whose first slot is an extended container holding a chain of Depth EBRs, each with one logical partition.

func MBRPlusBSD

func MBRPlusBSD(size int) []byte

MBRPlusBSD builds media on which both an MBR and a BSD disklabel parse cleanly, for exercising ambiguity resolution. A size of 0 uses DefaultSize.

func MBRPlusSun

func MBRPlusSun(size int) []byte

MBRPlusSun builds media on which both an MBR and a Sun label parse cleanly. The Sun label shares sector 0 with the MBR.

func NestedBSD

func NestedBSD(o NestedBSDOpts) []byte

NestedBSD builds an MBR whose slice contains a BSD disklabel.

func NestedIn

func NestedIn(sliceType byte, sliceStart, sliceSize uint32, blockSize int, inner []byte) []byte

NestedIn builds an MBR whose single slice of sliceType contains inner, an image in its own right, rebased so inner's LBA 0 is the first sector of the slice. Use it to place any scheme inside an MBR container.

func SetGPTCRCs

func SetGPTCRCs(disk []byte, bs, headerLBA int)

SetGPTCRCs recomputes the header and entry-table checksums for the header at headerLBA. Call it after mutating a GPT image to keep it internally valid.

func SunI386

func SunI386(o SunOpts) []byte

SunI386 builds a Solaris x86 VTOC label.

func SunSparc

func SunSparc(o SunOpts) []byte

SunSparc builds a big-endian SPARC VTOC label.

func WriteBSDLabel

func WriteBSDLabel(sector []byte, o BSDOpts)

WriteBSDLabel writes a disklabel at the start of sector.

Types

type APMEntry

type APMEntry struct {
	StartLBA uint32
	SizeLBA  uint32
	Name     string
	Type     string
	// Status of 0 marks the partition unallocated.
	Status uint32
}

APMEntry describes one Apple Partition Map record.

type APMOpts

type APMOpts struct {
	Size      int
	BlockSize int
	// MapCount is the entry count written into every record. Zero uses the
	// actual number of entries.
	MapCount uint32
	Entries  []APMEntry
	Mutate   func(disk []byte)
}

type BSDOpts

type BSDOpts struct {
	Size      int
	BlockSize int
	// LabelLBA is the sector holding the disklabel. It is 1 for a whole disk and
	// sliceStart+1 for a label nested in an MBR slice.
	LabelLBA int
	NumParts uint16
	Parts    []BSDPart
	Mutate   func(disk []byte)
}

type BSDPart

type BSDPart struct {
	SizeLBA  uint32
	StartLBA uint32
	FSType   byte
}

BSDPart describes one BSD disklabel partition.

type GPTEntry

type GPTEntry struct {
	TypeGUID   string
	UniqueGUID string
	StartLBA   uint64
	EndLBA     uint64
	Attributes uint64
	Name       string
}

GPTEntry describes one GPT partition entry.

type GPTOpts

type GPTOpts struct {
	Size      int
	BlockSize int
	// EntryCount is the value written to the header. Zero means 128.
	EntryCount uint32
	// EntrySize is the value written to the header. Zero means 128.
	EntrySize uint32
	Entries   []GPTEntry
	// ProtectiveSlot places the 0xEE record in a slot other than 0.
	ProtectiveSlot int
	// HybridEntries are additional real MBR records written alongside the
	// protective entry, producing a hybrid MBR.
	HybridEntries []MBREntry
	// NoProtectiveMBR omits the 0xEE record entirely.
	NoProtectiveMBR bool
	// CorruptPrimary invalidates the primary header so only the backup parses.
	CorruptPrimary bool
	// NoBackup omits the secondary GPT at the end of the disk. Real tools always
	// write one, so it is written by default and this opts out -- which is what
	// a truncated image or a wiped tail looks like.
	NoBackup bool
	// DiskGUID is written to both copies. Zero means an all-zero GUID.
	DiskGUID string
	// SkipCRC leaves the CRC fields zero.
	SkipCRC bool
	Mutate  func(disk []byte, blockSize int)
}

type MBREntry

type MBREntry struct {
	Type     byte
	StartLBA uint32
	SizeLBA  uint32
	Boot     bool
}

MBREntry describes one 16-byte MBR partition record.

type MBRExtendedOpts

type MBRExtendedOpts struct {
	Size      int
	BlockSize int
	// Depth is the number of extended boot records to chain.
	Depth int
	// BreakLast writes a self-referential link in the final EBR.
	BreakLast bool
	// OmitSignature drops the 0xAA55 magic from the final EBR.
	OmitSignature bool
}

type MBROpts

type MBROpts struct {
	Size      int
	BlockSize int
	// OEM is written at offset 3; set it to a filesystem signature such as
	// "MSDOS5.0" to exercise the boot-sector rejection heuristic.
	OEM     string
	Entries []MBREntry
	Mutate  func(disk []byte)
}

type NestedBSDOpts

type NestedBSDOpts struct {
	Size      int
	BlockSize int
	// SliceStart and SliceSize describe the MBR container.
	SliceStart uint32
	SliceSize  uint32
	// SliceType is the MBR type byte; 0xA5 is FreeBSD.
	SliceType byte
	// Relative writes disklabel addresses relative to the slice. When false the
	// addresses are disk-absolute, as some real BSD installers write them.
	Relative bool
	Parts    []BSDPart
}

type SunOpts

type SunOpts struct {
	Size      int
	BlockSize int
	LabelLBA  int
	NumParts  uint16
	Parts     []SunPart
	// SecPerTrack and Heads form the SPARC cylinder conversion.
	SecPerTrack uint16
	Heads       uint16
	Mutate      func(disk []byte)
}

type SunPart

type SunPart struct {
	Type     uint16
	StartLBA uint32
	SizeLBA  uint32
	// StartCyl is used by the SPARC layout, which addresses in cylinders.
	StartCyl uint32
}

SunPart describes one Sun VTOC slice.

Jump to

Keyboard shortcuts

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