1#!/bin/sh 2# 3# Called from udev 4# 5# Attempt to mount any added block devices and umount any removed devices 6 7BASE_INIT="`readlink -f "@base_sbindir@/init"`" 8INIT_SYSTEMD="@systemd_unitdir@/systemd" 9 10if [ "x$BASE_INIT" = "x$INIT_SYSTEMD" ];then 11 # systemd as init uses systemd-mount to mount block devices 12 MOUNT="/usr/bin/systemd-mount" 13 UMOUNT="/usr/bin/systemd-umount" 14 15 if [ -x $MOUNT ] && [ -x $UMOUNT ]; 16 then 17 logger "Using systemd-mount to finish mount" 18 else 19 logger "Linux init is using systemd, so please install systemd-mount to finish mount" 20 exit 1 21 fi 22else 23 MOUNT="/bin/mount" 24 UMOUNT="/bin/umount" 25fi 26 27PMOUNT="/usr/bin/pmount" 28 29for line in `grep -h -v ^# /etc/udev/mount.blacklist /etc/udev/mount.blacklist.d/*` 30do 31 if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ]; 32 then 33 logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring" 34 exit 0 35 fi 36done 37 38automount_systemd() { 39 name="`basename "$DEVNAME"`" 40 41 # Skip the partition which are already in /etc/fstab 42 grep "^[[:space:]]*$DEVNAME" /etc/fstab && return 43 for n in LABEL PARTLABEL UUID PARTUUID; do 44 tmp="$(lsblk -o $n $DEVNAME | sed -e '1d')" 45 test -z "$tmp" && continue 46 tmp="$n=$tmp" 47 grep "^[[:space:]]*$tmp" /etc/fstab && return 48 done 49 50 [ -d "/run/media/$name" ] || mkdir -p "/run/media/$name" 51 52 MOUNT="$MOUNT -o silent" 53 54 # If filesystemtype is vfat, change the ownership group to 'disk', and 55 # grant it with w/r/x permissions. 56 case $ID_FS_TYPE in 57 vfat|fat) 58 MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`" 59 ;; 60 # TODO 61 *) 62 ;; 63 esac 64 65 if ! $MOUNT --no-block -t auto $DEVNAME "/run/media/$name" 66 then 67 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!" 68 rm_dir "/run/media/$name" 69 else 70 logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful" 71 touch "/tmp/.automount-$name" 72 fi 73} 74 75automount() { 76 name="`basename "$DEVNAME"`" 77 78 if [ -x "$PMOUNT" ]; then 79 $PMOUNT $DEVNAME 2> /dev/null 80 elif [ -x $MOUNT ]; then 81 $MOUNT $DEVNAME 2> /dev/null 82 fi 83 84 # If the device isn't mounted at this point, it isn't 85 # configured in fstab 86 grep -q "^$DEVNAME " /proc/mounts && return 87 88 ! test -d "/run/media/$name" && mkdir -p "/run/media/$name" 89 # Silent util-linux's version of mounting auto 90 if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ; 91 then 92 MOUNT="$MOUNT -o silent" 93 fi 94 95 # If filesystem type is vfat, change the ownership group to 'disk', and 96 # grant it with w/r/x permissions. 97 case $ID_FS_TYPE in 98 vfat|fat) 99 MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`" 100 ;; 101 # TODO 102 *) 103 ;; 104 esac 105 106 if ! $MOUNT -t auto $DEVNAME "/run/media/$name" 107 then 108 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!" 109 rm_dir "/run/media/$name" 110 else 111 logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful" 112 touch "/tmp/.automount-$name" 113 fi 114} 115 116rm_dir() { 117 # We do not want to rm -r populated directories 118 if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1" 119 then 120 ! test -z "$1" && rm -r "$1" 121 else 122 logger "mount.sh/automount" "Not removing non-empty directory [$1]" 123 fi 124} 125 126# No ID_FS_TYPE for cdrom device, yet it should be mounted 127name="`basename "$DEVNAME"`" 128[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media` 129 130if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then 131 # Note the root filesystem can show up as /dev/root in /proc/mounts, 132 # so check the device number too 133 if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then 134 if [ "`basename $MOUNT`" = "systemd-mount" ];then 135 automount_systemd 136 else 137 automount 138 fi 139 fi 140fi 141 142if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then 143 for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " ` 144 do 145 $UMOUNT $mnt 146 done 147 148 # Remove empty directories from auto-mounter 149 name="`basename "$DEVNAME"`" 150 test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name" 151fi 152