From nobody Wed May 14 19:28:01 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 1523446238934798.8963850257938; Wed, 11 Apr 2018 04:30:38 -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 159B4312FF68; Wed, 11 Apr 2018 11:30:37 +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 A96D11757E; Wed, 11 Apr 2018 11:30:36 +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 A8F4E4CA9C; Wed, 11 Apr 2018 11:30:35 +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 w3BBTbI1029812 for ; Wed, 11 Apr 2018 07:29:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id 70BCD6AFF7; Wed, 11 Apr 2018 11:29:37 +0000 (UTC) Received: from mx1.redhat.com (ext-mx18.extmail.prod.ext.phx2.redhat.com [10.5.110.47]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C1E4217F26; Wed, 11 Apr 2018 11:29:34 +0000 (UTC) Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) (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 1C6123130F8A; Wed, 11 Apr 2018 11:29:33 +0000 (UTC) Received: by mail-wm0-f67.google.com with SMTP id r131so3135361wmb.2; Wed, 11 Apr 2018 04:29:33 -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.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Apr 2018 04:29:30 -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=OyoDAzJ8NRwUwex7lnCB+Swjp6+f84l5kJr2TiL5NZY=; b=Ve6SWDUl2ekk7ihm0VYX4UTiIxDC8sF9FIVc4q2RuTGUkOU9dAD+TOJ904ocjv3fYo PjgfPNbKPz/g+I/dBkE8jYlPBsLYyna02uu2P3TvFL5UQmlBfLZzp7mFr+wGLZje8e4a umCqg7jWYxSFw6F+00mFeSRsqJMsgLP2Vq5CowdGY95aNf6JiCUSR99MEEWofC8EFouy ETRggvMpRYxy0lrGCXD0XoyIpOfdfc+0qbjixGBGU+T6S+fPSfeWjG0RjSVw69qiw5tb oka1h2KPIhXaHiV8FiH16t3JZIKB3kKFljlwreGYm2w3fchU8/q57/U5UmLYSMW3M5N1 T7ZA== 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=OyoDAzJ8NRwUwex7lnCB+Swjp6+f84l5kJr2TiL5NZY=; b=mb7FhlyrGeff4ZADOYjbMRBSWPxz3HOP/Qs4YWUwJX9wHmauRZchM/gdEbBGO8iACp tB3J9WBBRtFcEFfXlTdwWZPAeapJmdVcbzzYLFj0TYJk6TsrKpXdkjsTjT/IumI5VGSk ggLl5gwc0k1fx9EurQgLjy03GB98zovUrpB7XAo89wEyDYXt6QuMtbWkQ967P9h2/w1d p0f6ZO8sr8Gf4A5x1hxA0evKXdEk4kYiUB0QtuToeu8OcjVDuPvg9/s+xgqCt2QwpUol 3Orti+pBaGIJzy/fSakc92csx8gIpvpz3NqvOghcswaXPUi342+8IZtVbZGlbzW5TSx0 du3A== X-Gm-Message-State: ALQs6tCxFeqn4gnYBUthxBWWndYx76hhWJk3V9yoWEw9E6FpRHN2PqIs 647CRSQHhuHLiXiWDATeFEgYJRBC X-Google-Smtp-Source: AIpwx4/LfBPRw64V+QILt4Y1eItlWt9svXFgyiK/g+MbRmmFG75owtQcOJDPEU4krCLDaR2WuhsUiA== X-Received: by 10.28.69.93 with SMTP id s90mr2260560wma.71.1523446171347; Wed, 11 Apr 2018 04:29:31 -0700 (PDT) From: Radostin Stoyanov To: libvir-list@redhat.com Date: Wed, 11 Apr 2018 12:29:13 +0100 Message-Id: <20180411112915.5890-3-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.47]); Wed, 11 Apr 2018 11:29:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 11 Apr 2018 11:29:33 +0000 (UTC) for IP:'74.125.82.67' DOMAIN:'mail-wm0-f67.google.com' HELO:'mail-wm0-f67.google.com' FROM:'rstoyanov1@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.12 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS) 74.125.82.67 mail-wm0-f67.google.com 74.125.82.67 mail-wm0-f67.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.47 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: kkoukiou@redhat.com, mprivozn@redhat.com, cbosdonnat@suse.com, Radostin Stoyanov Subject: [libvirt] [RFC PATCH 2/4] lxc: Add save/restore helper functions 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.43]); Wed, 11 Apr 2018 11:30:37 +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 helper functions for checkpoint/restore of linux containers using CRIU. Signed-off-by: Radostin Stoyanov --- po/POTFILES.in | 1 + src/lxc/Makefile.inc.am | 4 + src/lxc/lxc_criu.c | 253 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/lxc/lxc_criu.h | 36 +++++++ 4 files changed, 294 insertions(+) create mode 100644 src/lxc/lxc_criu.c create mode 100644 src/lxc/lxc_criu.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 0dcd1cab2..a6d3a5743 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -106,6 +106,7 @@ src/lxc/lxc_fuse.c src/lxc/lxc_hostdev.c src/lxc/lxc_native.c src/lxc/lxc_process.c +src/lxc/lxc_criu.c src/network/bridge_driver.c src/network/bridge_driver_linux.c src/network/leaseshelper.c diff --git a/src/lxc/Makefile.inc.am b/src/lxc/Makefile.inc.am index 8dd2e9ea9..fbdc87b24 100644 --- a/src/lxc/Makefile.inc.am +++ b/src/lxc/Makefile.inc.am @@ -42,6 +42,8 @@ LXC_DRIVER_SOURCES =3D \ lxc/lxc_native.h \ lxc/lxc_driver.c \ lxc/lxc_driver.h \ + lxc/lxc_criu.c \ + lxc/lxc_criu.h \ $(NULL) =20 LXC_CONTROLLER_SOURCES =3D \ @@ -58,6 +60,8 @@ LXC_CONTROLLER_SOURCES =3D \ lxc/lxc_fuse.c \ lxc/lxc_fuse.h \ lxc/lxc_controller.c \ + lxc/lxc_criu.c \ + lxc/lxc_criu.h \ $(NULL) =20 =20 diff --git a/src/lxc/lxc_criu.c b/src/lxc/lxc_criu.c new file mode 100644 index 000000000..b45f9b9f3 --- /dev/null +++ b/src/lxc/lxc_criu.c @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2016 Katerina Koukiou + * + * lxc_criu.c: Helper functions for checkpoint/restore of linux containers + * + * Authors: + * Katerina Koukiou + * Radostin Stoyanov + * + * 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 + * . + */ + +#include + +#include +#include +#include + +#include "virobject.h" +#include "virerror.h" +#include "virlog.h" +#include "virfile.h" +#include "vircommand.h" +#include "virstring.h" +#include "viralloc.h" + +#include "lxc_domain.h" +#include "lxc_driver.h" +#include "lxc_criu.h" + +#define VIR_FROM_THIS VIR_FROM_LXC + +VIR_LOG_INIT("lxc.lxc_criu"); + +#ifdef CRIU + +int lxcCriuDump(virLXCDriverPtr driver ATTRIBUTE_UNUSED, + virDomainObjPtr vm, + const char *checkpointdir) +{ + int fd; + int ret =3D -1; + pid_t initpid; + virCommandPtr cmd; + struct stat sb; + char *path =3D NULL; + char *tty_info_path =3D NULL; + char *ttyinfo =3D NULL; + int status; + + initpid =3D ((virLXCDomainObjPrivatePtr) vm->privateData)->initpid; + + if (virFileMakePath(checkpointdir) < 0) { + virReportSystemError(errno, _("Failed to mkdir %s"), checkpointdir= ); + return -1; + } + + fd =3D open(checkpointdir, O_DIRECTORY); + if (fd < 0) { + virReportSystemError(errno, + _("Failed to open directory %s"), checkpointd= ir); + return -1; + } + + cmd =3D virCommandNew(CRIU); + virCommandAddArg(cmd, "dump"); + virCommandAddArg(cmd, "--tree"); + virCommandAddArgFormat(cmd, "%d", initpid); + virCommandAddArgList(cmd, + "--images-dir", checkpointdir, + "--tcp-established", + "--log-file", "dump.log", + "-v4", + "--file-locks", + "--link-remap", + "--force-irmap", + "--manage-cgroups=3Dfull", + "--enable-fs", "hugetlbfs", + "--enable-fs", "tracefs", + "--external", "mnt[]{:ms}", + "--external", "mnt[/proc/meminfo]:fuse", + "--external", "mnt[/dev/console]:console", + "--external", "mnt[/dev/tty1]:tty1", + NULL + ); + + /* The master pair of the /dev/pts device lives outside from what is d= umped + * inside the libvirt-lxc process. Add the slave pair as an external t= ty + * otherwise criu will fail. + */ + if (virAsprintf(&path, "/proc/%d/root/dev/pts/0", initpid) < 0) + goto cleanup; + + if (stat(path, &sb) < 0) { + virReportSystemError(errno, _("Unable to stat %s"), path); + goto cleanup; + } + + if (virAsprintf(&tty_info_path, "%s/tty.info", checkpointdir) < 0) + goto cleanup; + + if (virAsprintf(&ttyinfo, "tty[%llx:%llx]", + (long long unsigned) sb.st_rdev, + (long long unsigned) sb.st_dev) < 0) + goto cleanup; + + if (virFileWriteStr(tty_info_path, ttyinfo, 0600) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to write tty info to %s"), tty_info_path); + goto cleanup; + } + + virCommandAddArg(cmd, "--external"); + virCommandAddArgFormat(cmd, "tty[%llx:%llx]", + (long long unsigned) sb.st_rdev, + (long long unsigned) sb.st_dev); + + virCommandAddEnvString(cmd, "PATH=3D/bin:/sbin"); + + VIR_DEBUG("About to checkpoint domain %s (pid =3D %d)", + vm->def->name, initpid); + virCommandRawStatus(cmd); + if (virCommandRun(cmd, &status) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FORCE_CLOSE(fd); + VIR_FREE(path); + VIR_FREE(tty_info_path); + VIR_FREE(ttyinfo); + virCommandFree(cmd); + + return (ret < 0) ? ret : status; +} + + +int lxcCriuRestore(virDomainDefPtr def, int restorefd, + int ttyfd) +{ + int ret =3D -1; + virCommandPtr cmd; + char *ttyinfo =3D NULL; + char *inheritfd =3D NULL; + char *tty_info_path =3D NULL; + char *checkpointfd =3D NULL; + char *checkpointdir =3D NULL; + virDomainFSDefPtr root; + gid_t *groups =3D NULL; + int ngroups; + + cmd =3D virCommandNew(CRIU); + virCommandAddArg(cmd, "restore"); + + if (virAsprintf(&checkpointfd, "/proc/self/fd/%d", restorefd) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to write checkpoint dir path")); + goto cleanup; + } + + if (virFileResolveLink(checkpointfd, &checkpointdir) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to readlink checkpoint dir path")); + goto cleanup; + } + + virCommandAddArgList(cmd, + "--pidfile", "pidfile", + "--restore-detached", + "--restore-sibling", + "--tcp-established", + "--file-locks", + "--link-remap", + "--manage-cgroups=3Dfull", + "--enable-fs", "hugetlbfs", + "--enable-fs", "tracefs", + "--images-dir", checkpointdir, + "--log-file", "restore.log", + "-v4", + "--external", "mnt[]{:ms}", + "--external", "mnt[fuse]:/proc/meminfo", + "--external", "mnt[console]:/dev/console", + "--external", "mnt[tty1]:/dev/tty1", + NULL + ); + + /* Restore external tty from tty.info file */ + if (virAsprintf(&tty_info_path, "%s/tty.info", checkpointdir) < 0) + goto cleanup; + + if (virFileReadAll(tty_info_path, 1024, &ttyinfo) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to read tty info from %s"), tty_info_path= ); + goto cleanup; + } + if (virAsprintf(&inheritfd, "fd[%d]:%s", ttyfd, ttyinfo) < 0) + goto cleanup; + + virCommandAddArgList(cmd, "--inherit-fd", inheritfd, NULL); + + root =3D virDomainGetFilesystemForTarget(def, "/"); + virCommandAddArgList(cmd, "--root", root->src->path, NULL); + + virCommandAddEnvString(cmd, "PATH=3D/bin:/sbin"); + + if ((ngroups =3D virGetGroupList(virCommandGetUID(cmd), virCommandGetG= ID(cmd), &groups)) < 0) + goto cleanup; + + /* If virCommandExec returns here we have an error */ + ignore_value(virCommandExec(cmd, groups, ngroups)); + + ret =3D -1; + + cleanup: + VIR_FREE(tty_info_path); + VIR_FREE(ttyinfo); + VIR_FREE(inheritfd); + VIR_FREE(checkpointdir); + VIR_FREE(checkpointfd); + virCommandFree(cmd); + + return ret; +} +#else +int lxcCriuDump(virLXCDriverPtr driver ATTRIBUTE_UNUSED, + virDomainObjPtr vm ATTRIBUTE_UNUSED, + const char *checkpointdir ATTRIBUTE_UNUSED) +{ + virReportUnsupportedError(); + return -1; +} + +int lxcCriuRestore(virDomainDefPtr def ATTRIBUTE_UNUSED, + int fd ATTRIBUTE_UNUSED, + int ttyfd ATTRIBUTE_UNUSED) +{ + virReportUnsupportedError(); + return -1; +} +#endif diff --git a/src/lxc/lxc_criu.h b/src/lxc/lxc_criu.h new file mode 100644 index 000000000..aadc3ac34 --- /dev/null +++ b/src/lxc/lxc_criu.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 Katerina Koukiou + * + * lxc_criu.h: Helper functions for checkpoint/restore of linux containers + * + * Authors: + * Katerina Koukiou + * Radostin Stoyanov + * + * 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 + * . + */ + +#ifndef LXC_CRIU_H +# define LXC_CRIU_H + +# include "virobject.h" + +int lxcCriuDump(virLXCDriverPtr driver, + virDomainObjPtr vm, + const char *checkpointdir); + +int lxcCriuRestore(virDomainDefPtr def, int fd, + int ttyfd); +#endif /* LXC_CRIU_H */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list