Tips for OCaml section added.
[virt-p2v.git] / virt-p2v
index 20baf62..d54da3a 100755 (executable)
--- a/virt-p2v
+++ b/virt-p2v
@@ -450,34 +450,7 @@ let auto_network state =
   (* Fedora gives an error if this file doesn't exist. *)
   sh "touch /etc/resolv.conf";
 
-(*
-  (* We can run /mnt/root/etc/init.d/network in a chroot environment,
-   * however this won't work directly because the architecture of the
-   * binaries under /mnt/root (eg. /mnt/root/sbin/ip) might not match
-   * the architecture of the live CD kernel.  In particular, a 32 bit
-   * live CD cannot run 64 bit binaries.  So we also have to bind-mount
-   * the live CD's /bin, /sbin, /lib etc. over the equivalents in
-   * /mnt/root.
-   *)
-  let bind dir =
-    if is_dir dir = Some true then
-      sh ("mount -o bind " ^ quote dir ^ " " ^ quote ("/mnt/root" ^ dir))
-  in
-  let unbind dir =
-    if is_dir dir = Some true then sh ("umount -l " ^ quote ("/mnt/root" ^ dir))
-  in
-  let dirs = [
-    "/bin"; "/sbin"; "/lib"; "/lib64";
-    "/usr/bin"; "/usr/sbin"; "/usr/lib"; "/usr/lib64";
-    "/proc"; "/sys"
-  ] in
-  List.iter bind dirs;
-  let status = shwithstatus "chroot /mnt/root /etc/init.d/network start" in
-  List.iter unbind dirs;
-*)
-
-  (* Simpler way to do the above.
-   * NB. Lazy unmount is required because dhclient keeps its current
+  (* NB. Lazy unmount is required because dhclient keeps its current
    * directory open on /etc/sysconfig/network-scripts/
    *)
   sh "mount -o bind /mnt/root/etc /etc";
@@ -563,6 +536,22 @@ let rewrite_fstab state devices_to_send =
     close_out chan
   )
 
+let () = Random.self_init ()
+
+let random_mac_address () =
+  let random =
+    List.map (sprintf "%02x") (
+      List.map (fun _ -> Random.int 256) [0;0;0]
+    ) in
+  String.concat ":" ("00"::"16"::"3e"::random)
+
+let random_uuid =
+  let hex = "0123456789abcdef" in
+  fun () ->
+  let str = String.create 32 in
+  for i = 0 to 31 do str.[i] <- hex.[Random.int 16] done;
+  str
+
 (* Main entry point. *)
 let rec main ttyname =
   (* Running from an init script.  We don't have much of a
@@ -1319,12 +1308,7 @@ MAC address:  %s"
     | Some n -> n in
   let mac_address =
     match state.mac_address with
-    | Some "" | None ->
-       let random =
-         List.map (sprintf "%02x") (
-           List.map (fun _ -> Random.int 256) [0;0;0]
-         ) in
-       String.concat ":" ("00"::"16"::"3e"::random)
+    | Some "" | None -> random_mac_address ()
     | Some mac -> mac in
 
   let xml =
@@ -1335,6 +1319,8 @@ MAC address:  %s"
 
     (* Standard stuff for every domain. *)
     let name = leaf "name" hostname in
+    let uuid = leaf "uuid" (random_uuid ()) in
+    let maxmem = leaf "maxmem" (string_of_int (memory * 1024)) in
     let memory = leaf "memory" (string_of_int (memory * 1024)) in
     let vcpu = leaf "vcpu" (string_of_int vcpus) in
 
@@ -1413,7 +1399,7 @@ MAC address:  %s"
        | Some QEMU -> ["type", "qemu"]
        | Some KVM -> ["type", "kvm"]
        | None -> []),
-      name :: memory :: vcpu :: extras @ [devices]
+      name :: uuid :: memory :: maxmem :: vcpu :: extras @ [devices]
     ) in
 
   (* Convert XML configuration file to a string, then send it to the
@@ -1478,8 +1464,8 @@ MAC address:  %s"
          let bytes_sent = Int64.add bytes_sent (Int64.of_int n) in
          let last_printed_at =
            let now = gettimeofday () in
-           (* Print progress once per second. *)
-           if now -. last_printed_at > 1. then (
+           (* Print progress every few seconds. *)
+           if now -. last_printed_at > 5. then (
              let elapsed = Int64.to_float bytes_sent /. Int64.to_float size in
              let secs_elapsed = now -. start in
              printf "%.0f%%" (100. *. elapsed);