]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
LXC: set IP addresses to veth devices in the container
authorCédric Bosdonnat <cbosdonnat@suse.com>
Tue, 22 Jul 2014 11:35:48 +0000 (13:35 +0200)
committerCédric Bosdonnat <cbosdonnat@suse.com>
Mon, 5 Jan 2015 19:24:17 +0000 (20:24 +0100)
Uses the new virDomainNetDef ips to set the IP addresses on the network
interfaces in the container.

src/lxc/lxc_container.c
src/util/virsocketaddr.h

index 1b9e2f2da643daae402668cef5c68a255f749002..6152df8513b090dcf3f1f784a0801446d1828c29 100644 (file)
@@ -495,7 +495,7 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
                                                  char **veths)
 {
     int rc = 0;
-    size_t i;
+    size_t i, j;
     char *newname = NULL;
     virDomainNetDefPtr netDef;
     bool privNet = vmDef->features[VIR_DOMAIN_FEATURE_PRIVNET] ==
@@ -516,6 +516,24 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
         if (rc < 0)
             goto error_out;
 
+        for (j = 0; j < netDef->nips; j++) {
+            virDomainNetIpDefPtr ip = netDef->ips[j];
+            unsigned int prefix = (ip->prefix > 0) ? ip->prefix :
+                                  VIR_SOCKET_ADDR_DEFAULT_PREFIX;
+            char *ipStr = virSocketAddrFormat(&ip->address);
+
+            VIR_DEBUG("Adding IP address '%s/%u' to '%s'",
+                      ipStr, ip->prefix, newname);
+            if (virNetDevSetIPAddress(newname, &ip->address, prefix) < 0) {
+                virReportError(VIR_ERR_SYSTEM_ERROR,
+                               _("Failed to set IP address '%s' on %s"),
+                               ipStr, newname);
+                VIR_FREE(ipStr);
+                goto error_out;
+            }
+            VIR_FREE(ipStr);
+        }
+
         VIR_DEBUG("Enabling %s", newname);
         rc = virNetDevSetOnline(newname, true);
         if (rc < 0)
index 053855be4ceb42d246bcc62821d0d727b5348557..ba8c912971c8fc5f0388713695f3146922a662dc 100644 (file)
@@ -54,6 +54,8 @@ typedef struct {
 # define VIR_SOCKET_ADDR_FAMILY(s)              \
     ((s)->data.sa.sa_family)
 
+# define VIR_SOCKET_ADDR_DEFAULT_PREFIX 24
+
 typedef virSocketAddr *virSocketAddrPtr;
 
 typedef struct _virSocketAddrRange virSocketAddrRange;