blob: 79eb0145c2a6b9da6e00bbe53eda21a3fd72c00d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
2#
3# Called from udev
4#
5# Attempt to mount any added block devices and umount any removed devices
6
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08007BASE_INIT="`readlink -f "@base_sbindir@/init"`"
8INIT_SYSTEMD="@systemd_unitdir@/systemd"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080010if [ "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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027PMOUNT="/usr/bin/pmount"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080028
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050029for line in `grep -h -v ^# /etc/udev/mount.blacklist /etc/udev/mount.blacklist.d/*`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030do
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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038automount_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 ;;
Brad Bishop96ff1982019-08-19 13:50:42 -040060 swap)
61 return ;;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080062 # TODO
63 *)
64 ;;
65 esac
66
67 if ! $MOUNT --no-block -t auto $DEVNAME "/run/media/$name"
68 then
69 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
70 rm_dir "/run/media/$name"
71 else
72 logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
73 touch "/tmp/.automount-$name"
74 fi
75}
76
77automount() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 name="`basename "$DEVNAME"`"
79
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080080 if [ -x "$PMOUNT" ]; then
81 $PMOUNT $DEVNAME 2> /dev/null
82 elif [ -x $MOUNT ]; then
83 $MOUNT $DEVNAME 2> /dev/null
84 fi
85
86 # If the device isn't mounted at this point, it isn't
87 # configured in fstab
88 grep -q "^$DEVNAME " /proc/mounts && return
89
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090 ! test -d "/run/media/$name" && mkdir -p "/run/media/$name"
91 # Silent util-linux's version of mounting auto
92 if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
93 then
94 MOUNT="$MOUNT -o silent"
95 fi
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 # If filesystem type is vfat, change the ownership group to 'disk', and
98 # grant it with w/r/x permissions.
99 case $ID_FS_TYPE in
100 vfat|fat)
101 MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
102 ;;
Brad Bishop96ff1982019-08-19 13:50:42 -0400103 swap)
104 return ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 # TODO
106 *)
107 ;;
108 esac
109
110 if ! $MOUNT -t auto $DEVNAME "/run/media/$name"
111 then
112 #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
113 rm_dir "/run/media/$name"
114 else
115 logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
116 touch "/tmp/.automount-$name"
117 fi
118}
119
120rm_dir() {
121 # We do not want to rm -r populated directories
122 if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
123 then
124 ! test -z "$1" && rm -r "$1"
125 else
126 logger "mount.sh/automount" "Not removing non-empty directory [$1]"
127 fi
128}
129
130# No ID_FS_TYPE for cdrom device, yet it should be mounted
131name="`basename "$DEVNAME"`"
132[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media`
133
134if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135 # Note the root filesystem can show up as /dev/root in /proc/mounts,
136 # so check the device number too
137 if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then
138 if [ "`basename $MOUNT`" = "systemd-mount" ];then
139 automount_systemd
140 else
141 automount
142 fi
143 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144fi
145
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
148 do
149 $UMOUNT $mnt
150 done
151
152 # Remove empty directories from auto-mounter
153 name="`basename "$DEVNAME"`"
154 test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155fi