Codebase list virt-viewer / 61292f3
virt-viewer: Allow more precise VM selection Theoretically a VM name can be a valid VM id or uuid. In that case connecting to the VMs may be problematic since virt-viewer selects the VM by its id then by uuid if not found then by its name. Introduce new command line options to cover this situation: "--id" to connect to the VM by its id "--uuid" to connect to the VM by its uuid "--domain-name" to connect to the VM by its name The options are mutually exclusive Resolves: rhbz#1399077 Pavel Grunt 7 years ago
2 changed file(s) with 83 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
121121 instead. Please note that --reconnect takes precedence over this
122122 option, and will attempt to do a reconnection before it quits.
123123
124 =item --id, --uuid, --domain-name
125
126 Connect to the virtual machine by its id, uuid or name. These options
127 are mutual exclusive. For example the following command may sometimes
128 connect to a virtual machine with the id 2 or with the name 2 (depending
129 on the number of running machines):
130
131 virt-viewer 2
132
133 To always connect to the virtual machine with the name "2" use the
134 "--domain-name" option:
135
136 virt-viewer --domain-name 2
137
124138 =back
125139
126140 =head1 CONFIGURATION
8181 static gboolean opt_waitvm = FALSE;
8282 static gboolean opt_reconnect = FALSE;
8383
84 typedef enum {
85 DOMAIN_SELECTION_ID = (1 << 0),
86 DOMAIN_SELECTION_UUID = (1 << 1),
87 DOMAIN_SELECTION_NAME = (1 << 2),
88 DOMAIN_SELECTION_DEFAULT = DOMAIN_SELECTION_ID | DOMAIN_SELECTION_UUID | DOMAIN_SELECTION_NAME,
89 } DomainSelection;
90
91 static const gchar* domain_selection_to_opt[] = {
92 [DOMAIN_SELECTION_ID] = "--id",
93 [DOMAIN_SELECTION_UUID] = "--uuid",
94 [DOMAIN_SELECTION_NAME] = "--domain-name",
95 };
96
97 static DomainSelection domain_selection_type = DOMAIN_SELECTION_DEFAULT;
98
99 static gboolean
100 opt_domain_selection_cb(const gchar *option_name,
101 const gchar *value G_GNUC_UNUSED,
102 gpointer data G_GNUC_UNUSED,
103 GError **error)
104 {
105 guint i;
106 if (domain_selection_type != DOMAIN_SELECTION_DEFAULT) {
107 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
108 "selection type has been already set");
109 return FALSE;
110 }
111
112 for (i = DOMAIN_SELECTION_ID; i <= G_N_ELEMENTS(domain_selection_to_opt); i++) {
113 if (g_strcmp0(option_name, domain_selection_to_opt[i]) == 0) {
114 domain_selection_type = i;
115 return TRUE;
116 }
117 }
118
119 g_assert_not_reached();
120 return FALSE;
121 }
122
84123 static void
85124 virt_viewer_add_option_entries(VirtViewerApp *self, GOptionContext *context, GOptionGroup *group)
86125 {
95134 N_("Wait for domain to start"), NULL },
96135 { "reconnect", 'r', 0, G_OPTION_ARG_NONE, &opt_reconnect,
97136 N_("Reconnect to domain upon restart"), NULL },
137 { "domain-name", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
138 N_("Select the virtual machine only by its name"), NULL },
139 { "id", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
140 N_("Select the virtual machine only by its id"), NULL },
141 { "uuid", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
142 N_("Select the virtual machine only by its uuid"), NULL },
98143 { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_args,
99144 NULL, "-- DOMAIN-NAME|ID|UUID" },
100145 { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
130175 }
131176
132177
133 if (opt_waitvm) {
178 if (opt_waitvm || domain_selection_type != DOMAIN_SELECTION_DEFAULT) {
134179 if (!self->priv->domkey) {
135 g_printerr(_("\nNo DOMAIN-NAME|ID|UUID was specified for '--wait'\n\n"));
180 g_printerr(_("\nNo DOMAIN-NAME|ID|UUID was specified for '%s'\n\n"),
181 opt_waitvm ? "--wait" : domain_selection_to_opt[domain_selection_type]);
136182 ret = TRUE;
137183 *status = 1;
138184 goto end;
139185 }
140186
141 self->priv->waitvm = TRUE;
187 self->priv->waitvm = opt_waitvm;
142188 }
143189
144190 virt_viewer_app_set_direct(app, opt_direct);
302348 {
303349 char *end;
304350 VirtViewerPrivate *priv = self->priv;
305 int id;
306351 virDomainPtr dom = NULL;
307 unsigned char uuid[16];
308352
309353 if (priv->domkey == NULL) {
310354 return NULL;
311355 }
312356
313 id = strtol(priv->domkey, &end, 10);
314 if (id >= 0 && end && !*end) {
315 dom = virDomainLookupByID(priv->conn, id);
316 }
317 if (!dom && virt_viewer_parse_uuid(priv->domkey, uuid) == 0) {
318 dom = virDomainLookupByUUID(priv->conn, uuid);
319 }
320 if (!dom) {
321 dom = virDomainLookupByName(priv->conn, priv->domkey);
322 }
357 if (domain_selection_type & DOMAIN_SELECTION_ID) {
358 long int id = strtol(priv->domkey, &end, 10);
359 if (id >= 0 && end && !*end) {
360 dom = virDomainLookupByID(priv->conn, id);
361 }
362 }
363
364 if (domain_selection_type & DOMAIN_SELECTION_UUID) {
365 unsigned char uuid[16];
366 if (dom == NULL && virt_viewer_parse_uuid(priv->domkey, uuid) == 0) {
367 dom = virDomainLookupByUUID(priv->conn, uuid);
368 }
369 }
370
371 if (domain_selection_type & DOMAIN_SELECTION_NAME) {
372 if (dom == NULL) {
373 dom = virDomainLookupByName(priv->conn, priv->domkey);
374 }
375 }
376
323377 return dom;
324378 }
325379