diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-07-25 16:50:40 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-07-25 16:51:42 +0100 |
commit | 208d1c1a09e9d7cbc27207ea7ff1c84ff1f64fbd (patch) | |
tree | fef9d20bb1c584c3917ddce6d8cce4aae6780294 | |
parent | 0b3b5f984c2011632c6d56e5f0ae1194d2c5074b (diff) | |
download | libguestfs-208d1c1a09e9d7cbc27207ea7ff1c84ff1f64fbd.tar.gz libguestfs-208d1c1a09e9d7cbc27207ea7ff1c84ff1f64fbd.tar.xz libguestfs-208d1c1a09e9d7cbc27207ea7ff1c84ff1f64fbd.zip |
sparsify: Detect qemu-img version and use qcow2 v3 for overlay if available.
qcow2 v3 is generally more efficient, although unfortunately it
doesn't support automatically sparsifying zero writes.
Note this *only* uses qcow2 v3 for the intermediate overlay file, not
for the final output (since we want the output to be broadly
compatible with old hypervisors).
-rw-r--r-- | sparsify/sparsify.ml | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/sparsify/sparsify.ml b/sparsify/sparsify.ml index 1d0669e5..8e431895 100644 --- a/sparsify/sparsify.ml +++ b/sparsify/sparsify.ml @@ -17,6 +17,7 @@ *) open Unix +open Scanf open Printf open Sparsify_gettext.Gettext @@ -152,6 +153,43 @@ read the man page virt-sparsify(1). debug_gc, format, ignores, machine_readable, option, quiet, verbose, trace, zeroes +(* Try to determine the version of the 'qemu-img' program. + * All known versions of qemu-img display the following first + * line when you run 'qemu-img --help': + * + * "qemu-img version x.y.z, Copyright [...]" + * + * Parse out 'x.y'. + *) +let qemu_img_version = + let cmd = "qemu-img --help" in + let chan = open_process_in cmd in + let line = input_line chan in + let stat = close_process_in chan in + (match stat with + | WEXITED _ -> () + | WSIGNALED i -> + error (f_"external command '%s' killed by signal %d") cmd i + | WSTOPPED i -> + error (f_"external command '%s' stopped by signal %d") cmd i + ); + + try + sscanf line "qemu-img version %d.%d" ( + fun major minor -> + let minor = if minor > 9 then 9 else minor in + float major +. float minor /. 10. + ) + with + Scan_failure msg -> + eprintf (f_"warning: failed to read qemu-img version\n line: %S\n message: %s\n%!") + line msg; + 0.9 + +let () = + if not quiet then + printf (f_"qemu-img version %g\n%!") qemu_img_version + let () = if not quiet then printf (f_"Create overlay file to protect source disk ...\n%!") @@ -176,7 +214,9 @@ let overlaydisk = match format with | None -> [] | Some fmt -> [sprintf "backing_fmt=%s" fmt] in - backing_file_option @ backing_fmt_option in + let version3 = + if qemu_img_version >= 1.1 then ["compat=1.1"] else [] in + backing_file_option @ backing_fmt_option @ version3 in sprintf "qemu-img create -f qcow2 -o %s %s > /dev/null" (Filename.quote (String.concat "," options)) (Filename.quote tmp) in if verbose then |