Skip to content

Commit

Permalink
v2v: -o rhev/vdsm: Fix ovf:size and ovf:actual_size fields (RHBZ#1142…
Browse files Browse the repository at this point in the history
…008).

These are integers in gigabytes (2^30).

Thanks: Shahar Havivi.
  • Loading branch information
rwmjones committed Sep 17, 2014
1 parent 7739a6d commit fe36b2c
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions v2v/lib_ovf.ml
Expand Up @@ -326,22 +326,27 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =

let fileref = image_uuid // vol_uuid in

let size_gb =
Int64.to_float ov.ov_virtual_size /. 1024. /. 1024. /. 1024. in
let usage_gb =
(* ovf:size and ovf:actual_size fields are integer GBs. If you
* use floating point numbers then RHEV will fail to parse them.
* In case the size is just below a gigabyte boundary, round up.
*)
let bytes_to_gb b =
let b = roundup64 b 1073741824L in
b /^ 1073741824L
in
let size_gb = bytes_to_gb ov.ov_virtual_size in
let actual_size_gb =
if Sys.file_exists t.target_file then (
let usage_mb = du_m t.target_file in
if usage_mb > 0L then (
let usage_gb = Int64.to_float usage_mb /. 1024. in
Some usage_gb
) else None
let actual_size = du t.target_file in
if actual_size > 0L then Some (bytes_to_gb actual_size)
else None
) else (
(* In the --no-copy case the target file does not exist. In
* that case we use the estimated size.
*)
match t.target_estimated_size with
| None -> None
| Some size -> Some (Int64.to_float size /. 1024. /. 1024. /. 1024.)
| Some size -> Some (bytes_to_gb size)
) in

let format_for_rhev =
Expand All @@ -361,7 +366,7 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
e "File" [
"ovf:href", fileref;
"ovf:id", vol_uuid;
"ovf:size", Int64.to_string ov.ov_virtual_size;
"ovf:size", Int64.to_string ov.ov_virtual_size; (* NB: in bytes *)
"ovf:description", title;
] [] in
append_child disk references;
Expand All @@ -370,7 +375,7 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
let disk =
let attrs = [
"ovf:diskId", vol_uuid;
"ovf:size", sprintf "%.1f" size_gb;
"ovf:size", Int64.to_string size_gb;
"ovf:fileRef", fileref;
"ovf:parentRef", "";
"ovf:vm_snapshot_id", uuidgen ~prog ();
Expand All @@ -384,10 +389,10 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
"ovf:boot", if is_boot_drive then "True" else "False";
] in
let attrs =
match usage_gb with
match actual_size_gb with
| None -> attrs
| Some usage_gb ->
("ovf:actual_size", sprintf "%.1f" usage_gb) :: attrs in
| Some actual_size_gb ->
("ovf:actual_size", Int64.to_string actual_size_gb) :: attrs in
e "Disk" attrs [] in
append_child disk disk_section;

Expand All @@ -409,11 +414,11 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
append_child item virtualhardware_section;
) (List.combine targets vol_uuids)

and du_m filename =
(* There's no OCaml binding for st_blocks, so run coreutils 'du -m'
* to get the used size in megabytes.
and du filename =
(* There's no OCaml binding for st_blocks, so run coreutils 'du'
* to get the used size in bytes.
*)
let cmd = sprintf "du -m %s | awk '{print $1}'" (quote filename) in
let cmd = sprintf "du -b %s | awk '{print $1}'" (quote filename) in
let lines = external_command ~prog cmd in
(* We really don't want the metadata generation to fail because
* of some silly usage information, so ignore errors here.
Expand Down

0 comments on commit fe36b2c

Please sign in to comment.