|
| 1 | +(* |
| 2 | + * Copyright (c) Cloud Software Group |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + *) |
| 14 | + |
| 15 | +module D = Debug.Make (struct let name = "storage_smapiv1_migrate" end) |
| 16 | + |
| 17 | +module Unixext = Xapi_stdext_unix.Unixext |
| 18 | +open Storage_interface |
| 19 | +open Storage_migrate_helper |
| 20 | +module State = Storage_migrate_helper.State |
| 21 | +module SXM = Storage_migrate_helper.SXM |
| 22 | + |
| 23 | +module type SMAPIv2_MIRROR = Storage_interface.MIRROR |
| 24 | + |
| 25 | +module MIRROR : SMAPIv2_MIRROR = struct |
| 26 | + type context = unit |
| 27 | + |
| 28 | + let u x = raise Storage_interface.(Storage_error (Errors.Unimplemented x)) |
| 29 | + |
| 30 | + let send_start _ctx = u __FUNCTION__ |
| 31 | + |
| 32 | + let receive_start_common ~dbg ~sr ~vdi_info ~id ~similar ~vm = |
| 33 | + let on_fail : (unit -> unit) list ref = ref [] in |
| 34 | + let vdis = Local.SR.scan dbg sr in |
| 35 | + (* We drop cbt_metadata VDIs that do not have any actual data *) |
| 36 | + let vdis = List.filter (fun vdi -> vdi.ty <> "cbt_metadata") vdis in |
| 37 | + let leaf_dp = Local.DP.create dbg Uuidx.(to_string (make ())) in |
| 38 | + try |
| 39 | + let vdi_info = {vdi_info with sm_config= [("base_mirror", id)]} in |
| 40 | + let leaf = Local.VDI.create dbg sr vdi_info in |
| 41 | + D.info "Created leaf VDI for mirror receive: %s" (string_of_vdi_info leaf) ; |
| 42 | + on_fail := (fun () -> Local.VDI.destroy dbg sr leaf.vdi) :: !on_fail ; |
| 43 | + (* dummy VDI is created so that the leaf VDI becomes a differencing disk, |
| 44 | + useful for calling VDI.compose later on *) |
| 45 | + let dummy = Local.VDI.snapshot dbg sr leaf in |
| 46 | + on_fail := (fun () -> Local.VDI.destroy dbg sr dummy.vdi) :: !on_fail ; |
| 47 | + D.debug "%s Created dummy snapshot for mirror receive: %s" __FUNCTION__ |
| 48 | + (string_of_vdi_info dummy) ; |
| 49 | + let _ : backend = Local.VDI.attach3 dbg leaf_dp sr leaf.vdi vm true in |
| 50 | + Local.VDI.activate3 dbg leaf_dp sr leaf.vdi vm ; |
| 51 | + let nearest = |
| 52 | + List.fold_left |
| 53 | + (fun acc content_id -> |
| 54 | + match acc with |
| 55 | + | Some _ -> |
| 56 | + acc |
| 57 | + | None -> ( |
| 58 | + try |
| 59 | + Some |
| 60 | + (List.find |
| 61 | + (fun vdi -> |
| 62 | + vdi.content_id = content_id |
| 63 | + && vdi.virtual_size <= vdi_info.virtual_size |
| 64 | + ) |
| 65 | + vdis |
| 66 | + ) |
| 67 | + with Not_found -> None |
| 68 | + ) |
| 69 | + ) |
| 70 | + None similar |
| 71 | + in |
| 72 | + D.debug "Nearest VDI: content_id=%s vdi=%s" |
| 73 | + (Option.fold ~none:"None" ~some:(fun x -> x.content_id) nearest) |
| 74 | + (Option.fold ~none:"None" |
| 75 | + ~some:(fun x -> Storage_interface.Vdi.string_of x.vdi) |
| 76 | + nearest |
| 77 | + ) ; |
| 78 | + let parent = |
| 79 | + match nearest with |
| 80 | + | Some vdi -> |
| 81 | + D.debug "Cloning VDI" ; |
| 82 | + let vdi = add_to_sm_config vdi "base_mirror" id in |
| 83 | + let vdi_clone = Local.VDI.clone dbg sr vdi in |
| 84 | + D.debug "Clone: %s" (Storage_interface.Vdi.string_of vdi_clone.vdi) ; |
| 85 | + ( if vdi_clone.virtual_size <> vdi_info.virtual_size then |
| 86 | + let new_size = |
| 87 | + Local.VDI.resize dbg sr vdi_clone.vdi vdi_info.virtual_size |
| 88 | + in |
| 89 | + D.debug "Resize local clone VDI to %Ld: result %Ld" |
| 90 | + vdi_info.virtual_size new_size |
| 91 | + ) ; |
| 92 | + vdi_clone |
| 93 | + | None -> |
| 94 | + D.debug "Creating a blank remote VDI" ; |
| 95 | + Local.VDI.create dbg sr vdi_info |
| 96 | + in |
| 97 | + D.debug "Parent disk content_id=%s" parent.content_id ; |
| 98 | + State.add id |
| 99 | + State.( |
| 100 | + Recv_op |
| 101 | + Receive_state. |
| 102 | + { |
| 103 | + sr |
| 104 | + ; dummy_vdi= dummy.vdi |
| 105 | + ; leaf_vdi= leaf.vdi |
| 106 | + ; leaf_dp |
| 107 | + ; parent_vdi= parent.vdi |
| 108 | + ; remote_vdi= vdi_info.vdi |
| 109 | + ; mirror_vm= vm |
| 110 | + } |
| 111 | + ) ; |
| 112 | + let nearest_content_id = Option.map (fun x -> x.content_id) nearest in |
| 113 | + Mirror.Vhd_mirror |
| 114 | + { |
| 115 | + Mirror.mirror_vdi= leaf |
| 116 | + ; mirror_datapath= leaf_dp |
| 117 | + ; copy_diffs_from= nearest_content_id |
| 118 | + ; copy_diffs_to= parent.vdi |
| 119 | + ; dummy_vdi= dummy.vdi |
| 120 | + } |
| 121 | + with e -> |
| 122 | + List.iter |
| 123 | + (fun op -> |
| 124 | + try op () |
| 125 | + with e -> |
| 126 | + D.debug "Caught exception in on_fail: %s" (Printexc.to_string e) |
| 127 | + ) |
| 128 | + !on_fail ; |
| 129 | + raise e |
| 130 | + |
| 131 | + let receive_start _ctx ~dbg ~sr ~vdi_info ~id ~similar = |
| 132 | + receive_start_common ~dbg ~sr ~vdi_info ~id ~similar ~vm:(Vm.of_string "0") |
| 133 | + |
| 134 | + let receive_start2 _ctx ~dbg ~sr ~vdi_info ~id ~similar ~vm = |
| 135 | + receive_start_common ~dbg ~sr ~vdi_info ~id ~similar ~vm |
| 136 | + |
| 137 | + let receive_finalize _ctx ~dbg ~id = |
| 138 | + let recv_state = State.find_active_receive_mirror id in |
| 139 | + let open State.Receive_state in |
| 140 | + Option.iter (fun r -> Local.DP.destroy dbg r.leaf_dp false) recv_state ; |
| 141 | + State.remove_receive_mirror id |
| 142 | + |
| 143 | + let receive_finalize2 _ctx ~dbg ~id = |
| 144 | + let recv_state = State.find_active_receive_mirror id in |
| 145 | + let open State.Receive_state in |
| 146 | + Option.iter |
| 147 | + (fun r -> |
| 148 | + SXM.info |
| 149 | + "%s Mirror done. Compose on the dest sr %s parent %s and leaf %s" |
| 150 | + __FUNCTION__ (Sr.string_of r.sr) |
| 151 | + (Vdi.string_of r.parent_vdi) |
| 152 | + (Vdi.string_of r.leaf_vdi) ; |
| 153 | + Local.DP.destroy2 dbg r.leaf_dp r.sr r.leaf_vdi r.mirror_vm false ; |
| 154 | + Local.VDI.compose dbg r.sr r.parent_vdi r.leaf_vdi ; |
| 155 | + (* On SMAPIv3, compose would have removed the now invalid dummy vdi, so |
| 156 | + there is no need to destroy it anymore, while this is necessary on SMAPIv1 SRs. *) |
| 157 | + D.log_and_ignore_exn (fun () -> Local.VDI.destroy dbg r.sr r.dummy_vdi) ; |
| 158 | + Local.VDI.remove_from_sm_config dbg r.sr r.leaf_vdi "base_mirror" |
| 159 | + ) |
| 160 | + recv_state ; |
| 161 | + State.remove_receive_mirror id |
| 162 | + |
| 163 | + let receive_cancel _ctx ~dbg ~id = |
| 164 | + let receive_state = State.find_active_receive_mirror id in |
| 165 | + let open State.Receive_state in |
| 166 | + Option.iter |
| 167 | + (fun r -> |
| 168 | + D.log_and_ignore_exn (fun () -> Local.DP.destroy dbg r.leaf_dp false) ; |
| 169 | + List.iter |
| 170 | + (fun v -> |
| 171 | + D.log_and_ignore_exn (fun () -> Local.VDI.destroy dbg r.sr v) |
| 172 | + ) |
| 173 | + [r.dummy_vdi; r.leaf_vdi; r.parent_vdi] |
| 174 | + ) |
| 175 | + receive_state ; |
| 176 | + State.remove_receive_mirror id |
| 177 | +end |
0 commit comments