block

package
v2.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MPL-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package block provides support for operations on blockdevices.

Index

Constants

View Source
const DefaultBlockSize = 512

DefaultBlockSize is the default block size in bytes.

View Source
const (
	// FastWipeRange fast wipe block.
	FastWipeRange = 1024 * 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Device

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

Device wraps blockdevice operations.

func NewFromFile

func NewFromFile(f *os.File) *Device

NewFromFile returns a new Device from the specified file.

func NewFromPath

func NewFromPath(path string, opts ...Option) (*Device, error)

NewFromPath returns a new Device from the specified path.

func (*Device) Close

func (d *Device) Close() error

Close the device.

No-op if the device was created from a file.

func (*Device) FastWipe

func (d *Device) FastWipe(ranges ...Range) error

FastWipe the device contents.

This method is much faster than Wipe(), but it doesn't guarantee that device will be zeroed out completely.

If ranges are given, only those ranges will be wiped. Otherwise, the first FastWipeRange and the last FastWipeRange bytes will be wiped.

func (*Device) File

func (d *Device) File() *os.File

File returns the underlying file.

func (*Device) GetDevNo

func (d *Device) GetDevNo() (uint64, error)

GetDevNo returns the device number of the blockdevice.

func (*Device) GetIOSize

func (d *Device) GetIOSize() (uint, error)

GetIOSize returns blockdevice optimal I/O size in bytes.

func (*Device) GetKernelLastPartitionNum

func (d *Device) GetKernelLastPartitionNum() (int, error)

GetKernelLastPartitionNum returns the maximum partition number in the kernel.

func (*Device) GetProperties

func (d *Device) GetProperties() (*DeviceProperties, error)

GetProperties returns the properties of the block device.

func (*Device) GetSectorSize

func (d *Device) GetSectorSize() uint

GetSectorSize returns blockdevice sector size in bytes.

func (*Device) GetSize

func (d *Device) GetSize() (uint64, error)

GetSize returns blockdevice size in bytes.

func (*Device) GetWholeDisk

func (d *Device) GetWholeDisk() (*Device, error)

GetWholeDisk returns the whole disk for the blockdevice.

If the blockdevice is a whole disk, it returns itself. The returned block device should be closed.

func (*Device) IsCD

func (d *Device) IsCD() bool

IsCD returns true if the blockdevice is a CD-ROM device.

func (*Device) IsCDNoMedia

func (d *Device) IsCDNoMedia() bool

IsCDNoMedia returns true if the blockdevice is a CD-ROM device without media.

func (*Device) IsPrivateDeviceMapper

func (d *Device) IsPrivateDeviceMapper() (bool, error)

IsPrivateDeviceMapper returns true if this is a private device-mapper device.

func (*Device) IsReadOnly

func (d *Device) IsReadOnly() (bool, error)

IsReadOnly returns true if the blockdevice is read-only.

func (*Device) IsWholeDisk

func (d *Device) IsWholeDisk() (bool, error)

IsWholeDisk returns true if the blockdevice is a whole disk.

func (*Device) KernelPartitionAdd

func (d *Device) KernelPartitionAdd(no int, start, length uint64) error

KernelPartitionAdd invokes the BLKPG_ADD_PARTITION ioctl.

func (*Device) KernelPartitionDelete

func (d *Device) KernelPartitionDelete(no int) error

KernelPartitionDelete invokes the BLKPG_DEL_PARTITION ioctl.

func (*Device) KernelPartitionResize

func (d *Device) KernelPartitionResize(no int, first, length uint64) error

KernelPartitionResize invokes the BLKPG_RESIZE_PARTITION ioctl.

func (*Device) Lock

func (d *Device) Lock(exclusive bool) error

Lock (and block until the lock is acquired) for the block device.

func (*Device) RetryLock

func (d *Device) RetryLock(ctx context.Context, exclusive bool) error

RetryLock until the context deadline.

func (*Device) RetryLockWithTimeout

func (d *Device) RetryLockWithTimeout(ctx context.Context, exclusive bool, timeout time.Duration) error

RetryLockWithTimeout retries locking until the timeout.

func (*Device) TryLock

func (d *Device) TryLock(exclusive bool) error

TryLock (and return an error if failed).

func (*Device) Unlock

func (d *Device) Unlock() error

Unlock releases any lock.

func (*Device) Wipe

func (d *Device) Wipe() (string, error)

Wipe the device contents.

In order of availability this tries to perform the following:

  • secure discard (secure erase)
  • discard with zeros
  • zero out via ioctl
  • zero out from userland

func (*Device) WipeRange

func (d *Device) WipeRange(start, length uint64) (string, error)

WipeRange the device [start, start+length).

type DeviceProperties

type DeviceProperties struct {
	// Device name, as in 'sda'.
	DeviceName string
	// Model from /sys/block/*/device/model.
	Model string
	// Serial /sys/block/<dev>/device/serial.
	Serial string
	// Modalias /sys/block/<dev>/device/modalias.
	Modalias string
	// WWID /sys/block/<dev>/wwid.
	WWID string
	// UUID /sys/block/<dev>/uuid.
	// BusPath PCI bus path.
	BusPath string
	// SubSystem is the dest path of symlink /sys/block/<dev>/subsystem.
	SubSystem string
	// Transport of the device: SCSI, ata, ahci, nvme, etc.
	Transport string
	// Rotational is true if the device is a rotational disk.
	Rotational bool
}

DeviceProperties contains the properties of a block device.

type Option

type Option func(*Options)

Option is a function that modifies Options.

func OpenForWrite

func OpenForWrite() Option

OpenForWrite opens the device for writing.

type Options

type Options struct {
	Flag int
}

Options for NewFromPath.

type Range added in v2.0.5

type Range struct {
	Offset uint64
	Size   uint64
}

Range is a range of bytes.

Jump to

Keyboard shortcuts

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