public inbox for openembedded-core@lists.openembedded.org
 help / color / mirror / Atom feed
From: liu.ming50@gmail.com
To: openembedded-core@lists.openembedded.org
Cc: steve@sakoman.com, Muhammad Hamza <Muhammad_Hamza@mentor.com>,
	Awais Belal <awais_belal@mentor.com>,
	Muhammad Hamza <muhammad_hamza@mentor.com>,
	Luca Ceresoli <luca.ceresoli@bootlin.com>,
	Ming Liu <liu.ming50@gmail.com>
Subject: [OE-core] [kirkstone] [PATCH 5/9] udev-extraconf/mount.sh: only mount devices on hotplug
Date: Sun, 17 Jul 2022 14:56:44 +0200	[thread overview]
Message-ID: <20220717125648.2942046-6-liu.ming50@gmail.com> (raw)
In-Reply-To: <20220717125648.2942046-1-liu.ming50@gmail.com>

From: Muhammad Hamza <Muhammad_Hamza@mentor.com>

fdisk from util-linux (2.31.1) and above allows the user to
manipulate an already mounted device. In order to achieve this
functionality it issues a BLKRRPART (block device re-read part)
ioctl and in response the kernel generates remove/change/add
events if the device is not mounted (manually unmounted etc)
which are caught and processed by udev. This causes our auto-mounter
to remount everything because it does not keep track and things
go out of control.
Differentiating between types of remove events such as the one
described above (generated by BLKRRPART) and one where the device
is physically plugged out is only possible using the DEVPATH variable
which is cleaned up only when the device is actually plugged-out.
This fixes the above anomaly by only mounting a device in add event
which is cleaned up properly (tmp cache deleted) in the remove event
or is not present in the tmp cache while making use of the DEVPATH
variable during the remove action.

Signed-off-by: Awais Belal <awais_belal@mentor.com>
Signed-off-by: Muhammad Hamza <muhammad_hamza@mentor.com>
Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
(cherry picked from commit 11a5e6c17535438ea1e7a8403ed260c8b3a22bc8)
Signed-off-by: Ming Liu <liu.ming50@gmail.com>
---
 .../recipes-core/udev/udev-extraconf/mount.sh | 34 +++++++++++++++----
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/meta/recipes-core/udev/udev-extraconf/mount.sh b/meta/recipes-core/udev/udev-extraconf/mount.sh
index c4695ee27d..537828e3e3 100644
--- a/meta/recipes-core/udev/udev-extraconf/mount.sh
+++ b/meta/recipes-core/udev/udev-extraconf/mount.sh
@@ -45,6 +45,13 @@ automount_systemd() {
         return
     fi
 
+    # Only go for auto-mounting when the device has been cleaned up in remove
+    # or has not been identified yet
+    if [ -e "/tmp/.automount-$name" ]; then
+            logger "mount.sh/automount" "[$MOUNT_BASE/$name] is already cached"
+            return
+    fi
+
     # Skip the partition which are already in /etc/fstab
     grep "^[[:space:]]*$DEVNAME" /etc/fstab && return
     for n in LABEL PARTLABEL UUID PARTUUID; do
@@ -100,6 +107,13 @@ automount() {
 	# Get the unique name for mount point
 	get_label_name "${DEVNAME}"
 
+        # Only go for auto-mounting when the device has been cleaned up in remove
+        # or has not been identified yet
+        if [ -e "/tmp/.automount-$name" ]; then
+                logger "mount.sh/automount" "[$MOUNT_BASE/$name] is already cached"
+                return
+        fi
+
 	! test -d "$MOUNT_BASE/$name" && mkdir -p "$MOUNT_BASE/$name"
 	# Silent util-linux's version of mounting auto
 	if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
@@ -172,12 +186,18 @@ if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_t
 fi
 
 if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
-    for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
-    do
-        $UMOUNT $mnt
-    done
-
-    # Remove empty directories from auto-mounter
     name="`basename "$DEVNAME"`"
-    test -e "/tmp/.automount-$name" && rm_dir "$MOUNT_BASE/$name"
+    tmpfile=`find /tmp | grep "\.automount-.*${name}$"`
+    if [ ! -e "/sys/$DEVPATH" -a -e "$tmpfile" ]; then
+        logger "mount.sh/remove" "cleaning up $DEVNAME, was mounted by the auto-mounter"
+        for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
+        do
+                $UMOUNT $mnt
+        done
+        # Remove mount directory created by the auto-mounter
+        # and clean up our tmp cache file
+        mntdir=`cat "$tmpfile"`
+        rm_dir "$MOUNT_BASE/$mntdir"
+        rm "$tmpfile"
+    fi
 fi
-- 
2.25.1



  parent reply	other threads:[~2022-07-17 12:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-17 12:56 [OE-core] [kirkstone] [PATCH 0/9] udev-extraconf fixes liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 1/9] udev-extraconf/initrdscripts/parted: Rename mount.blacklist -> mount.ignorelist liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 2/9] udev-extraconf: let automount base directory configurable liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 3/9] udev-extraconf/mount.sh: add LABELs to mountpoints liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 4/9] udev-extraconf/mount.sh: save mount name in our tmp filecache liu.ming50
2022-07-17 12:56 ` liu.ming50 [this message]
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 6/9] udev-extraconf: force systemd-udevd to use shared MountFlags liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 7/9] udev-extraconf/mount.sh: ignore lvm in automount liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 8/9] udev-extraconf: fix some systemd automount issues liu.ming50
2022-07-17 12:56 ` [OE-core] [kirkstone] [PATCH 9/9] udev-extraconf:mount.sh: fix path mismatching issues liu.ming50

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220717125648.2942046-6-liu.ming50@gmail.com \
    --to=liu.ming50@gmail.com \
    --cc=Muhammad_Hamza@mentor.com \
    --cc=awais_belal@mentor.com \
    --cc=luca.ceresoli@bootlin.com \
    --cc=openembedded-core@lists.openembedded.org \
    --cc=steve@sakoman.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox