]> xenbits.xensource.com Git - xen-guest-agent.git/commitdiff
detect early if not in a xen guest
authormillefeuille <mathieu.labourier@vates.tech>
Thu, 3 Oct 2024 08:31:37 +0000 (10:31 +0200)
committermillefeuille <mathieu.labourier@vates.tech>
Fri, 11 Oct 2024 09:17:48 +0000 (11:17 +0200)
Co-authored-by: Daniil Baturin <daniil@baturin.org>
Co-authored-by: Yann Dirson <yann.dirson@vates.tech>
Signed-off-by: millefeuille <mathieu.labourier@vates.tech>
CHANGELOG.md
src/error.rs [new file with mode: 0644]
src/hypervisor.rs [new file with mode: 0644]
src/hypervisor_linux.rs [new file with mode: 0644]
src/main.rs

index 4b9baaa70af40728900e772926daf3e1cbb8302e..3037141e7ab7463ba17a78137839b122c8240786 100644 (file)
@@ -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 (file)
index 0000000..12ba900
--- /dev/null
@@ -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 (file)
index 0000000..29a8ce6
--- /dev/null
@@ -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 (file)
index 0000000..1222916
--- /dev/null
@@ -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)
+        }
+    }
+}
index 476d9b455303f58b613b6dc4de301a2178fccbe9..3a56fbc3b07a277e297bb79063e7cdd848abf1ef 100644 (file)
@@ -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<dyn Error>> {
     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()?;