Blame SOURCES/0025-virt-viewer-Allow-more-precise-VM-selection.patch

140d58
From efb365492c540a175243d746e4d18a2c58c2c701 Mon Sep 17 00:00:00 2001
140d58
From: Pavel Grunt <pgrunt@redhat.com>
140d58
Date: Fri, 3 Mar 2017 14:08:45 +0100
140d58
Subject: [PATCH 25/26] virt-viewer: Allow more precise VM selection
140d58
140d58
Theoretically a VM name can be a valid VM id or uuid. In that case
140d58
connecting to the VMs may be problematic since virt-viewer selects
140d58
the VM by its id then by uuid if not found then by its name.
140d58
140d58
Introduce new command line options to cover this situation:
140d58
 "--id" to connect to the VM by its id
140d58
 "--uuid" to connect to the VM by its uuid
140d58
 "--domain-name" to connect to the VM by its name
140d58
The options are mutually exclusive
140d58
140d58
Resolves: rhbz#1399077
140d58
---
140d58
 man/virt-viewer.pod | 14 ++++++++++
140d58
 src/virt-viewer.c   | 78 ++++++++++++++++++++++++++++++++++++++++++++---------
140d58
 2 files changed, 80 insertions(+), 12 deletions(-)
140d58
140d58
diff --git a/man/virt-viewer.pod b/man/virt-viewer.pod
140d58
index 9abf03c..30af8db 100644
140d58
--- a/man/virt-viewer.pod
140d58
+++ b/man/virt-viewer.pod
140d58
@@ -122,6 +122,20 @@ kiosk-quit option to "on-disconnect" value, virt-viewer will quit
140d58
 instead. Please note that --reconnect takes precedence over this
140d58
 option, and will attempt to do a reconnection before it quits.
140d58
 
140d58
+=item --id, --uuid, --domain-name
140d58
+
140d58
+Connect to the virtual machine by its id, uuid or name. These options
140d58
+are mutual exclusive. For example the following command may sometimes
140d58
+connect to a virtual machine with the id 2 or with the name 2 (depending
140d58
+on the number of running machines):
140d58
+
140d58
+    virt-viewer 2
140d58
+
140d58
+To always connect to the virtual machine with the name "2" use the
140d58
+"--domain-name" option:
140d58
+
140d58
+    virt-viewer --domain-name 2
140d58
+
140d58
 =back
140d58
 
140d58
 =head1 CONFIGURATION
140d58
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
140d58
index 1f99552..22a952a 100644
140d58
--- a/src/virt-viewer.c
140d58
+++ b/src/virt-viewer.c
140d58
@@ -82,6 +82,45 @@ static gboolean opt_attach = FALSE;
140d58
 static gboolean opt_waitvm = FALSE;
140d58
 static gboolean opt_reconnect = FALSE;
140d58
 
140d58
+typedef enum {
140d58
+    DOMAIN_SELECTION_ID = (1 << 0),
140d58
+    DOMAIN_SELECTION_UUID = (1 << 1),
140d58
+    DOMAIN_SELECTION_NAME = (1 << 2),
140d58
+    DOMAIN_SELECTION_DEFAULT = DOMAIN_SELECTION_ID | DOMAIN_SELECTION_UUID | DOMAIN_SELECTION_NAME,
140d58
+} DomainSelection;
140d58
+
140d58
+static const gchar* domain_selection_to_opt[] = {
140d58
+    [DOMAIN_SELECTION_ID] = "--id",
140d58
+    [DOMAIN_SELECTION_UUID] = "--uuid",
140d58
+    [DOMAIN_SELECTION_NAME] = "--domain-name",
140d58
+};
140d58
+
140d58
+static DomainSelection domain_selection_type = DOMAIN_SELECTION_DEFAULT;
140d58
+
140d58
+static gboolean
140d58
+opt_domain_selection_cb(const gchar *option_name,
140d58
+                        const gchar *value G_GNUC_UNUSED,
140d58
+                        gpointer data G_GNUC_UNUSED,
140d58
+                        GError **error)
140d58
+{
140d58
+    guint i;
140d58
+    if (domain_selection_type != DOMAIN_SELECTION_DEFAULT) {
140d58
+        g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
140d58
+                    "selection type has been already set");
140d58
+        return FALSE;
140d58
+    }
140d58
+
140d58
+    for (i = DOMAIN_SELECTION_ID; i <= G_N_ELEMENTS(domain_selection_to_opt); i++) {
140d58
+        if (g_strcmp0(option_name, domain_selection_to_opt[i]) == 0) {
140d58
+            domain_selection_type = i;
140d58
+            return TRUE;
140d58
+        }
140d58
+    }
140d58
+
140d58
+    g_assert_not_reached();
140d58
+    return FALSE;
140d58
+}
140d58
+
140d58
 static void
140d58
 virt_viewer_add_option_entries(VirtViewerApp *self, GOptionContext *context, GOptionGroup *group)
140d58
 {
140d58
@@ -96,6 +135,12 @@ virt_viewer_add_option_entries(VirtViewerApp *self, GOptionContext *context, GOp
140d58
           N_("Wait for domain to start"), NULL },
140d58
         { "reconnect", 'r', 0, G_OPTION_ARG_NONE, &opt_reconnect,
140d58
           N_("Reconnect to domain upon restart"), NULL },
140d58
+        { "domain-name", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
140d58
+          N_("Select the virtual machine only by its name"), NULL },
140d58
+        { "id", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
140d58
+          N_("Select the virtual machine only by its id"), NULL },
140d58
+        { "uuid", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, opt_domain_selection_cb,
140d58
+          N_("Select the virtual machine only by its uuid"), NULL },
140d58
         { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_args,
140d58
           NULL, "-- DOMAIN-NAME|ID|UUID" },
140d58
         { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
140d58
@@ -131,15 +176,16 @@ virt_viewer_local_command_line (GApplication   *gapp,
140d58
     }
140d58
 
140d58
 
140d58
-    if (opt_waitvm) {
140d58
+    if (opt_waitvm || domain_selection_type != DOMAIN_SELECTION_DEFAULT) {
140d58
         if (!self->priv->domkey) {
140d58
-            g_printerr(_("\nNo DOMAIN-NAME|ID|UUID was specified for '--wait'\n\n"));
140d58
+            g_printerr(_("\nNo DOMAIN-NAME|ID|UUID was specified for '%s'\n\n"),
140d58
+                       opt_waitvm ? "--wait" : domain_selection_to_opt[domain_selection_type]);
140d58
             ret = TRUE;
140d58
             *status = 1;
140d58
             goto end;
140d58
         }
140d58
 
140d58
-        self->priv->waitvm = TRUE;
140d58
+        self->priv->waitvm = opt_waitvm;
140d58
     }
140d58
 
140d58
     virt_viewer_app_set_direct(app, opt_direct);
140d58
@@ -303,24 +349,32 @@ virt_viewer_lookup_domain(VirtViewer *self)
140d58
 {
140d58
     char *end;
140d58
     VirtViewerPrivate *priv = self->priv;
140d58
-    int id;
140d58
     virDomainPtr dom = NULL;
140d58
-    unsigned char uuid[16];
140d58
 
140d58
     if (priv->domkey == NULL) {
140d58
         return NULL;
140d58
     }
140d58
 
140d58
-    id = strtol(priv->domkey, &end, 10);
140d58
-    if (id >= 0 && end && !*end) {
140d58
-        dom = virDomainLookupByID(priv->conn, id);
140d58
+    if (domain_selection_type & DOMAIN_SELECTION_ID) {
140d58
+        long int id = strtol(priv->domkey, &end, 10);
140d58
+        if (id >= 0 && end && !*end) {
140d58
+            dom = virDomainLookupByID(priv->conn, id);
140d58
+        }
140d58
     }
140d58
-    if (!dom && virt_viewer_parse_uuid(priv->domkey, uuid) == 0) {
140d58
-        dom = virDomainLookupByUUID(priv->conn, uuid);
140d58
+
140d58
+    if (domain_selection_type & DOMAIN_SELECTION_UUID) {
140d58
+        unsigned char uuid[16];
140d58
+        if (dom == NULL && virt_viewer_parse_uuid(priv->domkey, uuid) == 0) {
140d58
+            dom = virDomainLookupByUUID(priv->conn, uuid);
140d58
+        }
140d58
     }
140d58
-    if (!dom) {
140d58
-        dom = virDomainLookupByName(priv->conn, priv->domkey);
140d58
+
140d58
+    if (domain_selection_type & DOMAIN_SELECTION_NAME) {
140d58
+        if (dom == NULL) {
140d58
+            dom = virDomainLookupByName(priv->conn, priv->domkey);
140d58
+        }
140d58
     }
140d58
+
140d58
     return dom;
140d58
 }
140d58
 
140d58
-- 
140d58
2.12.0
140d58