-
Notifications
You must be signed in to change notification settings - Fork 7
/
gui.c
2454 lines (2142 loc) · 81.9 KB
/
gui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* virt-p2v
* Copyright (C) 2009-2019 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* This file implements almost all of the virt-p2v graphical user
* interface (GUI).
*
* The GUI has three main dialogs:
*
* =over 4
*
* =item Connection dialog
*
* The connection dialog is the one shown initially. It asks the user
* to type in the login details for the remote conversion server and
* invites the user to test the ssh connection.
*
* =item Conversion dialog
*
* The conversion dialog asks for information about the target VM
* (eg. the number of vCPUs required), and about what to convert
* (eg. which network interfaces should be copied and which should be
* ignored).
*
* =item Running dialog
*
* The running dialog is displayed when the P2V process is underway.
* It mainly displays the virt-v2v debug messages.
*
* =back
*
* Note that the other major dialog (C<"Configure network ...">) is
* handled entirely by NetworkManager's L<nm-connection-editor(1)>
* program and has nothing to do with this code.
*
* This file is written in a kind of "pseudo-Gtk" that currently targets Gtk
* 3.22 exclusively, but may later be extended to a broader Gtk version range.
* This is done using a few macros to implement old C<gtk_*> functions or map
* them to newer functions.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <locale.h>
#include <assert.h>
#include <libintl.h>
#include <pthread.h>
/* errors in <gtk.h> */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#if defined(__GNUC__) && __GNUC__ >= 6 /* gcc >= 6 */
#pragma GCC diagnostic ignored "-Wshift-overflow"
#endif
#include <gtk/gtk.h>
#pragma GCC diagnostic pop
#include "ignore-value.h"
#include "p2v.h"
/* See note about "pseudo-Gtk" above. */
#include "gui-gtk3-compat.h"
/* Maximum vCPUs and guest memory that we will allow users to set.
* These limits come from
* https://access.redhat.com/articles/rhel-kvm-limits
*/
#define MAX_SUPPORTED_VCPUS 160
#define MAX_SUPPORTED_MEMORY_MB (UINT64_C (4000 * 1024))
static void create_connection_dialog (struct config *);
static void create_conversion_dialog (struct config *config,
const char * const *disks,
const char * const *removable);
static void create_running_dialog (void);
static void show_connection_dialog (void);
static void show_conversion_dialog (void);
static void show_running_dialog (void);
static void set_info_label (void);
/* The connection dialog. */
static GtkWidget *conn_dlg,
*server_entry, *port_entry,
*username_entry, *password_entry, *identity_entry, *sudo_button,
*spinner_hbox,
*spinner,
*spinner_message, *next_button;
/* The conversion dialog. */
static GtkWidget *conv_dlg,
*guestname_entry, *vcpu_topo, *vcpus_entry, *memory_entry,
*vcpus_warning, *memory_warning, *target_warning_label,
*o_combo, *oc_entry, *os_entry, *of_entry, *oa_combo, *oo_entry,
*info_label,
*disks_list, *removable_list, *interfaces_list;
static int vcpus_entry_when_last_sensitive;
/* The running dialog which is displayed when virt-v2v is running. */
static GtkWidget *run_dlg,
*v2v_output_sw, *v2v_output, *log_label, *status_label,
*cancel_button, *shutdown_button;
/* Colour tags used in the v2v_output GtkTextBuffer. */
static GtkTextTag *v2v_output_tags[16];
/**
* The entry point from the main program.
*
* Note that C<gtk_init> etc have already been called in C<main>.
*/
void
gui_conversion (struct config *config,
const char * const *disks,
const char * const *removable)
{
/* Create the dialogs. */
create_connection_dialog (config);
create_conversion_dialog (config, disks, removable);
create_running_dialog ();
/* Start by displaying the connection dialog. */
show_connection_dialog ();
gtk_main ();
}
/**
* Trivial helper (shorthand) function for duplicating the contents of a
* GTK_ENTRY.
*/
static char *
entry_text_dup (GtkWidget *entry)
{
return strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
}
/**
* Trivial helper (shorthand) function for getting the active/inactive state
* of a GTK_TOGGLE_BUTTON.
*/
static bool
tgl_btn_is_act (GtkWidget *toggle_button)
{
return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle_button));
}
/*----------------------------------------------------------------------*/
/* Connection dialog. */
static void username_changed_callback (GtkWidget *w, gpointer data);
static void password_or_identity_changed_callback (GtkWidget *w, gpointer data);
static void test_connection_clicked (GtkWidget *w, gpointer data);
static void *test_connection_thread (void *data);
static gboolean start_spinner (gpointer user_data);
static gboolean stop_spinner (gpointer user_data);
static gboolean test_connection_error (gpointer user_data);
static gboolean test_connection_ok (gpointer user_data);
static void configure_network_button_clicked (GtkWidget *w, gpointer data);
static void xterm_button_clicked (GtkWidget *w, gpointer data);
static void about_button_clicked (GtkWidget *w, gpointer data);
static void connection_next_clicked (GtkWidget *w, gpointer data);
static void repopulate_output_combo (struct config *config);
/**
* Create the connection dialog.
*
* This creates the dialog, but it is not displayed. See
* C<show_connection_dialog>.
*/
static void
create_connection_dialog (struct config *config)
{
GtkWidget *intro, *table;
GtkWidget *server_label;
GtkWidget *server_hbox;
GtkWidget *port_colon_label;
GtkWidget *username_label;
GtkWidget *password_label;
GtkWidget *identity_label;
GtkWidget *test_hbox, *test;
GtkWidget *about;
GtkWidget *configure_network;
GtkWidget *xterm;
char port_str[64];
int row;
conn_dlg = gtk_dialog_new ();
gtk_window_set_title (GTK_WINDOW (conn_dlg), g_get_prgname ());
gtk_window_set_resizable (GTK_WINDOW (conn_dlg), FALSE);
/* The main dialog area. */
intro = gtk_label_new (_("Connect to a virt-v2v conversion server over "
"SSH:"));
gtk_label_set_line_wrap (GTK_LABEL (intro), TRUE);
set_padding (intro, 10, 10);
table_new (table, 5, 2);
row = 0;
server_label = gtk_label_new_with_mnemonic (_("Conversion _server:"));
table_attach (table, server_label,
0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
set_alignment (server_label, 1., 0.5);
hbox_new (server_hbox, FALSE, 4);
server_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (server_label), server_entry);
if (config->remote.server != NULL)
gtk_entry_set_text (GTK_ENTRY (server_entry), config->remote.server);
port_colon_label = gtk_label_new (":");
port_entry = gtk_entry_new ();
gtk_entry_set_width_chars (GTK_ENTRY (port_entry), 6);
snprintf (port_str, sizeof port_str, "%d", config->remote.port);
gtk_entry_set_text (GTK_ENTRY (port_entry), port_str);
gtk_box_pack_start (GTK_BOX (server_hbox), server_entry, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (server_hbox), port_colon_label, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (server_hbox), port_entry, FALSE, FALSE, 0);
table_attach (table, server_hbox,
1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
row++;
username_label = gtk_label_new_with_mnemonic (_("_User name:"));
table_attach (table, username_label,
0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
set_alignment (username_label, 1., 0.5);
username_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (username_label), username_entry);
if (config->auth.username != NULL)
gtk_entry_set_text (GTK_ENTRY (username_entry), config->auth.username);
else
gtk_entry_set_text (GTK_ENTRY (username_entry), "root");
table_attach (table, username_entry,
1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
row++;
password_label = gtk_label_new_with_mnemonic (_("_Password:"));
table_attach (table, password_label,
0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
set_alignment (password_label, 1., 0.5);
password_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (password_label), password_entry);
gtk_entry_set_visibility (GTK_ENTRY (password_entry), FALSE);
gtk_entry_set_input_purpose (GTK_ENTRY (password_entry),
GTK_INPUT_PURPOSE_PASSWORD);
if (config->auth.password != NULL)
gtk_entry_set_text (GTK_ENTRY (password_entry), config->auth.password);
table_attach (table, password_entry,
1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
row++;
identity_label = gtk_label_new_with_mnemonic (_("SSH _Identity URL:"));
table_attach (table, identity_label,
0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
set_alignment (identity_label, 1., 0.5);
identity_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (identity_label), identity_entry);
if (config->auth.identity.url != NULL)
gtk_entry_set_text (GTK_ENTRY (identity_entry), config->auth.identity.url);
table_attach (table, identity_entry,
1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
row++;
sudo_button =
gtk_check_button_new_with_mnemonic (_("Use su_do when running virt-v2v"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sudo_button),
config->auth.sudo);
table_attach (table, sudo_button,
1, 2, row, GTK_FILL, GTK_FILL, 4, 4);
hbox_new (test_hbox, FALSE, 0);
test = gtk_button_new_with_mnemonic (_("_Test connection"));
gtk_box_pack_start (GTK_BOX (test_hbox), test, TRUE, FALSE, 0);
hbox_new (spinner_hbox, FALSE, 10);
spinner = gtk_spinner_new ();
gtk_box_pack_start (GTK_BOX (spinner_hbox), spinner, FALSE, FALSE, 0);
spinner_message = gtk_label_new (NULL);
gtk_label_set_line_wrap (GTK_LABEL (spinner_message), TRUE);
set_padding (spinner_message, 10, 10);
gtk_box_pack_start (GTK_BOX (spinner_hbox), spinner_message, TRUE, TRUE, 0);
gtk_box_pack_start
(GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (conn_dlg))),
intro, TRUE, TRUE, 0);
gtk_box_pack_start
(GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (conn_dlg))),
table, TRUE, TRUE, 0);
gtk_box_pack_start
(GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (conn_dlg))),
test_hbox, FALSE, FALSE, 0);
gtk_box_pack_start
(GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (conn_dlg))),
spinner_hbox, TRUE, TRUE, 0);
/* Buttons. */
gtk_dialog_add_buttons (GTK_DIALOG (conn_dlg),
_("_Configure network ..."), 1,
_("_XTerm ..."), 2,
_("_About virt-p2v " PACKAGE_VERSION " ..."), 3,
_("_Next"), 4,
NULL);
next_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (conn_dlg), 4);
gtk_widget_set_sensitive (next_button, FALSE);
configure_network =
gtk_dialog_get_widget_for_response (GTK_DIALOG (conn_dlg), 1);
xterm = gtk_dialog_get_widget_for_response (GTK_DIALOG (conn_dlg), 2);
about = gtk_dialog_get_widget_for_response (GTK_DIALOG (conn_dlg), 3);
/* Signals. */
g_signal_connect_swapped (G_OBJECT (conn_dlg), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (test), "clicked",
G_CALLBACK (test_connection_clicked), config);
g_signal_connect (G_OBJECT (configure_network), "clicked",
G_CALLBACK (configure_network_button_clicked), NULL);
g_signal_connect (G_OBJECT (xterm), "clicked",
G_CALLBACK (xterm_button_clicked), NULL);
g_signal_connect (G_OBJECT (about), "clicked",
G_CALLBACK (about_button_clicked), NULL);
g_signal_connect (G_OBJECT (next_button), "clicked",
G_CALLBACK (connection_next_clicked), NULL);
g_signal_connect (G_OBJECT (username_entry), "changed",
G_CALLBACK (username_changed_callback), NULL);
g_signal_connect (G_OBJECT (password_entry), "changed",
G_CALLBACK (password_or_identity_changed_callback), NULL);
g_signal_connect (G_OBJECT (identity_entry), "changed",
G_CALLBACK (password_or_identity_changed_callback), NULL);
/* Call this signal to initialize the sensitivity of the sudo
* button correctly.
*/
username_changed_callback (NULL, NULL);
}
/**
* If the username is "root", disable the sudo button.
*/
static void
username_changed_callback (GtkWidget *w, gpointer data)
{
const char *str;
int username_is_root;
int sudo_is_set;
str = gtk_entry_get_text (GTK_ENTRY (username_entry));
username_is_root = str != NULL && STREQ (str, "root");
sudo_is_set = tgl_btn_is_act (sudo_button);
/* The sudo button is sensitive if:
* - The username is not "root", or
* - The button is not already checked (to allow the user to uncheck it)
*/
gtk_widget_set_sensitive (sudo_button, !username_is_root || sudo_is_set);
}
/**
* The password or SSH identity URL entries are mutually exclusive, so
* if one contains text then disable the other. This function is
* called when the "changed" signal is received on either.
*/
static void
password_or_identity_changed_callback (GtkWidget *w, gpointer data)
{
const char *str;
int password_set;
int identity_set;
str = gtk_entry_get_text (GTK_ENTRY (password_entry));
password_set = str != NULL && STRNEQ (str, "");
str = gtk_entry_get_text (GTK_ENTRY (identity_entry));
identity_set = str != NULL && STRNEQ (str, "");
if (!password_set && !identity_set) {
gtk_widget_set_sensitive (password_entry, TRUE);
gtk_widget_set_sensitive (identity_entry, TRUE);
}
else if (identity_set)
gtk_widget_set_sensitive (password_entry, FALSE);
else if (password_set)
gtk_widget_set_sensitive (identity_entry, FALSE);
}
/**
* Hide all other dialogs and show the connection dialog.
*/
static void
show_connection_dialog (void)
{
/* Hide the other dialogs. */
gtk_widget_hide (conv_dlg);
gtk_widget_hide (run_dlg);
/* Show everything except the spinner. */
gtk_widget_show_all (conn_dlg);
gtk_widget_hide (spinner_hbox);
}
/**
* Callback from the C<Test connection> button.
*
* This initiates a background thread which actually does the ssh to
* the conversion server and the rest of the testing (see
* C<test_connection_thread>).
*/
static void
test_connection_clicked (GtkWidget *w, gpointer data)
{
struct config *config = data;
const gchar *port_str;
const gchar *identity_str;
size_t errors = 0;
struct config *copy;
int err;
pthread_t tid;
pthread_attr_t attr;
gtk_label_set_text (GTK_LABEL (spinner_message), "");
gtk_widget_show_all (spinner_hbox);
gtk_widget_hide (spinner);
/* Get the fields from the various widgets. */
free (config->remote.server);
config->remote.server = entry_text_dup (server_entry);
if (STREQ (config->remote.server, "")) {
gtk_label_set_text (GTK_LABEL (spinner_message),
_("error: No conversion server given."));
gtk_widget_grab_focus (server_entry);
errors++;
}
port_str = gtk_entry_get_text (GTK_ENTRY (port_entry));
if (sscanf (port_str, "%d", &config->remote.port) != 1 ||
config->remote.port <= 0 || config->remote.port >= 65536) {
gtk_label_set_text (GTK_LABEL (spinner_message),
_("error: Invalid port number. If in doubt, use "
"\"22\"."));
gtk_widget_grab_focus (port_entry);
errors++;
}
free (config->auth.username);
config->auth.username = entry_text_dup (username_entry);
if (STREQ (config->auth.username, "")) {
gtk_label_set_text (GTK_LABEL (spinner_message),
_("error: No user name. If in doubt, use \"root\"."));
gtk_widget_grab_focus (username_entry);
errors++;
}
free (config->auth.password);
config->auth.password = entry_text_dup (password_entry);
free (config->auth.identity.url);
identity_str = gtk_entry_get_text (GTK_ENTRY (identity_entry));
if (identity_str && STRNEQ (identity_str, ""))
config->auth.identity.url = strdup (identity_str);
else
config->auth.identity.url = NULL;
config->auth.identity.file_needs_update = 1;
config->auth.sudo = tgl_btn_is_act (sudo_button);
if (errors)
return;
/* Give the testing thread its own copy of the config in case we
* update the config in the main thread.
*/
copy = copy_config (config);
/* No errors so far, so test the connection in a background thread. */
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
err = pthread_create (&tid, &attr, test_connection_thread, copy);
if (err != 0)
error (EXIT_FAILURE, err, "pthread_create");
pthread_attr_destroy (&attr);
}
/**
* Run C<test_connection> (in a detached background thread). Once it
* finishes stop the spinner and set the spinner message
* appropriately. If the test is successful then we enable the
* C<Next> button. If unsuccessful, an error is shown in the
* connection dialog.
*/
static void *
test_connection_thread (void *data)
{
struct config *copy = data;
int r;
g_idle_add (start_spinner, NULL);
wait_network_online (copy);
r = test_connection (copy);
free_config (copy);
g_idle_add (stop_spinner, NULL);
if (r == -1)
g_idle_add (test_connection_error, NULL);
else
g_idle_add (test_connection_ok, NULL);
/* Thread is detached anyway, so no one is waiting for the status. */
return NULL;
}
/**
* Idle task called from C<test_connection_thread> (but run on the
* main thread) to start the spinner in the connection dialog.
*/
static gboolean
start_spinner (gpointer user_data)
{
gtk_label_set_text (GTK_LABEL (spinner_message),
_("Testing the connection to the conversion server ..."));
gtk_widget_show (spinner);
gtk_spinner_start (GTK_SPINNER (spinner));
return FALSE;
}
/**
* Idle task called from C<test_connection_thread> (but run on the
* main thread) to stop the spinner in the connection dialog.
*/
static gboolean
stop_spinner (gpointer user_data)
{
gtk_spinner_stop (GTK_SPINNER (spinner));
gtk_widget_hide (spinner);
return FALSE;
}
/**
* Idle task called from C<test_connection_thread> (but run on the
* main thread) when there is an error. Display the error message and
* disable the C<Next> button so the user is forced to correct it.
*/
static gboolean
test_connection_error (gpointer user_data)
{
const char *err = get_ssh_error ();
gtk_label_set_text (GTK_LABEL (spinner_message), err);
/* Disable the Next button. */
gtk_widget_set_sensitive (next_button, FALSE);
return FALSE;
}
/**
* Idle task called from C<test_connection_thread> (but run on the
* main thread) when the connection test was successful.
*/
static gboolean
test_connection_ok (gpointer user_data)
{
gtk_label_set_text
(GTK_LABEL (spinner_message),
_("Connected to the conversion server.\n"
"Press the \"Next\" button to configure the conversion process."));
/* Enable the Next button. */
gtk_widget_set_sensitive (next_button, TRUE);
gtk_widget_grab_focus (next_button);
/* Update the information in the conversion dialog. */
set_info_label ();
return FALSE;
}
/**
* Callback from the C<Configure network ...> button. This dialog is
* handled entirely by an external program which is part of
* NetworkManager.
*/
static void
configure_network_button_clicked (GtkWidget *w, gpointer data)
{
if (access ("/sbin/yast2", X_OK) >= 0)
ignore_value (system ("yast2 lan &"));
else
ignore_value (system ("nm-connection-editor &"));
}
/**
* Callback from the C<XTerm ...> button.
*/
static void
xterm_button_clicked (GtkWidget *w, gpointer data)
{
ignore_value (system ("xterm &"));
}
/**
* Callback from the C<About virt-p2v ...> button.
*
* See also F<p2v/about-authors.c> and F<p2v/about-license.c>.
*/
static void
about_button_clicked (GtkWidget *w, gpointer data)
{
GtkWidget *dialog;
GtkWidget *parent = conn_dlg;
dialog = gtk_about_dialog_new ();
g_object_set (G_OBJECT (dialog),
"program-name", g_get_prgname (),
"version", PACKAGE_VERSION_FULL " (" host_cpu ")",
"copyright", "\u00A9 2009-2019 Red Hat Inc.",
"comments",
_("Virtualize a physical machine to run on KVM"),
"license-type", GTK_LICENSE_GPL_2_0,
"website", "http://libguestfs.org/",
"authors", authors,
NULL);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/**
* Callback when the connection dialog C<Next> button has been
* clicked.
*/
static void
connection_next_clicked (GtkWidget *w, gpointer data)
{
/* Switch to the conversion dialog. */
show_conversion_dialog ();
}
/*----------------------------------------------------------------------*/
/* Conversion dialog. */
static void populate_disks_store (GtkListStore *disks_store,
const char * const *disks);
static void populate_disks (GtkTreeView *disks_list_p,
const char * const *disks);
static void populate_removable_store (GtkListStore *removable_store,
const char * const *removable);
static void populate_removable (GtkTreeView *removable_list_p,
const char * const *removable);
static void populate_interfaces (GtkTreeView *interfaces_list_p);
static void populate_misc_opts (GtkEntry *entry, char * const *misc);
static void toggled (GtkCellRendererToggle *cell,
gchar *path_str,
gpointer data);
static void network_edited_callback (GtkCellRendererToggle *cell,
gchar *path_str,
gchar *new_text,
gpointer data);
static gboolean maybe_identify_click (GtkWidget *interfaces_list_p,
GdkEventButton *event,
gpointer data);
static void set_disks_from_ui (struct config *);
static void set_removable_from_ui (struct config *);
static void set_interfaces_from_ui (struct config *);
static void conversion_back_clicked (GtkWidget *w, gpointer data);
static void refresh_disks_clicked (GtkWidget *w, gpointer data);
static void start_conversion_clicked (GtkWidget *w, gpointer data);
static void vcpu_topo_toggled (GtkWidget *w, gpointer data);
static void vcpus_or_memory_check_callback (GtkWidget *w, gpointer data);
static void notify_ui_callback (int type, const char *data);
static int get_vcpus_from_conv_dlg (void);
static uint64_t get_memory_from_conv_dlg (void);
enum {
DISKS_COL_CONVERT = 0,
DISKS_COL_HW_NAME,
DISKS_COL_DEVICE,
NUM_DISKS_COLS,
};
enum {
REMOVABLE_COL_CONVERT = 0,
REMOVABLE_COL_HW_NAME,
REMOVABLE_COL_DEVICE,
NUM_REMOVABLE_COLS,
};
enum {
INTERFACES_COL_CONVERT = 0,
INTERFACES_COL_DEVICE,
INTERFACES_COL_NETWORK,
NUM_INTERFACES_COLS,
};
/**
* Create the conversion dialog.
*
* This creates the dialog, but it is not displayed. See
* C<show_conversion_dialog>.
*/
static void
create_conversion_dialog (struct config *config,
const char * const *disks,
const char * const *removable)
{
GtkWidget *back, *refresh_disks, *start_button;
GtkWidget *hbox, *left_vbox, *right_vbox;
GtkWidget *target_frame, *target_vbox, *target_tbl;
GtkWidget *guestname_label, *vcpus_label, *memory_label;
GtkWidget *output_frame, *output_vbox, *output_tbl;
GtkWidget *o_label, *oa_label, *oc_label, *of_label, *os_label, *oo_label;
GtkWidget *info_frame;
GtkWidget *disks_frame, *disks_sw;
GtkWidget *removable_frame, *removable_sw;
GtkWidget *interfaces_frame, *interfaces_sw;
char vcpus_str[64];
char memory_str[64];
int row;
conv_dlg = gtk_dialog_new ();
gtk_window_set_title (GTK_WINDOW (conv_dlg), g_get_prgname ());
gtk_window_set_resizable (GTK_WINDOW (conv_dlg), FALSE);
/* XXX It would be nice not to have to set this explicitly, but
* if we don't then Gtk chooses a very small window.
*/
gtk_widget_set_size_request (conv_dlg, 900, 600);
/* The main dialog area. */
hbox_new (hbox, TRUE, 1);
vbox_new (left_vbox, FALSE, 1);
vbox_new (right_vbox, TRUE, 1);
/* The left column: target properties and output options. */
target_frame = gtk_frame_new (_("Target properties"));
gtk_container_set_border_width (GTK_CONTAINER (target_frame), 4);
vbox_new (target_vbox, FALSE, 1);
table_new (target_tbl, 4, 3);
row = 0;
guestname_label = gtk_label_new_with_mnemonic (_("_Name:"));
table_attach (target_tbl, guestname_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (guestname_label, 1., 0.5);
guestname_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (guestname_label), guestname_entry);
if (config->guestname != NULL)
gtk_entry_set_text (GTK_ENTRY (guestname_entry), config->guestname);
table_attach (target_tbl, guestname_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
vcpu_topo = gtk_check_button_new_with_mnemonic (
_("Copy fully populated _pCPU topology"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vcpu_topo),
config->vcpu.phys_topo);
table_attach (target_tbl, vcpu_topo, 0, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
vcpus_label = gtk_label_new_with_mnemonic (_("# _vCPUs:"));
table_attach (target_tbl, vcpus_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (vcpus_label, 1., 0.5);
vcpus_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (vcpus_label), vcpus_entry);
snprintf (vcpus_str, sizeof vcpus_str, "%d", config->vcpu.cores);
gtk_entry_set_text (GTK_ENTRY (vcpus_entry), vcpus_str);
vcpus_entry_when_last_sensitive = config->vcpu.cores;
table_attach (target_tbl, vcpus_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
vcpus_warning = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING,
GTK_ICON_SIZE_BUTTON);
table_attach (target_tbl, vcpus_warning,
2, 3, row, 0, 0, 1, 1);
row++;
memory_label = gtk_label_new_with_mnemonic (_("_Memory (MB):"));
table_attach (target_tbl, memory_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (memory_label, 1., 0.5);
memory_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (memory_label), memory_entry);
snprintf (memory_str, sizeof memory_str, "%" PRIu64,
config->memory / 1024 / 1024);
gtk_entry_set_text (GTK_ENTRY (memory_entry), memory_str);
table_attach (target_tbl, memory_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
memory_warning = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING,
GTK_ICON_SIZE_BUTTON);
table_attach (target_tbl, memory_warning,
2, 3, row, 0, 0, 1, 1);
gtk_box_pack_start (GTK_BOX (target_vbox), target_tbl, TRUE, TRUE, 0);
target_warning_label = gtk_label_new ("");
gtk_label_set_line_wrap (GTK_LABEL (target_warning_label), TRUE);
gtk_label_set_line_wrap_mode (GTK_LABEL (target_warning_label),
PANGO_WRAP_WORD);
gtk_label_set_max_width_chars (GTK_LABEL (target_warning_label), 50);
gtk_widget_set_size_request (target_warning_label, -1, 7 * 16);
gtk_box_pack_end (GTK_BOX (target_vbox), target_warning_label, TRUE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (target_frame), target_vbox);
output_frame = gtk_frame_new (_("Virt-v2v output options"));
gtk_container_set_border_width (GTK_CONTAINER (output_frame), 4);
vbox_new (output_vbox, FALSE, 1);
table_new (output_tbl, 5, 2);
row = 0;
o_label = gtk_label_new_with_mnemonic (_("Output _to (-o):"));
table_attach (output_tbl, o_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (o_label, 1., 0.5);
o_combo = gtk_combo_box_text_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (o_label), o_combo);
gtk_widget_set_tooltip_markup (o_combo,
_("<b>libvirt</b> means send the converted "
"guest to libvirt-managed KVM on the "
"conversion server. "
"<b>local</b> means put it in a directory "
"on the conversion server. "
"<b>rhv</b> means write it to RHV-M/oVirt. "
"<b>openstack</b> means write it to "
"OpenStack. "
"<b>glance</b> means write it to OpenStack "
"Glance. "
"See the virt-v2v(1) manual page for more "
"information about output options."));
repopulate_output_combo (config);
table_attach (output_tbl, o_combo,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
oc_label = gtk_label_new_with_mnemonic (_("_Output conn. (-oc):"));
table_attach (output_tbl, oc_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (oc_label, 1., 0.5);
oc_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (oc_label), oc_entry);
gtk_widget_set_tooltip_markup (oc_entry,
_("For <b>libvirt</b> only, the libvirt "
"connection URI, or leave blank to add the "
"guest to the default libvirt instance on "
"the conversion server. "
"For others, leave this field blank."));
if (config->output.connection != NULL)
gtk_entry_set_text (GTK_ENTRY (oc_entry), config->output.connection);
table_attach (output_tbl, oc_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
os_label = gtk_label_new_with_mnemonic (_("Output _storage (-os):"));
table_attach (output_tbl, os_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (os_label, 1., 0.5);
os_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (os_label), os_entry);
gtk_widget_set_tooltip_markup (os_entry,
_("For <b>local</b>, put the directory name "
"on the conversion server. "
"For <b>rhv</b>, put the Export Storage "
"Domain (server:/mountpoint). "
"For others, leave this field blank."));
if (config->output.storage != NULL)
gtk_entry_set_text (GTK_ENTRY (os_entry), config->output.storage);
table_attach (output_tbl, os_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
of_label = gtk_label_new_with_mnemonic (_("Output _format (-of):"));
table_attach (output_tbl, of_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (of_label, 1., 0.5);
of_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (of_label), of_entry);
gtk_widget_set_tooltip_markup (of_entry,
_("The output disk format, typically "
"<b>raw</b> or <b>qcow2</b>. "
"If blank, defaults to <b>raw</b>."));
if (config->output.format != NULL)
gtk_entry_set_text (GTK_ENTRY (of_entry), config->output.format);
table_attach (output_tbl, of_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
oa_label = gtk_label_new_with_mnemonic (_("Output _allocation (-oa):"));
table_attach (output_tbl, oa_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (oa_label, 1., 0.5);
oa_combo = gtk_combo_box_text_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (oa_label), oa_combo);
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (oa_combo),
"sparse");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (oa_combo),
"preallocated");
switch (config->output.allocation) {
case OUTPUT_ALLOCATION_PREALLOCATED:
gtk_combo_box_set_active (GTK_COMBO_BOX (oa_combo), 1);
break;
default:
gtk_combo_box_set_active (GTK_COMBO_BOX (oa_combo), 0);
break;
}
table_attach (output_tbl, oa_combo,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
row++;
oo_label = gtk_label_new_with_mnemonic (_("M_isc. options (-oo):"));
table_attach (output_tbl, oo_label,
0, 1, row, GTK_FILL, GTK_FILL, 1, 1);
set_alignment (oo_label, 1., 0.5);
oo_entry = gtk_entry_new ();
gtk_label_set_mnemonic_widget (GTK_LABEL (oo_label), oo_entry);
gtk_widget_set_tooltip_markup (oo_entry,
_("A comma-separated list of "
"<b>OPTION=VALUE</b> output-specific "
"options. Each option is passed to "
"virt-v2v as the argument of a separate "
"<b>-oo</b> option. Do not place a space "
"character at either side of either comma "
"separator. Mainly useful for "
"<b>openstack</b>."));
populate_misc_opts (GTK_ENTRY (oo_entry), config->output.misc);
table_attach (output_tbl, oo_entry,
1, 2, row, GTK_FILL, GTK_FILL, 1, 1);
gtk_box_pack_start (GTK_BOX (output_vbox), output_tbl, TRUE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (output_frame), output_vbox);
info_frame = gtk_frame_new (_("Information"));
gtk_container_set_border_width (GTK_CONTAINER (info_frame), 4);
info_label = gtk_label_new (NULL);
set_alignment (info_label, 0.1, 0.5);
set_info_label ();
gtk_container_add (GTK_CONTAINER (info_frame), info_label);
/* The right column: select devices to be converted. */
disks_frame = gtk_frame_new (_("Fixed hard disks"));
gtk_container_set_border_width (GTK_CONTAINER (disks_frame), 4);
disks_sw = gtk_scrolled_window_new (NULL, NULL);
gtk_container_set_border_width (GTK_CONTAINER (disks_sw), 8);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (disks_sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
disks_list = gtk_tree_view_new ();
populate_disks (GTK_TREE_VIEW (disks_list), disks);
scrolled_window_add_with_viewport (disks_sw, disks_list);
gtk_container_add (GTK_CONTAINER (disks_frame), disks_sw);
removable_frame = gtk_frame_new (_("Removable media"));
gtk_container_set_border_width (GTK_CONTAINER (removable_frame), 4);
removable_sw = gtk_scrolled_window_new (NULL, NULL);
gtk_container_set_border_width (GTK_CONTAINER (removable_sw), 8);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (removable_sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
removable_list = gtk_tree_view_new ();
populate_removable (GTK_TREE_VIEW (removable_list), removable);
scrolled_window_add_with_viewport (removable_sw, removable_list);
gtk_container_add (GTK_CONTAINER (removable_frame), removable_sw);
interfaces_frame = gtk_frame_new (_("Network interfaces"));
gtk_container_set_border_width (GTK_CONTAINER (interfaces_frame), 4);
interfaces_sw = gtk_scrolled_window_new (NULL, NULL);
gtk_container_set_border_width (GTK_CONTAINER (interfaces_sw), 8);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (interfaces_sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
interfaces_list = gtk_tree_view_new ();
/* See maybe_identify_click below for what we're doing. */
g_signal_connect (interfaces_list, "button-press-event",
G_CALLBACK (maybe_identify_click), NULL);
gtk_widget_set_tooltip_markup (interfaces_list,
_("Left click on an interface name to flash "
"the light on the physical interface."));