From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (unknown [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 0993E8015A for ; Wed, 9 Sep 2026 21:34:28 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 689LXGEP023975; Thu, 10 Sep 2026 00:33:17 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 689LXGEP023975 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="mPZfq5bp" Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 689LX1tf023942 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 10 Sep 2026 00:33:01 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 689LX1tf023942 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-49cc9f5bee2so52078115e9.2 for ; Wed, 09 Sep 2026 14:33:01 -0700 (PDT) X-Forwarded-Encrypted: i=2; AKwUvBzw0toGSOM8RLuEkJtoLGJZLrn4MtPuyDKaXv2vnydvdaThtOz7k8fnJtS9TjTZoEKmvXUryQ==@openvz.org X-Gm-Message-State: AFuF++kHH/0CQRyWVA1VL9mcyBShNgDPgLAarzC8wVgWDdyYgH2q6ebp KOSbr1dX2Tyf5kP1xsJpX8ZjjhH6xT6cava+zh3AcXmmiPCTZaGBPq/M5wVqisLjROodupoBJ9g 0tHqHebIS9k0BGQtEVrNyZhHQqv+cQJHsIncdaGLLwSKr2r1NvIqlqw== X-Gm-Gg: AYBFou26+Ulq4zZUPIfxzgjkFuvUB0HOdclISDEGn3LNAU0VLGsuR7AR5p/3gmlw5uh ncMnBVQs3QNKq/F4bbfMgpFw1RhMsOZZZR3GKuaKmjESerlAlQuFcn1224Uq1UCS1z8QRW/PaJs Qv7QFZTGlLHM2qiXOG2rRu0dkG5r1fDnGjt9DeIJuWEQOUgZeXhzb+ws7TBEVCDjVr54SO0y/eP oaRCLcbyXuMeHYKrx5EvFas5ox02LDRtxiIwX9QzzeNQxnSjuTIjs03U8HRYeO3Q8cDan3fxb9p wOf+w5omujTzb1E+z4AvDYMHAum392YP1Zfh8iw3EpzbMcA4uZA8RqyIvo8mZ3CQxsj1tuv3ZPt Jf5xsqbbyUuC6WyMfR7mkAvCwwtpN X-Received: by 2002:a05:600c:4e4c:b0:499:621a:2ec2 with SMTP id 5b1f17b1804b1-49cf8206e4dmr393102075e9.3.1788989580970; Wed, 09 Sep 2026 14:33:00 -0700 (PDT) X-Received: by 2002:a05:600c:4e4c:b0:499:621a:2ec2 with SMTP id 5b1f17b1804b1-49cf8206e4dmr393101785e9.3.1788989580487; Wed, 09 Sep 2026 14:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1788989580; cv=none; d=google.com; s=arc-20260327; b=EAS+chvfGkUzSBiWe2MIk+Hbs4sWPPKa3Ru7IP3XJi0K5oLRquYuPPPXmLQZtrWEPV IpXYm4898ZLirj3gj1eQQdSFX8MVWL3jppmlI7orYavj4Ad00TtdaNJPfiHDX2/Pvam0 t55yLJ3wY0x7YCzmSqA7eYftyGLvYZSTqrDA5TvWL6FvS8QptPZrkYijTO7cUcrAmhzT j1NH+aKRGm8i3LhwBexYz8VcLT8w8yQ9YN0rZ+JYB89atsD56V0DuKa2COF/cNs6I/2x BhANdNkBSd/I/Pji2LAYCwBaiwAR+Ndd03qA8/qOZXRGmY0s2CGR8aQNnlBEBCKj+lUi kV1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=vUqehU2pBA9xyyTY7RRdH68F0hHKyU7wWpByduljgk8=; fh=vUECVpjOjIdz431s8bRiKYBdScEDOVhPZlDu2AQ+txg=; b=QDoQiDAGL+oD0bjzavVyienf7JrR4nmcbThzhTV+/nIchdoHk5GEo4Q7qn7AY77N8a zzRKq1jHFa2xPWJ3DZ/tXpw9sQSwauQcR5t9Py3mQ3T3CT5lKAKsmHRspMBNxuo2+p/Y JdMK+p5TF3Tk9JlKiGtLt6StPnkdsJsQ5IFTUQv71uLB/w9AiVmvpXwhSXapLJH5SI7l 1KNcgjK7gA6sKt4RZL7YNxA+bVeUeT1Jd+E6RBQvlK2Gk6LZRvSVDEuxlZkzIWKh+RMo Nr2D3a+T6w+0mPdS+CGTRAad/d86nYgllAlryIQriYzny1kUbEEP0VBKiCuJXcOkP03g Ebag==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=mPZfq5bp; spf=pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=vladimir.riabchun@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-4858c59df1esi34433725f8f.301.2026.09.09.14.33.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 14:33:00 -0700 (PDT) Received-SPF: pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=mPZfq5bp; spf=pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=vladimir.riabchun@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Message-Id:Content-Type:MIME-Version:Subject:Date :From; bh=vUqehU2pBA9xyyTY7RRdH68F0hHKyU7wWpByduljgk8=; b=mPZfq5bp0RnBHfWO0mw biYYbGpWEE+dx8vShiLu7gUIGcq4tmHtYXeDy0PD04U3qFZF231hMwNJqYnAGZpAoLez/bk49VGiT OEAJVJ6FD+7j8xk5kdKao4tGkBzZ64c0WPXkORQBbMISCHIPAj8mEj5AHpNmUvOFNjGb5SWtbCKs+ NAss9+8wzehBIdvV6lOGSiduYpF/KvAJG2eL5uXNIgtR14QncCrO+U3LrQv8wTPwMs9heF2EtbfBX TSsOwN0cr/Qf/feLtk6A4KO6YIMrLswaAmQmmtc2nSzHHlM0citTRUSK/Rh4G7R9SHIqPL8PcP+r5 FxjMQWnOoMdTnug==; Received: from [130.117.225.5] (helo=dev012.aci.vzint.dev) by relay.virtuozzo.com with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4Ps3-00DS6s-2T; Wed, 09 Sep 2026 23:32:58 +0200 From: Vladimir Riabchun Date: Wed, 09 Sep 2026 21:32:48 +0000 MIME-Version: 1.0 Message-Id: <20260909-ve-selftest-improv-v3-1-4b41980d9a45@virtuozzo.com> References: <20260909-ve-selftest-improv-v3-0-4b41980d9a45@virtuozzo.com> In-Reply-To: <20260909-ve-selftest-improv-v3-0-4b41980d9a45@virtuozzo.com> To: Konstantin Khorenko , vasileios.almpanis@virtuozzo.com X-Mailer: b4 0.16.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH VZ10 v3 1/3] selftests/ve: Add namespace management helpers X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org When mount namespace is created in a child, it is destroyed when the child dies. We may need to keep it for a while to check how VE handles resources. Instead of keeping a thread which lives in a new ve+mnt namespaces, let's store open namespace file descriptors to enter them later without keeping children alive. https://virtuozzo.atlassian.net/browse/VSTOR-144504 Feature: ve selftests Signed-off-by: Vladimir Riabchun --- tools/testing/selftests/ve/ve_selftest.h | 126 +++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/tools/testing/selftests/ve/ve_selftest.h b/tools/testing/selftests/ve/ve_selftest.h index c53bf7900d20..22c5c8dfc2d1 100644 --- a/tools/testing/selftests/ve/ve_selftest.h +++ b/tools/testing/selftests/ve/ve_selftest.h @@ -31,6 +31,132 @@ #define VE_CONTROLLERS \ "+cpuset +cpu +cpuacct +io +memory +hugetlb +pids +rdma +misc +ve" +struct namespace_ctx { + int ns_save_mask; /* Mask of stored CLONE_NEW* namespaces. */ + /* Namespace file descriptors. Name should match /proc/PID/ns/NAME */ + int ve_fd; + int mnt_fd; +}; + +static inline void close_namespace_fds(struct namespace_ctx *ctx) +{ + if (ctx->ns_save_mask & CLONE_NEWVE) + close(ctx->ve_fd); + if (ctx->ns_save_mask & CLONE_NEWNS) + close(ctx->mnt_fd); + + ctx->ns_save_mask = 0; +} + +static inline int save_namespace_fds(struct namespace_ctx *ctx, int flags) +{ +#define OPEN_NS(name, flag) \ +do { \ + if (flags & (flag)) { \ + ctx->name##_fd = open("/proc/self/ns/" #name, O_RDONLY); \ + if (ctx->name##_fd < 0) { \ + fprintf(stderr, "%s: could not open fd %s: %s\n", \ + __func__, #name, strerror(errno)); \ + goto err; \ + } \ + ctx->ns_save_mask |= (flag); \ + } \ +} while (0) + + ctx->ns_save_mask = 0; + + OPEN_NS(ve, CLONE_NEWVE); + OPEN_NS(mnt, CLONE_NEWNS); + +#undef OPEN_NS + return 0; + +err: + close_namespace_fds(ctx); + return -1; +} + +static inline int enter_namespaces(struct namespace_ctx *ctx, int flags) +{ +#define JOIN_NS(name, flag) \ +do { \ + if (flags & (flag)) { \ + if (!(ctx->ns_save_mask & (flag))) { \ + fprintf(stderr, "%s: could not switch ns %s:" \ + " no fd in context.\n", __func__, #name); \ + return -1; \ + } \ + if (setns(ctx->name##_fd, (flag))) { \ + fprintf(stderr, "%s: could not switch ns %s: %s\n", \ + __func__, #name, strerror(errno)); \ + return -1; \ + } \ + } \ +} while (0) + + JOIN_NS(ve, CLONE_NEWVE); + JOIN_NS(mnt, CLONE_NEWNS); + +#undef JOIN_NS + return 0; +} + +static inline int enter_cgroup(int cgv2_fd, int ctid); + +/* + * Create new namespaces and store them in ctx. + * CLONE_NEWVE is treated specially - before unshare corresponding + * CT cgroup is entered, after it root cgroup is entered(not original one). + */ +static inline int prepare_namespaces(struct namespace_ctx *ctx, int flags, + int cgv2_fd, int ctid) +{ + struct namespace_ctx tmp_ctx; + int ret = 0; + + if (save_namespace_fds(&tmp_ctx, flags) < 0) + return -1; + + if (flags & CLONE_NEWVE) { + if (enter_cgroup(cgv2_fd, ctid) < 0) { + fprintf(stderr, "%s: failed to enter CT cgroup\n", __func__); + ret = -1; + goto restore_ns; + } + } + + if (unshare(flags) < 0) { + fprintf(stderr, "%s: failed to unshare, flags %x: %s\n", + __func__, flags, strerror(errno)); + ret = -1; + goto restore_cg; + } + + if (save_namespace_fds(ctx, flags)) { + fprintf(stderr, "%s: failed to save new namespaces, flags %x\n", + __func__, flags); + ret = -1; + } + +restore_cg: + if (flags & CLONE_NEWVE) { + if (enter_cgroup(cgv2_fd, 0) < 0) { + fprintf(stderr, "%s: failed to restore root cgroup\n", __func__); + ret = -1; + } + } + +restore_ns: + if (enter_namespaces(&tmp_ctx, flags) < 0) { + fprintf(stderr, "%s: failed to restore old namespaces, flags %x\n", + __func__, flags); + ret = -1; + } + + close_namespace_fds(&tmp_ctx); + return ret; +} + static inline int write_file_at(int dirfd, const char *path, const char *val) { int fd, ret; -- 2.47.1 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel