@@ -4839,56 +4839,73 @@ let vm_migrate printer rpc session_id params =
48394839 (read_map_params " vgpu" params)
48404840 in
48414841 let preferred_sr =
4842- (* The preferred SR is determined to be as the SR that the destine host has a PDB attached to it,
4843- and among the choices of that the shared is preferred first(as it is recommended to have shared storage
4844- in pool to host VMs), and then the one with the maximum available space *)
4842+ (* The preferred SR is determined to be as the SR that the
4843+ destination host has a PDB attached to it, and among the choices
4844+ of that the shared is preferred first (as it is recommended to
4845+ have shared storage in pool to host VMs), and then the one with
4846+ the maximum available space *)
48454847 try
4846- let expr =
4847- Printf. sprintf
4848- {| (field " host" = " %s" ) and (field " currently_attached" = " true" )| }
4849- (Ref. string_of host)
4848+ let host_attached_pbds =
4849+ let expr =
4850+ Printf. sprintf
4851+ {| (field " host" = " %s" ) and (field " currently_attached" = " true" )| }
4852+ (Ref. string_of host)
4853+ in
4854+ remote Client.PBD. get_all_records_where ~expr
48504855 in
4851- let srs =
4852- remote Client.PBD. get_all_where ~expr
4853- |> List. map (fun pbd ->
4854- let sr = remote Client.PBD. get_SR ~self: pbd in
4855- (sr, remote Client.SR. get_record ~self: sr)
4856- )
4856+ let shared_non_iso_srs () =
4857+ let expr =
4858+ {| (not (field " content_type" = " iso" )) and (field " shared" = " true" )| }
4859+ in
4860+ remote Client.SR. get_all_where ~expr
48574861 in
4858- (* In the following loop, the current SR:sr' will be compared with previous checked ones,
4859- first if it is an ISO type, then pass this one for selection, then the only shared one from this and
4860- previous one will be valued, and if not that case (both shared or none shared), choose the one with
4861- more space available *)
4862- let sr, _ =
4863- List. fold_left
4864- (fun (sr , free_space ) ((_ , sr_rec' ) as sr' ) ->
4865- if sr_rec'.API. sR_content_type = " iso" then
4866- (sr, free_space)
4867- else
4868- let free_space' =
4869- Int64. sub sr_rec'.API. sR_physical_size
4870- sr_rec'.API. sR_physical_utilisation
4862+ let local_non_iso_srs () =
4863+ let expr =
4864+ {| (not (field " content_type" = " iso" )) and (field " shared" = " false" )| }
4865+ in
4866+ remote Client.SR. get_all_where ~expr
4867+ in
4868+ let get_free_space_of non_iso_srs =
4869+ host_attached_pbds
4870+ |> List. filter_map (fun (_ , pbd_rec ) ->
4871+ let sr = pbd_rec.API. pBD_SR in
4872+ if List. mem sr non_iso_srs then
4873+ let size = remote Client.SR. get_physical_size ~self: sr in
4874+ let used =
4875+ remote Client.SR. get_physical_utilisation ~self: sr
48714876 in
4872- match sr with
4873- | None ->
4874- (Some sr', free_space')
4875- | Some ((_ , sr_rec ) as sr ) -> (
4876- match (sr_rec.API. sR_shared, sr_rec'.API. sR_shared) with
4877- | true , false ->
4878- (Some sr, free_space)
4879- | false , true ->
4880- (Some sr', free_space')
4881- | _ ->
4882- if free_space' > free_space then
4883- (Some sr', free_space')
4884- else
4885- (Some sr, free_space)
4886- )
4887- )
4888- (None , Int64. zero) srs
4877+ Some (sr, Int64. sub size used)
4878+ else
4879+ None
4880+ )
48894881 in
4890- match sr with Some (sr_ref , _ ) -> Some sr_ref | _ -> None
4891- with _ -> None
4882+ let find_most_free_space srs =
4883+ match
4884+ List. fast_sort
4885+ (fun (_ , a ) (_ , b ) -> Int64. compare b a)
4886+ (get_free_space_of srs)
4887+ with
4888+ | (sr , _ ) :: _ ->
4889+ Some sr
4890+ | [] ->
4891+ None
4892+ in
4893+ match find_most_free_space (shared_non_iso_srs () ) with
4894+ | Some sr ->
4895+ Some sr
4896+ | None ->
4897+ find_most_free_space (local_non_iso_srs () )
4898+ with exn ->
4899+ printer
4900+ (Cli_printer. PMsg
4901+ (Printf. sprintf
4902+ " Couldn't compute preferred SR, continuing with the \
4903+ user-provided VDI mapping. The reason is: %s"
4904+ (Printexc. to_string exn )
4905+ )
4906+ ) ;
4907+
4908+ None
48924909 in
48934910 let vdi_map =
48944911 match preferred_sr with
0 commit comments