runtime-rs: use withContext to evaluate lazily

Fixes: #4129
Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
Zhongtao Hu 2022-05-19 14:52:31 +08:00 committed by Fupan Li
parent fd4c26f9c1
commit 06f398a34f
7 changed files with 31 additions and 22 deletions

View File

@ -11,7 +11,7 @@ use anyhow::{anyhow, Context, Result};
fn override_driver(bdf: &str, driver: &str) -> Result<()> {
let driver_override = format!("/sys/bus/pci/devices/{}/driver_override", bdf);
fs::write(&driver_override, driver)
.context(format!("echo {} > {}", driver, &driver_override))?;
.with_context(|| format!("echo {} > {}", driver, &driver_override))?;
info!(sl!(), "echo {} > {}", driver, driver_override);
Ok(())
}
@ -138,7 +138,7 @@ pub fn bind_device_to_host(bdf: &str, host_driver: &str, _vendor_device_id: &str
// echo bdf > /sys/bus/pci/drivers_probe
std::fs::write(PCI_DRIVER_PROBE, bdf)
.context(format!("echo {} > {}", bdf, PCI_DRIVER_PROBE))?;
.with_context(|| format!("echo {} > {}", bdf, PCI_DRIVER_PROBE))?;
info!(sl!(), "echo {} > {}", bdf, PCI_DRIVER_PROBE);
Ok(())

View File

@ -65,11 +65,11 @@ impl PhysicalEndpoint {
// get vendor and device id from pci space (sys/bus/pci/devices/$bdf)
let iface_device_path = sys_pci_devices_path.join(&bdf).join("device");
let device_id = std::fs::read_to_string(&iface_device_path)
.context(format!("read device path {:?}", &iface_device_path))?;
.with_context(|| format!("read device path {:?}", &iface_device_path))?;
let iface_vendor_path = sys_pci_devices_path.join(&bdf).join("vendor");
let vendor_id = std::fs::read_to_string(&iface_vendor_path)
.context(format!("read vendor path {:?}", &iface_vendor_path))?;
.with_context(|| format!("read vendor path {:?}", &iface_vendor_path))?;
Ok(Self {
iface_name: name.to_string(),
@ -99,10 +99,7 @@ impl Endpoint for PhysicalEndpoint {
&self.driver,
&self.vendor_device_id.vendor_device_id(),
)
.context(format!(
"bind physical endpoint from {} to vfio",
&self.driver
))?;
.with_context(|| format!("bind physical endpoint from {} to vfio", &self.driver))?;
// set vfio's bus type, pci or mmio. Mostly use pci by default.
let mode = match self.driver.as_str() {
@ -116,7 +113,7 @@ impl Endpoint for PhysicalEndpoint {
sysfs_path: "".to_string(),
bus_slot_func: self.bdf.clone(),
mode: device::VfioBusMode::new(mode)
.context(format!("new vfio bus mode {:?}", mode))?,
.with_context(|| format!("new vfio bus mode {:?}", mode))?,
});
hypervisor.add_device(d).await.context("add device")?;
Ok(())
@ -136,10 +133,12 @@ impl Endpoint for PhysicalEndpoint {
&self.driver,
&self.vendor_device_id.vendor_device_id(),
)
.context(format!(
"bind physical endpoint device from vfio to {}",
&self.driver
))?;
.with_context(|| {
format!(
"bind physical endpoint device from vfio to {}",
&self.driver
)
})?;
Ok(())
}
}

View File

@ -56,7 +56,7 @@ impl NetworkModel for RouteModel {
.args(&ca)
.output()
.await
.context(format!("run command ip args {:?}", &ca))?;
.with_context(|| format!("run command ip args {:?}", &ca))?;
if !output.status.success() {
return Err(anyhow!(
"run command ip args {:?} error {}",

View File

@ -19,11 +19,11 @@ impl NetnsGuard {
let old_netns = if !new_netns_path.is_empty() {
let current_netns_path = format!("/proc/{}/task/{}/ns/{}", getpid(), gettid(), "net");
let old_netns = File::open(&current_netns_path)
.context(format!("open current netns path {}", &current_netns_path))?;
.with_context(|| format!("open current netns path {}", &current_netns_path))?;
let new_netns = File::open(&new_netns_path)
.context(format!("open new netns path {}", &new_netns_path))?;
.with_context(|| format!("open new netns path {}", &new_netns_path))?;
setns(new_netns.as_raw_fd(), CloneFlags::CLONE_NEWNET)
.context("set netns to new netns")?;
.with_context(|| "set netns to new netns")?;
info!(
sl!(),
"set netns from old {:?} to new {:?} tid {}",

View File

@ -30,7 +30,7 @@ pub(crate) fn share_to_guest(
) -> Result<String> {
let host_dest = do_get_host_path(target, sid, cid, is_volume, false);
mount::bind_mount_unchecked(source, &host_dest, readonly)
.context(format!("failed to bind mount {} to {}", source, &host_dest))?;
.with_context(|| format!("failed to bind mount {} to {}", source, &host_dest))?;
// bind mount remount event is not propagated to mount subtrees, so we have
// to remount the read only dir mount point directly.

View File

@ -49,18 +49,18 @@ impl VolumeResource {
let shm_size = shm_volume::DEFAULT_SHM_SIZE;
Arc::new(
shm_volume::ShmVolume::new(m, shm_size)
.context(format!("new shm volume {:?}", m))?,
.with_context(|| format!("new shm volume {:?}", m))?,
)
} else if share_fs_volume::is_share_fs_volume(m) {
Arc::new(
share_fs_volume::ShareFsVolume::new(share_fs, m, cid)
.await
.context(format!("new share fs volume {:?}", m))?,
.with_context(|| format!("new share fs volume {:?}", m))?,
)
} else if block_volume::is_block_volume(m) {
Arc::new(
block_volume::BlockVolume::new(m)
.context(format!("new block volume {:?}", m))?,
.with_context(|| format!("new block volume {:?}", m))?,
)
} else if is_skip_volume(m) {
info!(sl!(), "skip volume {:?}", m);
@ -68,7 +68,7 @@ impl VolumeResource {
} else {
Arc::new(
default_volume::DefaultVolume::new(m)
.context(format!("new default volume {:?}", m))?,
.with_context(|| format!("new default volume {:?}", m))?,
)
};

View File

@ -282,6 +282,16 @@ languages:
building Kata
newest-version: "1.58.1"
golangci-lint:
description: "golangci-lint"
notes: "'version' is the default minimum version used by this project."
version: "1.41.1"
meta:
description: |
'newest-version' is the latest version known to work when
building Kata
newest-version: "1.46.2"
specs:
description: "Details of important specifications"