Expand description
duende-ublk - ublk device lifecycle management
This crate provides tools for managing ublk (userspace block device) lifecycle, particularly for swap-critical daemons that need to clean up orphaned devices.
§Problem: Orphaned ublk Devices
When a ublk daemon crashes or is killed, the kernel may retain device state
even after the /dev/ublkbN block device disappears. This causes:
- “File exists” errors when creating new devices
- Device ID conflicts
- System requires reboot to clear stale state
§Solution
use duende_ublk::{UblkControl, cleanup_orphaned_devices};
fn main() -> Result<(), duende_ublk::Error> {
// Clean up any orphaned devices from previous crashes
let cleaned = cleanup_orphaned_devices()?;
println!("Cleaned {} orphaned devices", cleaned);
// Now safe to create new devices
Ok(())
}§Kernel Interface
This crate uses io_uring URING_CMD to communicate with the ublk kernel driver. Linux 6.0+ is required.
Structs§
- Ublk
Control - ublk control device handle
- Ublk
Ctrl Cmd - Control command payload (32 bytes) - matches kernel ublksrv_ctrl_cmd
- Ublk
Ctrl CmdExt - Extended control command for io_uring 128-byte SQE (80 bytes total)
- Ublk
Ctrl DevInfo - Device info structure (64 bytes) - matches kernel ublksrv_ctrl_dev_info
Enums§
- Error
- Errors that can occur during ublk operations
Constants§
- UBLK_
CTRL_ DEV - Path to ublk control device
Functions§
- block_
device_ exists - Check if a block device exists for a given device ID.
- block_
device_ path - Build the block device path for a device ID.
- build_
device_ command - Build a command structure for a device operation.
- build_
get_ info_ command - Build a command structure for getting device info.
- char_
device_ path - Build the character device path for a device ID.
- cleanup_
orphaned_ devices - Clean up all orphaned ublk devices
- control_
device_ available - Check if the ublk control device is available.
- detect_
orphaned_ devices - Detect orphaned ublk devices
- detect_
orphans_ in_ dir - Detect orphaned devices in a given directory (for testing).
- interpret_
command_ result - Interpret a command result code.
- parse_
char_ device_ id - Parse a ublk character device name to extract the device ID.