@@ -3888,22 +3888,22 @@ module VBD = struct
38883888 )
38893889 (fun () -> cleanup_attached_vdis vm vbd.id)
38903890
3891- let unplug task vm vbd force =
3891+ let deactivate task vm vbd force =
38923892 with_xc_and_xs (fun xc xs ->
38933893 try
38943894 (* On destroying the datapath
38953895
3896- 1. if the device has already been shutdown and deactivated (as in
3897- suspend) we must call DP.destroy here to avoid leaks
3896+ 1. if the device has already been shutdown and deactivated (as in
3897+ suspend) we must call DP.destroy here to avoid leaks
38983898
3899- 2. if the device is successfully shutdown here then we must call
3900- DP.destroy because no-one else will
3899+ 2. if the device is successfully shutdown here then we must call
3900+ DP.destroy because no-one else will
39013901
3902- 3. if the device shutdown is rejected then we should leave the DP
3903- alone and rely on the event thread calling us again later. *)
3902+ 3. if the device shutdown is rejected then we should leave the DP
3903+ alone and rely on the event thread calling us again later. *)
39043904 let domid = domid_of_uuid ~xs (uuid_of_string vm) in
39053905 (* If the device is gone then we don't need to shut it down but we do
3906- need to free any storage resources. *)
3906+ need to free any storage resources. *)
39073907 let dev =
39083908 try
39093909 Some (device_by_id xc xs vm (device_kind_of ~xs vbd) (id_of vbd))
@@ -3941,7 +3941,7 @@ module VBD = struct
39413941 vm (id_of vbd) ;
39423942 (* this happens on normal shutdown too *)
39433943 (* Case (1): success; Case (2): success; Case (3): an exception is
3944- thrown *)
3944+ thrown *)
39453945 with_tracing ~task ~name: " VBD_device_shutdown" @@ fun () ->
39463946 Xenops_task. with_subtask task
39473947 (Printf. sprintf " Vbd.clean_shutdown %s" (id_of vbd))
@@ -3952,7 +3952,7 @@ module VBD = struct
39523952 )
39533953 dev ;
39543954 (* We now have a shutdown device but an active DP: we should destroy
3955- the DP if the backend is of type VDI *)
3955+ the DP if the backend is of type VDI *)
39563956 finally
39573957 (fun () ->
39583958 with_tracing ~task ~name: " VBD_device_release" (fun () ->
@@ -3996,11 +3996,14 @@ module VBD = struct
39963996 ()
39973997 )
39983998 (fun () ->
3999- with_tracing ~task ~name: " VBD_dp_destroy" @@ fun () ->
3999+ with_tracing ~task ~name: " VBD_deactivate" @@ fun () ->
4000+ let vmid = Storage. vm_of_domid domid in
40004001 match (domid, backend) with
4001- | Some x , None | Some x , Some (VDI _ ) ->
4002- Storage. dp_destroy task
4003- (Storage. id_of (string_of_int x) vbd.Vbd. id)
4002+ | Some x , Some (VDI path ) ->
4003+ let sr, vdi = Storage. get_disk_by_name task path in
4004+ let dp = Storage. id_of (string_of_int x) vbd.id in
4005+ Storage. deactivate task dp sr vdi vmid
4006+ (* We don't need to detach Local or CDROM *)
40044007 | _ ->
40054008 ()
40064009 )
@@ -4009,6 +4012,46 @@ module VBD = struct
40094012 raise (Xenopsd_error (Device_detach_rejected (" VBD" , id_of vbd, s)))
40104013 )
40114014
4015+ let detach task vm vbd =
4016+ with_xc_and_xs (fun xc xs ->
4017+ let domid = domid_of_uuid ~xs (uuid_of_string vm) in
4018+ let dev =
4019+ try
4020+ Some (device_by_id xc xs vm (device_kind_of ~xs vbd) (id_of vbd))
4021+ with
4022+ | Xenopsd_error (Does_not_exist (_ , _ )) ->
4023+ debug " VM = %s; VBD = %s; Ignoring missing domain" vm (id_of vbd) ;
4024+ None
4025+ | Xenopsd_error Device_not_connected ->
4026+ debug " VM = %s; VBD = %s; Ignoring missing device" vm (id_of vbd) ;
4027+ None
4028+ in
4029+ let backend =
4030+ match dev with
4031+ | None ->
4032+ None
4033+ | Some dv -> (
4034+ match
4035+ Rpcmarshal. unmarshal typ_of_backend
4036+ (Device.Generic. get_private_key ~xs dv _vdi_id
4037+ |> Jsonrpc. of_string
4038+ )
4039+ with
4040+ | Ok x ->
4041+ x
4042+ | Error (`Msg m ) ->
4043+ internal_error " Failed to unmarshal VBD backend: %s" m
4044+ )
4045+ in
4046+ with_tracing ~task ~name: " VBD_dp_destroy" @@ fun () ->
4047+ match (domid, backend) with
4048+ | Some x , None | Some x , Some (VDI _ ) ->
4049+ Storage. dp_destroy task (Storage. id_of (string_of_int x) vbd.Vbd. id)
4050+ | _ ->
4051+ ()
4052+ ) ;
4053+ cleanup_attached_vdis vm vbd.id
4054+
40124055 let insert task vm vbd d =
40134056 on_frontend
40144057 (fun xc xs frontend_domid domain_type ->
0 commit comments