From c34eb6a77652daa93692c68af23f5707b45c5320 Mon Sep 17 00:00:00 2001 From: gangj <62988402+gangj@users.noreply.github.com> Date: Wed, 1 Nov 2023 18:51:20 +0800 Subject: [PATCH] CP-45571: Add VM.restart_device_model function (API/CLI) Implemented as a localhost migration, can be made more efficient in future. As it is simply a "pool_migrate", just reuse message forwarding of "pool_migrate" to handle "allowed operation" and "message routing". Signed-off-by: Gang Ji --- ocaml/idl/datamodel_vm.ml | 17 ++++++++++++++++- ocaml/xapi-cli-server/cli_frontend.ml | 9 +++++++++ ocaml/xapi-cli-server/cli_operations.ml | 9 +++++++++ ocaml/xapi/message_forwarding.ml | 4 ++++ ocaml/xapi/xapi_vm.ml | 10 ++++++++++ ocaml/xapi/xapi_vm.mli | 2 ++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ocaml/idl/datamodel_vm.ml b/ocaml/idl/datamodel_vm.ml index 6b553c9d737..014244ee41e 100644 --- a/ocaml/idl/datamodel_vm.ml +++ b/ocaml/idl/datamodel_vm.ml @@ -1033,7 +1033,7 @@ let pool_migrate = ; Api_errors.other_operation_in_progress ; Api_errors.vm_is_template ; Api_errors.operation_not_allowed - ; Api_errors.vm_migrate_failed + ; Api_errors.vm_bad_power_state ] ~allowed_roles:(_R_VM_POWER_ADMIN ++ _R_CLIENT_CERT) () @@ -1686,6 +1686,20 @@ let set_NVRAM_EFI_variables = ~params:[(Ref _vm, "self", "The VM"); (String, "value", "The value")] ~hide_from_docs:true ~allowed_roles:_R_LOCAL_ROOT_ONLY () +let restart_device_models = + call ~flags:[`Session] ~name:"restart_device_models" ~lifecycle:[] + ~params:[(Ref _vm, "self", "The VM")] + ~errs: + [ + Api_errors.vm_bad_power_state + ; Api_errors.other_operation_in_progress + ; Api_errors.vm_is_template + ; Api_errors.operation_not_allowed + ; Api_errors.vm_bad_power_state + ] + ~allowed_roles:(_R_VM_POWER_ADMIN ++ _R_CLIENT_CERT) + () + (** VM (or 'guest') configuration: *) let t = create_obj ~in_db:true ~in_product_since:rel_rio ~in_oss_since:oss_since_303 @@ -1819,6 +1833,7 @@ let t = ; set_domain_type ; set_HVM_boot_policy ; set_NVRAM_EFI_variables + ; restart_device_models ] ~contents: ([uid _vm] diff --git a/ocaml/xapi-cli-server/cli_frontend.ml b/ocaml/xapi-cli-server/cli_frontend.ml index 4fcb9726689..1803a8af640 100644 --- a/ocaml/xapi-cli-server/cli_frontend.ml +++ b/ocaml/xapi-cli-server/cli_frontend.ml @@ -1570,6 +1570,15 @@ let rec cmdtable_data : (string * cmd_spec) list = ; flags= [Standard; Vm_selectors] } ) + ; ( "vm-restart-device-models" + , { + reqd= [] + ; optn= [] + ; help= "Restart device models of a VM." + ; implementation= No_fd Cli_operations.vm_restart_device_models + ; flags= [Standard; Vm_selectors] + } + ) ; ( "vm-pause" , { reqd= [] diff --git a/ocaml/xapi-cli-server/cli_operations.ml b/ocaml/xapi-cli-server/cli_operations.ml index f4e478cbdc7..51eb018e4ad 100644 --- a/ocaml/xapi-cli-server/cli_operations.ml +++ b/ocaml/xapi-cli-server/cli_operations.ml @@ -3815,6 +3815,15 @@ let vm_resume printer rpc session_id params = params ["on"; "progress"] ) +let vm_restart_device_models printer rpc session_id params = + ignore + (do_vm_op printer rpc session_id + (fun vm -> + Client.VM.restart_device_models ~rpc ~session_id ~self:(vm.getref ()) + ) + params [] + ) + let vm_pause printer rpc session_id params = ignore (do_vm_op printer rpc session_id diff --git a/ocaml/xapi/message_forwarding.ml b/ocaml/xapi/message_forwarding.ml index f1a2a65bd6c..4d08bb5933a 100644 --- a/ocaml/xapi/message_forwarding.ml +++ b/ocaml/xapi/message_forwarding.ml @@ -3023,6 +3023,10 @@ functor (* called by varstored, bypasses VM powerstate check *) info "VM.set_NVRAM_EFI_variables: self = '%s'" (vm_uuid ~__context self) ; Local.VM.set_NVRAM_EFI_variables ~__context ~self ~value + + let restart_device_models ~__context ~self = + info "VM.restart_device_models: self = '%s'" (vm_uuid ~__context self) ; + Local.VM.restart_device_models ~__context ~self end module VM_metrics = struct end diff --git a/ocaml/xapi/xapi_vm.ml b/ocaml/xapi/xapi_vm.ml index 879ca1ae3e2..4db86acbdef 100644 --- a/ocaml/xapi/xapi_vm.ml +++ b/ocaml/xapi/xapi_vm.ml @@ -1611,3 +1611,13 @@ let set_NVRAM_EFI_variables ~__context ~self ~value = let value = (key, value) :: List.remove_assoc key nvram in Db.VM.set_NVRAM ~__context ~self ~value ) + +let restart_device_models ~__context ~self = + let host = Db.VM.get_resident_on ~__context ~self in + (* As it is implemented as a localhost migration, just reuse message + * forwarding of "pool_migrate" to handle "allowed operation" and "message + * routing" *) + Helpers.call_api_functions ~__context (fun rpc session_id -> + Client.VM.pool_migrate ~rpc ~session_id ~vm:self ~host + ~options:[("live", "true")] + ) diff --git a/ocaml/xapi/xapi_vm.mli b/ocaml/xapi/xapi_vm.mli index 1c79730539e..c349ff8dcb0 100644 --- a/ocaml/xapi/xapi_vm.mli +++ b/ocaml/xapi/xapi_vm.mli @@ -420,3 +420,5 @@ val set_HVM_boot_policy : val set_NVRAM_EFI_variables : __context:Context.t -> self:API.ref_VM -> value:string -> unit + +val restart_device_models : __context:Context.t -> self:API.ref_VM -> unit