diff --git a/block/src/raw_async.rs b/block/src/raw_async.rs index 90332aa4b..7fa3208f4 100644 --- a/block/src/raw_async.rs +++ b/block/src/raw_async.rs @@ -3,7 +3,8 @@ // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause use std::fs::File; -use std::io::Error; +use std::io::{self, Error}; +use std::os::unix::fs::FileTypeExt; use std::os::unix::io::{AsRawFd, RawFd}; use io_uring::{IoUring, opcode, types}; @@ -68,9 +69,30 @@ impl disk_file::SparseCapable for RawFileDisk { impl disk_file::Resizable for RawFileDisk { fn resize(&mut self, size: u64) -> BlockResult<()> { - self.file - .set_len(size) - .map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e))) + let fd_metadata = self + .file + .metadata() + .map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))?; + + if fd_metadata.file_type().is_block_device() { + // Block devices cannot be resized via ftruncate - they are resized + // externally (LVM, losetup -c, etc.). Verify the size matches. + let (actual_size, _) = query_device_size(&self.file) + .map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))?; + if actual_size != size { + return Err(BlockError::new( + BlockErrorKind::Io, + DiskFileError::ResizeError(io::Error::other(format!( + "Block device size {actual_size} does not match requested size {size}" + ))), + )); + } + Ok(()) + } else { + self.file + .set_len(size) + .map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e))) + } } }