Enrich logs
The name of the VMs and identity of their requesters are shown in the
log. Previously, absence of this information made it difficult for
developers to identity which invocation of crosvm is for which
application.
Bug: 251751405
Test: run a VM and observe the log
Non-protected virtual machine "VmRunApp" (owner: "shell", cid: 10) created
Non-protected virtual machine "VmRunApp" (owner: "shell", cid: 10) started
Non-protected virtual machine "VmRunApp" (owner: "shell", cid: 10) stopped
Change-Id: I3678ca2257eb6437cb9ca7a7ad84643bb9612bc4
diff --git a/virtualizationservice/src/crosvm.rs b/virtualizationservice/src/crosvm.rs
index db6da43..299411d 100644
--- a/virtualizationservice/src/crosvm.rs
+++ b/virtualizationservice/src/crosvm.rs
@@ -22,11 +22,12 @@
use libc::{sysconf, _SC_CLK_TCK};
use log::{debug, error, info};
use semver::{Version, VersionReq};
-use nix::{fcntl::OFlag, unistd::pipe2};
+use nix::{fcntl::OFlag, unistd::pipe2, unistd::Uid, unistd::User};
use regex::{Captures, Regex};
use shared_child::SharedChild;
use std::borrow::Cow;
use std::cmp::max;
+use std::fmt;
use std::fs::{read_to_string, remove_dir_all, File};
use std::io::{self, Read};
use std::mem;
@@ -225,6 +226,19 @@
payload_state: Mutex<PayloadState>,
/// Represents the condition that payload_state was updated
payload_state_updated: Condvar,
+ /// The human readable name of requester_uid
+ requester_uid_name: String,
+}
+
+impl fmt::Display for VmInstance {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let adj = if self.protected { "Protected" } else { "Non-protected" };
+ write!(
+ f,
+ "{} virtual machine \"{}\" (owner: {}, cid: {})",
+ adj, self.name, self.requester_uid_name, self.cid
+ )
+ }
}
impl VmInstance {
@@ -239,7 +253,11 @@
let cid = config.cid;
let name = config.name.clone();
let protected = config.protected;
- Ok(VmInstance {
+ let requester_uid_name = User::from_uid(Uid::from_raw(requester_uid))
+ .ok()
+ .flatten()
+ .map_or_else(|| format!("{}", requester_uid), |u| u.name);
+ let instance = VmInstance {
vm_state: Mutex::new(VmState::NotStarted { config }),
cid,
name,
@@ -252,7 +270,10 @@
vm_metric: Mutex::new(Default::default()),
payload_state: Mutex::new(PayloadState::Starting),
payload_state_updated: Condvar::new(),
- })
+ requester_uid_name,
+ };
+ info!("{} created", &instance);
+ Ok(instance)
}
/// Starts an instance of `crosvm` to manage the VM. The `crosvm` instance will be killed when
@@ -260,7 +281,11 @@
pub fn start(self: &Arc<Self>) -> Result<(), Error> {
let mut vm_metric = self.vm_metric.lock().unwrap();
vm_metric.start_timestamp = Some(SystemTime::now());
- self.vm_state.lock().unwrap().start(self.clone())
+ let ret = self.vm_state.lock().unwrap().start(self.clone());
+ if ret.is_ok() {
+ info!("{} started", &self);
+ }
+ ret.with_context(|| format!("{} failed to start", &self))
}
/// Monitors the exit of the VM (i.e. termination of the `child` process). When that happens,
@@ -284,6 +309,7 @@
*vm_state = VmState::Dead;
// Ensure that the mutex is released before calling the callbacks.
drop(vm_state);
+ info!("{} exited", &self);
// Read the pipe to see if any failure reason is written
let mut failure_reason = String::new();