blob: 5ba66e98e2f403e5b7d911c8181e152302db0c37 [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
Andrew Geisslerd5838332022-05-27 11:33:10 -050029for line in `grep -h -v ^# /etc/udev/mount.ignorelist /etc/udev/mount.ignorelist.d/*`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030do
31 if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ];
32 then
Andrew Geisslerd5838332022-05-27 11:33:10 -050033 logger "udev/mount.sh" "[$DEVNAME] is marked to ignore"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 exit 0
35 fi
36done
37
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038automount_systemd() {
39 name="`basename "$DEVNAME"`"
40
Brad Bishopa34c0302019-09-23 22:34:48 -040041 # 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080047 # 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 ;;
Brad Bishop96ff1982019-08-19 13:50:42 -040066 swap)
67 return ;;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080068 # 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() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 name="`basename "$DEVNAME"`"
85
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080086 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 ! 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 # 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 ;;
Brad Bishop96ff1982019-08-19 13:50:42 -0400109 swap)
110 return ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 # 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141 # 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150fi
151
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800153 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"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161fi