mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-01 00:46:38 +00:00
runtime-rs: Refactor block device config
Support aio, block driver and other block device info. match block aio mode and its cache mode. Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
@@ -8,15 +8,15 @@ use std::{collections::HashMap, sync::Arc};
|
|||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use kata_sys_util::rand::RandomBytes;
|
use kata_sys_util::rand::RandomBytes;
|
||||||
use kata_types::config::hypervisor::{TopologyConfigInfo, VIRTIO_SCSI};
|
use kata_types::config::hypervisor::{BlockDeviceInfo, TopologyConfigInfo, VIRTIO_SCSI};
|
||||||
use tokio::sync::{Mutex, RwLock};
|
use tokio::sync::{Mutex, RwLock};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor,
|
vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, BlockDeviceAio,
|
||||||
NetworkDevice, PCIePortDevice, ProtectionDevice, ShareFsDevice, VfioDevice, VhostUserConfig,
|
HybridVsockDevice, Hypervisor, NetworkDevice, PCIePortDevice, ProtectionDevice, ShareFsDevice,
|
||||||
VhostUserNetDevice, VsockDevice, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE,
|
VfioDevice, VhostUserConfig, VhostUserNetDevice, VsockDevice, KATA_BLK_DEV_TYPE,
|
||||||
KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO,
|
KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE,
|
||||||
VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
|
VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -117,12 +117,8 @@ impl DeviceManager {
|
|||||||
self.pcie_topology.clone()
|
self.pcie_topology.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_block_driver(&self) -> String {
|
async fn get_block_driver(&self) -> BlockDeviceInfo {
|
||||||
self.hypervisor
|
self.hypervisor.hypervisor_config().await.blockdev_info
|
||||||
.hypervisor_config()
|
|
||||||
.await
|
|
||||||
.blockdev_info
|
|
||||||
.block_device_driver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn try_add_device(&mut self, device_id: &str) -> Result<()> {
|
async fn try_add_device(&mut self, device_id: &str) -> Result<()> {
|
||||||
@@ -498,6 +494,14 @@ impl DeviceManager {
|
|||||||
.context("failed to get host path")?;
|
.context("failed to get host path")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let block_device_aio = self.get_block_driver().await.block_device_aio;
|
||||||
|
block_config.is_direct = if BlockDeviceAio::Native == BlockDeviceAio::new(&block_device_aio)
|
||||||
|
{
|
||||||
|
Some(true)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Arc::new(Mutex::new(BlockDevice::new(
|
Ok(Arc::new(Mutex::new(BlockDevice::new(
|
||||||
device_id,
|
device_id,
|
||||||
block_config,
|
block_config,
|
||||||
@@ -623,7 +627,7 @@ pub async fn do_update_device(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_block_driver(d: &RwLock<DeviceManager>) -> String {
|
pub async fn get_block_driver(d: &RwLock<DeviceManager>) -> BlockDeviceInfo {
|
||||||
d.read().await.get_block_driver().await
|
d.read().await.get_block_driver().await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -670,7 +674,7 @@ mod tests {
|
|||||||
assert!(dm.is_ok());
|
assert!(dm.is_ok());
|
||||||
|
|
||||||
let d = dm.unwrap();
|
let d = dm.unwrap();
|
||||||
let block_driver = get_block_driver(&d).await;
|
let block_driver = get_block_driver(&d).await.block_device_driver;
|
||||||
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
||||||
path_on_host: "/dev/dddzzz".to_string(),
|
path_on_host: "/dev/dddzzz".to_string(),
|
||||||
driver_option: block_driver,
|
driver_option: block_driver,
|
||||||
|
@@ -24,9 +24,9 @@ pub use vfio::{
|
|||||||
pub use vhost_user::{VhostUserConfig, VhostUserDevice, VhostUserType};
|
pub use vhost_user::{VhostUserConfig, VhostUserDevice, VhostUserType};
|
||||||
pub use vhost_user_net::VhostUserNetDevice;
|
pub use vhost_user_net::VhostUserNetDevice;
|
||||||
pub use virtio_blk::{
|
pub use virtio_blk::{
|
||||||
BlockConfig, BlockDevice, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE,
|
BlockConfig, BlockDevice, BlockDeviceAio, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE,
|
||||||
KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO,
|
KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW,
|
||||||
VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
|
VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
|
||||||
};
|
};
|
||||||
pub use virtio_fs::{
|
pub use virtio_fs::{
|
||||||
ShareFsConfig, ShareFsDevice, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType,
|
ShareFsConfig, ShareFsDevice, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType,
|
||||||
|
@@ -25,7 +25,7 @@ pub const KATA_CCW_DEV_TYPE: &str = "ccw";
|
|||||||
pub const KATA_NVDIMM_DEV_TYPE: &str = "nvdimm";
|
pub const KATA_NVDIMM_DEV_TYPE: &str = "nvdimm";
|
||||||
pub const KATA_SCSI_DEV_TYPE: &str = "scsi";
|
pub const KATA_SCSI_DEV_TYPE: &str = "scsi";
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
pub enum BlockDeviceAio {
|
pub enum BlockDeviceAio {
|
||||||
// IoUring is the Linux io_uring I/O implementation.
|
// IoUring is the Linux io_uring I/O implementation.
|
||||||
#[default]
|
#[default]
|
||||||
|
@@ -156,7 +156,9 @@ impl SwapTask {
|
|||||||
let swap_path = swap_path.to_string_lossy().to_string();
|
let swap_path = swap_path.to_string_lossy().to_string();
|
||||||
|
|
||||||
// Add swap file to sandbox
|
// Add swap file to sandbox
|
||||||
let block_driver = get_block_driver(&self.device_manager).await;
|
let block_driver = get_block_driver(&self.device_manager)
|
||||||
|
.await
|
||||||
|
.block_device_driver;
|
||||||
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
||||||
path_on_host: swap_path.clone(),
|
path_on_host: swap_path.clone(),
|
||||||
driver_option: block_driver,
|
driver_option: block_driver,
|
||||||
|
@@ -396,7 +396,7 @@ impl ResourceManagerInner {
|
|||||||
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
||||||
major: d.major(),
|
major: d.major(),
|
||||||
minor: d.minor(),
|
minor: d.minor(),
|
||||||
driver_option: block_driver,
|
driver_option: block_driver.block_device_driver,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -50,7 +50,7 @@ impl BlockRootfs {
|
|||||||
fs::create_dir_all(&host_path)
|
fs::create_dir_all(&host_path)
|
||||||
.map_err(|e| anyhow!("failed to create rootfs dir {}: {:?}", host_path, e))?;
|
.map_err(|e| anyhow!("failed to create rootfs dir {}: {:?}", host_path, e))?;
|
||||||
|
|
||||||
let block_driver = get_block_driver(d).await;
|
let block_driver = get_block_driver(d).await.block_device_driver;
|
||||||
|
|
||||||
let block_device_config = &mut BlockConfig {
|
let block_device_config = &mut BlockConfig {
|
||||||
major: stat::major(dev_id) as i64,
|
major: stat::major(dev_id) as i64,
|
||||||
|
@@ -39,7 +39,7 @@ impl BlockVolume {
|
|||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
None => return Err(anyhow!("mount source path is empty")),
|
None => return Err(anyhow!("mount source path is empty")),
|
||||||
};
|
};
|
||||||
let block_driver = get_block_driver(d).await;
|
let block_driver = get_block_driver(d).await.block_device_driver;
|
||||||
let fstat = stat::stat(mnt_src).context(format!("stat {}", mnt_src.display()))?;
|
let fstat = stat::stat(mnt_src).context(format!("stat {}", mnt_src.display()))?;
|
||||||
let block_device_config = BlockConfig {
|
let block_device_config = BlockConfig {
|
||||||
major: stat::major(fstat.st_rdev) as i64,
|
major: stat::major(fstat.st_rdev) as i64,
|
||||||
|
@@ -36,7 +36,7 @@ impl RawblockVolume {
|
|||||||
read_only: bool,
|
read_only: bool,
|
||||||
sid: &str,
|
sid: &str,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let block_driver = get_block_driver(d).await;
|
let block_driver = get_block_driver(d).await.block_device_driver;
|
||||||
|
|
||||||
// check volume type
|
// check volume type
|
||||||
if mount_info.volume_type != KATA_DIRECT_VOLUME_TYPE {
|
if mount_info.volume_type != KATA_DIRECT_VOLUME_TYPE {
|
||||||
|
@@ -74,7 +74,7 @@ impl SPDKVolume {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let block_driver = get_block_driver(d).await;
|
let block_driver = get_block_driver(d).await.block_device_driver;
|
||||||
|
|
||||||
let vhu_blk_config = &mut VhostUserConfig {
|
let vhu_blk_config = &mut VhostUserConfig {
|
||||||
socket_path: device,
|
socket_path: device,
|
||||||
|
Reference in New Issue
Block a user