Pino Toscano
2015-Nov-10 10:43 UTC
[Libguestfs] [PATCH] OCaml tools: use open_guestfs everywhere
Instead of creating Guestfs handles and manually apply common options (e.g. debug and trace), use the open_guestfs in Common_utils. This also applies the common options to handles which didn't set them before, so we can inspect also their messages if needed. --- builder/builder.ml | 8 ++------ customize/customize_main.ml | 4 +--- dib/dib.ml | 4 +--- get-kernel/get_kernel.ml | 4 +--- resize/resize.ml | 10 +++------- sparsify/cmdline.ml | 2 +- sparsify/copying.ml | 16 +++++----------- sparsify/in_place.ml | 4 +--- sysprep/main.ml | 4 +--- v2v/v2v.ml | 6 +++--- 10 files changed, 19 insertions(+), 43 deletions(-) diff --git a/builder/builder.ml b/builder/builder.ml index feb6e0d..2c51550 100644 --- a/builder/builder.ml +++ b/builder/builder.ml @@ -570,9 +570,7 @@ let main () (human_size osize); let preallocation = if oformat = "qcow2" then Some "metadata" else None in let () - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in g#disk_create ?preallocation ofile oformat osize in let cmd sprintf "virt-resize%s%s%s --output-format %s%s%s --unknown-filesystems error %s %s" @@ -626,9 +624,7 @@ let main () (* Now mount the output disk so we can make changes. *) message (f_"Opening the new disk"); let g - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in (match memsize with None -> () | Some memsize -> g#set_memsize memsize); (match smp with None -> () | Some smp -> g#set_smp smp); diff --git a/customize/customize_main.ml b/customize/customize_main.ml index 49d510c..e161e82 100644 --- a/customize/customize_main.ml +++ b/customize/customize_main.ml @@ -165,9 +165,7 @@ read the man page virt-customize(1). (* Connect to libguestfs. *) let g - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in (match memsize with None -> () | Some memsize -> g#set_memsize memsize); (match smp with None -> () | Some smp -> g#set_smp smp); diff --git a/dib/dib.ml b/dib/dib.ml index caf13f2..1ae8876 100644 --- a/dib/dib.ml +++ b/dib/dib.ml @@ -626,9 +626,7 @@ let main () let is_ramdisk_build = is_ramdisk || StringSet.mem "ironic-agent" all_elements in let g, tmpdisk, tmpdiskfmt, drive_partition - let g = new G.guestfs () in - if verbose () then g#set_verbose true; - if trace () then g#set_trace true; + let g = open_guestfs () in (match memsize with None -> () | Some memsize -> g#set_memsize memsize); (match smp with None -> () | Some smp -> g#set_smp smp); diff --git a/get-kernel/get_kernel.ml b/get-kernel/get_kernel.ml index 2bea559..59bd9d6 100644 --- a/get-kernel/get_kernel.ml +++ b/get-kernel/get_kernel.ml @@ -182,9 +182,7 @@ let main () let add, output, unversioned, prefix = parse_cmdline () in (* Connect to libguestfs. *) - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in add g; g#launch (); diff --git a/resize/resize.ml b/resize/resize.ml index af7b37f..b2802c7 100644 --- a/resize/resize.ml +++ b/resize/resize.ml @@ -284,7 +284,7 @@ read the man page virt-resize(1). printf "alignment\n"; printf "align-first\n"; printf "infile-uri\n"; - let g = new G.guestfs () in + let g = open_guestfs () in g#add_drive "/dev/null"; g#launch (); if g#feature_available [| "ntfsprogs"; "ntfs3g" |] then @@ -329,9 +329,7 @@ read the man page virt-resize(1). (* Add in and out disks to the handle and launch. *) let connect_both_disks () - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in let _, { URI.path = path; protocol = protocol; server = server; username = username; password = password } = infile in @@ -1295,9 +1293,7 @@ read the man page virt-resize(1). g#shutdown (); g#close (); - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in (* The output disk is being created, so use cache=unsafe here. *) g#add_drive ?format:output_format ~readonly:false ~cachemode:"unsafe" outfile; diff --git a/sparsify/cmdline.ml b/sparsify/cmdline.ml index 17b5542..868456f 100644 --- a/sparsify/cmdline.ml +++ b/sparsify/cmdline.ml @@ -107,7 +107,7 @@ read the man page virt-sparsify(1). printf "check-tmpdir\n"; printf "in-place\n"; printf "tmp-option\n"; - let g = new Guestfs.guestfs () in + let g = open_guestfs () in g#add_drive "/dev/null"; g#launch (); if g#feature_available [| "ntfsprogs"; "ntfs3g" |] then diff --git a/sparsify/copying.ml b/sparsify/copying.ml index ef196f6..bef0664 100644 --- a/sparsify/copying.ml +++ b/sparsify/copying.ml @@ -60,7 +60,7 @@ let run indisk outdisk check_tmpdir compress convert | Some fmt -> fmt (* user specified input format, use that *) | None -> (* Don't know, so we must autodetect. *) - match (new G.guestfs ())#disk_format indisk with + match (open_guestfs ())#disk_format indisk with | "unknown" -> error (f_"cannot detect input disk format; use the --format parameter") | fmt -> fmt in @@ -79,9 +79,7 @@ let run indisk outdisk check_tmpdir compress convert let file = String.sub file 9 (String.length file - 9) in if not (Sys.file_exists file) then error (f_"--tmp prebuilt:file: %s: file does not exist") file; - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in if g#disk_format file <> "qcow2" then error (f_"--tmp prebuilt:file: %s: file format is not qcow2") file; if not (g#disk_has_backing_file file) then @@ -97,7 +95,7 @@ let run indisk outdisk check_tmpdir compress convert | Prebuilt_file _ -> () | Directory tmpdir -> (* Get virtual size of the input disk. *) - let virtual_size = (new G.guestfs ())#disk_virtual_size indisk in + let virtual_size = (open_guestfs ())#disk_virtual_size indisk in if verbose () then printf "input disk virtual size is %Ld bytes (%s)\n%!" virtual_size (human_size virtual_size); @@ -153,9 +151,7 @@ You can ignore this warning or change it to a hard failure using the (* Create 'tmp' with the indisk as the backing file. *) let create tmp - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in g#disk_create ~backingfile:indisk ?backingformat:format ~compat:"1.1" tmp "qcow2" Int64.minus_one @@ -180,9 +176,7 @@ You can ignore this warning or change it to a hard failure using the (* Connect to libguestfs. *) let g - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in (* Note that the temporary overlay disk is always qcow2 format. *) g#add_drive ~format:"qcow2" ~readonly:false ~cachemode:"unsafe" overlaydisk; diff --git a/sparsify/in_place.ml b/sparsify/in_place.ml index 3a2b32c..6a42a11 100644 --- a/sparsify/in_place.ml +++ b/sparsify/in_place.ml @@ -31,9 +31,7 @@ module G = Guestfs let rec run disk format ignores machine_readable zeroes (* Connect to libguestfs. *) - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in (* Capture ^C and clean up gracefully. *) let quit = ref false in diff --git a/sysprep/main.ml b/sysprep/main.ml index 411c595..12d4b02 100644 --- a/sysprep/main.ml +++ b/sysprep/main.ml @@ -211,9 +211,7 @@ read the man page virt-sysprep(1). message (f_"Examining the guest ..."); (* Connect to libguestfs. *) - let g = new G.guestfs () in - if trace () then g#set_trace true; - if verbose () then g#set_verbose true; + let g = open_guestfs () in add g dryrun; g#launch (); diff --git a/v2v/v2v.ml b/v2v/v2v.ml index dabec7f..3610446 100644 --- a/v2v/v2v.ml +++ b/v2v/v2v.ml @@ -236,12 +236,12 @@ and create_overlays src_disks error (f_"qemu-img command failed, see earlier errors"); (* Sanity check created overlay (see below). *) - if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then + if not ((open_guestfs ())#disk_has_backing_file overlay_file) then error (f_"internal error: qemu-img did not create overlay with backing file"); let sd = "sd" ^ drive_name i in - let vsize = (new G.guestfs ())#disk_virtual_size overlay_file in + let vsize = (open_guestfs ())#disk_virtual_size overlay_file in { ov_overlay_file = overlay_file; ov_sd = sd; ov_virtual_size = vsize; ov_source = source } @@ -780,7 +780,7 @@ and copy_targets targets input output output_alloc * backing file. Just sanity check this here. *) let overlay_file = t.target_overlay.ov_overlay_file in - if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then + if not ((open_guestfs ())#disk_has_backing_file overlay_file) then error (f_"internal error: qemu corrupted the overlay file"); (* Give the input module a chance to adjust the parameters -- 2.1.0
Richard W.M. Jones
2015-Nov-10 11:56 UTC
Re: [Libguestfs] [PATCH] OCaml tools: use open_guestfs everywhere
On Tue, Nov 10, 2015 at 11:43:22AM +0100, Pino Toscano wrote:> Instead of creating Guestfs handles and manually apply common options > (e.g. debug and trace), use the open_guestfs in Common_utils. > This also applies the common options to handles which didn't set them > before, so we can inspect also their messages if needed. > --- > builder/builder.ml | 8 ++------ > customize/customize_main.ml | 4 +--- > dib/dib.ml | 4 +--- > get-kernel/get_kernel.ml | 4 +--- > resize/resize.ml | 10 +++------- > sparsify/cmdline.ml | 2 +- > sparsify/copying.ml | 16 +++++----------- > sparsify/in_place.ml | 4 +--- > sysprep/main.ml | 4 +--- > v2v/v2v.ml | 6 +++--- > 10 files changed, 19 insertions(+), 43 deletions(-) > > diff --git a/builder/builder.ml b/builder/builder.ml > index feb6e0d..2c51550 100644 > --- a/builder/builder.ml > +++ b/builder/builder.ml > @@ -570,9 +570,7 @@ let main () > (human_size osize); > let preallocation = if oformat = "qcow2" then Some "metadata" else None in > let () > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > g#disk_create ?preallocation ofile oformat osize in > let cmd > sprintf "virt-resize%s%s%s --output-format %s%s%s --unknown-filesystems error %s %s" > @@ -626,9 +624,7 @@ let main () > (* Now mount the output disk so we can make changes. *) > message (f_"Opening the new disk"); > let g > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > > (match memsize with None -> () | Some memsize -> g#set_memsize memsize); > (match smp with None -> () | Some smp -> g#set_smp smp); > diff --git a/customize/customize_main.ml b/customize/customize_main.ml > index 49d510c..e161e82 100644 > --- a/customize/customize_main.ml > +++ b/customize/customize_main.ml > @@ -165,9 +165,7 @@ read the man page virt-customize(1). > > (* Connect to libguestfs. *) > let g > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > > (match memsize with None -> () | Some memsize -> g#set_memsize memsize); > (match smp with None -> () | Some smp -> g#set_smp smp); > diff --git a/dib/dib.ml b/dib/dib.ml > index caf13f2..1ae8876 100644 > --- a/dib/dib.ml > +++ b/dib/dib.ml > @@ -626,9 +626,7 @@ let main () > let is_ramdisk_build = is_ramdisk || StringSet.mem "ironic-agent" all_elements in > > let g, tmpdisk, tmpdiskfmt, drive_partition > - let g = new G.guestfs () in > - if verbose () then g#set_verbose true; > - if trace () then g#set_trace true; > + let g = open_guestfs () in > > (match memsize with None -> () | Some memsize -> g#set_memsize memsize); > (match smp with None -> () | Some smp -> g#set_smp smp); > diff --git a/get-kernel/get_kernel.ml b/get-kernel/get_kernel.ml > index 2bea559..59bd9d6 100644 > --- a/get-kernel/get_kernel.ml > +++ b/get-kernel/get_kernel.ml > @@ -182,9 +182,7 @@ let main () > let add, output, unversioned, prefix = parse_cmdline () in > > (* Connect to libguestfs. *) > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > add g; > g#launch (); > > diff --git a/resize/resize.ml b/resize/resize.ml > index af7b37f..b2802c7 100644 > --- a/resize/resize.ml > +++ b/resize/resize.ml > @@ -284,7 +284,7 @@ read the man page virt-resize(1). > printf "alignment\n"; > printf "align-first\n"; > printf "infile-uri\n"; > - let g = new G.guestfs () in > + let g = open_guestfs () in > g#add_drive "/dev/null"; > g#launch (); > if g#feature_available [| "ntfsprogs"; "ntfs3g" |] then > @@ -329,9 +329,7 @@ read the man page virt-resize(1). > > (* Add in and out disks to the handle and launch. *) > let connect_both_disks () > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > let _, { URI.path = path; protocol = protocol; > server = server; username = username; > password = password } = infile in > @@ -1295,9 +1293,7 @@ read the man page virt-resize(1). > g#shutdown (); > g#close (); > > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > (* The output disk is being created, so use cache=unsafe here. *) > g#add_drive ?format:output_format ~readonly:false ~cachemode:"unsafe" > outfile; > diff --git a/sparsify/cmdline.ml b/sparsify/cmdline.ml > index 17b5542..868456f 100644 > --- a/sparsify/cmdline.ml > +++ b/sparsify/cmdline.ml > @@ -107,7 +107,7 @@ read the man page virt-sparsify(1). > printf "check-tmpdir\n"; > printf "in-place\n"; > printf "tmp-option\n"; > - let g = new Guestfs.guestfs () in > + let g = open_guestfs () in > g#add_drive "/dev/null"; > g#launch (); > if g#feature_available [| "ntfsprogs"; "ntfs3g" |] then > diff --git a/sparsify/copying.ml b/sparsify/copying.ml > index ef196f6..bef0664 100644 > --- a/sparsify/copying.ml > +++ b/sparsify/copying.ml > @@ -60,7 +60,7 @@ let run indisk outdisk check_tmpdir compress convert > | Some fmt -> fmt (* user specified input format, use that *) > | None -> > (* Don't know, so we must autodetect. *) > - match (new G.guestfs ())#disk_format indisk with > + match (open_guestfs ())#disk_format indisk with > | "unknown" -> > error (f_"cannot detect input disk format; use the --format parameter") > | fmt -> fmt in > @@ -79,9 +79,7 @@ let run indisk outdisk check_tmpdir compress convert > let file = String.sub file 9 (String.length file - 9) in > if not (Sys.file_exists file) then > error (f_"--tmp prebuilt:file: %s: file does not exist") file; > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > if g#disk_format file <> "qcow2" then > error (f_"--tmp prebuilt:file: %s: file format is not qcow2") file; > if not (g#disk_has_backing_file file) then > @@ -97,7 +95,7 @@ let run indisk outdisk check_tmpdir compress convert > | Prebuilt_file _ -> () > | Directory tmpdir -> > (* Get virtual size of the input disk. *) > - let virtual_size = (new G.guestfs ())#disk_virtual_size indisk in > + let virtual_size = (open_guestfs ())#disk_virtual_size indisk in > if verbose () then > printf "input disk virtual size is %Ld bytes (%s)\n%!" > virtual_size (human_size virtual_size); > @@ -153,9 +151,7 @@ You can ignore this warning or change it to a hard failure using the > > (* Create 'tmp' with the indisk as the backing file. *) > let create tmp > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > g#disk_create > ~backingfile:indisk ?backingformat:format ~compat:"1.1" > tmp "qcow2" Int64.minus_one > @@ -180,9 +176,7 @@ You can ignore this warning or change it to a hard failure using the > > (* Connect to libguestfs. *) > let g > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > > (* Note that the temporary overlay disk is always qcow2 format. *) > g#add_drive ~format:"qcow2" ~readonly:false ~cachemode:"unsafe" overlaydisk; > diff --git a/sparsify/in_place.ml b/sparsify/in_place.ml > index 3a2b32c..6a42a11 100644 > --- a/sparsify/in_place.ml > +++ b/sparsify/in_place.ml > @@ -31,9 +31,7 @@ module G = Guestfs > > let rec run disk format ignores machine_readable zeroes > (* Connect to libguestfs. *) > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > > (* Capture ^C and clean up gracefully. *) > let quit = ref false in > diff --git a/sysprep/main.ml b/sysprep/main.ml > index 411c595..12d4b02 100644 > --- a/sysprep/main.ml > +++ b/sysprep/main.ml > @@ -211,9 +211,7 @@ read the man page virt-sysprep(1). > message (f_"Examining the guest ..."); > > (* Connect to libguestfs. *) > - let g = new G.guestfs () in > - if trace () then g#set_trace true; > - if verbose () then g#set_verbose true; > + let g = open_guestfs () in > add g dryrun; > g#launch (); > > diff --git a/v2v/v2v.ml b/v2v/v2v.ml > index dabec7f..3610446 100644 > --- a/v2v/v2v.ml > +++ b/v2v/v2v.ml > @@ -236,12 +236,12 @@ and create_overlays src_disks > error (f_"qemu-img command failed, see earlier errors"); > > (* Sanity check created overlay (see below). *) > - if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then > + if not ((open_guestfs ())#disk_has_backing_file overlay_file) then > error (f_"internal error: qemu-img did not create overlay with backing file"); > > let sd = "sd" ^ drive_name i in > > - let vsize = (new G.guestfs ())#disk_virtual_size overlay_file in > + let vsize = (open_guestfs ())#disk_virtual_size overlay_file in > > { ov_overlay_file = overlay_file; ov_sd = sd; > ov_virtual_size = vsize; ov_source = source } > @@ -780,7 +780,7 @@ and copy_targets targets input output output_alloc > * backing file. Just sanity check this here. > *) > let overlay_file = t.target_overlay.ov_overlay_file in > - if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then > + if not ((open_guestfs ())#disk_has_backing_file overlay_file) then > error (f_"internal error: qemu corrupted the overlay file"); > > (* Give the input module a chance to adjust the parametersLooks good - ACK. Rich. -- 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
Seemingly Similar Threads
- [PATCH v3 10/13] v2v: factor out opening and populating guestfs handle
- [PATCH] v2v: use open_guestfs everywhere
- [PATCH] v2v: move open_guestfs to Types
- [PATCH v3 02/13] v2v: factor out overlay creation
- [PATCH 0/4] Only tell people to use -v -x when reporting bugs if they're not using those flags.