@@ -47,12 +47,25 @@ let get_master_dom0 ~__context =
4747 let vms = Db.Host. get_resident_VMs ~__context ~self: master in
4848 List. hd (List. filter (fun vm -> Db.VM. get_is_control_domain ~__context ~self: vm) vms)
4949
50+ (* Unplug and destroy any existing VBDs owned by the VDI. *)
51+ let destroy_all_vbds ~__context ~vdi =
52+ let existing_vbds = Db.VDI. get_VBDs ~__context ~self: vdi in
53+ Helpers. call_api_functions ~__context
54+ (fun rpc session_id -> List. iter
55+ (fun vbd ->
56+ try
57+ Client.VBD. unplug ~rpc ~session_id ~self: vbd;
58+ Client.VBD. destroy ~rpc ~session_id ~self: vbd
59+ with Api_errors. Server_error (code , _ ) when code = Api_errors. device_already_detached ->
60+ Client.VBD. destroy ~rpc ~session_id ~self: vbd)
61+ existing_vbds)
62+
5063(* Create and plug a VBD from the VDI, then create a redo log and point it at the block device. *)
5164let enable_database_replication ~__context ~vdi =
5265 Mutex. execute redo_log_lifecycle_mutex (fun () ->
5366 let name_label = Db.VDI. get_name_label ~__context ~self: vdi in
5467 let uuid = Db.VDI. get_uuid ~__context ~self: vdi in
55- debug " Attempting to disable metadata replication on VDI [%s:%s]." name_label uuid;
68+ debug " Attempting to enable metadata replication on VDI [%s:%s]." name_label uuid;
5669 if Hashtbl. mem metadata_replication vdi then
5770 debug " Metadata is already being replicated to VDI [%s:%s]." name_label uuid
5871 else begin
@@ -62,6 +75,8 @@ let enable_database_replication ~__context ~vdi =
6275 raise (Api_errors. Server_error (Api_errors. no_more_redo_logs_allowed, [] ));
6376 let log = Redo_log. create () in
6477 let dom0 = get_master_dom0 ~__context in
78+ (* We've established that metadata is not being replicated to this VDI, so it should be safe to do this. *)
79+ destroy_all_vbds ~__context ~vdi ;
6580 (* Create and plug vbd *)
6681 let vbd = Helpers. call_api_functions ~__context (fun rpc session_id ->
6782 let vbd = Client.VBD. create ~rpc ~session_id ~v M:dom0 ~empty: false ~v DI:vdi
@@ -77,6 +92,7 @@ let enable_database_replication ~__context ~vdi =
7792 try
7893 Redo_log. enable_block log (" /dev/" ^ device);
7994 Redo_log. startup log;
95+ Redo_log. flush_db_to_redo_log (Db_ref. get_database (Db_backend. make () ));
8096 Hashtbl. add metadata_replication vdi (vbd, log);
8197 let vbd_uuid = Db.VBD. get_uuid ~__context ~self: vbd in
8298 debug " Redo log started on VBD %s" vbd_uuid
0 commit comments