From nobody Mon Apr 29 07:40:02 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 14985833095501009.1174631608336; Tue, 27 Jun 2017 10:08:29 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9302CC04B946; Tue, 27 Jun 2017 17:08:25 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3CAB917153; Tue, 27 Jun 2017 17:08:25 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DCAD03FAE1; Tue, 27 Jun 2017 17:08:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5RH3cGr026670 for ; Tue, 27 Jun 2017 13:03:38 -0400 Received: by smtp.corp.redhat.com (Postfix) id D14FE17100; Tue, 27 Jun 2017 17:03:38 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C7FF417157 for ; Tue, 27 Jun 2017 17:03:36 +0000 (UTC) Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0331BC04B929 for ; Tue, 27 Jun 2017 17:03:25 +0000 (UTC) Received: by mail-pf0-f195.google.com with SMTP id s66so5499124pfs.2 for ; Tue, 27 Jun 2017 10:03:24 -0700 (PDT) Received: from venkat-ThinkPad-E450.domain.name ([103.69.20.53]) by smtp.gmail.com with ESMTPSA id y72sm5825455pfk.72.2017.06.27.10.03.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Jun 2017 10:03:22 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9302CC04B946 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9302CC04B946 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lILmAlbn" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0331BC04B929 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=nhvenkatdatta@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 0331BC04B929 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ojawWSMjYNri8P8T4Cl9xFPTSaD2vUsS30uYjYJvtnc=; b=lILmAlbnE675p5NCP8DNWiqYVTSNP3PZV18pHPe2+TaFXSiqy1s5sp3TR6BSL4k8lu TKuMyf2dzM64GskeA859BWQ6ULqOaDzD1LTqKlT29exLdOTMuICT0pVCLKI+tlX977f/ Pz2KBgaAfcuTYTWQgfncUSrCMk+nXRGC2HF6K8BuKerbhFDEpiCmMwW/poSektN+vaDG NulGpaaeSBUrEfphbq55d4YNit+JBbNr8EjklrbFqa5nrGPzSR5IDEAInn+quPoipn1D wh69d1EB1i4UvqCNwSnwr/VQrcLwh95CFZ1EUNu72qJq/eoeXjAjoJowvpPZQrCgkXdp wbWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ojawWSMjYNri8P8T4Cl9xFPTSaD2vUsS30uYjYJvtnc=; b=Eg7xapFS8eKS3hiTUUNauzPUmmsybV+09AjPKnNa9RuL1Ywr+cgakb3imicBBOJmnQ Oa2f+QMCWE4sdVBmheblHCQ2/uak8t/erVFydSfYzTWYz0Iw+gYhuCZQGB9yRZB8il6N X5y6Pbo7OyyEvr1Ith6q1Ry8yGRLE+uhlkAsHd0R3hKUV/YYV+cXZpEdBTPuhgw9f97Q jQwOTUvfhF3UurM8puD2uG1r3mlNISq0yqH6HIhnPv5tMrHceOvtLaPeHjCBdiJzU7p5 lZVSJc6PxyePltgz+QGK8EV4KHA4lLCJ0YEs71Fk8sz112X+hgNwoqGdyGac5kufN5Wh QJYQ== X-Gm-Message-State: AKS2vOzcpBxXVyZUfYuXvke2NtDheVH2gTGwfFEx/EMPPofj1qUOxRsv KgDZsAwLALPrjunl X-Received: by 10.84.224.206 with SMTP id k14mr6851042pln.72.1498583003854; Tue, 27 Jun 2017 10:03:23 -0700 (PDT) From: Venkat Datta N H To: libvir-list@redhat.com Date: Tue, 27 Jun 2017 22:32:41 +0530 Message-Id: <1498582962-12970-2-git-send-email-nhvenkatdatta@gmail.com> In-Reply-To: <1498582962-12970-1-git-send-email-nhvenkatdatta@gmail.com> References: <1498582962-12970-1-git-send-email-nhvenkatdatta@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 27 Jun 2017 17:03:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 27 Jun 2017 17:03:25 +0000 (UTC) for IP:'209.85.192.195' DOMAIN:'mail-pf0-f195.google.com' HELO:'mail-pf0-f195.google.com' FROM:'nhvenkatdatta@gmail.com' RCPT:'' X-RedHat-Spam-Score: 2.358 ** (BAYES_50, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, RCVD_IN_SORBS_SPAM, RCVD_IN_SORBS_WEB, SPF_PASS, T_DKIM_INVALID) 209.85.192.195 mail-pf0-f195.google.com 209.85.192.195 mail-pf0-f195.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Venkat Datta N H Subject: [libvirt] [PATCHv2 1/2] lxc: add support for docker-json Memory and VCPU conversion X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 27 Jun 2017 17:08:26 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Docker Memory and VCPU configuration is converted to fit for LXC container = XML configuration --- po/POTFILES.in | 1 + src/Makefile.am | 1 + src/lxc/lxc_docker.c | 116 ++++++++++++++++ src/lxc/lxc_docker.h | 32 +++++ src/lxc/lxc_driver.c | 13 +- src/lxc/lxc_native.h | 1 + tests/Makefile.am | 8 +- .../lxcdocker2xmldata-simple.json | 36 +++++ .../lxcdocker2xmldata/lxcdocker2xmldata-simple.xml | 15 +++ tests/lxcdocker2xmltest.c | 149 +++++++++++++++++= ++++ 10 files changed, 366 insertions(+), 6 deletions(-) create mode 100644 src/lxc/lxc_docker.c create mode 100644 src/lxc/lxc_docker.h create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.json create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.xml create mode 100644 tests/lxcdocker2xmltest.c diff --git a/po/POTFILES.in b/po/POTFILES.in index 275df1f..421b32e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -111,6 +111,7 @@ src/lxc/lxc_driver.c src/lxc/lxc_fuse.c src/lxc/lxc_hostdev.c src/lxc/lxc_native.c +src/lxc/lxc_docker.c src/lxc/lxc_process.c src/network/bridge_driver.c src/network/bridge_driver_linux.c diff --git a/src/Makefile.am b/src/Makefile.am index eae32dc..1341e5a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -839,6 +839,7 @@ LXC_DRIVER_SOURCES =3D \ lxc/lxc_process.c lxc/lxc_process.h \ lxc/lxc_fuse.c lxc/lxc_fuse.h \ lxc/lxc_native.c lxc/lxc_native.h \ + lxc/lxc_docker.c lxc/lxc_docker.h \ lxc/lxc_driver.c lxc/lxc_driver.h =20 LXC_CONTROLLER_SOURCES =3D \ diff --git a/src/lxc/lxc_docker.c b/src/lxc/lxc_docker.c new file mode 100644 index 0000000..dbb2a81 --- /dev/null +++ b/src/lxc/lxc_docker.c @@ -0,0 +1,116 @@ +/* + * lxc_docker.c: LXC native docker configuration import + * + * Copyright (C) 2017 Venkat Datta N H + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Venkat Datta N H + */ + +#include + +#include "util/viralloc.h" +#include "util/virfile.h" +#include "util/virstring.h" +#include "util/virconf.h" +#include "util/virjson.h" +#include "util/virutil.h" +#include "virerror.h" +#include "virlog.h" +#include "conf/domain_conf.h" +#include "lxc_docker.h" +#include "secret_conf.h" + +#define VIR_FROM_THIS VIR_FROM_LXC + +VIR_LOG_INIT("lxc.lxc_docker"); + +static int virLXCDockerParseCpu(virDomainDefPtr dom, + virDomainXMLOptionPtr xmlopt, + virJSONValuePtr prop) +{ + int vcpus; + + if (virJSONValueObjectGetNumberInt(prop, "CpuShares", &vcpus) !=3D 0) + return -1; + + if (virDomainDefSetVcpusMax(dom, vcpus, xmlopt) < 0) + return -1; + + if (virDomainDefSetVcpus(dom, vcpus) < 0) + return -1; + + return 0; +} + +static int virLXCDockerParseMem(virDomainDefPtr dom, + virJSONValuePtr prop) +{ + unsigned long long mem; + + if (virJSONValueObjectGetNumberUlong(prop, "Memory", &mem) !=3D 0) + return -1; + + virDomainDefSetMemoryTotal(dom, mem / 1024); + dom->mem.cur_balloon =3D mem / 1024; + + return 0; +} + +virDomainDefPtr virLXCDockerParseJSONConfig(virCapsPtr caps ATTRIBUTE_UNUS= ED, + virDomainXMLOptionPtr xmlopt, + const char *config) +{ + virJSONValuePtr json_obj; + virJSONValuePtr host_config; + + if (!(json_obj =3D virJSONValueFromString(config))) + return NULL; + + virDomainDefPtr def; + + if (!(def =3D virDomainDefNew())) + goto error; + + def->id =3D -1; + def->mem.cur_balloon =3D 64*1024; + virDomainDefSetMemoryTotal(def, def->mem.cur_balloon); + + if ((host_config =3D virJSONValueObjectGetObject(json_obj, "HostConfig= ")) !=3D NULL) { + if (virLXCDockerParseCpu(def, xmlopt, host_config) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to pars= e VCpu")); + goto error; + } + + if (virLXCDockerParseMem(def, host_config) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to pars= e Memory")); + goto error; + } + } + + def->clock.offset =3D VIR_DOMAIN_CLOCK_OFFSET_UTC; + def->onReboot =3D VIR_DOMAIN_LIFECYCLE_RESTART; + def->onCrash =3D VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY; + def->onPoweroff =3D VIR_DOMAIN_LIFECYCLE_DESTROY; + def->virtType =3D VIR_DOMAIN_VIRT_LXC; + def->os.type =3D VIR_DOMAIN_OSTYPE_EXE; + + return def; + + error: + virDomainDefFree(def); + return NULL; +} diff --git a/src/lxc/lxc_docker.h b/src/lxc/lxc_docker.h new file mode 100644 index 0000000..f081e07 --- /dev/null +++ b/src/lxc/lxc_docker.h @@ -0,0 +1,32 @@ +/* + * lxc_docker.h: Header file for LXC native docker configuration + * + * Copyright (C) 2017 Venkat Datta N H + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Venkat Datta N H + */ + +#ifndef __LXC_DOCKER_H__ +# define __LXC_DOCKER_H__ + +# include "domain_conf.h" + +virDomainDefPtr virLXCDockerParseJSONConfig(virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + const char *config); + +#endif /* __LXC_NATIVE_DOCKER_H__ */ diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 22c8b58..711bb16 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -53,6 +53,7 @@ #include "lxc_driver.h" #include "lxc_native.h" #include "lxc_process.h" +#include "lxc_docker.h" #include "viralloc.h" #include "virnetdevbridge.h" #include "virnetdevveth.h" @@ -1062,15 +1063,17 @@ static char *lxcConnectDomainXMLFromNative(virConne= ctPtr conn, if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0) goto cleanup; =20 - if (STRNEQ(nativeFormat, LXC_CONFIG_FORMAT)) { + if (STREQ(nativeFormat, DOCKER_CONFIG_FORMAT)) { + if (!(def =3D virLXCDockerParseJSONConfig(caps, driver->xmlopt, na= tiveConfig))) + goto cleanup; + } else if (STREQ(nativeFormat, LXC_CONFIG_FORMAT)) { + if (!(def =3D lxcParseConfigString(nativeConfig, caps, driver->xml= opt))) + goto cleanup; + } else { virReportError(VIR_ERR_INVALID_ARG, _("unsupported config type %s"), nativeFormat); goto cleanup; } - - if (!(def =3D lxcParseConfigString(nativeConfig, caps, driver->xmlopt)= )) - goto cleanup; - xml =3D virDomainDefFormat(def, caps, 0); =20 cleanup: diff --git a/src/lxc/lxc_native.h b/src/lxc/lxc_native.h index 15fa0d5..88263ae 100644 --- a/src/lxc/lxc_native.h +++ b/src/lxc/lxc_native.h @@ -26,6 +26,7 @@ # include "domain_conf.h" =20 # define LXC_CONFIG_FORMAT "lxc-tools" +# define DOCKER_CONFIG_FORMAT "docker" =20 virDomainDefPtr lxcParseConfigString(const char *config, virCapsPtr caps, diff --git a/tests/Makefile.am b/tests/Makefile.am index 19986dc..0948bfa 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -93,6 +93,7 @@ EXTRA_DIST =3D \ capabilityschemadata \ commanddata \ cputestdata \ + lxcdocker2xmldata \ domaincapsschemadata \ domainconfdata \ domainschemadata \ @@ -295,7 +296,7 @@ test_libraries +=3D libqemumonitortestutils.la \ endif WITH_QEMU =20 if WITH_LXC -test_programs +=3D lxcxml2xmltest lxcconf2xmltest +test_programs +=3D lxcxml2xmltest lxcconf2xmltest lxcdocker2xmltest endif WITH_LXC =20 if WITH_OPENVZ @@ -693,6 +694,11 @@ lxcconf2xmltest_SOURCES =3D \ lxcconf2xmltest.c testutilslxc.c testutilslxc.h \ testutils.c testutils.h lxcconf2xmltest_LDADD =3D $(lxc_LDADDS) + +lxcdocker2xmltest_SOURCES =3D \ + lxcdocker2xmltest.c testutilslxc.c testutilslxc.h \ + testutils.c testutils.h +lxcdocker2xmltest_LDADD =3D $(lxc_LDADDS) else ! WITH_LXC EXTRA_DIST +=3D lxcxml2xmltest.c testutilslxc.c testutilslxc.h endif ! WITH_LXC diff --git a/tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.json b/tests/= lxcdocker2xmldata/lxcdocker2xmldata-simple.json new file mode 100644 index 0000000..63470be --- /dev/null +++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.json @@ -0,0 +1,36 @@ +{ + "Id": "dbb1ae21dac15973d66e6c2b8516d270b32ca766e0cf7551d8b7973513e5f079", + "Created": "2017-05-25T18:55:17.922934825Z", + "Path": "/bin/bash", + "Args": [], + "HostConfig": { + "Binds": null, + "ContainerIDFile": "", + "LogConfig": { + "Type": "json-file", + "Config": {} + }, + "NetworkMode": "default", + "PortBindings": {}, + "ShmSize": 67108864, + "Runtime": "runc", + "Isolation": "", + "CpuShares": 2, + "Memory": 1073741824, + "CgroupParent": "", + "CpuPeriod": 0, + "CpuQuota": 0, + "CpusetCpus": "", + "CpusetMems": "", + "KernelMemory": 0, + "MemoryReservation": 0, + "MemorySwap": -1, + "MemorySwappiness": -1, + "PidsLimit": 0, + "Ulimits": null, + "CpuCount": 0, + "CpuPercent": 0, + "IOMaximumIOps": 0, + "IOMaximumBandwidth": 0 + } +} diff --git a/tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.xml b/tests/l= xcdocker2xmldata/lxcdocker2xmldata-simple.xml new file mode 100644 index 0000000..8be1ace --- /dev/null +++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-simple.xml @@ -0,0 +1,15 @@ + + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 1048576 + 1048576 + 2 + + exe + + + destroy + restart + destroy + + + diff --git a/tests/lxcdocker2xmltest.c b/tests/lxcdocker2xmltest.c new file mode 100644 index 0000000..ccac4c4 --- /dev/null +++ b/tests/lxcdocker2xmltest.c @@ -0,0 +1,149 @@ +/* + * lxcdocker2xmltest.c: Test LXC Docker Configuration + * + * Copyright (C) 2017 Venkat Datta N H + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Venkat Datta N H + */ + +#include + +#include "testutils.h" + +#ifdef WITH_LXC + +# include "lxc/lxc_docker.h" +# include "lxc/lxc_conf.h" +# include "testutilslxc.h" + +# define VIR_FROM_THIS VIR_FROM_NONE + +static virCapsPtr caps; +static virDomainXMLOptionPtr xmlopt; + +static int testSanitizeDef(virDomainDefPtr vmdef) +{ + /* Remove UUID randomness */ + if (virUUIDParse("c7a5fdbd-edaf-9455-926a-d65c16db1809", vmdef->uuid) = < 0) + return -1; + return 0; +} + +static int +testCompareXMLToConfigFiles(const char *xmlfile, + const char *configfile, + bool expectError) +{ + int ret =3D -1; + char *config =3D NULL; + char *actualxml =3D NULL; + virDomainDefPtr vmdef =3D NULL; + + if (virTestLoadFile(configfile, &config) < 0) + goto fail; + + vmdef =3D virLXCDockerParseJSONConfig(caps, xmlopt, config); + + if (vmdef && expectError) { + if (testSanitizeDef(vmdef) < 0) + goto fail; + + if (!(actualxml =3D virDomainDefFormat(vmdef, caps, 0))) + goto fail; + + if (virTestCompareToFile(actualxml, xmlfile) < 0) + goto fail; + } + + ret =3D 0; + + fail: + VIR_FREE(actualxml); + VIR_FREE(config); + virDomainDefFree(vmdef); + return ret; +} + +struct testInfo { + const char *name; + bool expectError; +}; + +static int +testCompareXMLToConfigHelper(const void *data) +{ + int result =3D -1; + const struct testInfo *info =3D data; + char *xml =3D NULL; + char *config =3D NULL; + + if (virAsprintf(&xml, "%s/lxcdocker2xmldata/lxcdocker2xmldata-%s.xml", + abs_srcdir, info->name) < 0 || + virAsprintf(&config, "%s/lxcdocker2xmldata/lxcdocker2xmldata-%s.js= on", + abs_srcdir, info->name) < 0) + goto cleanup; + + result =3D testCompareXMLToConfigFiles(xml, config, info->expectError); + + cleanup: + VIR_FREE(xml); + VIR_FREE(config); + return result; +} + +static int +mymain(void) +{ + int ret =3D EXIT_SUCCESS; + + if (!(caps =3D testLXCCapsInit())) + return EXIT_FAILURE; + + if (!(xmlopt =3D lxcDomainXMLConfInit())) { + virObjectUnref(caps); + return EXIT_FAILURE; + } + + +# define DO_TEST(name, expectError) \ + do { \ + const struct testInfo info =3D { name, expectError }; \ + if (virTestRun("DOCKER JSON-2-XML " name, \ + testCompareXMLToConfigHelper, \ + &info) < 0) \ + ret =3D EXIT_FAILURE; \ + } while (0) + + DO_TEST("simple", true); + + virObjectUnref(xmlopt); + virObjectUnref(caps); + + return ret; +} + +VIR_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_LXC */ --=20 2.7.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 07:40:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1498583293067882.9351086504018; Tue, 27 Jun 2017 10:08:13 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D16F43DBEB; Tue, 27 Jun 2017 17:08:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9268517100; Tue, 27 Jun 2017 17:08:06 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0403C3FADE; Tue, 27 Jun 2017 17:08:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5RH3aK9026662 for ; Tue, 27 Jun 2017 13:03:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id E19BD17CC0; Tue, 27 Jun 2017 17:03:36 +0000 (UTC) Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D9FEA95341 for ; Tue, 27 Jun 2017 17:03:34 +0000 (UTC) Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 49467FB9F for ; Tue, 27 Jun 2017 17:03:31 +0000 (UTC) Received: by mail-pf0-f196.google.com with SMTP id z6so5500969pfk.3 for ; Tue, 27 Jun 2017 10:03:31 -0700 (PDT) Received: from venkat-ThinkPad-E450.domain.name ([103.69.20.53]) by smtp.gmail.com with ESMTPSA id y72sm5825455pfk.72.2017.06.27.10.03.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Jun 2017 10:03:29 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D16F43DBEB Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D16F43DBEB Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="o90XwJeu" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 49467FB9F Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=nhvenkatdatta@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 49467FB9F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pGkix35BTJUCp1T60InwwAwZl5owa2pF+9Svw/JkqU8=; b=o90XwJeu2BRhL3CWcCPU9kwNr33Wt6GELYl42IcXiSYgGm6Q1RqkcSVSeKEc6EbzNQ z1wm6q47QhWTQI7AZj1x/Wq8YXXSTZl46U8It4vsDs1RHXTje+9DyTfz1hECHM3kLH6h +jt2N9fcoLYSSHSS0RBQft3QbWYLnJn2BeGF1JhnjyfhQfhAhj8sSPEDVgvQiF8eIo1g dRe/pyaFcHN0YvuDT3rch0Tbmz6agpndz+59IdePlRuAcxDRHn+x5/c+If1ZfH/PiEDW KiGx7O2IlDvg1wuXoIRcGOsDdyo4LFSABJ8fkU0xnRKJam+tMyZL670euWGQBj4KFqaE +KSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pGkix35BTJUCp1T60InwwAwZl5owa2pF+9Svw/JkqU8=; b=MBwFfjBpRVfZbgRGZ1QSW2rIqDIvTfeNwOOYArvoTzEblulF9Ho2vQhL7mMOvv8M5s OW/FDZauTVTzk3XLw4MxJNNJBbCgvXgnlU5xBIZBGkG+Pze8Jk5TkVTv05+l2Wamqsbu f/lIBowqDutXPl+hk1MQiTJ5T4g+RprpmXIZGFEFkXZzZF2jLVTOCqoOaQTP2nxUVnb0 EhX+61IP8yqlWTM9IyrY8XpmjGND1KEjjsbz+KnoAoPx4+giWsXIL5h0yGRdnTqvRYSt /Y2s/c9BUs/pz/aesRIy7f9nutkkmF3WLu6ADY/e8RgSkArM1putTa68W8epVyVGJiQW Ykgw== X-Gm-Message-State: AKS2vOxxP2p1HwJJgOH5/aUIg6/0xUzXkCEH+5lNtE3JYZf2go5PAGQJ OGt4diceEBhs8iWq X-Received: by 10.98.43.7 with SMTP id r7mr6183039pfr.202.1498583010165; Tue, 27 Jun 2017 10:03:30 -0700 (PDT) From: Venkat Datta N H To: libvir-list@redhat.com Date: Tue, 27 Jun 2017 22:32:42 +0530 Message-Id: <1498582962-12970-3-git-send-email-nhvenkatdatta@gmail.com> In-Reply-To: <1498582962-12970-1-git-send-email-nhvenkatdatta@gmail.com> References: <1498582962-12970-1-git-send-email-nhvenkatdatta@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 27 Jun 2017 17:03:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 27 Jun 2017 17:03:31 +0000 (UTC) for IP:'209.85.192.196' DOMAIN:'mail-pf0-f196.google.com' HELO:'mail-pf0-f196.google.com' FROM:'nhvenkatdatta@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.247 * (BAYES_50, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, RCVD_IN_SORBS_SPAM, RCVD_IN_SORBS_WEB, SPF_PASS) 209.85.192.196 mail-pf0-f196.google.com 209.85.192.196 mail-pf0-f196.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Venkat Datta N H Subject: [libvirt] [PATCHv2 2/2] lxc: add support for docker-json command conversion X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 27 Jun 2017 17:08:09 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Docker Json command is split between entrypoint and command properties. Com= pute a command out of those to fit the LXC container XML configuration. --- src/lxc/lxc_docker.c | 65 ++++++++++++++++++= ++++ .../lxcdocker2xmldata-command.json | 47 ++++++++++++++++ .../lxcdocker2xmldata-command.xml | 17 ++++++ tests/lxcdocker2xmltest.c | 1 + 4 files changed, 130 insertions(+) create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml diff --git a/src/lxc/lxc_docker.c b/src/lxc/lxc_docker.c index dbb2a81..b3e25f7 100644 --- a/src/lxc/lxc_docker.c +++ b/src/lxc/lxc_docker.c @@ -70,12 +70,70 @@ static int virLXCDockerParseMem(virDomainDefPtr dom, return 0; } =20 +struct virLXCDockerCmdArgsIteratorArgs { + virDomainDefPtr vmdef; + size_t ninitargs; +}; + +static int virLXCDockerCmdArgsIterator(size_t pos ATTRIBUTE_UNUSED, + virJSONValuePtr item, + void *opaque) +{ + struct virLXCDockerCmdArgsIteratorArgs *args =3D opaque; + const char *value =3D virJSONValueGetString(item); + + if (!args->vmdef->os.init) { + if (VIR_STRDUP(args->vmdef->os.init, value) < 0) + return -1; + else + return 1; + } + + if (VIR_EXPAND_N(args->vmdef->os.initargv, args->ninitargs, 1) < 0) + return -1; + + if (VIR_STRDUP(args->vmdef->os.initargv[args->ninitargs - 1], value) <= 0) + return -1; + + return 1; +} + + +static int virLXCDockerBuildInitCmd(virDomainDefPtr vmdef, + virJSONValuePtr config) +{ + virJSONValuePtr entry_point =3D virJSONValueObjectGetArray(config, "En= trypoint"); + virJSONValuePtr command =3D virJSONValueObjectGetArray(config, "Cmd"); + struct virLXCDockerCmdArgsIteratorArgs iterator_args =3D { vmdef, 0 }; + + if (entry_point && virJSONValueArrayForeachSteal(entry_point, + &virLXCDockerCmdArgsI= terator, + &iterator_args) < 0) + goto error; + + if (command && virJSONValueArrayForeachSteal(command, + &virLXCDockerCmdArgsItera= tor, + &iterator_args) < 0) + goto error; + + /* Append NULL element at the end */ + if (iterator_args.ninitargs > 0 && + VIR_EXPAND_N(vmdef->os.initargv, iterator_args.ninitargs, 1) < 0) + goto error; + + return 0; + + error: + return -1; +} + virDomainDefPtr virLXCDockerParseJSONConfig(virCapsPtr caps ATTRIBUTE_UNUS= ED, virDomainXMLOptionPtr xmlopt, const char *config) { virJSONValuePtr json_obj; virJSONValuePtr host_config; + virJSONValuePtr docker_config; =20 if (!(json_obj =3D virJSONValueFromString(config))) return NULL; @@ -101,6 +159,13 @@ virDomainDefPtr virLXCDockerParseJSONConfig(virCapsPtr= caps ATTRIBUTE_UNUSED, } } =20 + if ((docker_config =3D virJSONValueObjectGetObject(json_obj, "Config")= ) !=3D NULL) { + if (virLXCDockerBuildInitCmd(def, docker_config) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to pars= e Command")); + goto error; + } + } + def->clock.offset =3D VIR_DOMAIN_CLOCK_OFFSET_UTC; def->onReboot =3D VIR_DOMAIN_LIFECYCLE_RESTART; def->onCrash =3D VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY; diff --git a/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json b/tests= /lxcdocker2xmldata/lxcdocker2xmldata-command.json new file mode 100644 index 0000000..14e4d92 --- /dev/null +++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json @@ -0,0 +1,47 @@ +{ + "Id": "618a2e773b84976ca2736e443a0c98eeeecdcfb2584e3dab1a3cafa313a= 936da", + "Created": "2017-06-07T00:46:07.149971669Z", + "Path": "/bin/ping", + "Args": [ + "localhost" + ], + "HostConfig": { + "LogConfig": { + "Type": "json-file", + "Config": {} + }, + "NetworkMode": "default", + "PortBindings": {}, + "RestartPolicy": { + "Name": "no", + "MaximumRetryCount": 0 + }, + "ShmSize": 67108864, + "CpuShares": 0, + "Memory": 0, + "MemorySwap": 0, + "MemorySwappiness": -1, + "CpuCount": 0, + "CpuPercent": 0, + "IOMaximumBandwidth": 0 + }, + "GraphDriver": { + "Name": "aufs", + "Data": null + }, + "Mounts": [], + "Config": { + "Hostname": "618a2e773b84", + "Domainname": "", + "User": "", + "Cmd": [ + "/bin/ping", + "localhost" + ], + "WorkingDir": "", + "Entrypoint": null, + "OnBuild": null, + "Labels": {} + } + +} diff --git a/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml b/tests/= lxcdocker2xmldata/lxcdocker2xmldata-command.xml new file mode 100644 index 0000000..52155d1 --- /dev/null +++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml @@ -0,0 +1,17 @@ + + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 0 + 0 + 0 + + exe + /bin/ping + localhost + + + destroy + restart + destroy + + + diff --git a/tests/lxcdocker2xmltest.c b/tests/lxcdocker2xmltest.c index ccac4c4..dc7778a 100644 --- a/tests/lxcdocker2xmltest.c +++ b/tests/lxcdocker2xmltest.c @@ -129,6 +129,7 @@ mymain(void) } while (0) =20 DO_TEST("simple", true); + DO_TEST("command", true); =20 virObjectUnref(xmlopt); virObjectUnref(caps); --=20 2.7.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list