From 69fc236243aca7a352b14be5dfce9461e57b9b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Date: Wed, 5 Feb 2014 15:10:05 +0100 Subject: [PATCH] LXC from native: convert phys network types to net hostdev devices --- src/lxc/lxc_native.c | 44 ++++++++++++++++--- .../lxcconf2xml-physnetwork.config | 6 +++ .../lxcconf2xml-physnetwork.xml | 26 +++++++++++ tests/lxcconf2xmltest.c | 1 + 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-physnetwork.config create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-physnetwork.xml diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index 734160ca31..2e4245535c 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -369,6 +369,26 @@ error: return NULL; } +static virDomainHostdevDefPtr +lxcCreateHostdevDef(int mode, int type, const char *data) +{ + virDomainHostdevDefPtr hostdev = virDomainHostdevDefAlloc(); + + if (!hostdev) + return NULL; + + hostdev->mode = mode; + hostdev->source.caps.type = type; + + if (type == VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET && + VIR_STRDUP(hostdev->source.caps.u.net.iface, data) < 0) { + virDomainHostdevDefFree(hostdev); + hostdev = NULL; + } + + return hostdev; +} + static int lxcAddNetworkDefinition(virDomainDefPtr def, const char *type, @@ -377,22 +397,36 @@ lxcAddNetworkDefinition(virDomainDefPtr def, const char *flag) { virDomainNetDefPtr net = NULL; + virDomainHostdevDefPtr hostdev = NULL; if ((type == NULL) || STREQ(type, "empty") || STREQ(type, "") || STREQ(type, "none")) return 0; - if (!(net = lxcCreateNetDef(type, link, mac, flag))) - goto error; + if (type != NULL && STREQ(type, "phys")) { + if (!link || + !(hostdev = lxcCreateHostdevDef(VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES, + VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET, + link))) + goto error; - if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0) - goto error; - def->nets[def->nnets - 1] = net; + if (VIR_EXPAND_N(def->hostdevs, def->nhostdevs, 1) < 0) + goto error; + def->hostdevs[def->nhostdevs - 1] = hostdev; + } else { + if (!(net = lxcCreateNetDef(type, link, mac, flag))) + goto error; + + if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0) + goto error; + def->nets[def->nnets - 1] = net; + } return 1; error: virDomainNetDefFree(net); + virDomainHostdevDefFree(hostdev); return -1; } diff --git a/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.config new file mode 100644 index 0000000000..63a4aa1e3e --- /dev/null +++ b/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.config @@ -0,0 +1,6 @@ +lxc.network.type = phys +lxc.network.link = eth0 + +lxc.rootfs = /var/lib/lxc/migrate_test/rootfs +lxc.utsname = migrate_test +lxc.autodev=1 diff --git a/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.xml new file mode 100644 index 0000000000..35a2a96233 --- /dev/null +++ b/tests/lxcconf2xmldata/lxcconf2xml-physnetwork.xml @@ -0,0 +1,26 @@ + + migrate_test + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 65536 + 0 + 1 + + exe + /sbin/init + + + destroy + restart + destroy + + + + + + + + eth0 + + + + diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c index 50041a591d..c888b42221 100644 --- a/tests/lxcconf2xmltest.c +++ b/tests/lxcconf2xmltest.c @@ -106,6 +106,7 @@ mymain(void) DO_TEST("fstab", true); DO_TEST("nonetwork", false); DO_TEST("nonenetwork", false); + DO_TEST("physnetwork", false); return ret; }