Crate duende_ublk

Crate duende_ublk 

Source
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§

UblkControl
ublk control device handle
UblkCtrlCmd
Control command payload (32 bytes) - matches kernel ublksrv_ctrl_cmd
UblkCtrlCmdExt
Extended control command for io_uring 128-byte SQE (80 bytes total)
UblkCtrlDevInfo
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.