Documentation
¶
Overview ¶
Package blockdevice provides utilities for interacting with raw block devices. This package is designed specifically for SBD (Storage-Based Death) operations that require direct, synchronous access to block devices for reliable fencing.
Index ¶
Constants ¶
const ( // MaxRetryAttempts is the maximum number of retry attempts for transient errors MaxRetryAttempts = 3 // InitialRetryDelay is the initial delay between retry attempts InitialRetryDelay = 100 * time.Millisecond // MaxRetryDelay is the maximum delay between retry attempts MaxRetryDelay = 5 * time.Second // RetryBackoffFactor is the exponential backoff factor for retry delays RetryBackoffFactor = 2.0 // DefaultIOTimeout is the default timeout for individual I/O operations // This prevents indefinite hanging when storage becomes unresponsive DefaultIOTimeout = 30 * time.Second )
Retry configuration constants for block device operations
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device represents a raw block device that can be read from and written to. It implements the io.ReaderAt and io.WriterAt interfaces for positioned I/O operations. All operations are performed synchronously to ensure data integrity for SBD operations.
func Open ¶
Open opens a raw block device at the specified path for read/write operations. The device is opened with O_RDWR and O_SYNC flags to ensure synchronous I/O, which is critical for SBD operations where data must be immediately written to disk.
Parameters:
- path: The filesystem path to the block device (e.g., "/dev/sdb1")
Returns:
- *Device: A new Device instance if successful
- error: An error if the device cannot be opened
Example:
device, err := blockdevice.Open("/dev/sdb1")
if err != nil {
log.Fatalf("Failed to open device: %v", err)
}
defer device.Close()
func OpenWithLogger ¶
OpenWithLogger opens a raw block device with a logger for retry operations
func OpenWithTimeout ¶
OpenWithTimeout opens a raw block device with custom I/O timeout and logger This allows customization of the timeout for I/O operations to prevent indefinite hanging.
Parameters:
- path: The filesystem path to the block device (e.g., "/dev/sdb1")
- ioTimeout: The timeout for individual I/O operations
- logger: Logger for device operations and retries
Returns:
- *Device: A new Device instance if successful
- error: An error if the device cannot be opened
Example:
device, err := blockdevice.OpenWithTimeout("/dev/sdb1", 45*time.Second, logger)
if err != nil {
log.Fatalf("Failed to open device: %v", err)
}
defer device.Close()
func (*Device) Close ¶
Close closes the block device and releases any associated resources. After calling Close(), the Device instance should not be used for any further operations.
It is safe to call Close() multiple times; subsequent calls will have no effect.
Returns:
- error: An error if the close operation fails
Example:
device, err := blockdevice.Open("/dev/sdb1")
if err != nil {
log.Fatalf("Failed to open device: %v", err)
}
defer device.Close()
// Use device...
if err := device.Close(); err != nil {
log.Printf("Warning: failed to close device: %v", err)
}
func (*Device) ReadAt ¶
ReadAt reads len(p) bytes from the device starting at byte offset off. It implements the io.ReaderAt interface, allowing for positioned reads without affecting the device's current file position.
This method is safe for concurrent use as it doesn't modify any shared state and uses the positioned read system call. It includes retry logic for transient errors.
Parameters:
- p: The buffer to read data into
- off: The byte offset from the beginning of the device to start reading
Returns:
- n: The number of bytes actually read
- err: An error if the read operation fails
The method returns an error if:
- The device has been closed
- The offset is negative
- A system-level read error occurs after retries
func (*Device) String ¶
String returns a string representation of the Device for debugging purposes.
func (*Device) Sync ¶
Sync flushes any buffered writes to the underlying storage device. This method explicitly calls the system sync operation to ensure all pending writes are committed to disk before returning.
While the device is opened with O_SYNC flag (meaning writes should be synchronous), calling Sync() provides an additional guarantee that all data has been written to persistent storage. This is particularly important for SBD operations where data integrity is critical. It includes retry logic for transient errors.
Returns:
- error: An error if the sync operation fails
The method returns an error if:
- The device has been closed
- A system-level sync error occurs after retries
func (*Device) WriteAt ¶
WriteAt writes len(p) bytes to the device starting at byte offset off. It implements the io.WriterAt interface, allowing for positioned writes without affecting the device's current file position.
Since the device is opened with O_SYNC, this method ensures that data is immediately written to the underlying storage before returning. It includes retry logic for transient errors.
This method is safe for concurrent use as it doesn't modify any shared state and uses the positioned write system call.
Parameters:
- p: The data to write to the device
- off: The byte offset from the beginning of the device to start writing
Returns:
- n: The number of bytes actually written
- err: An error if the write operation fails
The method returns an error if:
- The device has been closed
- The offset is negative
- A system-level write error occurs after retries
- Not all bytes could be written