From nobody Wed May 14 19:30:51 2025 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.zohomail.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; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1523446251405416.2380044944657; Wed, 11 Apr 2018 04:30:51 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 017203130F8D; Wed, 11 Apr 2018 11:30:50 +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 BD7CB5E1AC; Wed, 11 Apr 2018 11:30:49 +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 69F924CA9F; Wed, 11 Apr 2018 11:30:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3BBTeSL029833 for ; Wed, 11 Apr 2018 07:29:40 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0DA35863D0; Wed, 11 Apr 2018 11:29:40 +0000 (UTC) Received: from mx1.redhat.com (ext-mx13.extmail.prod.ext.phx2.redhat.com [10.5.110.42]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 43D3D88849; Wed, 11 Apr 2018 11:29:37 +0000 (UTC) Received: from mail-wr0-f180.google.com (mail-wr0-f180.google.com [209.85.128.180]) (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 1A35C3133C08; Wed, 11 Apr 2018 11:29:36 +0000 (UTC) Received: by mail-wr0-f180.google.com with SMTP id d1so1410319wrj.13; Wed, 11 Apr 2018 04:29:36 -0700 (PDT) Received: from rst.Home ([2a02:c7d:692a:c500:1ebc:73f3:5f60:b131]) by smtp.gmail.com with ESMTPSA id 185sm1502933wmj.46.2018.04.11.04.29.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Apr 2018 04:29:33 -0700 (PDT) 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=io4O6YinP1AXuYtTPSYyItG3huGS5pi7U8MvJlPIaNo=; b=vNxOKaZrgGyhujCb+qkm2qxuf3yRQ9i/MrunuQb27Z/OBD4Qz6s4q9BXaKYlZe8X09 qq20fBhsXWTtrYeQahzm0uDVOky4OwWrmeI0QEHD7/ArCH5vj3kYFpcrW6AAj+mCqr/9 0H1/m/9QzqS39DYY3daw4beqf+wixW2egfK/IRVR37FDzfM4d68cP0J8IQL91expH4nf Qhhi907GvUQV82r8Z/ZEo3MZrnM6X5EwWCWcEAvQNWsjzkB3dfLuKx7IPV+Fv0ZtZsEE HsGkWKauLvI6sfO1ClHrEnM2No/BAgjCi04zMCpyR26TsAgK4bkaorCSMNKn+fhpundg vo3A== 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=io4O6YinP1AXuYtTPSYyItG3huGS5pi7U8MvJlPIaNo=; b=OyM7waWTaCo1L48y8UVvybo4/BvmLA9RAFSZ8VyTNS7GFhdtdfmZ7SbzDbsjNq7853 kq42Y9dfeeaUruum2Mc7aANG1RvxEl0qaryYu8Fu0SKxJ5C2Rr4xAGr+v5wCPy/BBPPI tX8kUBIThHuzUBQ3Kq76gQQzdhZp5XcQHjsaLFefQkRucc0YfQVWWw4Gg19m7lYbjFFR zVlL/tu44ctsIwgkfIA6Vh/wuufbqASptsw/YipJe3TVhl9t4Rx0737x317YOJF5nMRO WlXtPrO1p7W6S/YHW7reexyjiEND94h7/EyVltJZA4cxN8Kllbl2s0mJUqRIt8z5aL5V Cbsw== X-Gm-Message-State: ALQs6tB8bZIRgY+G9AnVaQcwPh2q/SazqXuc3LbVLU+QG5ni/LOhEwAZ JXMhM0eG8L4JUyZ7e/fO+x7b5yvq X-Google-Smtp-Source: AIpwx49Iq0eSnvjsJ0xVyLqEZwDBhRIitPicNOAOQCnjkTO4LNfD6pGB+AKlMn8hfEfH31I7WqTlXw== X-Received: by 10.223.153.244 with SMTP id y107mr1613988wrb.215.1523446174460; Wed, 11 Apr 2018 04:29:34 -0700 (PDT) From: Radostin Stoyanov To: libvir-list@redhat.com Date: Wed, 11 Apr 2018 12:29:15 +0100 Message-Id: <20180411112915.5890-5-rstoyanov1@gmail.com> In-Reply-To: <20180411112915.5890-1-rstoyanov1@gmail.com> References: <20180411112915.5890-1-rstoyanov1@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 11 Apr 2018 11:29:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 11 Apr 2018 11:29:36 +0000 (UTC) for IP:'209.85.128.180' DOMAIN:'mail-wr0-f180.google.com' HELO:'mail-wr0-f180.google.com' FROM:'rstoyanov1@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.139 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.128.180 mail-wr0-f180.google.com 209.85.128.180 mail-wr0-f180.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.42 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: kkoukiou@redhat.com, mprivozn@redhat.com, cbosdonnat@suse.com, Radostin Stoyanov Subject: [libvirt] [RFC PATCH 4/4] lxc: Add save/restore support 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 11 Apr 2018 11:30:50 +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" Add support for saving the state of lxc domain into a set of files. Usage: virsh save [domain-name or domain-uuid] [path] virsh restore [path] Example: virsh -c lxc:// save container1 /tmp/container1-dump virsh -c lxc:// restore /tmp/container1-dump Signed-off-by: Radostin Stoyanov --- src/lxc/lxc_driver.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 234 insertions(+) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index f52085ebf..871e853a0 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -81,6 +81,7 @@ #include "virhostdev.h" #include "netdev_bandwidth_conf.h" =20 +#include "lxc_criu.h" #define VIR_FROM_THIS VIR_FROM_LXC =20 VIR_LOG_INIT("lxc.lxc_driver"); @@ -3198,6 +3199,235 @@ static int lxcDomainResume(virDomainPtr dom) return ret; } =20 +static int +lxcDoDomainSave(virLXCDriverPtr driver, virDomainObjPtr vm, + const char *to) +{ + int ret =3D -1; + virCapsPtr caps =3D NULL; + uint32_t xml_len =3D -1; + char *xml =3D NULL; + char xmlLen[33]; + char *xml_image_path =3D NULL; + char *str =3D NULL; + + if (!(caps =3D virLXCDriverGetCapabilities(driver, false))) + goto cleanup; + + if ((xml =3D virDomainDefFormat(vm->def, caps, 0)) =3D=3D NULL) + goto cleanup; + + if ((ret =3D lxcCriuDump(driver, vm, to)) !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to checkpoint domain with CRIU")); + goto cleanup; + } + + virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, + VIR_DOMAIN_SHUTOFF_SAVED); + + if (virAsprintf(&xml_image_path, "%s/xml-image", to) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to write image path")); + goto cleanup; + } + + xml_len =3D strlen(xml) + 1; + snprintf(xmlLen, sizeof(xmlLen), "%d", xml_len); + VIR_DEBUG("xmlLen =3D %d %s", xml_len, xmlLen); + + if (virAsprintf(&str, "%s\n%s", xmlLen, xml) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to write xml info to string")); + goto cleanup; + } + + if (virFileWriteStr(xml_image_path, str, 0666) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("Failed to write xml description to %s"), + xml_image_path); + goto cleanup; + } + + ret =3D 0; + + cleanup: + VIR_FREE(xml); + VIR_FREE(xml_image_path); + VIR_FREE(str); + return ret !=3D 0 ? -1 : ret; +} + +static int +lxcDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml, + unsigned int flags) +{ + int ret =3D -1; + virLXCDriverPtr driver =3D dom->conn->privateData; + virDomainObjPtr vm; + bool remove_dom =3D false; + + virCheckFlags(0, -1); + if (dxml) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("xml modification unsupported")); + return -1; + } + + if (!(vm =3D lxcDomObjFromDomain(dom))) + goto cleanup; + + if (virDomainSaveFlagsEnsureACL(dom->conn, vm->def) < 0) + goto cleanup; + + if (virLXCDomainObjBeginJob(driver, vm, LXC_JOB_MODIFY) < 0) + goto cleanup; + + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("Domain is not running")); + goto endjob; + } + + if (lxcDoDomainSave(driver, vm, to) < 0) + goto endjob; + + if (!vm->persistent) + remove_dom =3D true; + + ret =3D 0; + + endjob: + virLXCDomainObjEndJob(driver, vm); + + cleanup: + if (remove_dom && vm) + virDomainObjListRemove(driver->domains, vm); + virDomainObjEndAPI(&vm); + return ret; +} + +static int +lxcDomainSave(virDomainPtr dom, const char *to) +{ + return lxcDomainSaveFlags(dom, to, NULL, 0); +} + +static int +lxcDomainRestoreFlags(virConnectPtr conn, const char *from, + const char* dxml, unsigned int flags) +{ + virLXCDriverPtr driver =3D conn->privateData; + virDomainObjPtr vm =3D NULL; + virDomainDefPtr def =3D NULL; + virCapsPtr caps =3D NULL; + int ret =3D -1; + int restorefd; + char *xml =3D NULL; + u_int32_t xmlLen =3D 0; + int fd; + char *xml_image_path =3D NULL; + char c; + + virCheckFlags(0, -1); + if (dxml) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("xml modification unsupported")); + goto out; + } + + if (virAsprintf(&xml_image_path, "%s/xml-image", from) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to write xml image path")); + goto out; + } + + if ((fd =3D virFileOpenAs(xml_image_path, O_RDONLY, 0, -1, -1, 0)) < 0= ) { + virReportSystemError(-fd, + _("Failed to open domain image file '%s'"), + xml_image_path); + goto out; + } + + while ((saferead(fd, &c, 1) =3D=3D 1) && c !=3D '\n') + xmlLen =3D xmlLen*10 + c - '0'; + xmlLen--; + + if (VIR_ALLOC_N(xml, xmlLen) < 0) + goto cleanup; + + if (saferead(fd, xml, xmlLen) !=3D xmlLen) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to read X= ML")); + goto cleanup; + } + + if (!xml) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("no domain XML parsed")); + goto cleanup; + } + + if (!(caps =3D virLXCDriverGetCapabilities(driver, false))) + goto cleanup; + + if (!(def =3D virDomainDefParseString(xml, caps, driver->xmlopt, + NULL, VIR_DOMAIN_DEF_PARSE_INACTIV= E))) + goto cleanup; + + if (virDomainRestoreFlagsEnsureACL(conn, def) < 0) + goto cleanup; + + if (!(vm =3D virDomainObjListAdd(driver->domains, def, + driver->xmlopt, + VIR_DOMAIN_OBJ_LIST_ADD_LIVE | + VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE, + NULL))) + goto cleanup; + def =3D NULL; + + if (virLXCDomainObjBeginJob(driver, vm, LXC_JOB_MODIFY) < 0) { + if (!vm->persistent) + virDomainObjListRemove(driver->domains, vm); + goto cleanup; + } + + restorefd =3D open(from, O_DIRECTORY); + if (restorefd < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("Can't open images dir")); + if (!vm->persistent) + virDomainObjListRemove(driver->domains, vm); + virLXCDomainObjEndJob(driver, vm); + goto cleanup; + } + + ret =3D virLXCProcessStart(conn, driver, vm, + 0, NULL, + 0, restorefd, + VIR_DOMAIN_RUNNING_RESTORED); + + VIR_FORCE_CLOSE(restorefd); + + if (ret < 0 && !vm->persistent) + virDomainObjListRemove(driver->domains, vm); + + virLXCDomainObjEndJob(driver, vm); + cleanup: + VIR_FORCE_CLOSE(fd); + out: + virDomainDefFree(def); + VIR_FREE(xml_image_path); + VIR_FREE(xml); + virDomainObjEndAPI(&vm); + return ret; +} + +static int +lxcDomainRestore(virConnectPtr conn, const char *from) +{ + return lxcDomainRestoreFlags(conn, from, NULL, 0); +} + static int lxcDomainOpenConsole(virDomainPtr dom, const char *dev_name, @@ -5550,6 +5780,10 @@ static virHypervisorDriver lxcHypervisorDriver =3D { .domainLookupByName =3D lxcDomainLookupByName, /* 0.4.2 */ .domainSuspend =3D lxcDomainSuspend, /* 0.7.2 */ .domainResume =3D lxcDomainResume, /* 0.7.2 */ + .domainSave =3D lxcDomainSave, /* x.x.x */ + .domainSaveFlags =3D lxcDomainSaveFlags, /* x.x.x */ + .domainRestore =3D lxcDomainRestore, /* x.x.x */ + .domainRestoreFlags =3D lxcDomainRestoreFlags, /* x.x.x */ .domainDestroy =3D lxcDomainDestroy, /* 0.4.4 */ .domainDestroyFlags =3D lxcDomainDestroyFlags, /* 0.9.4 */ .domainGetOSType =3D lxcDomainGetOSType, /* 0.4.2 */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list