]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Factor qemuBuildNicStr() out from qemuBuildCommandLine()
authorMark McLoughlin <markmc@redhat.com>
Fri, 17 Jul 2009 21:08:33 +0000 (22:08 +0100)
committerMark McLoughlin <markmc@redhat.com>
Wed, 22 Jul 2009 10:34:06 +0000 (11:34 +0100)
Re-factor this code so that it can be used for NIC hotplug
too. The awkward arguments are needed to allow use to do
"pci_add auto nic macaddr=..."

* src/qemu_conf.c: factor the nic string formatting code into
  its own function

src/qemu_conf.c

index 4043d706afd8ce6b4f6aeae7fbd8d94d85040611..679dac3d69ba127c46397c8a09180a47935d9ef2 100644 (file)
@@ -833,6 +833,30 @@ qemudNetworkIfaceConnect(virConnectPtr conn,
     return NULL;
 }
 
+static int
+qemuBuildNicStr(virConnectPtr conn,
+                virDomainNetDefPtr net,
+                const char *prefix,
+                char type_sep,
+                int vlan,
+                char **str)
+{
+    if (virAsprintf(str,
+                    "%snic%cmacaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s",
+                    prefix ? prefix : "",
+                    type_sep,
+                    net->mac[0], net->mac[1],
+                    net->mac[2], net->mac[3],
+                    net->mac[4], net->mac[5],
+                    vlan,
+                    (net->model ? ",model=" : ""),
+                    (net->model ? net->model : "")) < 0) {
+        virReportOOMError(conn);
+        return -1;
+    }
+
+    return 0;
+}
 
 static int qemudBuildCommandLineChrDevStr(virDomainChrDefPtr dev,
                                           char *buf,
@@ -1366,21 +1390,14 @@ int qemudBuildCommandLine(virConnectPtr conn,
     } else {
         int vlan = 0;
         for (i = 0 ; i < def->nnets ; i++) {
-            char nic[100];
             virDomainNetDefPtr net = def->nets[i];
+            char *nic;
 
-            if (snprintf(nic, sizeof(nic),
-                         "nic,macaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s",
-                         net->mac[0], net->mac[1],
-                         net->mac[2], net->mac[3],
-                         net->mac[4], net->mac[5],
-                         vlan,
-                         (net->model ? ",model=" : ""),
-                         (net->model ? net->model : "")) >= sizeof(nic))
+            if (qemuBuildNicStr(conn, net, NULL, ',', vlan, &nic) < 0)
                 goto error;
 
             ADD_ARG_LIT("-net");
-            ADD_ARG_LIT(nic);
+            ADD_ARG(nic);
             ADD_ARG_LIT("-net");
 
             switch (net->type) {