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

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