From patchwork Tue Oct 6 09:19:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 1377301 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=XcZlKkav; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C5BlN0S6Sz9sTK for ; Tue, 6 Oct 2020 20:20:32 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject: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=0BVrKtEt7cWqDkCLKgW3tKnV2gZWYoAOgFl7a/JJywo=; b=XcZlKkav73LVAD+fnmItzB6DLs ulk/zTYfvoDUvWOf+mtq/B/d5qLiwV46jgaSuFKZ/u4SWnn3s4L1RAe7PAk3JpXr3ZJDmN25cWn20 zsppvM0MRqcjy1l5Xru6zVdl+m7mve1kc1D28frJQ5Z/oMmCMUzTJazvFSh6XaBBUGebnbhRhxpyg 3f3yvCvaYMPCkbGT5sD8HeMpihBW+FTnNNhyG6cgoGJOcvipIwvdVv90w0Dm5qP0M5bJSd9IfTUNi Nf5ZMm2KdIv2NRj90eYb+/OQz+nYH4DMpo4oAOp6ACckZbnftC7kRWPlYSg/7DtuQ0BBCZtr34rBG 0ai7I3Ew==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kPj8X-0005LI-4X; Tue, 06 Oct 2020 09:19:57 +0000 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kPj8U-0005KS-RM for linux-mtd@lists.infradead.org; Tue, 06 Oct 2020 09:19:55 +0000 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kPj8L-00037V-L4; Tue, 06 Oct 2020 11:19:45 +0200 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1kPj8J-0007Rv-Sg; Tue, 06 Oct 2020 11:19:43 +0200 From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: David Oberhollenzer Subject: [PATCH mtd-utils RFC] Add an ubifs mount helper Date: Tue, 6 Oct 2020 11:19:13 +0200 Message-Id: <20201006091912.1139071-1-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mtd@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201006_051954_887477_872A1D9E X-CRM114-Status: GOOD ( 22.19 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Weinberger , linux-mtd@lists.infradead.org, entwicklung@pengutronix.de Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This abstracts away attaching of the right ubi and then selecting the right ubi device and volume to mount. As described in the comment at the top this allows to mount ubifs volumes directly from /etc/fstab without having to use hardcoded numbers (which depend on mount order and so are unreliable) and extra magic to care for attaching. Signed-off-by: Uwe Kleine-König --- Hello, this is a nice script that helps attaching ubis and mounting ubifs'. I already discussed this a bit with Richard on irc and the idea was welcome in principle. A concern was that the entry in /etc/fstab is too magic and there should be an indication that this relies on the mount helper. Something like using ubifs.autoattach as file system type (fs_vfstype) or requiring "autoattach" in the fourth field (fs_mntops). Having thought a bit I'm not convinced we need this indication. Yes there is magic but nothing out of the ordinary as mount helpers are a known concept and the fs_spec already looks interesting enough to make it obvious there is something going on. And once people are used to the magic it is annoying to have to always write this indication in your fstab. Additionally it complicates the implementation. Also "autoattach" doesn't completely cover the script as it does a translation from mtd name to mtd number, too. "autoattach,mtdtranslate" seems a bit excessive. Best regards Uwe ubifs-utils/Makemodule.am | 2 + ubifs-utils/mount.ubifs | 101 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100755 ubifs-utils/mount.ubifs diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am index 59109ccd613c..5c5d99f7572b 100644 --- a/ubifs-utils/Makemodule.am +++ b/ubifs-utils/Makemodule.am @@ -47,4 +47,6 @@ UBIFS_EXTRA = \ EXTRA_DIST += $(UBIFS_HEADER) $(UBIFS_EXTRA) +dist_sbin_SCRIPTS = ubifs-utils/mount.ubifs + sbin_PROGRAMS += $(UBIFS_BINS) diff --git a/ubifs-utils/mount.ubifs b/ubifs-utils/mount.ubifs new file mode 100755 index 000000000000..b94ddc5649f4 --- /dev/null +++ b/ubifs-utils/mount.ubifs @@ -0,0 +1,101 @@ +#!/bin/sh + +# This script should be installed as /sbin/mount.ubifs. The benefit is that an +# fstab entry like: +# +# mtd=mtddev:home /home ubifs defaults 0 0 +# +# results in the ubi contained in the mtd named "mtddev" to be attached (if not +# already done) and then the volume named "home" being mounted to /home. + +# This is called by mount with the following options: +# /sbin/mount.ubifs spec dir [-sfnv] [-N namespace] [-o options] [-t type.subtype] + +spec="$1" +shift + +mtdname2num() { + local name + + name="$1" + + for d in $(find /sys/class/mtd/ -regex '.*/mtd[0-9]*'); do + case "$d" in + *ro) + continue + ;; + esac + + if test "$name" = "$(cat "$d/name")"; then + local dev mtdnum + + dev="$(basename "$d")" + mtdnum="${dev#mtd}" + echo "$mtdnum" + return + fi + done + + return 1 +} + +mtdnum2ubi() { + local mtdnum + + mtdnum="$1" + + for d in $(find /sys/class/ubi/ -regex '.*/ubi[0-9]*'); do + case "$d" in + *_[0-9]*) + continue + ;; + esac + + if test "$mtdnum" = "$(cat "$d/mtd_num")"; then + local ubi + + ubi="$(basename "$d")" + echo "$ubi" + return; + fi + done + + return 1 +} + +mtdnum2ubi_autoattach() { + local mtdnum ubi + + mtdnum="$1" + + ubi="$(mtdnum2ubi "$mtdnum")" && { echo "$ubi"; return; } + + # ubiattach might fail with "mtdX is already attached to ubiY" if there + # is more than one mount to do in the same mtd partition. So ignore errors. + ubiattach -m "$mtdnum" >&2 || true + + mtdnum2ubi "$mtdnum" +} + +case "$spec" in + mtd=*:*) + spec="${spec#mtd=}" + mtd="${spec%:*}" + rspec="${spec#*:}" + + mtdnum="$(mtdname2num "$mtd")" || { + echo "Failed to find mtdnum for mtd \"$mtd\"" + exit 1 + } + + ubi="$(mtdnum2ubi_autoattach "$mtdnum")" || { + echo "Failed to find ubi for mtd \"$mtd\"" + exit 1 + } + + spec="$ubi:$rspec" + + ;; +esac + +/bin/mount -i -t ubifs "$spec" "$@"