[Libvirt-cim] [PATCH] Add default network card existence checking

Xu Wang gesaint at linux.vnet.ibm.com
Thu Aug 1 01:27:29 UTC 2013


From: Xu Wang <cngesaint at gmail.com>

The default network card (used as forward device in network pool) name
was set as "eth0" in Virt_SettingDefineCapabilities.c. This patch added
check if there is such a network card exists in the network info list.
If it exists, use it. If not, the default network card would be changed
into the first available one except lo.

Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/misc_util.c                 |   38 +++++++++++++++++++++++++++++++++
 libxkutil/misc_util.h                 |    2 +-
 src/Virt_SettingsDefineCapabilities.c |    8 ++++++-
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/libxkutil/misc_util.c b/libxkutil/misc_util.c
index 9e7e0d5..ada664c 100644
--- a/libxkutil/misc_util.c
+++ b/libxkutil/misc_util.c
@@ -900,6 +900,44 @@ int virt_set_status(const CMPIBroker *broker,
         return ret;
 }
 
+char *get_avai_net(char *def_name)
+{
+    char buf[512];
+    FILE *pp;
+    char *delims = ": ";
+    char *sub_str = NULL;
+    char *avai_net = NULL;
+    bool avai_found = false;
+
+    pp = popen("ip addr", "r");
+    if (!pp) {
+        CU_DEBUG("popen() error.\n");
+        return NULL;
+    }
+
+    while (fgets(buf, sizeof(buf), pp)) {
+        sub_str = strtok(buf, delims);
+        while (sub_str != NULL) {
+            if (!strncmp(sub_str, "eth", 3) || !strncmp(sub_str, "em", 2)) {
+                if (!strcmp(def_name, sub_str)) {
+                    avai_net = strdup(sub_str);
+                    goto out;
+                }
+
+                if (!avai_found) {
+                    avai_net = strdup(sub_str);
+                    avai_found = true;
+                }
+            }
+            sub_str = strtok(NULL, delims);
+        }
+    }
+    pclose(pp);
+
+out:
+    return avai_net;
+}
+
 
 /*
  * Local Variables:
diff --git a/libxkutil/misc_util.h b/libxkutil/misc_util.h
index fd4f191..fc63000 100644
--- a/libxkutil/misc_util.h
+++ b/libxkutil/misc_util.h
@@ -157,7 +157,7 @@ const char *get_mig_ssh_tmp_key(void);
 bool get_disable_kvm(void);
 const char *get_lldptool_query_options(void);
 const char *get_vsi_support_key_string(void);
-
+char *get_avai_net(char *def_name);
 /*
  * Local Variables:
  * mode: C
diff --git a/src/Virt_SettingsDefineCapabilities.c b/src/Virt_SettingsDefineCapabilities.c
index 78c128c..09ae49f 100644
--- a/src/Virt_SettingsDefineCapabilities.c
+++ b/src/Virt_SettingsDefineCapabilities.c
@@ -777,6 +777,7 @@ static CMPIStatus set_net_pool_props(const CMPIObjectPath *ref,
         int dev_count;
         int i;
         char *tmp_str = NULL;
+        char *forward_device = "eth0";
 
         /* Isolated network pools don't have a forward device */
         if (pool_type == NETPOOL_FORWARD_NONE)
@@ -836,8 +837,13 @@ static CMPIStatus set_net_pool_props(const CMPIObjectPath *ref,
                               (CMPIValue *)&pool_type, CMPI_uint16);
 
                 if (i == 1) {
+                        forward_device = get_avai_net(forward_device);
+                        if (!forward_device) {
+                            goto out;
+                        }
+
                         CMSetProperty(inst, "ForwardDevice",
-                                      (CMPIValue *)"eth0", CMPI_chars);
+                                      (CMPIValue *)forward_device, CMPI_chars);
                 }
 
                 inst_list_add(list, inst);
-- 
1.7.1




More information about the Libvirt-cim mailing list