Autodetect system memory and #CPUs. Ask for ssh username.
authorRichard W.M. Jones <rjones@redhat.com>
Sat, 2 Feb 2008 15:39:30 +0000 (15:39 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Sat, 2 Feb 2008 15:39:30 +0000 (15:39 +0000)
virt-p2v.ml

index 23b59f0..1dd3dfc 100755 (executable)
@@ -36,6 +36,7 @@ open ExtString
 type state = { greeting : bool;
               remote_host : string option; remote_port : string option;
               remote_directory : string option;
+              remote_username : string option;
               network : network option;
               devices_to_send : string list option;
               root_filesystem : partition option;
@@ -79,6 +80,11 @@ let defaults = {
    *)
   remote_directory = None;
 
+  (* Remote username for ssh.  Set to 'Some "username"', or None to
+   * ask the user.
+   *)
+  remote_username = None;
+
   (* List of devices to send.  Set to 'Some ["sda"; "sdb"]' for
    * example to select /dev/sda and /dev/sdb.
    *)
@@ -586,6 +592,9 @@ let rec main ttyname =
        close fd);
   printf "virt-p2v.ml starting up ...\n%!";
 
+  (* Disable screen blanking on tty. *)
+  sh "setterm -blank 0";
+
   (* Check that the environment is a sane-looking live CD.  If not, bail. *)
   if is_dir "/mnt/root" <> Some true then
     fail_dialog
@@ -828,6 +837,19 @@ let rec main ttyname =
     | Back -> Prev
   in
 
+  let ask_username state =
+    let default_username = "root" in
+    match
+    inputbox "Remote username" "Remote username for ssh access to server" 10 50
+      (Option.default default_username state.remote_username)
+    with
+    | Yes ([]|""::_) ->
+       Next { state with remote_username = Some default_username }
+    | Yes (user::_) -> Next { state with remote_username = Some user }
+    | No | Help | Error -> Ask_again
+    | Back -> Prev
+  in
+
   let ask_network state =
     match
     radiolist "Network configuration" "Network configuration" 10 50 2 [
@@ -1035,6 +1057,8 @@ MAC address:  %s"
       defaults.remote_port = None;
     ask_directory,                     (* Remote directory. *)
       defaults.remote_directory = None;
+    ask_username,                      (* Remote username. *)
+      defaults.remote_username = None;
     ask_network,                       (* Network configuration. *)
       defaults.network = None;
     ask_devices,                       (* Block devices to send. *)
@@ -1150,30 +1174,51 @@ MAC address:  %s"
   rewrite_fstab state devices_to_send;
   (* XXX Other files to rewrite? *)
 
-  (* XXX Autodetect architecture of root filesystem by looking for /bin/ls. *)
-  let system_architecture = X86_64 in
-
-  (* XXX Autodetect system memory. *)
-  let system_memory = 256 in
-
-  (* XXX Autodetect system # pCPUs. *)
-  let system_nr_cpus = 1 in
-
   (* Unmount the root filesystem and sync disks. *)
   sh "umount /mnt/root";
   sh "sync";                           (* Ugh, should be in stdlib. *)
 
-  (* Disable screen blanking on console. *)
-  sh "setterm -blank 0";
+  (* Get architecture of root filesystem, detected previously. *)
+  let system_architecture =
+    try
+      (match List.assoc root_filesystem all_partitions with
+       | LinuxRoot (arch, _) -> arch
+       | _ -> raise Not_found
+      )
+    with
+      Not_found ->
+       (* None was detected before, so assume same as live CD. *)
+       let arch = shget "uname -m" in
+       match arch with
+       | Some (("i386"|"i486"|"i586"|"i686")::_) -> I386
+       | Some ("x86_64"::_) -> X86_64
+       | Some ("ia64"::_) -> IA64
+       | _ -> I386 (* probably wrong XXX *) in
+
+  (* Autodetect system memory. *)
+  let system_memory =
+    let mem = shget "head -1 /proc/meminfo | awk '{print $2/1024}'" in
+    match mem with
+    | Some (mem::_) -> int_of_float (float_of_string mem)
+    | _ -> 256 in
+
+  (* Autodetect system # pCPUs. *)
+  let system_nr_cpus =
+    let cpus =
+      shget "grep ^processor /proc/cpuinfo | tail -1 | awk '{print $3+1}'" in
+    match cpus with
+    | Some (cpus::_) -> int_of_string cpus
+    | _ -> 1 in
 
   let remote_host = Option.get state.remote_host in
   let remote_port = Option.get state.remote_port in
   let remote_directory = Option.get state.remote_directory in
+  let remote_username = Option.get state.remote_username in
 
   (* Functions to connect and disconnect from the remote system. *)
   let do_connect remote_name _ =
-    let cmd = sprintf "ssh -C -p %s %s \"cat > %s/%s\""
-      (quote remote_port) (quote remote_host)
+    let cmd = sprintf "ssh -C -l %s -p %s %s \"cat > %s/%s\""
+      (quote remote_username) (quote remote_port) (quote remote_host)
       (quote remote_directory) (quote remote_name) in
     eprintf "connect: %s\n%!" cmd;
     let chan = open_process_out cmd in