From 7a2cd37ad349f1ca8402c4b52f6177394cd353c3 Mon Sep 17 00:00:00 2001 From: Javier Viguera Date: Wed, 19 Jun 2013 18:42:22 +0200 Subject: [PATCH] udev-extraconf: fix automount of block devices Some block devices are incorrectly mounted by udev. Instead of mounting the partition device it mounts the disk device. This commit enhances the automount udev rule and mount script so it only applies to partitions. https://jira.digi.com/browse/DEL-511 Signed-off-by: Javier Viguera Reviewed-by: Alex Gonzalez --- .../udev/udev-extraconf/automount.rules | 19 +++++ .../recipes-core/udev/udev-extraconf/mount.sh | 76 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 meta-digi-dey/recipes-core/udev/udev-extraconf/automount.rules create mode 100644 meta-digi-dey/recipes-core/udev/udev-extraconf/mount.sh diff --git a/meta-digi-dey/recipes-core/udev/udev-extraconf/automount.rules b/meta-digi-dey/recipes-core/udev/udev-extraconf/automount.rules new file mode 100644 index 000000000..d2b92b39d --- /dev/null +++ b/meta-digi-dey/recipes-core/udev/udev-extraconf/automount.rules @@ -0,0 +1,19 @@ +# There are a number of modifiers that are allowed to be used in some +# of the different fields. They provide the following subsitutions: +# +# %n the "kernel number" of the device. +# For example, 'sda3' has a "kernel number" of '3' +# %e the smallest number for that name which does not matches an existing node +# %k the kernel name for the device +# %M the kernel major number for the device +# %m the kernel minor number for the device +# %b the bus id for the device +# %c the string returned by the PROGRAM +# %s{filename} the content of a sysfs attribute +# %% the '%' char itself +# + +# Media automounting +SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ACTION=="add" RUN+="/etc/udev/scripts/mount.sh" +SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh" + diff --git a/meta-digi-dey/recipes-core/udev/udev-extraconf/mount.sh b/meta-digi-dey/recipes-core/udev/udev-extraconf/mount.sh new file mode 100644 index 000000000..d1419ed09 --- /dev/null +++ b/meta-digi-dey/recipes-core/udev/udev-extraconf/mount.sh @@ -0,0 +1,76 @@ +#!/bin/sh +# +# Called from udev +# +# Attempt to mount any added block devices and umount any removed devices + + +MOUNT="/bin/mount" +PMOUNT="/usr/bin/pmount" +UMOUNT="/bin/umount" +for line in `grep -v ^# /etc/udev/mount.blacklist` +do + if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ]; + then + logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring" + exit 0 + fi +done + +automount() { + name="`basename "$DEVNAME"`" + + ! test -d "/media/$name" && mkdir -p "/media/$name" + # Silent util-linux's version of mounting auto + if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ; + then + MOUNT="$MOUNT -o silent" + fi + + if ! $MOUNT -t auto $DEVNAME "/media/$name" + then + #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/media/$name\" failed!" + rm_dir "/media/$name" + else + logger "mount.sh/automount" "Auto-mount of [/media/$name] successful" + touch "/tmp/.automount-$name" + fi +} + +rm_dir() { + # We do not want to rm -r populated directories + if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1" + then + ! test -z "$1" && rm -r "$1" + else + logger "mount.sh/automount" "Not removing non-empty directory [$1]" + fi +} + +if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" ]; then + if [ -x "$PMOUNT" ]; then + $PMOUNT $DEVNAME 2> /dev/null + elif [ -x $MOUNT ]; then + $MOUNT $DEVNAME 2> /dev/null + fi + + # If the device isn't mounted at this point, it isn't + # configured in fstab (note the root filesystem can show up as + # /dev/root in /proc/mounts, so check the device number too) + if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then + grep -q "^$DEVNAME " /proc/mounts || automount + fi +fi + + + +if [ "$ACTION" = "remove" ] && [ -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 "/media/$name" +fi