From bb2139a40845629d93424d8e248d3187c50903ca Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Tue, 14 Apr 2020 18:13:48 +1000 Subject: [PATCH] vmm/api: Add vm.add-fs route Currently unimplemented. Once implemented, this API will allow for creating virtio-fs devices in the VM after it has booted. Signed-off-by: Dean Sheather --- docs/api.md | 3 ++- src/bin/ch-remote.rs | 29 +++++++++++++++++++++++ src/main.rs | 7 +----- vmm/src/api/http.rs | 5 ++-- vmm/src/api/http_endpoint.rs | 18 ++++++++++++++ vmm/src/api/openapi/cloud-hypervisor.yaml | 16 +++++++++++++ vmm/src/config.rs | 5 ++++ 7 files changed, 74 insertions(+), 9 deletions(-) diff --git a/docs/api.md b/docs/api.md index 28adee7d7..77a1d9959 100644 --- a/docs/api.md +++ b/docs/api.md @@ -93,8 +93,9 @@ Dump the VM information | `/vm.info` | N/A Add VFIO PCI device to the VM | `/vm.add-device` | `/schemas/VmAddDevice` | N/A | The VM is booted Remove VFIO PCI device from the VM | `/vm.remove-device` | `/schemas/VmRemoveDevice` | N/A | The VM is booted Add disk device to the VM | `/vm.add-disk` | `/schemas/DiskConfig` | N/A | The VM is booted +Add fs device to the VM | `/vm.add-fs` | `/schemas/FsConfig` | N/A | The VM is booted Add pmem device to the VM | `/vm.add-pmem` | `/schemas/PmemConfig` | N/A | The VM is booted -Add network device to the VM | `/vm.add-net` | `/schemas/NetConfig` | N/A | The VM is booted +Add network device to the VM | `/vm.add-net` | `/schemas/NetConfig` | N/A | The VM is booted ### REST API Examples diff --git a/src/bin/ch-remote.rs b/src/bin/ch-remote.rs index 3a7204ff1..98b3a2ad8 100644 --- a/src/bin/ch-remote.rs +++ b/src/bin/ch-remote.rs @@ -24,6 +24,7 @@ enum Error { InvalidMemorySize(std::num::ParseIntError), AddDeviceConfig(vmm::config::Error), AddDiskConfig(vmm::config::Error), + AddFsConfig(vmm::config::Error), AddPmemConfig(vmm::config::Error), AddNetConfig(vmm::config::Error), Restore(vmm::config::Error), @@ -228,6 +229,17 @@ fn add_disk_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Err ) } +fn add_fs_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { + let fs_config = vmm::config::FsConfig::parse(config).map_err(Error::AddFsConfig)?; + + simple_api_command( + socket, + "PUT", + "add-fs", + Some(&serde_json::to_string(&fs_config).unwrap()), + ) +} + fn add_pmem_api_command(socket: &mut UnixStream, config: &str) -> Result<(), Error> { let pmem_config = vmm::config::PmemConfig::parse(config).map_err(Error::AddPmemConfig)?; @@ -315,6 +327,14 @@ fn do_command(matches: &ArgMatches) -> Result<(), Error> { .value_of("disk_config") .unwrap(), ), + Some("add-fs") => add_fs_api_command( + &mut socket, + matches + .subcommand_matches("add-fs") + .unwrap() + .value_of("fs_config") + .unwrap(), + ), Some("add-pmem") => add_pmem_api_command( &mut socket, matches @@ -383,6 +403,15 @@ fn main() { .help(vmm::config::DiskConfig::SYNTAX), ), ) + .subcommand( + SubCommand::with_name("add-fs") + .about("Add virtio-fs backed fs device") + .arg( + Arg::with_name("fs_config") + .index(1) + .help(vmm::config::FsConfig::SYNTAX), + ), + ) .subcommand( SubCommand::with_name("add-pmem") .about("Add persistent memory device") diff --git a/src/main.rs b/src/main.rs index b512ac59b..a3cc734fe 100755 --- a/src/main.rs +++ b/src/main.rs @@ -155,12 +155,7 @@ fn create_app<'a, 'b>( .arg( Arg::with_name("fs") .long("fs") - .help( - "virtio-fs parameters \ - \"tag=,sock=,num_queues=,\ - queue_size=,dax=on|off,cache_size=\"", - ) + .help(config::FsConfig::SYNTAX) .takes_value(true) .min_values(1) .group("vm-config"), diff --git a/vmm/src/api/http.rs b/vmm/src/api/http.rs index 7e1a68717..f361b0e67 100644 --- a/vmm/src/api/http.rs +++ b/vmm/src/api/http.rs @@ -4,8 +4,8 @@ // use crate::api::http_endpoint::{ - VmActionHandler, VmAddDevice, VmAddDisk, VmAddNet, VmAddPmem, VmCreate, VmInfo, VmRemoveDevice, - VmResize, VmRestore, VmSnapshot, VmmPing, VmmShutdown, + VmActionHandler, VmAddDevice, VmAddDisk, VmAddFs, VmAddNet, VmAddPmem, VmCreate, VmInfo, + VmRemoveDevice, VmResize, VmRestore, VmSnapshot, VmmPing, VmmShutdown, }; use crate::api::{ApiRequest, VmAction}; use crate::seccomp_filters::{get_seccomp_filter, Thread}; @@ -70,6 +70,7 @@ lazy_static! { r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {})); r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmRemoveDevice {})); r.routes.insert(endpoint!("/vm.add-disk"), Box::new(VmAddDisk {})); + r.routes.insert(endpoint!("/vm.add-fs"), Box::new(VmAddFs {})); r.routes.insert(endpoint!("/vm.add-pmem"), Box::new(VmAddPmem {})); r.routes.insert(endpoint!("/vm.add-net"), Box::new(VmAddNet {})); diff --git a/vmm/src/api/http_endpoint.rs b/vmm/src/api/http_endpoint.rs index ef95771c9..3cc9ba20b 100644 --- a/vmm/src/api/http_endpoint.rs +++ b/vmm/src/api/http_endpoint.rs @@ -487,6 +487,24 @@ impl EndpointHandler for VmAddDisk { } } +// /api/v1/vm.add-fs handler +pub struct VmAddFs {} + +impl EndpointHandler for VmAddFs { + fn handle_request( + &self, + req: &Request, + _api_notifier: EventFd, + _api_sender: Sender, + ) -> Response { + match req.method() { + // Not implemented. + Method::Put => Response::new(Version::Http11, StatusCode::NotImplemented), + _ => Response::new(Version::Http11, StatusCode::BadRequest), + } + } +} + // /api/v1/vm.add-pmem handler pub struct VmAddPmem {} diff --git a/vmm/src/api/openapi/cloud-hypervisor.yaml b/vmm/src/api/openapi/cloud-hypervisor.yaml index 4a79b1406..9bb559b20 100644 --- a/vmm/src/api/openapi/cloud-hypervisor.yaml +++ b/vmm/src/api/openapi/cloud-hypervisor.yaml @@ -187,6 +187,22 @@ paths: 500: description: The new disk could not be added to the VM instance. + /vm.add-fs: + put: + summary: Add a new virtio-fs device to the VM + requestBody: + description: The details of the new virtio-fs + content: + application/json: + schema: + $ref: '#/components/schemas/FsConfig' + required: true + responses: + 204: + description: The new device was successfully added to the VM instance. + 500: + description: The new device could not be added to the VM instance. + /vm.add-pmem: put: summary: Add a new pmem device to the VM diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 990a0f84c..99a898358 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -876,6 +876,11 @@ impl Default for FsConfig { } impl FsConfig { + pub const SYNTAX: &'static str = "virtio-fs parameters \ + \"tag=,sock=,num_queues=,\ + queue_size=,dax=on|off,cache_size=\""; + pub fn parse(fs: &str) -> Result { let mut parser = OptionParser::new(); parser