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 EF1278015A for ; Wed, 9 Sep 2026 12:16:27 +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 689CFBJd019370; Wed, 9 Sep 2026 15:15:12 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 689CFBJd019370 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="diI9HBm1" Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 689CF9B0019353 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 9 Sep 2026 15:15:09 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 689CF9B0019353 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-49cdc4080ddso33847555e9.1 for ; Wed, 09 Sep 2026 05:15:09 -0700 (PDT) X-Forwarded-Encrypted: i=2; AKwUvBwXxpAsv11RA0wgwYiw0qswZchqZokRTmfe1sJjd5GY78Hx3PHU8VqMpym+ziijZdAo5WOK9w==@openvz.org X-Gm-Message-State: AFuF++mxtdW4j8PeMgp2Rg0jfVteWKc2bAFSlFAlG9m5OIH8ZEzQI4Ah lmrnTWjvREUbzZjHWSHX2dmHgkTCSjZ9WiBznA9F1AjOrA6y97K8NW+VepJkm/Mo1jxSPLsJwxS pZiNe4G3tkUxqLTHtm8spRkAXBRxdgnUvJZ8cmIMB4PSojK3g4134Ng== X-Gm-Gg: AYBFou3zbiA++X9zgE82r0VS2tGuIWJvImoUMfTYXvki5dMWLDZpY8nL3bz796cyeDg RMcoQqDSB5kgIewpmF5QgbMhzAk6H7V1VUet2c1Kod2HkcuJCT4dMxtcTmAO/Ra0KI2tW++aJ4i YtW3Gu7Ght7EdjQVWvAvqNb7ntuiu8KJJpF9Wj/9JtgWt7BRASuOtUf6dFlYj+gNIKj33Er3bB6 Z/LgUOHHAn5x4ii1dISHmbcgd1MysfVa5XcU+IJqgJY52CucuWLVvDvje6QQPwf4SV0/EXifndO eRrdXO1x5xE5Purfgtimlq4/7YqiXEm4b724DRL4BXK9Jl0zPnXZa++Fm+UNwWha7Qh0RANbmfv e3r5g1E/tZhzWUXFcUqc/t9h3LAaC X-Received: by 2002:a05:600c:3648:b0:49c:f13d:ffed with SMTP id 5b1f17b1804b1-49cf5988c09mr307820635e9.17.1788956109010; Wed, 09 Sep 2026 05:15:09 -0700 (PDT) X-Received: by 2002:a05:600c:3648:b0:49c:f13d:ffed with SMTP id 5b1f17b1804b1-49cf5988c09mr307820235e9.17.1788956108451; Wed, 09 Sep 2026 05:15:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1788956108; cv=none; d=google.com; s=arc-20260327; b=oOdSJ5ogV/EHD7yFvufOFd04VFyb7LQv2EkDytwqiFjYcGrQAjFDP/l7WL+qSBYCax k/t1qpfJKMt7Rq5SrADm8AK6Jw0xKmI0YyI4YzIo2dfTeL/Z3DFr7t0B3wgWjFFGvcLX 71+HpPqhS37kqepunWjNaRU/bmlvbgPCsbbwyDzAZc3Dk0TA0t6HBoOUo0qEfRqh2jkr 97ClkdEki/ZEs7Kne9yXcor16tWgUm0Pb1s1PxWN5DsbMioNLucn6vLDEy0ByWSZErEF MBrxBH1foOMVBL8MSfRA9AbGaUtbIwU2ww5gYHB7/unV6ylIQRncW/DCo5hktYCHMSWg wScg== 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=hxoJau9co9+eYsFViXh3rdr1vvYbUDcYC+r+LAWWdxM=; fh=4dppuILCfGifWHfxjIIA1BWoKQtGs1EMopxlFiqV6no=; b=llgt2ekKaKe7oCLzX4LSWs5+ZGRRUNomm43EymrSDJxQiqNFaHNtm6/jFg4kW1vF9J rNxwhb5TYed4DrqOadEP8mvh+Z1V8tw7JU6qalkLaB5t49Nmk4l/MhaVc+EMNGKq+fFm 3IHNi7LCpOoKbi6fHQ7mZvKxNsh4c2cDySzQN1pANCMLJ9JC0yomplT2PKSXYChB8Dnx /h2eb1L/McY3k+QRixi9eAO3l6Jwucfc3sF1GHPvxREdtAddysoH7wXUhiM3Ea8OmXun R2MCye4BmE6jQ974v7xDq2FLjyRYuNHKJ2oSra9GarS8nnV3L1lKxcJGh2VX2Y9g4Wkv i+vg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=diI9HBm1; 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-485883b9052si33682198f8f.106.2026.09.09.05.15.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 05:15:08 -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=diI9HBm1; 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=hxoJau9co9+eYsFViXh3rdr1vvYbUDcYC+r+LAWWdxM=; b=diI9HBm1MoWdCQ+Y2er QUQs7gebO6o53XkD43vOp7MfZ83Uwgqvu7Pr1n7u0kTc5xXh9HVYLppDzP1cP4sBnbgb6P6TqaaSx uEuA+zySCPIzvZ9dd2tjGc31suujrQzaSvqARxlOZAiqpuXlX8YqIkeUIAuKmKkZqqdRgIPI9IPlX +tzvSAVuBidwWq0e7ca8idjvTjV2Y85UWRZkZIBTNEyCsQsBFIAhf+Z/vl0Q9BwlpajxGESWgJLYq dsl4UaBuQ6O0gOPGdK1Hfz9YFgXSsxdRhlQ8o3iQdmXSHtE0X2Qtq/LJfzFgW+yOY5EmmOLiuYo37 fPQcg3zpAelxbDA==; 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 1x4HAC-002Qzu-04; Wed, 09 Sep 2026 14:15:07 +0200 From: Vladimir Riabchun Date: Wed, 09 Sep 2026 12:14:17 +0000 MIME-Version: 1.0 Message-Id: <20260909-ve-selftest-improv-v2-1-aad0a25d6569@virtuozzo.com> References: <20260909-ve-selftest-improv-v2-0-aad0a25d6569@virtuozzo.com> In-Reply-To: <20260909-ve-selftest-improv-v2-0-aad0a25d6569@virtuozzo.com> To: Konstantin Khorenko X-Mailer: b4 0.16.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH VZ10 v2 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 | 123 +++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/tools/testing/selftests/ve/ve_selftest.h b/tools/testing/selftests/ve/ve_selftest.h index c53bf7900d20..ee960f7db0b4 100644 --- a/tools/testing/selftests/ve/ve_selftest.h +++ b/tools/testing/selftests/ve/ve_selftest.h @@ -31,6 +31,129 @@ #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); + + 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); + 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