SSH configuration.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 18 Mar 2008 12:05:01 +0000 (12:05 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Tue, 18 Mar 2008 12:05:01 +0000 (12:05 +0000)
virt-p2v

index 269bbe2..3df66f3 100755 (executable)
--- a/virt-p2v
+++ b/virt-p2v
@@ -37,13 +37,11 @@ type network =
   | NoNetwork
 type ssh_config = {
   ssh_host : string;                   (* Remote host for SSH. *)
-  ssh_port : int;                      (* Remote port. *)
+  ssh_port : string;                   (* Remote port. *)
   ssh_directory : string;              (* Remote directory. *)
   ssh_username : string;               (* Remote username. *)
-  ssh_password : string;               (* Remote password/passphrase. *)
   ssh_compression : bool;              (* If true, use SSH compression. *)
   ssh_check : bool;                    (* If true, check SSH is working. *)
-  ssh_libvirtd : bool;                 (* If true, contact remote libvirtd. *)
 }
 type hypervisor =
   | Xen
@@ -94,12 +92,13 @@ let config_architecture = ref None
 let config_memory = ref None
 let config_vcpus = ref None
 let config_mac_address = ref None
+let config_libvirtd = ref None
 
 (* The name of the program as displayed in various places. *)
 let program_name = "virt-p2v"
 
 (* If you want to test the dialog stages, set this to true. *)
-let test_dialog_stages = true
+let test_dialog_stages = false
 
 (* END OF CUSTOM virt-p2v SCRIPT SECTION.                               *)
 (*----------------------------------------------------------------------*)
@@ -226,8 +225,7 @@ let message_box title text =
       Newt.textbox_set_text textbox text;
       let ok = Newt.button 28 16 "  OK  " in
       let form = Newt.form None None [] in
-      Newt.form_add_component form textbox;
-      Newt.form_add_component form ok;
+      Newt.form_add_components form [textbox; ok];
 
       Newt.component_takes_focus ok true;
 
@@ -322,7 +320,7 @@ type block_device = string * int64  (* "hda" & size in bytes *)
 let get_lvs =
   let devname = Pcre.regexp "^/dev/(.+)\\(.+\\)$" in
 
-  function () ->
+  fun () ->
     match
     shget "lvs --noheadings -o vg_name,lv_name,devices,lv_size"
     with
@@ -456,24 +454,49 @@ let remote_of_origin_dev =
   fun dev ->
     Pcre.replace ~rex:devsd ~itempl:devsd_subst dev
 
-(* Make an SSH connection to the remote machine. *)
-(*
-let ssh_connect config =
-  let cmd = sprintf "ssh%s -l %s -p %s %s" XXXXX
+(* Make an SSH connection to the remote machine, execute command.
+ * The connection remains open until you call ssh_disconnect, it
+ * times out or there is some error.
+ *
+ * NB. The command is NOT quoted.
+ *
+ * Returns a pair (file descriptor, channel), both referring to the
+ * same thing.  Use whichever is more convenient.
+ *)
+let ssh_connect config cmd =
+  let cmd = sprintf "ssh%s -l %s -p %s %s %s"
     (if config.ssh_compression then " -C" else "")
-    (quote config.ssh_username)
-    (quote config.ssh_port)
-    (quote config.ssh_host) in
+    (quote config.ssh_username) (quote config.ssh_port) (quote config.ssh_host)
+    cmd in
   eprintf "ssh_connect: %s\n%!" cmd;
   let chan = open_process_out cmd in
   descr_of_out_channel chan, chan
 
 let ssh_disconnect (_, chan) =
+  eprintf "ssh_disconnect\n%!";
   match close_process_out chan with
   | WEXITED 0 -> ()            (* OK *)
   | WEXITED i -> failwith (sprintf "ssh: exited with error code %d" i)
   | WSIGNALED i -> failwith (sprintf "ssh: killed by signal %d" i)
-  | WSTOPPED i -> failwith (sprintf "ssh: stopped by signal %d" i)*)
+  | WSTOPPED i -> failwith (sprintf "ssh: stopped by signal %d" i)
+
+(* Test SSH connection. *)
+let test_ssh config =
+  printf "Testing SSH connection.\n\n%!";
+
+  let cmd = sprintf "/bin/ls %s" (quote config.ssh_directory) in
+  let conn = ssh_connect config cmd in
+  ssh_disconnect conn;
+
+  if !config_greeting then (
+    printf "\n\nDid SSH work?\n";
+    printf "Hint: If not sure, there is a shell on console [ALT] [F2]\n";
+    printf "    (y/n) %!";
+    let line = read_line () in
+    String.length line > 0 && (line.[0] = 'y' || line.[0] = 'Y')
+  )
+  else
+    true
 
 (* Rewrite /mnt/root/etc/fstab. *)
 let rewrite_fstab state devices_to_send =
@@ -846,8 +869,7 @@ let rec main ttyname =
              let ok = Newt.button 28 6 "  OK  " in
 
              let form = Newt.form None None [] in
-             Newt.form_add_components form [p2v; v2v];
-             Newt.form_add_component form ok;
+             Newt.form_add_components form [p2v; v2v; ok];
 
              let t =
                let rec loop () =
@@ -976,14 +998,11 @@ let rec main ttyname =
              let ok = Newt.button 48 16 "  OK  " in
 
              let form = Newt.form None None [] in
-             Newt.form_add_component form auto;
-             Newt.form_add_component form autolist;
-             Newt.form_add_components form [shell;qemu;nonet;static];
-             Newt.form_add_components form
-               [label1;label2;label3;label4;label5];
-             Newt.form_add_components form
-               [entry1;entry2;entry3;entry4;entry5];
-             Newt.form_add_component form ok;
+             Newt.form_add_components form [auto; autolist;
+                                            shell;qemu;nonet;static;
+                                            label1;label2;label3;label4;label5;
+                                            entry1;entry2;entry3;entry4;entry5;
+                                            ok];
 
              let n =
                let rec loop () =
@@ -1083,57 +1102,42 @@ let rec main ttyname =
            let dir = Newt.entry 20 3 (Some "/var/lib/xen/images") 36 [] in
            let label4 = Newt.label 1 4 "SSH username" in
            let user = Newt.entry 20 4 (Some "root") 16 [] in
+           (*
+             There's no sensible way to support this for SSH:
            let label5 = Newt.label 1 5 "SSH password" in
            let pass = Newt.entry 20 5 None 16 [Newt.PASSWORD] in
+           *)
 
            let compr =
              Newt.checkbox 16 7 "Use SSH compression (not good for LANs)"
                ' ' None in
 
            let check = Newt.checkbox 16 9 "Test SSH connection" '*' None in
-           let libvirtd =
-             Newt.checkbox 16 10 "libvirtd is running on host" '*' None in
-
-           Newt.component_add_callback check
-             (fun () ->
-                if Newt.checkbox_get_value check = '*' then
-                  Newt.component_takes_focus libvirtd true
-                else (
-                  Newt.component_takes_focus libvirtd false;
-                  Newt.checkbox_set_value libvirtd ' '
-                )
-             );
 
            let ok = Newt.button 48 16 "  OK  " in
 
            let form = Newt.form None None [] in
-           Newt.form_add_components form [label1;label2;label3;label4;label5];
-           Newt.form_add_components form [host;port;dir;user;pass];
-           Newt.form_add_components form [compr;check;libvirtd];
-           Newt.form_add_component form ok;
+           Newt.form_add_components form [label1;label2;label3;label4;
+                                          host;port;dir;user;
+                                          compr;check;
+                                          ok];
 
            let c =
              let rec loop () =
                ignore (Newt.run_form form);
-               try
-                 let host = Newt.entry_get_value host in
-                 let port = int_of_string (Newt.entry_get_value port) in
-                 let dir = Newt.entry_get_value dir in
-                 let user = Newt.entry_get_value user in
-                 let pass = Newt.entry_get_value pass in
-                 let compr = Newt.checkbox_get_value compr = '*' in
-                 let check = Newt.checkbox_get_value check = '*' in
-                 let libvirtd = Newt.checkbox_get_value libvirtd = '*' in
-                 if host <> "" && port > 0 && port < 65536 &&
-                   user <> "" then
-                     { ssh_host = host; ssh_port = port; ssh_directory = dir;
-                       ssh_username = user; ssh_password = pass;
-                       ssh_compression = compr;
-                       ssh_check = check; ssh_libvirtd = libvirtd }
-                 else
-                   loop ()
-               with
-                 Failure "int_of_string" -> loop ()
+               let host = Newt.entry_get_value host in
+               let port = Newt.entry_get_value port in
+               let dir = Newt.entry_get_value dir in
+               let user = Newt.entry_get_value user in
+               let compr = Newt.checkbox_get_value compr = '*' in
+               let check = Newt.checkbox_get_value check = '*' in
+               if host <> "" && port <> "" && user <> "" then
+                   { ssh_host = host; ssh_port = port; ssh_directory = dir;
+                     ssh_username = user;
+                     ssh_compression = compr;
+                     ssh_check = check; }
+               else
+                 loop ()
              in
              loop () in
 
@@ -1141,18 +1145,10 @@ let rec main ttyname =
            c
     ) in
 
-  (* If asked, check the SSH connection.  At the same time
-   * grab the capabilities from the remote host.
-   *)
-  let capabilities =
-    if config_ssh.ssh_check then (
-      printf "Testing SSH connection.\n\n";
-
-      Some "foo"
-
-
-    )
-    else None in
+  (* If asked, check the SSH connection. *)
+  if config_ssh.ssh_check then
+    if not (test_ssh config_ssh) then
+      failwith "SSH configuration failed";
 
 (*