From: millefeuille Date: Thu, 3 Oct 2024 08:31:37 +0000 (+0200) Subject: detect early if not in a xen guest X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=dd8b1892e26b3396192eb2aeaed55d1436e39182;p=xen-guest-agent.git detect early if not in a xen guest Co-authored-by: Daniil Baturin Co-authored-by: Yann Dirson Signed-off-by: millefeuille --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b9baaa..3037141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ Changelog](https://keepachangelog.com/en/1.0.0/) ### new features +* in Linux guests, exits early with better diagnostic when not + running under Xen * the RPM now enables and starts the service on first install * the RPM now causes xe-guest-utilities to be uninstalled automatically diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..12ba900 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,24 @@ +use std::error::Error; +use std::fmt::{Debug, Formatter, Result, Display}; + +pub enum XenError { + NotInGuest, + HypervisorNotXen, +} + +impl Error for XenError {} + +impl Debug for XenError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + write!(f, "{}", self) + } +} + +impl Display for XenError { + fn fmt(&self, f: &mut Formatter) -> Result { + match *self { + XenError::NotInGuest => write!(f, "Cannot identify hypervisor"), + XenError::HypervisorNotXen => write!(f, "Hypervisor is not Xen"), + } + } +} diff --git a/src/hypervisor.rs b/src/hypervisor.rs new file mode 100644 index 0000000..29a8ce6 --- /dev/null +++ b/src/hypervisor.rs @@ -0,0 +1,7 @@ +use crate::error::XenError; + +pub fn check_is_in_xen_guest() -> Result<(), XenError> { + // NOTE: 'Ok' here implies that we do not know how to check for the hypervisor, + // so we assume the users are aware of their actions. + Ok(()) +} diff --git a/src/hypervisor_linux.rs b/src/hypervisor_linux.rs new file mode 100644 index 0000000..1222916 --- /dev/null +++ b/src/hypervisor_linux.rs @@ -0,0 +1,16 @@ +use std::fs; +use crate::error::XenError; + +pub fn check_is_in_xen_guest() -> Result<(), XenError> { + match fs::read_to_string("/sys/hypervisor/type") { + Ok(hypervisor_type) => { + let hypervisor_type = hypervisor_type.trim(); + log::debug!("hypervisor_type {hypervisor_type}"); + if hypervisor_type.eq("xen") { Ok(()) } else { Err(XenError::HypervisorNotXen) } + }, + Err(err) => { + log::error!("could not identify hypervisor type, {err}"); + Err(XenError::NotInGuest) + } + } +} diff --git a/src/main.rs b/src/main.rs index 476d9b4..3a56fbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,17 @@ mod collector_memory; #[cfg_attr(target_os = "freebsd", path = "vif_detect_freebsd.rs")] mod vif_detect; +#[cfg_attr(target_os = "linux", path = "hypervisor_linux.rs")] +mod hypervisor; + +mod error; + use clap::Parser; use crate::collector_memory::MemorySource; use crate::collector_net::NetworkSource; use crate::datastructs::KernelInfo; +use crate::hypervisor::check_is_in_xen_guest; use crate::publisher::Publisher; use futures::{pin_mut, select, FutureExt, TryStreamExt}; @@ -36,12 +42,18 @@ const REPORT_INTERNAL_NICS: bool = false; // FIXME make this a CLI flag const MEM_PERIOD_SECONDS: u64 = 60; const DEFAULT_LOGLEVEL: &str = "info"; + #[tokio::main] async fn main() -> Result<(), Box> { let cli = Cli::parse(); setup_logger(cli.stderr, &cli.loglevel)?; + if let Err(err) = check_is_in_xen_guest() { + log::error!("not starting xen-guest-agent, {err}"); + return Err(err.into()) + } + let mut publisher = Publisher::new()?; let mut collector_memory = MemorySource::new()?;