runtime-rs: made Qemu::stop_vm() idempotent

Since Hypervisor::stop_vm() is called from the WaitProcess request handling
which appears to be per-container, it can be called multiple times during
kata pod shutdown.  Currently the function errors out on any subsequent
call after the initial one since there's no VM to stop anymore.  This
commit makes the function tolerate that condition.

While it seems conceivable that sandbox shouldn't be stopped by WaitProcess
handling, and the right fix would then have to happen elsewhere, this
commit at least makes qemu driver's behaviour consistent with other
hypervisor drivers in runtime-rs.

We also slightly improve the error message in case there's no
QemuInner::qemu_process instance.

Signed-off-by: Pavel Mores <pmores@redhat.com>
This commit is contained in:
Pavel Mores 2024-06-18 12:05:36 +02:00 committed by Pavel Mores
parent beab17f765
commit af5492e773

View File

@ -167,10 +167,19 @@ impl QemuInner {
pub(crate) async fn stop_vm(&mut self) -> Result<()> {
info!(sl!(), "Stopping QEMU VM");
if let Some(ref mut qemu_process) = &mut self.qemu_process {
info!(sl!(), "QemuInner::stop_vm(): kill()'ing qemu");
qemu_process.kill().await.map_err(anyhow::Error::from)
let is_qemu_running = qemu_process.id().is_some();
if is_qemu_running {
info!(sl!(), "QemuInner::stop_vm(): kill()'ing qemu");
qemu_process.kill().await.map_err(anyhow::Error::from)
} else {
info!(
sl!(),
"QemuInner::stop_vm(): qemu process isn't running (likely stopped already)"
);
Ok(())
}
} else {
Err(anyhow!("qemu process not running"))
Err(anyhow!("qemu process has not been started yet"))
}
}