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 already mounted partitions
42    if [ -f /run/systemd/transient/run-media-$name.mount ]; then
43        logger "mount.sh/automount" "/run/media/$name already mounted"
44        return
45    fi
46
47    # Skip the partition which are already in /etc/fstab
48    grep "^[[:space:]]*$DEVNAME" /etc/fstab && return
49    for n in LABEL PARTLABEL UUID PARTUUID; do
50        tmp="$(lsblk -o $n $DEVNAME | sed -e '1d')"
51        test -z "$tmp" && continue
52        tmp="$n=$tmp"
53        grep "^[[:space:]]*$tmp" /etc/fstab && return
54    done
55
56    [ -d "/run/media/$name" ] || mkdir -p "/run/media/$name"
57
58    MOUNT="$MOUNT -o silent"
59
60    # If filesystemtype is vfat, change the ownership group to 'disk', and
61    # grant it with  w/r/x permissions.
62    case $ID_FS_TYPE in
63    vfat|fat)
64        MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
65        ;;
66    swap)
67        return ;;
68    # TODO
69    *)
70        ;;
71    esac
72
73    if ! $MOUNT --no-block -t auto $DEVNAME "/run/media/$name"
74    then
75        #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
76        rm_dir "/run/media/$name"
77    else
78        logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
79        touch "/tmp/.automount-$name"
80    fi
81}
82
83automount() {
84	name="`basename "$DEVNAME"`"
85
86	if [ -x "$PMOUNT" ]; then
87		$PMOUNT $DEVNAME 2> /dev/null
88	elif [ -x $MOUNT ]; then
89		$MOUNT $DEVNAME 2> /dev/null
90	fi
91
92	# If the device isn't mounted at this point, it isn't
93	# configured in fstab
94	grep -q "^$DEVNAME " /proc/mounts && return
95
96	! test -d "/run/media/$name" && mkdir -p "/run/media/$name"
97	# Silent util-linux's version of mounting auto
98	if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
99	then
100		MOUNT="$MOUNT -o silent"
101	fi
102
103	# If filesystem type is vfat, change the ownership group to 'disk', and
104	# grant it with  w/r/x permissions.
105	case $ID_FS_TYPE in
106	vfat|fat)
107		MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
108		;;
109	swap)
110		return ;;
111	# TODO
112	*)
113		;;
114	esac
115
116	if ! $MOUNT -t auto $DEVNAME "/run/media/$name"
117	then
118		#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
119		rm_dir "/run/media/$name"
120	else
121		logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
122		touch "/tmp/.automount-$name"
123	fi
124}
125
126rm_dir() {
127	# We do not want to rm -r populated directories
128	if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
129	then
130		! test -z "$1" && rm -r "$1"
131	else
132		logger "mount.sh/automount" "Not removing non-empty directory [$1]"
133	fi
134}
135
136# No ID_FS_TYPE for cdrom device, yet it should be mounted
137name="`basename "$DEVNAME"`"
138[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media`
139
140if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then
141    # Note the root filesystem can show up as /dev/root in /proc/mounts,
142    # so check the device number too
143    if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then
144        if [ "`basename $MOUNT`" = "systemd-mount" ];then
145            automount_systemd
146        else
147            automount
148        fi
149    fi
150fi
151
152if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
153    for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
154    do
155        $UMOUNT $mnt
156    done
157
158    # Remove empty directories from auto-mounter
159    name="`basename "$DEVNAME"`"
160    test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name"
161fi
162