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<()> { fn override_driver(bdf: &str, driver: &str) -> Result<()> {
let driver_override = format!("/sys/bus/pci/devices/{}/driver_override", bdf); let driver_override = format!("/sys/bus/pci/devices/{}/driver_override", bdf);
fs::write(&driver_override, driver) fs::write(&driver_override, driver)
.context(format!("echo {} > {}", driver, &driver_override))?; .with_context(|| format!("echo {} > {}", driver, &driver_override))?;
info!(sl!(), "echo {} > {}", driver, driver_override); info!(sl!(), "echo {} > {}", driver, driver_override);
Ok(()) 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 // echo bdf > /sys/bus/pci/drivers_probe
std::fs::write(PCI_DRIVER_PROBE, bdf) 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); info!(sl!(), "echo {} > {}", bdf, PCI_DRIVER_PROBE);
Ok(()) Ok(())

View File

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

View File

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

View File

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

View File

@ -30,7 +30,7 @@ pub(crate) fn share_to_guest(
) -> Result<String> { ) -> Result<String> {
let host_dest = do_get_host_path(target, sid, cid, is_volume, false); let host_dest = do_get_host_path(target, sid, cid, is_volume, false);
mount::bind_mount_unchecked(source, &host_dest, readonly) 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 // bind mount remount event is not propagated to mount subtrees, so we have
// to remount the read only dir mount point directly. // 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; let shm_size = shm_volume::DEFAULT_SHM_SIZE;
Arc::new( Arc::new(
shm_volume::ShmVolume::new(m, shm_size) 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) { } else if share_fs_volume::is_share_fs_volume(m) {
Arc::new( Arc::new(
share_fs_volume::ShareFsVolume::new(share_fs, m, cid) share_fs_volume::ShareFsVolume::new(share_fs, m, cid)
.await .await
.context(format!("new share fs volume {:?}", m))?, .with_context(|| format!("new share fs volume {:?}", m))?,
) )
} else if block_volume::is_block_volume(m) { } else if block_volume::is_block_volume(m) {
Arc::new( Arc::new(
block_volume::BlockVolume::new(m) block_volume::BlockVolume::new(m)
.context(format!("new block volume {:?}", m))?, .with_context(|| format!("new block volume {:?}", m))?,
) )
} else if is_skip_volume(m) { } else if is_skip_volume(m) {
info!(sl!(), "skip volume {:?}", m); info!(sl!(), "skip volume {:?}", m);
@ -68,7 +68,7 @@ impl VolumeResource {
} else { } else {
Arc::new( Arc::new(
default_volume::DefaultVolume::new(m) 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 building Kata
newest-version: "1.58.1" 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: specs:
description: "Details of important specifications" description: "Details of important specifications"