vmm: Add ability to add virtio-fs device post-boot

Adds DeviceManager method `make_virtio_fs_device` which creates a single
device, and modifies `make_virtio_fs_devices` to use this method.

Implements the new `vm.add-fs route`.

Signed-off-by: Dean Sheather <dean@coder.com>
This commit is contained in:
Dean Sheather
2020-04-14 19:21:24 +10:00
committed by Sebastien Boeuf
parent bb2139a408
commit c2abadc293
5 changed files with 213 additions and 94 deletions

View File

@@ -5,11 +5,11 @@
use crate::api::http::EndpointHandler;
use crate::api::{
vm_add_device, vm_add_disk, vm_add_net, vm_add_pmem, vm_boot, vm_create, vm_delete, vm_info,
vm_pause, vm_reboot, vm_remove_device, vm_resize, vm_restore, vm_resume, vm_shutdown,
vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_boot, vm_create, vm_delete,
vm_info, vm_pause, vm_reboot, vm_remove_device, vm_resize, vm_restore, vm_resume, vm_shutdown,
vm_snapshot, vmm_ping, vmm_shutdown, ApiError, ApiRequest, ApiResult, DeviceConfig, DiskConfig,
NetConfig, PmemConfig, RestoreConfig, VmAction, VmConfig, VmRemoveDeviceData, VmResizeData,
VmSnapshotConfig,
FsConfig, NetConfig, PmemConfig, RestoreConfig, VmAction, VmConfig, VmRemoveDeviceData,
VmResizeData, VmSnapshotConfig,
};
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use serde_json::Error as SerdeError;
@@ -71,6 +71,9 @@ pub enum HttpError {
/// Could not add a disk to a VM
VmAddDisk(ApiError),
/// Could not add a fs to a VM
VmAddFs(ApiError),
/// Could not add a pmem device to a VM
VmAddPmem(ApiError),
@@ -494,12 +497,33 @@ impl EndpointHandler for VmAddFs {
fn handle_request(
&self,
req: &Request,
_api_notifier: EventFd,
_api_sender: Sender<ApiRequest>,
api_notifier: EventFd,
api_sender: Sender<ApiRequest>,
) -> Response {
match req.method() {
// Not implemented.
Method::Put => Response::new(Version::Http11, StatusCode::NotImplemented),
Method::Put => {
match &req.body {
Some(body) => {
// Deserialize into a FsConfig
let vm_add_fs_data: FsConfig = match serde_json::from_slice(body.raw())
.map_err(HttpError::SerdeJsonDeserialize)
{
Ok(config) => config,
Err(e) => return error_response(e, StatusCode::BadRequest),
};
// Call vm_add_fs()
match vm_add_fs(api_notifier, api_sender, Arc::new(vm_add_fs_data))
.map_err(HttpError::VmAddFs)
{
Ok(_) => Response::new(Version::Http11, StatusCode::NoContent),
Err(e) => error_response(e, StatusCode::InternalServerError),
}
}
None => Response::new(Version::Http11, StatusCode::BadRequest),
}
}
_ => Response::new(Version::Http11, StatusCode::BadRequest),
}
}

View File

@@ -37,7 +37,9 @@ pub use self::http::start_http_thread;
pub mod http;
pub mod http_endpoint;
use crate::config::{DeviceConfig, DiskConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig};
use crate::config::{
DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig,
};
use crate::vm::{Error as VmError, VmState};
use std::io;
use std::sync::mpsc::{channel, RecvError, SendError, Sender};
@@ -122,6 +124,9 @@ pub enum ApiError {
/// The disk could not be added to the VM.
VmAddDisk(VmError),
/// The fs could not be added to the VM.
VmAddFs(VmError),
/// The pmem device could not be added to the VM.
VmAddPmem(VmError),
@@ -230,6 +235,9 @@ pub enum ApiRequest {
/// Add a disk to the VM.
VmAddDisk(Arc<DiskConfig>, Sender<ApiResponse>),
/// Add a fs to the VM.
VmAddFs(Arc<FsConfig>, Sender<ApiResponse>),
/// Add a pmem device to the VM.
VmAddPmem(Arc<PmemConfig>, Sender<ApiResponse>),
@@ -484,6 +492,24 @@ pub fn vm_add_disk(
Ok(())
}
pub fn vm_add_fs(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<FsConfig>,
) -> ApiResult<()> {
let (response_sender, response_receiver) = channel();
// Send the VM add-fs request.
api_sender
.send(ApiRequest::VmAddFs(data, response_sender))
.map_err(ApiError::RequestSend)?;
api_evt.write(1).map_err(ApiError::EventFdWrite)?;
response_receiver.recv().map_err(ApiError::ResponseRecv)??;
Ok(())
}
pub fn vm_add_pmem(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,