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" 9MOUNT_BASE="@MOUNT_BASE@" 10 11if [ "x$BASE_INIT" = "x$INIT_SYSTEMD" ];then 12 # systemd as init uses systemd-mount to mount block devices 13 MOUNT="/usr/bin/systemd-mount" 14 UMOUNT="/usr/bin/systemd-umount" 15 16 if [ -x $MOUNT ] && [ -x $UMOUNT ]; 17 then 18 logger "Using systemd-mount to finish mount" 19 else 20 logger "Linux init is using systemd, so please install systemd-mount to finish mount" 21 exit 1 22 fi 23else 24 MOUNT="/bin/mount" 25 UMOUNT="/bin/umount" 26fi 27 28PMOUNT="/usr/bin/pmount" 29 30for line in `grep -h -v ^# /etc/udev/mount.ignorelist /etc/udev/mount.ignorelist.d/*` 31do 32 if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ]; 33 then 34 logger "udev/mount.sh" "[$DEVNAME] is marked to ignore" 35 exit 0 36 fi 37done 38 39automount_systemd() { 40 name="`basename "$DEVNAME"`" 41 42 # Skip already mounted partitions 43 if [ -f /run/systemd/transient/$(echo $MOUNT_BASE | cut -d '/' -f 2- | sed 's#/#-#g')-*$name.mount ]; then 44 logger "mount.sh/automount" "$MOUNT_BASE/$name already mounted" 45 return 46 fi 47 48 # Get the unique name for mount point 49 get_label_name "${DEVNAME}" 50 51 # Only go for auto-mounting when the device has been cleaned up in remove 52 # or has not been identified yet 53 if [ -e "/tmp/.automount-$name" ]; then 54 logger "mount.sh/automount" "[$MOUNT_BASE/$name] is already cached" 55 return 56 fi 57 58 # Skip the partition which are already in /etc/fstab 59 grep "^[[:space:]]*$DEVNAME" /etc/fstab && return 60 for n in LABEL PARTLABEL UUID PARTUUID; do 61 tmp="$(lsblk -o $n $DEVNAME | sed -e '1d')" 62 test -z "$tmp" && continue 63 tmp="$n=$tmp" 64 grep "^[[:space:]]*$tmp" /etc/fstab && return 65 done 66 67 [ -d "$MOUNT_BASE/$name" ] || mkdir -p "$MOUNT_BASE/$name" 68 69 MOUNT="$MOUNT -o silent" 70 71 # If filesystemtype is vfat, change the ownership group to 'disk', and 72 # grant it with w/r/x permissions. 73 case $ID_FS_TYPE in 74 vfat|fat) 75 MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`" 76 ;; 77 swap) 78 return ;; 79 lvm*|LVM*) 80 return ;; 81 # TODO 82 *) 83 ;; 84 esac 85 86 if ! $MOUNT --no-block -t auto $DEVNAME "$MOUNT_BASE/$name" 87 then 88 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"$MOUNT_BASE/$name\" failed!" 89 rm_dir "$MOUNT_BASE/$name" 90 else 91 logger "mount.sh/automount" "Auto-mount of [$MOUNT_BASE/$name] successful" 92 echo "$name" > "/tmp/.automount-$name" 93 fi 94} 95 96automount() { 97 name="`basename "$DEVNAME"`" 98 99 if [ -x "$PMOUNT" ]; then 100 $PMOUNT $DEVNAME 2> /dev/null 101 elif [ -x $MOUNT ]; then 102 $MOUNT $DEVNAME 2> /dev/null 103 fi 104 105 # If the device isn't mounted at this point, it isn't 106 # configured in fstab 107 grep -q "^$DEVNAME " /proc/mounts && return 108 109 # Get the unique name for mount point 110 get_label_name "${DEVNAME}" 111 112 # Only go for auto-mounting when the device has been cleaned up in remove 113 # or has not been identified yet 114 if [ -e "/tmp/.automount-$name" ]; then 115 logger "mount.sh/automount" "[$MOUNT_BASE/$name] is already cached" 116 return 117 fi 118 119 ! test -d "$MOUNT_BASE/$name" && mkdir -p "$MOUNT_BASE/$name" 120 # Silent util-linux's version of mounting auto 121 if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ; 122 then 123 MOUNT="$MOUNT -o silent" 124 fi 125 126 # If filesystem type is vfat, change the ownership group to 'disk', and 127 # grant it with w/r/x permissions. 128 case $ID_FS_TYPE in 129 vfat|fat) 130 MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`" 131 ;; 132 swap) 133 return ;; 134 lvm*|LVM*) 135 return ;; 136 # TODO 137 *) 138 ;; 139 esac 140 141 if ! $MOUNT -t auto $DEVNAME "$MOUNT_BASE/$name" 142 then 143 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"$MOUNT_BASE/$name\" failed!" 144 rm_dir "$MOUNT_BASE/$name" 145 else 146 logger "mount.sh/automount" "Auto-mount of [$MOUNT_BASE/$name] successful" 147 # The actual device might not be present in the remove event so blkid cannot 148 # be used to calculate what name was generated here. Simply save the mount 149 # name in our tmp file. 150 echo "$name" > "/tmp/.automount-$name" 151 fi 152} 153 154rm_dir() { 155 # We do not want to rm -r populated directories 156 if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1" 157 then 158 ! test -z "$1" && rm -r "$1" 159 else 160 logger "mount.sh/automount" "Not removing non-empty directory [$1]" 161 fi 162} 163 164get_label_name() { 165 # Get the LABEL or PARTLABEL 166 LABEL=`/sbin/blkid | grep "$1:" | grep -o 'LABEL=".*"' | cut -d '"' -f2` 167 # If the $DEVNAME has a LABEL or a PARTLABEL 168 if [ -n "$LABEL" ]; then 169 # Set the mount location dir name to LABEL appended 170 # with $name e.g. label-sda. That would avoid overlapping 171 # mounts in case two devices have same LABEL 172 name="${LABEL}-${name}" 173 fi 174} 175 176# No ID_FS_TYPE for cdrom device, yet it should be mounted 177name="`basename "$DEVNAME"`" 178[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media` 179 180if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then 181 # Note the root filesystem can show up as /dev/root in /proc/mounts, 182 # so check the device number too 183 if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then 184 if [ "`basename $MOUNT`" = "systemd-mount" ];then 185 automount_systemd 186 else 187 automount 188 fi 189 fi 190fi 191 192if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then 193 name="`basename "$DEVNAME"`" 194 tmpfile=`find /tmp | grep "\.automount-.*${name}$"` 195 if [ ! -e "/sys/$DEVPATH" -a -e "$tmpfile" ]; then 196 logger "mount.sh/remove" "cleaning up $DEVNAME, was mounted by the auto-mounter" 197 for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " ` 198 do 199 $UMOUNT $mnt 200 done 201 # Remove mount directory created by the auto-mounter 202 # and clean up our tmp cache file 203 mntdir=`cat "$tmpfile"` 204 rm_dir "$MOUNT_BASE/$mntdir" 205 rm "$tmpfile" 206 fi 207fi 208