blockdev

package
v0.0.0-...-8d377ce Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotBlockDevice = errors.New("not a block device")
View Source
var ErrOutOfBounds = errors.New("write out of bounds")

Functions

func GenericZero

func GenericZero(b BlockDev, startByte, endByte int64) error

GenericZero implements software-based zeroing. This can be used to implement Zero when no acceleration is available or desired.

Types

type BlockDev

type BlockDev interface {
	io.ReaderAt
	io.WriterAt
	// BlockSize returns the block size of the block device in bytes. This must
	// be a power of two and is commonly (but not always) either 512 or 4096.
	BlockSize() int64

	// BlockCount returns the number of blocks on the block device or -1 if it
	// is an image with an undefined size.
	BlockCount() int64

	// OptimalBlockSize returns the optimal block size in bytes for aligning
	// to as well as issuing I/O. IO operations with block sizes below this
	// one might incur read-write overhead. This is the larger of the physical
	// block size and a device-reported value if available.
	OptimalBlockSize() int64

	// Discard discards a continuous set of blocks. Discarding means the
	// underlying device gets notified that the data in these blocks is no
	// longer needed. This can improve performance of the device device (as it
	// no longer needs to preserve the unused data) as well as bulk erase
	// operations. This command is advisory and not all implementations support
	// it. The contents of discarded blocks are implementation-defined.
	Discard(startByte int64, endByte int64) error

	// Zero zeroes a continouous set of blocks. On certain implementations this
	// can be significantly faster than just calling Write with zeroes.
	Zero(startByte, endByte int64) error
}

BlockDev represents a generic block device made up of equally-sized blocks. All offsets and intervals are expressed in bytes and must be aligned to BlockSize and are recommended to be aligned to OptimalBlockSize if feasible. Unless stated otherwise, intervals are inclusive-exclusive, i.e. the start byte is included but the end byte is not.

type Device

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

func FromFileHandle

func FromFileHandle(handle *os.File) (*Device, error)

FromFileHandle creates a blockdev from a device handle. The device handle is not duplicated, closing the returned Device will close it. If the handle is not a block device, i.e does not implement block device ioctls, an error is returned.

func Open

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

Open opens a block device given a path to its inode. TODO: exclusive, O_DIRECT

func (*Device) BlockCount

func (d *Device) BlockCount() int64

func (*Device) BlockSize

func (d *Device) BlockSize() int64

func (*Device) Close

func (d *Device) Close() error

func (*Device) Discard

func (d *Device) Discard(startByte int64, endByte int64) error

func (*Device) OptimalBlockSize

func (d *Device) OptimalBlockSize() int64

func (*Device) ReadAt

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

func (*Device) RefreshPartitionTable

func (d *Device) RefreshPartitionTable() error

RefreshPartitionTable refreshes the kernel's view of the partition table after changes made from userspace.

func (*Device) WriteAt

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

func (*Device) Zero

func (d *Device) Zero(startByte int64, endByte int64) error

type File

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

func CreateFile

func CreateFile(name string, blockSize int64, blockCount int64) (*File, error)

func (*File) BlockCount

func (d *File) BlockCount() int64

func (*File) BlockSize

func (d *File) BlockSize() int64

func (*File) Close

func (d *File) Close() error

func (*File) Discard

func (d *File) Discard(startByte int64, endByte int64) error

func (*File) OptimalBlockSize

func (d *File) OptimalBlockSize() int64

func (*File) ReadAt

func (d *File) ReadAt(p []byte, off int64) (n int, err error)

func (*File) WriteAt

func (d *File) WriteAt(p []byte, off int64) (n int, err error)

func (*File) Zero

func (d *File) Zero(startByte int64, endByte int64) error

type Memory

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

Memory is a memory-backed implementation of BlockDev. It is optimal for testing and temporary use, as it is fast and platform-independent.

func MustNewMemory

func MustNewMemory(blockSize, blockCount int64) *Memory

MustNewMemory works exactly like NewMemory, but panics when NewMemory would return an error. Intended for use in tests.

func NewMemory

func NewMemory(blockSize, blockCount int64) (*Memory, error)

NewMemory returns a new memory-backed block device with the given geometry.

func (*Memory) BlockCount

func (m *Memory) BlockCount() int64

func (*Memory) BlockSize

func (m *Memory) BlockSize() int64

func (*Memory) Discard

func (m *Memory) Discard(startByte, endByte int64) error

func (*Memory) OptimalBlockSize

func (m *Memory) OptimalBlockSize() int64

func (*Memory) ReadAt

func (m *Memory) ReadAt(p []byte, off int64) (int, error)

func (*Memory) WriteAt

func (m *Memory) WriteAt(p []byte, off int64) (int, error)

func (*Memory) Zero

func (m *Memory) Zero(startByte, endByte int64) error

type ReadWriteSeeker

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

ReadWriteSeeker provides an adapter implementing ReadWriteSeeker on top of a blockdev.

func NewRWS

func NewRWS(b BlockDev) *ReadWriteSeeker

func (*ReadWriteSeeker) Read

func (s *ReadWriteSeeker) Read(p []byte) (n int, err error)

func (*ReadWriteSeeker) Seek

func (s *ReadWriteSeeker) Seek(offset int64, whence int) (int64, error)

func (*ReadWriteSeeker) Write

func (s *ReadWriteSeeker) Write(p []byte) (n int, err error)

type Section

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

Section implements BlockDev on a slice of another BlockDev given a startBlock and endBlock.

func NewSection

func NewSection(b BlockDev, startBlock, endBlock int64) *Section

NewSection returns a new Section, implementing BlockDev over that subset of blocks. The interval is inclusive-exclusive.

func (*Section) BlockCount

func (s *Section) BlockCount() int64

func (*Section) BlockSize

func (s *Section) BlockSize() int64

func (*Section) Discard

func (s *Section) Discard(startByte, endByte int64) error

func (*Section) OptimalBlockSize

func (s *Section) OptimalBlockSize() int64

func (*Section) ReadAt

func (s *Section) ReadAt(p []byte, off int64) (n int, err error)

func (*Section) WriteAt

func (s *Section) WriteAt(p []byte, off int64) (n int, err error)

func (*Section) Zero

func (s *Section) Zero(startByte, endByte int64) error

Jump to

Keyboard shortcuts

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