runtime-rs: Propagate and map discard as Dragonball sparse

Map BlockConfigModern.discard_unmap to Dragonball block device
sparse support when adding modern block devices.
This lets runtime-rs request guest-visible discard support through
the Dragonball block device configuration while leaving vhost-user
block devices disabled.

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn
2026-07-13 11:57:19 +08:00
committed by Fabiano Fidêncio
parent 95e4703e84
commit 4f28b68af0
2 changed files with 23 additions and 2 deletions

View File

@@ -700,6 +700,10 @@ mod tests {
let mut q = vq.create_queue();
data_descs.clear();
// status desc read only
m.write_obj::<u32>(VIRTIO_BLK_T_GET_ID, GuestAddress(0x1000))
.unwrap();
vq.dtable(1)
.set(0x2000, 0x40, VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE, 2);
vq.dtable(2).flags().store(0);
assert!(matches!(
Request::parse(&mut q.pop_descriptor_chain(m).unwrap(), &mut data_descs, 32),
@@ -711,6 +715,10 @@ mod tests {
let mut q = vq.create_queue();
data_descs.clear();
// status desc too small
m.write_obj::<u32>(VIRTIO_BLK_T_GET_ID, GuestAddress(0x1000))
.unwrap();
vq.dtable(1)
.set(0x2000, 0x40, VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE, 2);
vq.dtable(2).flags().store(VIRTQ_DESC_F_WRITE);
vq.dtable(2).len().store(0);
assert!(matches!(
@@ -1032,6 +1040,7 @@ mod tests {
let mut dev = Block::<Arc<GuestMemoryMmap<()>>>::new(
vec![disk_image],
true,
false,
Arc::new(vec![128]),
epoll_mgr.clone(),
vec![],
@@ -1146,6 +1155,7 @@ mod tests {
let mut dev = Block::<Arc<GuestMemoryMmap>>::new(
vec![disk_image],
true,
false,
Arc::new(vec![128]),
epoll_mgr,
vec![],

View File

@@ -70,6 +70,7 @@ impl DragonballInner {
no_drop,
is_direct,
driver_option,
sparse,
) = {
let dev = block_device.lock().await;
let cfg = &dev.config;
@@ -80,6 +81,7 @@ impl DragonballInner {
cfg.no_drop,
cfg.is_direct,
cfg.driver_option.clone(),
cfg.discard_unmap,
)
};
@@ -106,13 +108,15 @@ impl DragonballInner {
no_drop,
is_direct,
use_pci_bus,
sparse,
)
.context("add block modern device")?;
info!(
sl!(),
"BlockModern hotplug result: device_id={}, guest_device_id={:?}",
device_id, guest_device_id
device_id,
guest_device_id
);
if let Some(slot) = guest_device_id {
@@ -132,6 +136,7 @@ impl DragonballInner {
block.no_drop,
None,
None,
false,
)
.context("add vhost user based block device")?;
Ok(DeviceType::VhostUserBlk(block))
@@ -253,6 +258,7 @@ impl DragonballInner {
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn add_block_device(
&mut self,
path: &str,
@@ -261,6 +267,7 @@ impl DragonballInner {
no_drop: bool,
is_direct: Option<bool>,
use_pci_bus: Option<bool>,
sparse: bool,
) -> Result<Option<i32>> {
let jailed_drive = self.get_resource(path, id).context("get resource")?;
self.cached_block_devices.insert(id.to_string());
@@ -294,6 +301,7 @@ impl DragonballInner {
no_drop,
is_read_only: read_only,
use_pci_bus,
sparse,
rate_limiter: Some(block_rate_limit),
..Default::default()
};
@@ -311,7 +319,10 @@ impl DragonballInner {
.insert_block_device(blk_cfg, Duration::from_millis(DEFAULT_HOTPLUG_TIMEOUT))
.context("insert block device");
match &result {
Ok(guest_id) => info!(sl!(), "add_block_device success: id={}, guest_id={:?}", id, guest_id),
Ok(guest_id) => info!(
sl!(),
"add_block_device success: id={}, guest_id={:?}", id, guest_id
),
Err(e) => error!(sl!(), "add_block_device failed: id={}, error={:?}", id, e),
}
result