block: Make detect_image_type return BlockResult with context

Convert detect_image_type() from io::Result to BlockResult so
that I/O failures carry the operation name in the error context.
Update the corresponding vmm error variant to wrap BlockError.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-05 16:03:19 +01:00
committed by Rob Bradford
parent 58bdfaee3a
commit b1bc376c91
2 changed files with 9 additions and 5 deletions

View File

@@ -59,6 +59,7 @@ use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::{aio, ioctl_io_nr};
use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult};
use crate::error::{BlockError, BlockErrorKind, BlockResult, ErrorOp};
use crate::vhdx::VhdxError;
const SECTOR_SHIFT: u8 = 9;
@@ -1065,13 +1066,16 @@ pub fn read_aligned_block_size(f: &mut File) -> std::io::Result<Vec<u8>> {
}
/// Determine image type through file parsing.
pub fn detect_image_type(f: &mut File) -> std::io::Result<ImageType> {
let block = read_aligned_block_size(f)?;
pub fn detect_image_type(f: &mut File) -> BlockResult<ImageType> {
let block = read_aligned_block_size(f)
.map_err(|e| BlockError::new(BlockErrorKind::Io, e).with_op(ErrorOp::DetectImageType))?;
// Check 4 first bytes to get the header value and determine the image type
let image_type = if u32::from_be_bytes(block[0..4].try_into().unwrap()) == QCOW_MAGIC {
ImageType::Qcow2
} else if vhd::is_fixed_vhd(f)? {
} else if vhd::is_fixed_vhd(f)
.map_err(|e| BlockError::new(BlockErrorKind::Io, e).with_op(ErrorOp::DetectImageType))?
{
ImageType::FixedVhd
} else if u64::from_le_bytes(block[0..8].try_into().unwrap()) == VHDX_SIGN {
ImageType::Vhdx

View File

@@ -41,7 +41,7 @@ use block::raw_sync::RawFileDiskSync;
use block::vhdx_sync::VhdxDiskSync;
use block::{
ImageType, block_aio_is_supported, block_io_uring_is_supported, detect_image_type,
preallocate_disk, qcow, vhdx,
preallocate_disk, vhdx,
};
#[cfg(feature = "io_uring")]
use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk};
@@ -266,7 +266,7 @@ pub enum DeviceManagerError {
/// Failed to parse disk image format
#[error("Failed to parse disk image format")]
DetectImageType(#[source] io::Error),
DetectImageType(#[source] BlockError),
/// Cannot create serial manager
#[error("Cannot create serial manager")]