From patchwork Fri Jun 16 08:47:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul HENRYS X-Patchwork-Id: 1795748 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=B5P5HfLB; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=softathome1.onmicrosoft.com header.i=@softathome1.onmicrosoft.com header.a=rsa-sha256 header.s=selector1-softathome1-onmicrosoft-com header.b=NTTVAoF6; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QjCXW2Kwwz20ZB for ; Fri, 16 Jun 2023 18:50:03 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=NMlSNvgQ6zZdF6ApDMYeSAdTEeJrZlEGOhg9cwzGJnA=; b=B5P5HfLBInCkuO vHMlKVm7mHtyV/zFnIKtiHSnPu+3S/qgTGo6+K0ZPbHdoz0Ug6KUZ9sqgUAWy1/x2UapVeLLgt04Y +pv9xyNxbAQNo32stdtaY9lkK2lDdXOrxmiwIsmoYzc9srYx/IFgEHZ41LjitjaF70rVapFk4zomQ blkfCqoXK/boKW9KnQPKo7aSvWsE45qYEuRVGwO/2ROZxiRyx8p2Bgj5UN0ynlsZAbVerhoxxXXbT tX2o17OxaUGsM0Fx2+nNt25Vjpz7TtYh9Srbqtl9eQuiw9//zRPmwJmoBng5Sgjlan6T9CtraimiZ 1Qe2/SRxTm7oNA8ya2Bw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qA57E-00HJvT-1e; Fri, 16 Jun 2023 08:47:32 +0000 Received: from mail-pr2fra01on20605.outbound.protection.outlook.com ([2a01:111:f400:7e18::605] helo=FRA01-PR2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qA57A-00HJsm-2a for openwrt-devel@lists.openwrt.org; Fri, 16 Jun 2023 08:47:31 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a+yOoy5+kh2/MG7POGTBRa4Ind+qQMs4BrBAs7+scYhHUv8DpZO9UvDk3FmrgH+awnowg9RiDxDOnqnesG7jrPRcMFIB1lMKSvP9i/JGV07xAXea1hPJtDxFFLL9nBTjCNBx+Qq9TE8nhWagXyi0JWq/OUCK8CKl4+EmgWTOWyD+qupLy8d4+AThSj68kIhCUL8ITF3ECeDwOKq1l+r3nETKUy+7vBnekL6USvuDoddaKD36SaAwcobKrSIxz0lyOXjx6veZDFtbB3qlefM6YP1/i6+7b9ZgCpweSng2XYLGvuWXfZge4lk/myD+qbnvJFi85pg4jasEaqdVyY4J6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4MQcP1ia88Pkujf5K5hTr/9FIr3b8yOjbGOdnAwmPD4=; b=KNlMWyCsn18P9iGpk94ONMu9tNThvgiS25dioyqofUJlF8ekwAtOEJ1iB1i2mhWmmtBfEnDr2Den8hKpGTATCRsoqUc3tNhzqFTUXMiYyJVs2QF6fLIWkY1cCr9nFpCRoaifeiVHoGdhx9ho8VNW4UwnMVBOOeIXhK8lyUZqnkdClF59WKoGuC5zE3kwgj4VlwaRRQGISRA9U3JCv9EtlHx6dpoendv3Ci5t0zLhZwDdROL8gM8Cp3vtTnpAqtsNdD+B9FJfNoXJQaqbcBvjVqamYL5S44IemtVth53uPDntcbrMlLUMbIbjKZFKGxOi7Gq4YJ/RChaQBzHuZlk7iQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.6.166.170) smtp.rcpttodomain=lists.openwrt.org smtp.mailfrom=softathome.com; dmarc=bestguesspass action=none header.from=softathome.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=softathome1.onmicrosoft.com; s=selector1-softathome1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4MQcP1ia88Pkujf5K5hTr/9FIr3b8yOjbGOdnAwmPD4=; b=NTTVAoF60fu5VzXsadTQa34yWsF3GPcGcD11iVN4v0ShqN6cuix/3L6Haui8OXs/nBlYY5b2B9OxaG94LKC6jz7BOtPxvCgHNHijjKi7mtGO+Sv6xUe+m0wX5lJiVpnJjXZWvNRi6n+txcse/lDixHqMhEIc8HYUHyV6Z0aoxnpl2drXEvEgCOtx2gNvbudfJIYEOAn0UPgmr9Kx8VOF/6daPQzWADuSXRl0XV8Ggc4Vk+YNzgufWt5ZvLzqK/2M+MxSpeJoz4TZ2LL38yuAZVx8j4R2WrBCJaxoTspbk9NJ9soswf4sDKv/dKkKZFUt+1fkrJDxIgjDKbH+5ZfQjg== Received: from PR0P264CA0279.FRAP264.PROD.OUTLOOK.COM (2603:10a6:100:1::27) by PR1P264MB1613.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:1b7::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.29; Fri, 16 Jun 2023 08:47:21 +0000 Received: from PR2FRA01FT002.eop-fra01.prod.protection.outlook.com (2603:10a6:100:1:cafe::a7) by PR0P264CA0279.outlook.office365.com (2603:10a6:100:1::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.29 via Frontend Transport; Fri, 16 Jun 2023 08:47:21 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.6.166.170) smtp.mailfrom=softathome.com; dkim=none (message not signed) header.d=none;dmarc=bestguesspass action=none header.from=softathome.com; Received-SPF: Pass (protection.outlook.com: domain of softathome.com designates 149.6.166.170 as permitted sender) receiver=protection.outlook.com; client-ip=149.6.166.170; helo=proxy.softathome.com; pr=C Received: from proxy.softathome.com (149.6.166.170) by PR2FRA01FT002.mail.protection.outlook.com (10.152.48.95) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6521.13 via Frontend Transport; Fri, 16 Jun 2023 08:47:20 +0000 Received: from sahess08-ThinkPad-T580.domains. (unknown [192.168.18.254]) by proxy.softathome.com (Postfix) with ESMTPSA id DE5EA1FE29; Fri, 16 Jun 2023 10:47:19 +0200 (CEST) From: paul.henrys_ext@softathome.com To: openwrt-devel@lists.openwrt.org Cc: Paul HENRYS Subject: [PATCH] fstools: add support of mapped rootfs Date: Fri, 16 Jun 2023 10:47:09 +0200 Message-Id: <20230616084709.3630672-1-paul.henrys_ext@softathome.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PR2FRA01FT002:EE_|PR1P264MB1613:EE_ X-MS-Office365-Filtering-Correlation-Id: b93d72a9-1d18-4dbd-0772-08db6e464bd3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mvoM/gGgBSvcImR2qqJFf/Ljz3s+u92ekfCZmvRsR2EsG5axbt7povo3g7yep9ZU5Cr7jbu/3W0AmuGB1Bz/qVKKpAd1YHIcNDc8BSGUgqEzAgyU3iBqXsO8b11QnFLsbZraCqjhYevUfAuOiQy+j8yLeEfU3+k/ovtWUFLWjJPCC1+KGvQYXEEhqSD4/zmGXSMjnsDRr5elErgnwv5WaJAsiUo/xusYdLhhEswSECrEIbrB4N2rCy+flXeEgQYYCHXFQBFlXzsTiTw6NbHjc2Pd1EyyNEn7LIjRPkrlC9BeWB1Nbh8RF7ntsvizAzKxPv6e+YWwgnBvShCaElhUeNTdCp7EpqAqxWelErHCHt6ZgVvNxmDtpAnS3k91vpGwzjnPjUU+r2orA+zY23HA/7Zy1g0slnH+chg6L8qgbAw/2S4WQfF+g4hC7+n3fdYnD+a9DiZwwUMGNLJ0+FOO83O2H64Mj8/hr59DIq2t5wjsXL1/OljE2tFWRDOgrjuRzqEnNKvcJjpGoxZNlpa2YP0b2eElIcjredGRy0hqRfK+/kgMdJvrS5dWHuD02mftLS2UQ1eKIecwKiKv27HPmOVtLk8nb/uSTO/6/t2k+y/SB/X++zcMtS+kVdJ4IZOk77o2kKvH0RGrVHAfgKl18m5sxB2J5bTUpuaW3A9xT8aPf5cKi2agxaVjdXGN07YTSsgrpcE+6D6DjgcOvyO8LJMgmm1c4CSgdbBZmdzaTJQMjBxR7lBEfKyjIJKopals4ALkm53aPJfSfCnSWAek/Q== X-Forefront-Antispam-Report: CIP:149.6.166.170;CTRY:FR;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:proxy.softathome.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(136003)(376002)(346002)(39850400004)(451199021)(40470700004)(36840700001)(46966006)(86362001)(82310400005)(36756003)(40460700003)(40480700001)(2906002)(26005)(5660300002)(107886003)(1076003)(9686003)(6266002)(186003)(36860700001)(336012)(2616005)(47076005)(83380400001)(70586007)(6966003)(478600001)(70206006)(8676002)(8936002)(41300700001)(6666004)(4326008)(6916009)(316002)(82740400003)(82960400001)(81166007)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: softathome.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2023 08:47:20.1475 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b93d72a9-1d18-4dbd-0772-08db6e464bd3 X-MS-Exchange-CrossTenant-Id: aa10e044-e405-4c10-8353-36b4d0cce511 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=aa10e044-e405-4c10-8353-36b4d0cce511;Ip=[149.6.166.170];Helo=[proxy.softathome.com] X-MS-Exchange-CrossTenant-AuthSource: PR2FRA01FT002.eop-fra01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR1P264MB1613 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230616_014728_918730_F27B0626 X-CRM114-Status: GOOD ( 17.90 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Paul HENRYS When the rootfs is signed and/or ciphered and dmsetup is used, virtual block devices are created and mapped to the physical block device where the rootfs is actually stored. Inside mount_root.c, when [...] Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Paul HENRYS When the rootfs is signed and/or ciphered and dmsetup is used, virtual block devices are created and mapped to the physical block device where the rootfs is actually stored. Inside mount_root.c, when volume_find("rootfs_data") is called in start(), the implementation iterates over the registered drivers and eventually when the rootfs is mapped, when calling rootdisk_volume_find(), the virtual device is pointed by rootdev instead of the physical device which should be used. Hereafter is an example of mapped rootfs (signed and ciphered) stored in a UBI device: / # dmsetup ls --tree vrootfs_crypt (253:1) `-vrootfs_linear (253:0) `- (254:0) / # cat /sys/class/block/dm-1/dev 253:1 / # cat /sys/class/block/dm-0/dev 253:0 / # cat /sys/class/block/ubiblock0_5/dev 254:0 / # cat /sys/class/ubi/ubi0_5/name rootfs / # ls -lrt /sys/class/block/dm-1/slaves/ lrwxrwxrwx 1 root root 0 Jan 1 00:01 dm-0 -> ../../dm-0 / # ls -lrt /sys/class/block/dm-1/slaves/dm-0/slaves/ lrwxrwxrwx 1 root root 0 Jan 1 00:01 ubiblock0_5 -> ../../ubiblock0_5 / # ls -lrt /sys/class/block/dm-1/slaves/dm-0/slaves/ubiblock0_5/slaves In such a case, the previous implementation failed to detect the rootfs is stored in a UBI and did not mount an overlayfs. The fix looks for slave devices in /sys/class/block to find the physical block device. Signed-off-by: Paul HENRYS --- libfstools/common.c | 36 ++++++++++++++++++++++++++++++++++++ libfstools/common.h | 13 +++++++++++++ libfstools/rootdisk.c | 8 ++++++++ 3 files changed, 57 insertions(+) -- 2.25.1 -- This message and any attachments herein are confidential, intended solely for the addressees and are SoftAtHome’s ownership. Any unauthorized use or dissemination is prohibited. If you are not the intended addressee of this message, please cancel it immediately and inform the sender. diff --git a/libfstools/common.c b/libfstools/common.c index e5b7bc3..07cf39d 100644 --- a/libfstools/common.c +++ b/libfstools/common.c @@ -176,3 +176,39 @@ do_format: return ret; } + +char *block_get_slave(const char *bdev) +{ + int fd = -1; + DIR *dirp = NULL; + struct dirent *d; + char path[256] = {0}; + static char slave[256]; + int slaves = 0; + + if (!bdev) + goto out; + + snprintf(path, sizeof(path), "/sys/class/block/%s/slaves", bdev); + fd = open(path, O_RDONLY|O_CLOEXEC); + if (fd < 0) + goto out; + + dirp = fdopendir(fd); + if (!dirp) + goto out; + + while ((d = xreaddir(dirp))) { + if (++slaves > 1) + goto out; /* more than one slave */ + strncpy(slave, d->d_name, sizeof(slave)); + slave[sizeof(slave) - 1] = '\0'; /* make sure slave is always NULL terminated */ + } + + out: + if (dirp) + closedir(dirp); + if (fd >= 0) + close(fd); + return slaves == 1 ? slave : NULL; +} diff --git a/libfstools/common.h b/libfstools/common.h index b5cc692..4330ecd 100644 --- a/libfstools/common.h +++ b/libfstools/common.h @@ -21,7 +21,20 @@ #define F2FS_MINSIZE (100ULL * 1024ULL * 1024ULL) +/* Look for directories but skip "." and ".." */ +static inline struct dirent *xreaddir(DIR *dirp) +{ + struct dirent *d; + + while ((d = readdir(dirp)) && + (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))) + ; + + return d; +} + int read_uint_from_file(char *dirname, char *filename, unsigned int *i); char *read_string_from_file(const char *dirname, const char *filename, char *buf, size_t bufsz); int block_file_identify(FILE *f, uint64_t offset); int block_volume_format(struct volume *v, uint64_t offset, const char *bdev); +char *block_get_slave(const char *bdev); diff --git a/libfstools/rootdisk.c b/libfstools/rootdisk.c index 9f2317f..ef6f29e 100644 --- a/libfstools/rootdisk.c +++ b/libfstools/rootdisk.c @@ -97,6 +97,8 @@ static struct volume *rootdisk_volume_find(char *name) { struct squashfs_super_block sb; struct rootdev_volume *p; + static char slave[256] = {0}; + char *tmp = NULL; if (strcmp(name, "rootfs_data") != 0) return NULL; @@ -108,6 +110,12 @@ static struct volume *rootdisk_volume_find(char *name) if (!rootdev) return NULL; + /* Check for slaves to reach the initial block device */ + while ((tmp = block_get_slave(basename((char *)rootdev)))) { + strncpy(slave, tmp, sizeof(slave)); + rootdev = slave; + } + if (strstr(rootdev, "mtdblock") || strstr(rootdev, "ubiblock")) return NULL;