]> www.infradead.org Git - mtd-utils.git/commitdiff
Add an ubifs mount helper
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Tue, 6 Oct 2020 09:19:13 +0000 (11:19 +0200)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Wed, 18 Nov 2020 14:29:19 +0000 (15:29 +0100)
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 <u.kleine-koenig@pengutronix.de>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
ubifs-utils/Makemodule.am
ubifs-utils/mount.ubifs [new file with mode: 0755]

index 59109ccd613c751b1360773175be9a4ea7228157..5c5d99f7572b8e7c04ddee3b893cbb411a553c9d 100644 (file)
@@ -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 (executable)
index 0000000..b94ddc5
--- /dev/null
@@ -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" "$@"