Documentation
¶
Index ¶
- Constants
- Variables
- func ConvertImageFormat(path, dstFormat string) error
- func Create(opts CreateOptions) error
- func DeleteDir(opts FileOptions) error
- func DeleteFile(opts FileOptions) error
- func Ext4Label(imagePath string, partIndex int) (string, error)
- func Grow(path string, sizeBytes int64) error
- func IsDmgSupportedFilesystem(fs FilesystemType) bool
- func MkDir(opts FileOptions, perm os.FileMode) error
- func ReadFile(opts FileOptions) ([]byte, error)
- func Rename(opts FileOptions, newPath string) error
- func ResizeImage(path string, newSizeBytes int64) error
- func SetExt4Label(imagePath string, partIndex int, label string) error
- func Stat(opts FileOptions) (filesystem.Stat, error)
- func WriteFile(opts FileOptions, data []byte, perm os.FileMode) error
- type BlockDevice
- type CreateOptions
- type FileOptions
- type FilesystemType
- type ImageFormat
- type ListEntry
- type ListOptions
- type PartitionScheme
- type UEFIVarsStore
Constants ¶
const ( FormatRaw ImageFormat = "raw" FormatDmg ImageFormat = "dmg" PartNone PartitionScheme = "none" PartMBR PartitionScheme = "mbr" PartGPT PartitionScheme = "gpt" FSNone FilesystemType = "none" FSExt4 FilesystemType = "ext4" FSFat32 FilesystemType = "fat32" FSBtrfs FilesystemType = "btrfs" FSXfs FilesystemType = "xfs" FSZfs FilesystemType = "zfs" FSExFAT FilesystemType = "exfat" FSNTFS FilesystemType = "ntfs" FSApfs FilesystemType = "apfs" FSHfsPlus FilesystemType = "hfsplus" )
const UEFIVarsSizeARM64 = filesystem_uefi.VarsSizeARM64
UEFIVarsSizeARM64 is the standard QEMU_VARS.fd size for arm64 (64 MiB).
const UEFIVarsSizeX86_64 = filesystem_uefi.VarsSizeX86_64
UEFIVarsSizeX86_64 is the standard OVMF_VARS.fd size for x86-64 (512 KiB).
Variables ¶
var ( // ValidFormats lists accepted --format values. ValidFormats = []string{"raw", "dmg"} // ValidPartSchemes lists accepted --part values. ValidPartSchemes = []string{"none", "mbr", "gpt"} // ValidFilesystems lists accepted --filesystem values. ValidFilesystems = []string{"none", "ext4", "fat32", "btrfs", "xfs", "zfs", "exfat", "ntfs", "apfs", "hfsplus"} // DmgSupportedFilesystems is the set of filesystems that may be embedded // in a DMG image. Only filesystems natively understood by macOS are // allowed: APFS (with optional FileVault FDE), HFS+, FAT32 and exFAT. // // HFS+ is what a distribution .dmg wants. It compresses far better under // UDZO than APFS, and the Finder metadata that gives such an image its // background picture and icon positions keys on an "H+" volume — the // alias inside the .DS_Store record names the filesystem type. DmgSupportedFilesystems = []string{"none", "apfs", "hfsplus", "fat32", "exfat"} )
Functions ¶
func ConvertImageFormat ¶
ConvertImageFormat converts the image at path to the given format (e.g. "UDZO" or "UDSP") in-place. Works on all platforms.
"UDRW" is the raw image, with no UDIF container: that is what hdiutil writes for it and the only shape macOS mounts read/write. A raw image is accepted as the source too, so the conversion goes both ways.
func Create ¶
func Create(opts CreateOptions) error
Create creates a disk image according to opts.
For --part none the filesystem is written directly at offset 0 of the image. For --part mbr/gpt the partition table is written first, then the filesystem is written at the partition offset (first partition, aligned to 1 MiB).
func DeleteDir ¶
func DeleteDir(opts FileOptions) error
DeleteDir removes a directory (recursively) inside the image.
func DeleteFile ¶
func DeleteFile(opts FileOptions) error
DeleteFile removes a file at the given path inside the image.
func Ext4Label ¶
Ext4Label reads the current volume label from the ext4 filesystem at imagePath/partIndex. Convenience helper for callers that want to confirm an image's label without round-tripping through e2label. Returns the empty string when no label has been set.
func Grow ¶
Grow resizes the backing image file at path to sizeBytes. It behaves like truncate: the file is grown or shrunk to the given size.
func IsDmgSupportedFilesystem ¶
func IsDmgSupportedFilesystem(fs FilesystemType) bool
IsDmgSupportedFilesystem reports whether fs is allowed inside a DMG image.
func MkDir ¶
func MkDir(opts FileOptions, perm os.FileMode) error
MkDir creates a directory inside the image.
func ReadFile ¶
func ReadFile(opts FileOptions) ([]byte, error)
ReadFile reads and returns the contents of a regular file inside an image.
func Rename ¶
func Rename(opts FileOptions, newPath string) error
Rename moves or renames a file or directory inside the image.
func ResizeImage ¶
ResizeImage resizes a disk image:
- UDIF images: uses the pure-Go UDIF resize, works on all platforms, and puts the image back in the shape it was found in. A sparse image stays sparse; it used to be refused here because growing one silently rewrote it uncompressed.
- Raw files: truncates to new size via Grow.
func SetExt4Label ¶
SetExt4Label opens the ext4 filesystem at imagePath/partIndex and writes a new s_volume_name. Works on both raw and QCOW2 images: OpenBlockDevice handles format detection, and the ext4 driver does the on-disk superblock write directly (no journal). When the filesystem has metadata_csum enabled the superblock CRC32c is recomputed automatically.
The label is capped at filesystem_ext4.MaxLabelLen (16 bytes); a longer label is rejected rather than silently truncated. Pass an empty string to clear the label.
SetExt4Label is intended for offline image manipulation (e.g. stamping a label on a cloud image before booting it under cloud- boot's `device = "LABEL=…"` plan target). Don't call it concurrently with a process actively mutating the same image — see filesystem_ext4.(*ext4FS).SetLabel for the locking story.
func Stat ¶
func Stat(opts FileOptions) (filesystem.Stat, error)
Stat returns filesystem metadata for the given path inside the image.
Types ¶
type BlockDevice ¶
type BlockDevice interface {
io.ReaderAt
WriteAt(p []byte, off int64) (int, error)
// Size returns the usable byte count of the device.
Size() int64
// Close releases all resources associated with the device.
Close() error
}
BlockDevice is a read-write block device backed by a disk image file. It is the common interface returned by OpenBlockDevice and OpenLUKSBlockDevice.
func OpenAPFSBlockDevice ¶
func OpenAPFSBlockDevice(path string, passphrase []byte) (BlockDevice, error)
OpenAPFSBlockDevice opens a raw or QCOW2 disk image that contains a FileVault-encrypted APFS container. The image format is detected automatically; the APFS key bag embedded in the container is unlocked with passphrase.
The returned BlockDevice transparently decrypts / encrypts all I/O with the Volume Encryption Key recovered from the container key bag.
Supported container formats:
- raw — the file itself is an APFS container (NX superblock at byte 0)
- qcow2 — the virtual disk exported by the QCOW2 layer begins with an APFS container; I/O is translated through both the QCOW2 and APFS layers
func OpenBlockDevice ¶
func OpenBlockDevice(path string) (BlockDevice, error)
OpenBlockDevice opens a disk image as a read-write block device. The format is detected automatically from the file header:
- QCOW2 → reads/writes go through the QCOW2 copy-on-write layer.
- UDIF DMG whose data fork is uncompressed → reads/writes hit it in place; the koly trailer's CRC-32 checksums are refreshed on Close. Compressed and sparse images are refused — they need an unpack-pack strategy that has a surprise Close cost (see dmg.UnpackToTemp / PackFromTemp if you want it explicitly).
- everything else → treated as a raw image; the device reads and writes the file directly.
func OpenLUKSBlockDevice ¶
func OpenLUKSBlockDevice(path string, passphrase []byte) (BlockDevice, error)
OpenLUKSBlockDevice opens a raw or QCOW2 disk image that contains a LUKS encrypted payload. The image format is detected automatically; the LUKS container embedded in the virtual disk is unlocked with passphrase.
The returned BlockDevice transparently encrypts/decrypts all I/O.
Supported container formats:
- raw — the file itself is a LUKS device (LUKS header at byte 0)
- qcow2 — the virtual disk exported by the QCOW2 layer begins with a LUKS header; I/O is translated through both the QCOW2 and LUKS layers
type CreateOptions ¶
type CreateOptions struct {
// Path is the output file path.
Path string
// SizeBytes is the total image size in bytes.
SizeBytes int64
// Format is the image container format (default: raw).
Format ImageFormat
// Partition is the partition table scheme (default: none).
Partition PartitionScheme
// Filesystem is the filesystem to write in the (first) partition or bare
// image. Use FSNone to create an image/partition without any filesystem.
Filesystem FilesystemType
// Label is passed to the filesystem formatter as volume label / pool name.
Label string
// DmgUDIFFormat selects the UDIF format the image is written in — "UDRW"
// (raw, the default), "UDZO" (zlib), or "UDSP" (sparse, zero runs
// elided). A distribution image wants UDZO: an HFS+ volume that is mostly
// empty compresses to a small fraction of its size, and macOS mounts it
// read-only, which is what a downloaded .dmg should be.
DmgUDIFFormat string
// DmgPassphrase, when non-empty and Format=FormatDmg with Filesystem=FSApfs,
// requests a FileVault-encrypted (FDE) APFS DMG. The passphrase is fed to
// pkg/go-fde/apfs which writes the APFS NX superblock and key bag using
// PBKDF2-SHA256 + AES Key Wrap (RFC 3394) over AES-256-XTS payload. It is
// only used in-process (never persisted in argv).
DmgPassphrase []byte
}
CreateOptions configures the disk image to create.
type FileOptions ¶
type FileOptions struct {
// Path is the path to the disk image file.
Path string
// Filesystem selects which filesystem driver to use. If empty, the
// filesystem type is auto-detected from the image.
Filesystem FilesystemType
// PartIndex selects the 0-based partition index to open.
PartIndex int
// FilePath is the absolute path inside the filesystem (e.g. "/etc/hosts").
FilePath string
// UnlockKeys provides optional keys used to unlock encrypted APFS images.
// For bi-key encrypted images provide two keys.
UnlockKeys []string
}
FileOptions configures ReadFile/WriteFile operations against an image.
type FilesystemType ¶
type FilesystemType string
FilesystemType identifies which filesystem to write inside the image.
func DetectFilesystem ¶
func DetectFilesystem(imagePath string, partIndex int) (FilesystemType, error)
DetectFilesystem probes the magic bytes in a disk image and returns the filesystem type found in the partition at partIndex. It supports: ext4, fat32, btrfs, xfs, zfs, exfat.
type ListEntry ¶
type ListEntry struct {
Name string
FileType uint8 // raw filesystem-specific type byte
Mode uint16 // POSIX mode (0 if not fetched)
Size uint64 // file size in bytes (0 if not fetched)
Inode uint64 // inode number (0 if not fetched)
}
ListEntry is a single entry returned by List.
func List ¶
func List(opts ListOptions) ([]ListEntry, error)
List opens a disk image and returns the directory entries at DirPath. If opts.Filesystem is empty, the filesystem type is auto-detected.
type ListOptions ¶
type ListOptions struct {
// Path is the path to the disk image file.
Path string
// Filesystem selects which filesystem driver to use.
// If empty, the filesystem type is auto-detected from the image.
Filesystem FilesystemType
// PartIndex is the 0-based partition index to open (0 for unpartitioned images).
PartIndex int
// DirPath is the directory inside the filesystem to list (default "/").
DirPath string
// FetchStat populates Mode, Size and Inode on each ListEntry.
FetchStat bool
// UnlockKeys provides optional keys used to unlock encrypted APFS images.
UnlockKeys []string
}
ListOptions controls the List operation.
type PartitionScheme ¶
type PartitionScheme string
PartitionScheme describes the partition table placed in the image.
type UEFIVarsStore ¶
type UEFIVarsStore = filesystem_uefi.Store
UEFIVarsStore is the handle returned by CreateUEFIVarsStore. It embeds the full uefi.Store interface so callers can read/write variables and enroll Secure Boot keys before handing the file to QEMU.
func CreateUEFIVarsStore ¶
func CreateUEFIVarsStore(path string, sizeBytes int64) (UEFIVarsStore, error)
CreateUEFIVarsStore creates a new UEFI NvVar variable store file at path with the given size in bytes.
The file is created (or truncated) and initialised with a valid VARIABLE_STORE_HEADER followed by erased-flash bytes (0xFF), matching the format expected by QEMU pflash images (OVMF_VARS.fd for x86-64, QEMU_VARS.fd for arm64).
Typical sizes:
x86-64 : UEFIVarsSizeX86_64 (512 KiB) arm64 : UEFIVarsSizeARM64 (64 MiB)
The returned UEFIVarsStore is open and ready for use; call Close when done.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
diskimage
command
diskimage is a command-line tool for creating and managing VM disk images.
|
diskimage is a command-line tool for creating and managing VM disk images. |
|
diskimage/create
Package create implements the diskimage create sub-commands.
|
Package create implements the diskimage create sub-commands. |
|
diskimage/grow
Package grow implements the diskimage grow sub-command.
|
Package grow implements the diskimage grow sub-command. |
