udev-151: mount.sh: Small bugfix (SD Labels with spaces in their names
[openembedded.git] / recipes / udev / udev-151 / omap3-pandora / mount.sh
1 #!/bin/sh
2 #
3 # Called from udev
4 #
5 # Attempt to mount any added block devices and remove any removed devices.
6 #
7
8 MOUNT="/bin/mount"
9 PMOUNT="/usr/bin/pmount"
10 UMOUNT="/bin/umount"
11 blkid="/usr/sbin/blkid"
12 name="`basename "$DEVNAME"`"
13 name2="`$blkid "$DEVNAME" -o value -s LABEL`"
14
15
16 for line in `cat /etc/udev/mount.blacklist | grep -v ^#`
17 do
18         if ( echo "$DEVNAME" | grep -q "$line" )
19         then
20                 logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring"
21                 exit 0
22         fi
23 done
24
25 automount() {   
26         if [ -n "$name2" ] 
27         then
28                 c=1
29                 while ( cat /proc/mounts | grep "/media/$name2" ); do
30                         name2="$name2-$c"
31                         c=$(expr $c + 1)
32                 done
33                 name="$name2"
34         fi
35         ! test -d "/media/$name" && mkdir -p "/media/$name"
36         
37         if ! $MOUNT -t auto -o dirsync,noatime,umask=0 $DEVNAME "/media/$name" && ! $MOUNT -t auto -o dirsync,noatime $DEVNAME "/media/$name"
38         then
39                 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/media/$name\" failed!"
40                 rm_dir "/media/$name"
41         else
42                 logger "mount.sh/automount" "Auto-mount of [/media/$name] successful"
43                 touch "/tmp/.automount-$name"
44         fi
45 }
46         
47 rm_dir() {
48         # We do not want to rm -r populated directories
49         if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
50         then
51                 ! test -z "$1" && rm -r "$1"
52         else
53                 logger "mount.sh/automount" "Not removing non-empty directory [$1]"
54         fi
55 }
56
57 if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ]; then
58         if [ -x "$PMOUNT" ]; then
59                 $PMOUNT $DEVNAME 2> /dev/null
60         elif [ -x $MOUNT ]; then
61                 $MOUNT $DEVNAME 2> /dev/null
62         fi
63         
64         # If the device isn't mounted at this point, it isn't configured in fstab
65         # 20061107: Small correction: The rootfs partition may be called just "rootfs" and not by
66         #           its true device name so this would break. If the rootfs is mounted on two places
67         #           during boot, it confuses the heck out of fsck. So Im auto-adding the root-partition
68         #           to /etc/udev/mount.blacklist via postinst 
69
70         cat /proc/mounts | awk '{print $1}' | grep -q "^$DEVNAME$" || automount 
71         
72 fi
73
74 if [ "$ACTION" = "remove" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
75         for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
76         do
77                 # 20100306: Remove the mount point forcibly (Lazy) as the user has already removed the device by the 
78                 # DJWillis: time this fires so any handles are bad anyway. This should stop 'stale' folders being
79                 #           left around all the time.
80                 $UMOUNT -l $mnt
81         done
82         
83         # Remove empty directories from auto-mounter
84         test -e "/tmp/.automount-$name" && rm_dir "$mnt"
85 fi