mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
block: raw: Implement disk preallocation for sparse=false
When sparse=false is configured, preallocate the entire raw disk file at startup using fallocate(). This provides space reservation and reduces fragmentation. Only applies to raw disks. QCOW2/VHD/VHDX formats manage their own allocation. Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
committed by
Rob Bradford
parent
0a287793df
commit
49a30cbbaf
@@ -787,6 +787,42 @@ fn probe_block_device_sparse_support(fd: libc::c_int) -> bool {
|
|||||||
supported
|
supported
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Preallocate disk space for a disk image file.
|
||||||
|
///
|
||||||
|
/// Uses `fallocate()` to allocate all disk space upfront, ensuring storage
|
||||||
|
/// availability and reducing fragmentation. Allocating all blocks upfront is
|
||||||
|
/// more likely to place them contiguously than allocating on demand during
|
||||||
|
/// random writes.
|
||||||
|
pub fn preallocate_disk<P: AsRef<Path>>(file: &File, path: P) {
|
||||||
|
let size = match file.metadata() {
|
||||||
|
Ok(m) => m.len(),
|
||||||
|
Err(e) => {
|
||||||
|
warn!("Failed to get metadata for {:?}: {}", path.as_ref(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if size == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SAFETY: FFI call with valid file descriptor and size
|
||||||
|
let ret = unsafe { libc::fallocate(file.as_raw_fd(), 0, 0, size as libc::off_t) };
|
||||||
|
|
||||||
|
if ret != 0 {
|
||||||
|
warn!(
|
||||||
|
"Failed to preallocate disk space for {:?}: {}",
|
||||||
|
path.as_ref(),
|
||||||
|
io::Error::last_os_error()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
debug!(
|
||||||
|
"Preallocated {size} bytes for disk image {:?}",
|
||||||
|
path.as_ref()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait AsyncAdaptor {
|
pub trait AsyncAdaptor {
|
||||||
fn read_vectored_sync(
|
fn read_vectored_sync(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ use block::raw_async_aio::RawFileDiskAio;
|
|||||||
use block::raw_sync::RawFileDiskSync;
|
use block::raw_sync::RawFileDiskSync;
|
||||||
use block::vhdx_sync::VhdxDiskSync;
|
use block::vhdx_sync::VhdxDiskSync;
|
||||||
use block::{
|
use block::{
|
||||||
ImageType, block_aio_is_supported, block_io_uring_is_supported, detect_image_type, qcow, vhdx,
|
ImageType, block_aio_is_supported, block_io_uring_is_supported, detect_image_type,
|
||||||
|
preallocate_disk, qcow, vhdx,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "io_uring")]
|
#[cfg(feature = "io_uring")]
|
||||||
use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk};
|
use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk};
|
||||||
@@ -2699,6 +2700,14 @@ impl DeviceManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ImageType::Raw => {
|
ImageType::Raw => {
|
||||||
|
// For non-sparse RAW disks, preallocate disk space
|
||||||
|
if !disk_cfg.readonly
|
||||||
|
&& !disk_cfg.sparse
|
||||||
|
&& let Some(path) = &disk_cfg.path
|
||||||
|
{
|
||||||
|
preallocate_disk(&file, path);
|
||||||
|
}
|
||||||
|
|
||||||
// Use asynchronous backend relying on io_uring if the
|
// Use asynchronous backend relying on io_uring if the
|
||||||
// syscalls are supported.
|
// syscalls are supported.
|
||||||
if cfg!(feature = "io_uring")
|
if cfg!(feature = "io_uring")
|
||||||
|
|||||||
Reference in New Issue
Block a user