image_qcow2

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: BSD-3-Clause Imports: 7 Imported by: 2 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-diskimages/qcow2

qcow2

Go Reference CI

Pure-Go reader, creator, and converter for the QEMU QCOW2 disk image format (versions 2 and 3). No qemu-img, no CGO, no external tools. Works on all platforms and all supported Go architectures.

Module

github.com/go-diskimages/qcow2

The package identifier is image_qcow2.

QCOW2 format

QCOW2 ("QEMU Copy-On-Write, version 2") stores a virtual disk sparsely: only clusters that have been written are allocated in the file, and clusters may be optionally compressed. This package reads the big-endian on-disk structures directly:

  • Header — magic QFI\xfb, version, cluster_bits, virtual size, encryption method, and the L1 table location.
  • Two-level mapping — an L1 table points to L2 tables; each L2 entry maps a virtual cluster to a file offset (or marks it unallocated → reads as zeros).
  • Refcount table / blocks — track cluster allocation for growth.
  • Compressed clusters — deflate (compression type 0). Reading is supported; ZSTD (type 1) returns an explicit error.

Encrypted images (encryption_method != 0) return an error. Backing files are not followed: unallocated clusters read back as zeros.

Public API

Package-level functions
// Create writes a new empty QCOW2 v2 image with the given virtual size.
// The file allocates no data clusters; reads return zeros.
func Create(path string, sizeBytes int64) error

// IsQCOW2File reports whether path begins with the QCOW2 magic (QFI\xfb).
func IsQCOW2File(path string) bool

// ConvertToRaw reads a QCOW2 v2/v3 image at src and writes an equivalent raw
// disk image to dst, emitting "N%\n" progress lines to w. Deflate-compressed
// clusters are decompressed. Encryption and ZSTD compression return an error.
func ConvertToRaw(src, dst string, w io.Writer) error
Format value

Format satisfies the github.com/go-diskimages/interface format interface by structural typing (no import of that module required):

type Format struct{}

func (Format) Name() string                                 // "qcow2"
func (Format) Create(path string, sizeBytes int64) error    // new blank QCOW2 v2 image
func (Format) Detect(path string) (bool, error)             // QCOW2 magic detection
func (Format) ToRaw(src, dst string, w io.Writer) error     // decode QCOW2 → raw
func (Format) Resize(path string, newSizeBytes int64) error // grow only (shrink errors)
Read-write block device

Device exposes a QCOW2 image as a random-access block device. Unallocated clusters are allocated lazily on first write (copy-on-write append). Compressed clusters are readable; writing to a compressed cluster returns an error (re-compression is not implemented).

func OpenDevice(path string) (*Device, error)

func (d *Device) ReadAt(p []byte, off int64) (int, error)
func (d *Device) WriteAt(p []byte, off int64) (int, error)
func (d *Device) Size() (int64, error)
func (d *Device) Truncate(size int64) error
func (d *Device) Sync() error
func (d *Device) Fd() uintptr
func (d *Device) Close() error

Examples

Create, inspect, convert
import image_qcow2 "github.com/go-diskimages/qcow2"

// Create a 4 GiB sparse QCOW2 image.
err := image_qcow2.Create("disk.qcow2", 4<<30)

// Detect.
ok := image_qcow2.IsQCOW2File("disk.qcow2") // true

// Convert to a raw image (progress printed to stdout).
err = image_qcow2.ConvertToRaw("disk.qcow2", "disk.raw", os.Stdout)
Random-access reads and writes
import image_qcow2 "github.com/go-diskimages/qcow2"

dev, err := image_qcow2.OpenDevice("disk.qcow2")
if err != nil {
    return err
}
defer dev.Close()

// Write allocates clusters lazily.
if _, err := dev.WriteAt([]byte("hello"), 1<<20); err != nil {
    return err
}
if err := dev.Sync(); err != nil {
    return err
}

buf := make([]byte, 5)
if _, err := dev.ReadAt(buf, 1<<20); err != nil {
    return err
}

Scope: no raw→QCOW2 encoder

This package deliberately does not implement raw→QCOW2 encoding (populated-cluster allocation with compression). The Format interface defines only ToRaw (decode QCOW2 → raw), and that decode path — together with Create (blank images) and the Device block interface — is the intended boundary. Producing a compressed QCOW2 image from an arbitrary raw source is out of scope; use the block Device to populate an image cluster-by-cluster, or convert to raw and hand the raw image to the caller's chosen tooling.

License

BSD-3-Clause. Copyright the go-diskimages/qcow2 authors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToRaw

func ConvertToRaw(src, dst string, w io.Writer) error

ConvertToRaw reads a QCOW2 v2/v3 image at src and writes an equivalent raw disk image to dst. It writes "N%\n" progress lines to w.

Supported features:

  • Compressed clusters: deflate (type 0, default)

Unsupported features (return an error):

  • Encryption (encryption_method != 0 in the header)
  • ZSTD compression (QCOW2 v3 with compression_type=1)

Backing files are not followed: unallocated clusters are written as zeros.

func Create

func Create(path string, sizeBytes int64) error

Create writes a new empty QCOW2 v2 image at path with the given virtual size in bytes. The file contains no allocated data clusters; reads from the raw image will return zeros.

func IsQCOW2File

func IsQCOW2File(path string) bool

IsQCOW2File returns true if path starts with the QCOW2 magic bytes (0x514649fb = "QFI\xfb").

Types

type Device

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

Device provides read-write block device access to a QCOW2 v2/v3 image. Unallocated virtual clusters are allocated lazily on first write (COW append). Compressed clusters are supported for reading; writing to a compressed cluster returns an error (re-compression is not implemented).

func OpenDevice

func OpenDevice(path string) (*Device, error)

OpenDevice opens the QCOW2 image at path as a read-write block device.

func (*Device) Close

func (d *Device) Close() error

Close closes the underlying file.

func (*Device) Fd

func (d *Device) Fd() uintptr

Fd returns the underlying file descriptor. Useful for callers that need to pass the QCOW2 container to syscall- or sparse-tools-flavoured APIs (note the fd is the container, not the virtual disk — random reads via the fd yield QCOW2-format bytes, not raw guest data; use ReadAt for the latter).

func (*Device) ReadAt

func (d *Device) ReadAt(p []byte, off int64) (int, error)

ReadAt implements io.ReaderAt for the virtual disk address space.

func (*Device) Size

func (d *Device) Size() (int64, error)

Size returns the virtual size of the QCOW2 image.

func (*Device) Sync

func (d *Device) Sync() error

Sync flushes all writes to the underlying file.

func (*Device) Truncate

func (d *Device) Truncate(size int64) error

Truncate is not supported; the virtual size is fixed at image creation.

func (*Device) WriteAt

func (d *Device) WriteAt(p []byte, off int64) (int, error)

WriteAt implements io.WriterAt for the virtual disk address space. Compressed clusters cannot be overwritten; use uncompressed images for read-write access.

type Format

type Format struct{}

Format is the QCOW2 disk image format. A Format value satisfies the diskimage_format.Format interface defined in github.com/go-diskimages/interface without requiring an import of that module (Go structural typing).

func (Format) Create

func (Format) Create(path string, sizeBytes int64) error

Create creates a new QCOW2 disk image. Delegates to the package-level Create.

func (Format) Detect

func (Format) Detect(path string) (bool, error)

Detect returns (true, nil) if path contains a QCOW2 image.

func (Format) Name

func (Format) Name() string

Name returns "qcow2".

func (Format) Resize

func (Format) Resize(path string, newSizeBytes int64) error

Resize changes the virtual size of the QCOW2 image at path. Only growing is supported; shrinking returns an error because it may silently discard data. The L1 table is extended in-place when the new size requires more entries.

func (Format) ToRaw

func (Format) ToRaw(src, dst string, w io.Writer) error

ToRaw converts the QCOW2 image at src to a raw disk image at dst. Progress messages are written to w.

Jump to

Keyboard shortcuts

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