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

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