vmm: api: Add "power-button" API entry point

This will lead to the triggering of an ACPI button inside the guest in
order to cleanly shutdown the guest.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-01-13 10:06:36 +00:00
parent 843a010163
commit 981bb72a09
4 changed files with 44 additions and 3 deletions

View File

@ -106,6 +106,9 @@ pub enum HttpError {
/// Error setting up migration sender /// Error setting up migration sender
VmSendMigration(ApiError), VmSendMigration(ApiError),
/// Error activating power button
VmPowerButton(ApiError),
} }
impl From<serde_json::Error> for HttpError { impl From<serde_json::Error> for HttpError {
@ -210,6 +213,7 @@ lazy_static! {
r.routes.insert(endpoint!("/vm.delete"), Box::new(VmActionHandler::new(VmAction::Delete))); r.routes.insert(endpoint!("/vm.delete"), Box::new(VmActionHandler::new(VmAction::Delete)));
r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {})); r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {}));
r.routes.insert(endpoint!("/vm.pause"), Box::new(VmActionHandler::new(VmAction::Pause))); r.routes.insert(endpoint!("/vm.pause"), Box::new(VmActionHandler::new(VmAction::Pause)));
r.routes.insert(endpoint!("/vm.power-button"), Box::new(VmActionHandler::new(VmAction::PowerButton)));
r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot))); r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot)));
r.routes.insert(endpoint!("/vm.receive-migration"), Box::new(VmActionHandler::new(VmAction::ReceiveMigration(Arc::default())))); r.routes.insert(endpoint!("/vm.receive-migration"), Box::new(VmActionHandler::new(VmAction::ReceiveMigration(Arc::default()))));
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmActionHandler::new(VmAction::RemoveDevice(Arc::default())))); r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmActionHandler::new(VmAction::RemoveDevice(Arc::default()))));

View File

@ -6,9 +6,10 @@
use crate::api::http::{error_response, EndpointHandler, HttpError}; use crate::api::http::{error_response, EndpointHandler, HttpError};
use crate::api::{ use crate::api::{
vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_vsock, vm_boot, vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_vsock, vm_boot,
vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_receive_migration, vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_power_button, vm_reboot,
vm_remove_device, vm_resize, vm_resize_zone, vm_restore, vm_resume, vm_send_migration, vm_receive_migration, vm_remove_device, vm_resize, vm_resize_zone, vm_restore, vm_resume,
vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, ApiRequest, VmAction, VmConfig, vm_send_migration, vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, ApiRequest, VmAction,
VmConfig,
}; };
use micro_http::{Body, Method, Request, Response, StatusCode, Version}; use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -177,6 +178,9 @@ impl EndpointHandler for VmActionHandler {
Reboot => vm_reboot(api_notifier, api_sender).map_err(HttpError::VmReboot), Reboot => vm_reboot(api_notifier, api_sender).map_err(HttpError::VmReboot),
Pause => vm_pause(api_notifier, api_sender).map_err(HttpError::VmPause), Pause => vm_pause(api_notifier, api_sender).map_err(HttpError::VmPause),
Resume => vm_resume(api_notifier, api_sender).map_err(HttpError::VmResume), Resume => vm_resume(api_notifier, api_sender).map_err(HttpError::VmResume),
PowerButton => {
vm_power_button(api_notifier, api_sender).map_err(HttpError::VmPowerButton)
}
_ => Err(HttpError::BadRequest), _ => Err(HttpError::BadRequest),
} }
} }

View File

@ -146,6 +146,9 @@ pub enum ApiError {
/// Error starting migration sender /// Error starting migration sender
VmSendMigration(MigratableError), VmSendMigration(MigratableError),
/// Error triggering power button
VmPowerButton(VmError),
} }
pub type ApiResult<T> = std::result::Result<T, ApiError>; pub type ApiResult<T> = std::result::Result<T, ApiError>;
@ -302,6 +305,9 @@ pub enum ApiRequest {
/// Outgoing migration /// Outgoing migration
VmSendMigration(Arc<VmSendMigrationData>, Sender<ApiResponse>), VmSendMigration(Arc<VmSendMigrationData>, Sender<ApiResponse>),
// Trigger power button
VmPowerButton(Sender<ApiResponse>),
} }
pub fn vm_create( pub fn vm_create(
@ -385,6 +391,9 @@ pub enum VmAction {
/// Outgoing migration /// Outgoing migration
SendMigration(Arc<VmSendMigrationData>), SendMigration(Arc<VmSendMigrationData>),
/// Power Button for clean shutdown
PowerButton,
} }
fn vm_action( fn vm_action(
@ -416,6 +425,7 @@ fn vm_action(
Snapshot(v) => ApiRequest::VmSnapshot(v, response_sender), Snapshot(v) => ApiRequest::VmSnapshot(v, response_sender),
ReceiveMigration(v) => ApiRequest::VmReceiveMigration(v, response_sender), ReceiveMigration(v) => ApiRequest::VmReceiveMigration(v, response_sender),
SendMigration(v) => ApiRequest::VmSendMigration(v, response_sender), SendMigration(v) => ApiRequest::VmSendMigration(v, response_sender),
PowerButton => ApiRequest::VmPowerButton(response_sender),
}; };
// Send the VM request. // Send the VM request.
@ -459,6 +469,13 @@ pub fn vm_counters(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResul
vm_action(api_evt, api_sender, VmAction::Counters) vm_action(api_evt, api_sender, VmAction::Counters)
} }
pub fn vm_power_button(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
) -> ApiResult<Option<Body>> {
vm_action(api_evt, api_sender, VmAction::PowerButton)
}
pub fn vm_receive_migration( pub fn vm_receive_migration(
api_evt: EventFd, api_evt: EventFd,
api_sender: Sender<ApiRequest>, api_sender: Sender<ApiRequest>,

View File

@ -677,6 +677,14 @@ impl Vmm {
} }
} }
fn vm_power_button(&mut self) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm {
vm.power_button()
} else {
Err(VmError::VmNotRunning)
}
}
fn vm_receive_config<T>( fn vm_receive_config<T>(
&mut self, &mut self,
req: &Request, req: &Request,
@ -1317,6 +1325,14 @@ impl Vmm {
.map(|_| ApiResponsePayload::Empty); .map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?; sender.send(response).map_err(Error::ApiResponseSend)?;
} }
ApiRequest::VmPowerButton(sender) => {
let response = self
.vm_power_button()
.map_err(ApiError::VmPowerButton)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
} }
} }
} }