VirtualizationService notifies to its client when ramdump is available
When a kernel panic occurs in a pVM and the ramdump is enabled there, a
ramdump file is generated.
This file should eventually be consumed by the client (the owner of the
VM) for further analysis. VirtualizationService let its client know that
a ramdump has been created and provide access to it.
Specifically, the end-to-end flow is as follows:
1) When starting a VM, an empty file is created under the VM-specific
directory (`/data/misc/virtualizationservice/<cid>/ramdump`).
2) The file becomes a backing store for a virtio-console device
(/dev/hvc3).
3) When a kernel panic occurs, the `crashdump` binary is executed and
the `/proc/vmcore` is written to `/dev/hvc3`. After the dump is done,
the VM triggers a reboot which is kills crosvm.
4) Virtualizationservice is notified with the exit of crosvm. It then
checks the size of the ramdump file. If that is not empty, it can
assume that a ramdump was occurred in the pVM.
5) Then virtualizationservice notifies the event via
`IVirtualMachineCallback.onRamdump(ParcelFileDescriptor)`, where the
parcel file descriptor is the handle to the ramdump file.
6) Client reads the ramdump file.
This change also adds `--ramdump` option to the `vm` tool to designate
the path where ramdump is saved to.
Bug: 238278104
Test: follow the steps. Automated tests will be added in a followup CL
1) Run a pVM:
adb shell /apex/com.android.virt/bin/vm run-app --debug full --mem 300 \
--ramdump /data/local/tmp/virt/myramdump \
/data/local/tmp/virt/MicrodroidDemoApp.apk \
/data/local/tmp/virt/apk.idsig /data/local/tmp/virt/instance.img \
assets/vm_config.json
2) Adb shell into the VM
adb forward tcp:8000 vsock:10:5555
adb connect localhost:8000
adb -s localhost:8000 root
adb -s localhost:8000 shell
3) Load the crashdump kernel
/system/bin/kexec \
/system/etc/microdroid_crashdump_kernel \
/system/etc/microdroid_crashdump_initrd.img \
"1 rdinit=/bin/crashdump nr_cpus=1 reset_devices console=hvc0 earlycon=uart8250,mmio,0x3f8"
4) Trigger a crash
echo c > /proc/sysrq-trigger
5) Check the ramdump at /data/local/tmp/virt/myramdump
Change-Id: I1f90537961632708ca5a889cdd53390030518bb8
diff --git a/vm/src/main.rs b/vm/src/main.rs
index 8450b41..60786ac 100644
--- a/vm/src/main.rs
+++ b/vm/src/main.rs
@@ -67,6 +67,10 @@
#[structopt(long)]
log: Option<PathBuf>,
+ /// Path to file where ramdump is recorded on kernel panic
+ #[structopt(long)]
+ ramdump: Option<PathBuf>,
+
/// Debug level of the VM. Supported values: "none" (default), "app_only", and "full".
#[structopt(long, default_value = "none", parse(try_from_str=parse_debug_level))]
debug: DebugLevel,
@@ -198,6 +202,7 @@
daemonize,
console,
log,
+ ramdump,
debug,
protected,
mem,
@@ -214,6 +219,7 @@
daemonize,
console.as_deref(),
log.as_deref(),
+ ramdump.as_deref(),
debug,
protected,
mem,
diff --git a/vm/src/run.rs b/vm/src/run.rs
index ca71665..44eb27a 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -49,6 +49,7 @@
daemonize: bool,
console_path: Option<&Path>,
log_path: Option<&Path>,
+ ramdump_path: Option<&Path>,
debug_level: DebugLevel,
protected: bool,
mem: Option<u32>,
@@ -115,6 +116,7 @@
daemonize,
console_path,
log_path,
+ ramdump_path,
)
}
@@ -149,6 +151,7 @@
daemonize,
console_path,
log_path,
+ /* ramdump_path */ None,
)
}
@@ -171,6 +174,7 @@
daemonize: bool,
console_path: Option<&Path>,
log_path: Option<&Path>,
+ ramdump_path: Option<&Path>,
) -> Result<(), Error> {
let console = if let Some(console_path) = console_path {
Some(
@@ -214,12 +218,27 @@
// Wait until the VM or VirtualizationService dies. If we just returned immediately then the
// IVirtualMachine Binder object would be dropped and the VM would be killed.
let death_reason = vm.wait_for_death();
+
+ if let Some(path) = ramdump_path {
+ save_ramdump_if_available(path, &vm)?;
+ }
println!("{}", death_reason);
}
Ok(())
}
+fn save_ramdump_if_available(path: &Path, vm: &VmInstance) -> Result<(), Error> {
+ if let Some(mut ramdump) = vm.get_ramdump() {
+ let mut file =
+ File::create(path).context(format!("Failed to create ramdump file {:?}", path))?;
+ let size = std::io::copy(&mut ramdump, &mut file)
+ .context(format!("Failed to save ramdump to file {:?}", path))?;
+ eprintln!("Ramdump ({} bytes) saved to {:?}", size, path);
+ }
+ Ok(())
+}
+
fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> {
let mut archive = ZipArchive::new(File::open(apk)?)?;
let config_file = archive.by_name(config_path)?;
@@ -268,6 +287,11 @@
Ok(())
}
+ fn onRamdump(&self, _cid: i32, _stream: &ParcelFileDescriptor) -> BinderResult<()> {
+ // Do nothing. We get ramdump from the vmclient library.
+ Ok(())
+ }
+
fn onDied(&self, _cid: i32, _reason: DeathReason) -> BinderResult<()> {
Ok(())
}