volume

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 2 Imported by: 6 modules, 5 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

interface

Go Reference CI

The shared block-device contract for the go-volumes family — Go package volume. A fixed-size, random-access store that higher layers read and write through, decoupling where the bytes live (a pooled file volume, an S3-backed chunk store, an NBD export, a qcow2/raw image) from the format layered on top (ext4, xfs, squashfs, an OCI image, …).

Zero dependencies — standard library only (io). CGO_ENABLED=0.

The contract

// Device is a fixed-size, random-access block device.
// Writes MAY be buffered; Sync durably commits them.
type Device interface {
	io.ReaderAt           // ReadAt(p []byte, off int64) (int, error)
	io.WriterAt           // WriteAt(p []byte, off int64) (int, error)
	Size() (int64, error)
	Sync() error
	io.Closer
}

Device is deliberately the intersection of what a real backing already offers: go-volumes/pool's *Volume satisfies it verbatim.

  • ReadOnly — the read-only subset (a read-only file, a squashfs/iso9660 blob, a read-only NBD export, an OCI image). Any Device also satisfies it.
Borrowed: a device on loan
// Borrowed may be read, written and synced by its holder, never closed:
// the lender owns the lifetime.
type Borrowed interface {
	io.ReaderAt
	io.WriterAt
	Size() (int64, error)
	Sync() error
}

A library that serves a volume another component owns (an unlocked encrypted container whose plaintext must stay in its process, a pooled volume) takes a Borrowed, so it cannot close the device and the owner stays the authority over the stop order (unmount, server stop, filesystem close, then the owner commits and releases). Every Device already has this method set; volume.Lend(d) returns it as a type without Close, and volume.File(f) lends an *os.File. Concurrency follows io.ReaderAt and io.WriterAt: parallel ReadAt, and parallel WriteAt on non-overlapping ranges. Sync may be expensive; call it when a client asked for durability.

Optional capabilities

A consumer type-asserts for these and degrades gracefully when a device does not implement one:

Interface Capability
Truncater resize in place — Truncate(size int64)
Named carries a stable identifier — Name()
ReadOnlyReporter reports whether it rejects writes — ReadOnly()
Discarder release (TRIM/unmap) a byte range so a thin/sparse backing reclaims space

Where it sits

 format     ext4 · xfs · squashfs · OCI image      (go-filesystems)
            ─────────── Device / ReadOnly ───────────   ← this contract
 backing    pool *Volume · s3 *Store · oci image · NBD · raw/qcow2

go-volumes/pool's *Volume is a Device; the s3 and oci repos are pluggable backings that decide where a pool's bytes actually live. A go-filesystems driver writes its on-disk image through a Device, never caring which backing is underneath.

BSD-3-Clause.

Documentation

Overview

Package volume defines the shared block-device contract for the go-volumes family: a fixed-size, random-access store that higher layers read and write through, decoupling WHERE the bytes live (a pooled file volume, an S3-backed chunk store, an NBD export, a qcow2/raw image) from the FORMAT layered on top (ext4, xfs, squashfs, an OCI image, …).

The contract is deliberately the intersection of what a real backing already offers: github.com/go-volumes/pool's *Volume satisfies Device verbatim.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Borrowed added in v0.2.0

type Borrowed interface {
	io.ReaderAt // ReadAt(p []byte, off int64) (n int, err error)
	io.WriterAt // WriteAt(p []byte, off int64) (n int, err error)

	// Size reports the device's current size in bytes.
	Size() (int64, error)

	// Sync durably commits writes made so far. It may be expensive.
	Sync() error
}

Borrowed is a Device on loan: the holder may read, write and sync it, and has NO Close. The lender (the vault, the pool, whoever opened it) owns the lifetime and decides when the device is released, so a library that serves the device (a file server, an image viewer) can never close it from under its owner.

A typical lender is an unlocked encrypted container: it decrypts on the fly, decrypted bytes never leave the lender's process, and the stop order is unmount, server stop, filesystem close, then the lender commits and wipes. That order only holds if the borrower cannot short-circuit it, which is why Close is absent from the method set rather than documented as "do not call".

Ownership contract:

  • The borrower MUST NOT close, wipe or otherwise release the device, and MUST NOT type-assert it to io.Closer to do so. Adapters in this package return a type whose method set has no Close.
  • The lender MUST keep the device usable until every borrower has stopped (all in-flight calls returned). After the lender releases it, calls return an error; they must not panic and must not touch freed memory.
  • Sync may be expensive (a journal write plus several fsyncs for an encrypted container, a network round trip for a remote store). Borrowers should call it when a durability promise to their own client requires it (a flush the client asked for), not after every write. Sync returning nil means every write that returned before the call is durable.
  • Size is the current size in bytes; an error means it could not be read and is not a size of zero. The borrower reads it once and passes it on (for example to a filesystem's OpenReader(r, size)) unless the device is known to resize.

Concurrency follows the standard library contract: "Clients of ReadAt can execute parallel ReadAt calls on the same input source" and "Clients of WriteAt can execute parallel WriteAt calls on the same destination if the ranges do not overlap" (https://pkg.go.dev/io#ReaderAt, https://pkg.go.dev/io#WriterAt). Implementations must be safe for those parallel calls. Overlapping writes, or a read overlapping a write, are ordered by the borrower, not the device. Sync and Size may be called concurrently with ReadAt and WriteAt. As in package io, ReadAt and WriteAt must not retain p.

func File added in v0.2.0

func File(f *os.File) Borrowed

File lends an open *os.File as a Borrowed, for tests and for servers that are handed a path. The caller keeps f and closes it. A file opened read-only makes WriteAt fail with the OS error; it is not a read-only device. Block devices report size 0 through Stat; use a device-aware Borrowed for those.

func Lend added in v0.2.0

func Lend(d Borrowed) Borrowed

Lend returns d as a Borrowed: same bytes, no way to close it. The returned value is a distinct type, so a type assertion to io.Closer fails. The caller keeps d and remains responsible for closing it.

type Device

type Device interface {
	io.ReaderAt // ReadAt(p []byte, off int64) (n int, err error)
	io.WriterAt // WriteAt(p []byte, off int64) (n int, err error)

	// Size reports the device's current size in bytes.
	Size() (int64, error)

	// Sync durably commits any buffered writes.
	Sync() error

	io.Closer // Close releases the device; further use is undefined.
}

Device is a fixed-size, random-access block device. Writes MAY be buffered (a write-back cache, an S3-backed chunk store); Sync durably commits them.

It is the read-write contract a filesystem-format driver writes its on-disk image through, and the shape an S3/NBD/pool backing exposes.

type Discarder

type Discarder interface {
	Discard(off, length int64) error
}

Discarder is a device that can release (TRIM/unmap) a byte range, letting a thin/sparse backing reclaim the space.

type Named

type Named interface {
	Name() string
}

Named is a device that carries a stable identifier (e.g. a volume name).

type ReadOnly

type ReadOnly interface {
	io.ReaderAt
	Size() (int64, error)
	io.Closer
}

ReadOnly is the read-only subset: a file opened read-only, a squashfs/iso9660 blob, a read-only NBD export, an OCI image layer set. Any Device also satisfies it.

type ReadOnlyReporter

type ReadOnlyReporter interface {
	ReadOnly() bool
}

ReadOnlyReporter is a device that reports whether it rejects writes.

type Truncater

type Truncater interface {
	Truncate(size int64) error
}

Truncater is a Device whose size can be changed in place.

Jump to

Keyboard shortcuts

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