grml-udev-rebuildfstab: update script header.
[grml-udev-config.git] / scripts / grml-udev-rebuildfstab
1 #!/bin/bash
2 # Filename:      scripts/grml-udev-rebuildfstab
3 # Purpose:       udev script to update /etc/fstab
4 # Authors:       grml-team (grml.org)
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2+.
7 ################################################################################
8
9 PATH="/bin:/sbin:/usr/bin:/usr/sbin"
10 TMP=$(mktemp 2>/dev/null)
11 TMPFILE=$(mktemp 2>/dev/null)
12 ADDEDBYGRML="# Added by GRML"
13 MNTFILE="/mnt/.grml-auto-created_do-not-delete-this-file"
14 MOUNTPOINT_PREFIX=/mnt/
15
16
17 logit() {
18   if [ -x /usr/bin/logger ] ; then
19      /usr/bin/logger -i -t rebuildfstab "$*"
20   fi
21 }
22
23 bailout() {
24    [ -n "$1" ] && EXITCODE="$1" || EXITCODE=1
25    rm -f $TMP $TMPFILE /var/run/rebuildfstab.pid
26    exit $EXITCODE
27 }
28
29 echo "$$" > /var/run/rebuildfstab.pid
30
31
32 umask 022
33
34
35 [ -e /etc/grml/autconfig ] && . /etc/grml/autoconfig
36
37 if ! [ -w "$(dirname $MNTFILE)" ] ; then
38    echo "$0: can not write to $MNTFILE">&2
39    logit "can not write to $MNTFILE"
40    bailout 10
41 fi
42
43 if [ -z "$TMP" -o -z "$TMPFILE" ] ; then
44    echo "$0: could not create tmpfile (is / mounted read-only?), exiting" >&2
45    logit 'fatal - could not create tmpfile (is / mounted read-only?), exiting'
46    bailout 3
47 fi
48
49 # trap "rm -f $TMP $TMPFILE" 2 3 11
50
51
52 if grep -q " nofstab" /proc/cmdline ; then
53    bailout 0
54 fi
55
56 grep -q 'nolabel' /proc/cmdline && NOLABEL='TRUE' || LABEL='TRUE'
57
58
59
60 if [ "$CONFIG_FSTAB" = "no" ] ; then
61     bailout 0
62 fi
63
64
65 if grep -q ' /mnt ' /proc/mounts ; then
66    logit '/mnt is a mounted directory, exiting.'
67    bailout 1
68 fi
69
70 # make sure we have the $MNTFILE before reading/writing into it
71 if [ -z "$NO_AUTO_FILE" ] ; then
72    [ -r $MNTFILE ] || touch $MNTFILE
73 fi
74
75
76 # make sure we have a /etc/fstab at all
77 (
78     flock -x 200
79 if ! [ -f /etc/fstab ] ; then
80   cat > /etc/fstab << EOF
81 # /etc/fstab - static file system information
82 # <filesystem> <mountpoint>   <type> <options>                             <dump> <pass>
83 proc           /proc          proc   rw,nosuid,nodev,noexec                 0      0
84 none           /proc/bus/usb  usbfs  defaults,noauto                        0      0
85 sysfs          /sys           sysfs  rw,nosuid,nodev,noexec                 0      0
86 devpts         /dev/pts       devpts noauto,mode=0622                       0      0
87 /dev/fd0       /mnt/floppy    auto   user,noauto,exec                       0      0
88 /dev/external  /mnt/external  auto   user,noauto,exec,rw,uid=grml,gid=grml  0      0
89 /dev/external1 /mnt/external1 auto   user,noauto,exec,rw,uid=grml,gid=grml  0      0
90 /dev/cdrom     /mnt/cdrom     auto   user,noauto,exec,ro                    0      0
91 /dev/dvd       /mnt/dvd       auto   user,noauto,exec,ro                    0      0
92 # some other examples:
93 # /dev/hda1      /Grml         ext3    dev,suid,user,noauto 0  2
94 # //1.2.3.4/pub  /smb/pub      smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
95 # linux:/pub     /beer         nfs     defaults             0  0
96 # tmpfs          /tmp          tmpfs   size=300M            0  0
97 # none           /proc/bus/usb usbfs   defaults,nodev,noexec,nosuid,noauto,devgid=1001,devmode=664 0 0
98 # 192.168.1.101:/backups /mnt/nfs nfs  defaults,user,wsize=8192,rsize=8192 0 0
99 #
100 # Warning! Please do *not* change any lines below because they are auto-generated by rebuildfstab!
101 # If you want to disable rebuildfstab set CONFIG_FSTAB='no' in /etc/grml/autoconfig!
102 # See 'man grml-rebuildfstab' for more details about the following entries.
103 EOF
104 fi
105 ) 200>/var/run/rebuildfstab.lock
106
107 [ ! -w /etc/fstab ] && {
108   logit "fatal - /etc/fstab not writeable, exiting"
109   bailout 0
110 }
111
112 # Simple shell grep, searches for lines STARTING with string
113 stringinfile () {
114   while read line ;do
115     case "$line" in
116             ($1*) return 0 ;;
117     esac
118   done < "$2"
119   return 1
120 }
121
122 # Remove comment line $1 and the following line from file $2
123 # sed '/^# Added by GRML/{N;d;}'
124 removeentries () {
125         entry="$1"
126         entry=$(echo $1 |  sed "s#/#\\\/#g")
127         (
128                 flock -x 200
129                 sed -i -e "/$entry/{N;d}" /etc/fstab
130         ) 200>/var/run/rebuildfstab.lock
131 }
132
133
134 get_device_name() {
135 case $ID_FS_TYPE in
136 # dont use labels for some filesystem
137 iso9660)
138 break;
139 ;;
140 *)
141 if [ -n "$LABEL" ]  && [ -n "$ID_FS_LABEL_ENC" ] ; then
142   # see check_for_label() in scanpartitions for details
143   case $ID_FS_LABEL_ENC in
144     *\ *|*\x20*)
145       addinfo=" # space char inside label ($ID_FS_LABEL_ENC) not supported"
146       break ;;
147     /)
148       addinfo=" # label '/' not supported"
149       break ;;
150   esac
151
152   echo "LABEL=$ID_FS_LABEL_ENC"
153   return
154 fi
155 ;;
156 esac
157 [ -n "$DM_NAME" ] && echo /dev/mapper/$DM_NAME && return
158
159 NAME="$DEVNAME"
160 for name in $DEVLINKS ; do
161     case $name in
162         *usb*|*cdrom*|*dvd*)
163             NAME="$name"
164             break;
165         ;;
166 esac
167 done
168 echo "$NAME"
169 }
170
171 get_mount_point() {
172     device="$1"
173     mountpoint="${device##*/}"
174     mountpoint="${mountpoint#*=}"
175     echo /mnt/$mountpoint
176 }
177
178 append_fstab() {
179         (
180                 flock -x 200
181                 echo $ADDEDBYGRML $1 >> /etc/fstab
182                 echo "$2" >> /etc/fstab
183         ) 200>/var/run/rebuildfstab.lock
184
185 }
186
187
188 if [ -n "$CONFIG_FSTAB_USER" ] ; then
189     user="$CONFIG_FSTAB_USER"
190 else
191     user='1000' # take default
192 fi
193 if [ -n "$CONFIG_FSTAB_GROUP" ] ; then
194     group="$CONFIG_FSTAB_GROUP"
195 else
196     group='users' # take default
197 fi
198
199 if [ -z "$ACTION" ] ; then
200     logit "Seems that $0 is not run in a udev environment, exiting." >&2
201     bailout 1
202 fi
203
204 # ignore loop devices for now.
205 case $DEVNAME in
206 /dev/loop*)
207         bailout 0
208         ;;
209 esac
210
211 removeentries "$ADDEDBYGRML $DEVNAME" /etc/fstab
212
213 device=$(get_device_name)
214 mountpoint=$(get_mount_point $device)
215
216 # if entry is already present in /etc/fstab ignore the current event
217 (
218     flock -x 200
219     grep -v '^#' /etc/fstab > $TMPFILE
220 ) 200>/var/run/rebuildfstab.lock
221
222 for devicelink in $DEVLINKS ; do
223     # ignore external
224     case "$devicelink" in *external*) continue ;; esac
225     stringinfile $devicelink $TMPFILE && bailout 0
226 done
227
228 # if entry is already present ignore it
229 stringinfile $device $TMPFILE && bailout 0
230
231 options=noauto,user,dev,suid,exec
232 case $ID_FS_TYPE in
233 ntfs)
234         options=$options,ro,umask=000
235         ;;
236 vfat|msdos)
237         options=$options,umask=000
238         ;;
239 ufs)
240         options=$options,ro
241         ;;
242 swap)
243         options=defaults
244         ;;
245 esac
246 case $ID_FS_TYPE in
247 ntfs|vfat|msdos)
248         test -n "$user" && options=$options,uid=$user
249         test -n "$group" && options=$options,gid=$group
250         ;;
251 esac
252
253 case $ACTION in
254 add|change)
255 (
256     flock -x 200
257     echo "$ADDEDBYGRML $DEVNAME" >> /etc/fstab
258     echo "$device $mountpoint $ID_FS_TYPE $options 0 0 $addinfo # $DEVNAME" >> /etc/fstab
259 ) 200>/var/run/rebuildfstab.lock
260
261
262 ;;
263 esac
264
265 # Clean /mnt/... according to info $MNTFILE
266 if [ -r "$MNTFILE" ] ; then
267    for directory in $(cat $MNTFILE) ; do
268      rmdir $directory 2>/dev/null && sed -i "s#$directory##" $MNTFILE
269    done
270    grep '^/.*$' $MNTFILE | sort -u > $TMPFILE
271    cat $TMPFILE > $MNTFILE
272 fi
273
274 # Make sure we have all /mnt/* directories
275 while read p m f relax; do
276   case "$m" in *none*|*proc*|*sys*|'') continue ;; esac
277   if ! grep -q "$m" /proc/mounts ; then
278      if ! [ -d "$m" ] ; then
279         if mkdir -m 755 -p "$m" ; then
280           [ -r "$MNTFILE" ] && grep -q "$m" $MNTFILE || echo "$m" >> $MNTFILE
281         fi
282      fi
283   fi
284 done <<EOT
285 $(cat /etc/fstab | grep -v '^#')
286 EOT
287
288 # sort the file again...
289 if [ -r "$MNTFILE" ] ; then
290    grep '^/.*$' $MNTFILE | sort -u > $TMPFILE
291    cat $TMPFILE > $MNTFILE
292 fi
293
294 bailout 0
295
296 ## END OF FILE #################################################################