[Libguestfs] [PATCH v4 4/7] v2v: -o libvirt: use a Lazy for the connection

Richard W.M. Jones rjones at redhat.com
Wed Apr 10 13:17:25 UTC 2019


On Mon, Apr 08, 2019 at 02:49:57PM +0200, Pino Toscano wrote:
> Store the Libvirt.Connect.t object as instance variable, so it can be
> used also outside of prepare_targets.  Use a private method to access
> it, so there is no need to directly use the Lazy object.
> ---
>  v2v/output_libvirt.ml | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
> 
> diff --git a/v2v/output_libvirt.ml b/v2v/output_libvirt.ml
> index 30857c572..e62e43bb8 100644
> --- a/v2v/output_libvirt.ml
> +++ b/v2v/output_libvirt.ml
> @@ -59,11 +59,16 @@ let target_features_of_capabilities_doc doc arch =
>      List.map Xml.node_name features
>    )
>  
> -class output_libvirt oc output_pool = object
> +class output_libvirt oc output_pool =
> +object (self)
>    inherit output
>  
>    val mutable capabilities_doc = None
>    val mutable pool_name = None
> +  val lazy_conn = lazy (Libvirt.Connect.connect ?name:oc ())
> +
> +  method private conn : Libvirt.rw Libvirt.Connect.t =
> +    Lazy.force lazy_conn

Here the name is hidden as a private object field, so I guess it's OK.

ACK

Rich.

>    method as_options =
>      match oc with
> @@ -71,13 +76,10 @@ class output_libvirt oc output_pool = object
>      | Some uri -> sprintf "-o libvirt -oc %s -os %s" uri output_pool
>  
>    method prepare_targets source overlays _ _ _ _ =
> -    (* Open the connection to libvirt. *)
> -    let conn = Libvirt.Connect.connect ?name:oc () in
> -
>      (* Get the capabilities from libvirt. *)
>      let xml =
>        try
> -        Libvirt.Connect.get_capabilities conn
> +        Libvirt.Connect.get_capabilities self#conn
>        with
>          Libvirt.Virterror { message } ->
>            error (f_"cannot get libvirt hypervisor capabilities: %s")
> @@ -96,7 +98,7 @@ class output_libvirt oc output_pool = object
>      capabilities_doc <- Some doc;
>  
>      (* Does the domain already exist on the target?  (RHBZ#889082) *)
> -    if Libvirt_utils.domain_exists conn source.s_name then (
> +    if Libvirt_utils.domain_exists self#conn source.s_name then (
>        if source.s_hypervisor = Physical then (* virt-p2v user *)
>          error (f_"a libvirt domain called ‘%s’ already exists on the target.\n\nIf using virt-p2v, select a different ‘Name’ in the ‘Target properties’. Or delete the existing domain on the target using the ‘virsh undefine’ command.")
>                source.s_name
> @@ -109,7 +111,7 @@ class output_libvirt oc output_pool = object
>       * and dump out its XML.
>       *)
>      let xml =
> -      let pool = Libvirt_utils.get_pool conn output_pool in
> +      let pool = Libvirt_utils.get_pool self#conn output_pool in
>        Libvirt.Pool.get_xml_desc (Libvirt.Pool.const pool) in
>      let doc = Xml.parse_memory xml in
>      let xpathctx = Xml.xpath_new_context doc in
> -- 
> 2.20.1
> 
> _______________________________________________
> Libguestfs mailing list
> Libguestfs at redhat.com
> https://www.redhat.com/mailman/listinfo/libguestfs

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list