Create the standard directories in the rebuildfstab script instead of grml-autoconfig.
[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 MOUNTPOINT_PREFIX=/mnt
14 MNTFILE="$MOUNTPOINT_PREFIX/.grml-auto-created_do-not-delete-this-file"
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 ' $MOUNTPOINT_PREFIX ' /proc/mounts ; then
66    logit '$MOUNTPOINT_PREFIX is a mounted directory, exiting.'
67    bailout 1
68 fi
69
70 # default directories {{{
71 if [ -d "$MOUNTPOINT_PREFIX" ] ; then
72    [ -d "$MOUNTPOINT_PREFIX/floppy" ] || mkdir "$MOUNTPOINT_PREFIX/floppy"
73    [ -d "$MOUNTPOINT_PREFIX/cdrom" ]  || mkdir "$MOUNTPOINT_PREFIX/cdrom"
74    [ -d "$MOUNTPOINT_PREFIX/test" ]   || mkdir "$MOUNTPOINT_PREFIX/test"
75 fi
76 # }}}
77
78 # make sure we have the $MNTFILE before reading/writing into it
79 if [ -z "$NO_AUTO_FILE" ] ; then
80    [ -r $MNTFILE ] || touch $MNTFILE
81 fi
82
83
84 # make sure we have a /etc/fstab at all
85 (
86     flock -x 200
87 if ! [ -f /etc/fstab ] ; then
88   cat > /etc/fstab << EOF
89 # /etc/fstab - static file system information
90 # <filesystem> <mountpoint>   <type> <options>                             <dump> <pass>
91 proc           /proc          proc   rw,nosuid,nodev,noexec                 0      0
92 none           /proc/bus/usb  usbfs  defaults,noauto                        0      0
93 sysfs          /sys           sysfs  rw,nosuid,nodev,noexec                 0      0
94 devpts         /dev/pts       devpts noauto,mode=0622                       0      0
95 /dev/external  $MOUNTPOINT_PREFIX/external  auto   user,noauto,exec,rw,uid=grml,gid=grml  0      0
96 /dev/external1 $MOUNTPOINT_PREFIX/external1 auto   user,noauto,exec,rw,uid=grml,gid=grml  0      0
97 /dev/cdrom     $MOUNTPOINT_PREFIX/cdrom     auto   user,noauto,exec,ro                    0      0
98 /dev/dvd       $MOUNTPOINT_PREFIX/dvd       auto   user,noauto,exec,ro                    0      0
99 # some other examples:
100 # /dev/sda1      /Grml         ext3    dev,suid,user,noauto 0  2
101 # //1.2.3.4/pub  /smb/pub      smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
102 # linux:/pub     /beer         nfs     defaults             0  0
103 # tmpfs          /tmp          tmpfs   size=300M            0  0
104 # none           /proc/bus/usb usbfs   defaults,nodev,noexec,nosuid,noauto,devgid=1001,devmode=664 0 0
105 # 192.168.1.101:/backups /mnt/nfs nfs  defaults,user,wsize=8192,rsize=8192 0 0
106 #
107 # Warning! Please do *not* change any lines below because they are auto-generated.
108 # If you want to disable rebuildfstab set CONFIG_FSTAB='no' in /etc/grml/autoconfig!
109 # See 'man grml-udev-rebuildfstab' for more details about the following entries.
110 EOF
111 fi
112 ) 200>/var/run/rebuildfstab.lock
113
114 [ ! -w /etc/fstab ] && {
115   logit "fatal - /etc/fstab not writeable, exiting"
116   bailout 0
117 }
118
119 # Simple shell grep, searches for lines STARTING with string
120 stringinfile () {
121   while read line ;do
122     case "$line" in
123             ($1*) return 0 ;;
124     esac
125   done < "$2"
126   return 1
127 }
128
129 # Remove comment line $1 and the following line from file $2
130 # sed '/^# Added by GRML/{N;d;}'
131 removeentries () {
132         entry="$1"
133         entry=$(echo $1 |  sed "s#/#\\\/#g")
134         (
135                 flock -x 200
136                 sed -i -e "/$entry/{N;d}" /etc/fstab
137         ) 200>/var/run/rebuildfstab.lock
138 }
139
140
141 get_device_name() {
142 case $ID_FS_TYPE in
143 # dont use labels for some filesystem
144 iso9660)
145 break;
146 ;;
147 *)
148 if [ -n "$LABEL" ]  && [ -n "$ID_FS_LABEL_ENC" ] ; then
149   # see check_for_label() in scanpartitions for details
150   case $ID_FS_LABEL_ENC in
151     *\x*)
152       addinfo=" # special char in label ($ID_FS_LABEL_ENC) not supported"
153       break ;;
154     *)
155       echo "LABEL=$ID_FS_LABEL_ENC"
156       return
157       break;
158   esac
159 fi
160 ;;
161 esac
162 [ -n "$DM_NAME" ] && echo /dev/mapper/$DM_NAME && return
163
164 NAME="$DEVNAME"
165 for name in $DEVLINKS ; do
166     case $name in
167         *usb-sd*|*cdrom*|*dvd*)
168             NAME="$name"
169             break;
170         ;;
171 esac
172 done
173 echo "$NAME"
174 }
175
176 get_mount_point() {
177     device="$1"
178     mountpoint="${device##*/}"
179     mountpoint="${mountpoint#*=}"
180     echo "$MOUNTPOINT_PREFIX/$mountpoint"
181 }
182
183 append_fstab() {
184         (
185                 flock -x 200
186                 echo $ADDEDBYGRML $1 >> /etc/fstab
187                 echo "$2" >> /etc/fstab
188         ) 200>/var/run/rebuildfstab.lock
189
190 }
191
192
193 if [ -n "$CONFIG_FSTAB_USER" ] ; then
194     user="$CONFIG_FSTAB_USER"
195 else
196     user='1000' # take default
197 fi
198 if [ -n "$CONFIG_FSTAB_GROUP" ] ; then
199     group="$CONFIG_FSTAB_GROUP"
200 else
201     group='users' # take default
202 fi
203
204 if [ -z "$ACTION" ] ; then
205     logit "Seems that $0 is not run in a udev environment, exiting." >&2
206     bailout 1
207 fi
208
209 # ignore loop devices for now.
210 case $DEVNAME in
211 /dev/loop*)
212         bailout 0
213         ;;
214 esac
215
216 removeentries "$ADDEDBYGRML $DEVNAME" /etc/fstab
217
218 device=$(get_device_name)
219 mountpoint=$(get_mount_point $device)
220
221 # if entry is already present in /etc/fstab ignore the current event
222 (
223     flock -x 200
224     grep -v '^#' /etc/fstab > $TMPFILE
225 ) 200>/var/run/rebuildfstab.lock
226
227 for devicelink in $DEVLINKS ; do
228     # ignore external
229     case "$devicelink" in *external*) continue ;; esac
230     stringinfile $devicelink $TMPFILE && bailout 0
231 done
232
233 # if entry is already present ignore it
234 stringinfile $device $TMPFILE && bailout 0
235
236 if [ "$ID_FS_USAGE" != "filesystem" -a "$ID_FS_TYPE" != "swap" ]; then
237     # blockdevice in question won't be mountable in this case
238     bailout 0
239 fi
240
241 options=noauto,user,dev,suid,exec
242 case $ID_FS_TYPE in
243 ntfs)
244         options=$options,ro,umask=000
245         ;;
246 vfat|msdos)
247         options=$options,umask=000
248         ;;
249 ufs)
250         options=$options,ro
251         ;;
252 swap)
253         options=defaults
254         ;;
255 esac
256 case $ID_FS_TYPE in
257 ntfs|vfat|msdos)
258         test -n "$user" && options=$options,uid=$user
259         test -n "$group" && options=$options,gid=$group
260         ;;
261 esac
262
263 case $ACTION in
264 add|change)
265 (
266     flock -x 200
267     echo "$ADDEDBYGRML $DEVNAME" >> /etc/fstab
268     echo "$device $mountpoint $ID_FS_TYPE $options 0 0 $addinfo # $DEVNAME" >> /etc/fstab
269 ) 200>/var/run/rebuildfstab.lock
270
271
272 ;;
273 esac
274
275 # Clean /mnt/... according to info $MNTFILE
276 if [ -r "$MNTFILE" ] ; then
277    for directory in $(cat $MNTFILE) ; do
278      rmdir $directory 2>/dev/null && sed -i "s#$directory##" $MNTFILE
279    done
280    grep '^/.*$' $MNTFILE | sort -u > $TMPFILE
281    cat $TMPFILE > $MNTFILE
282 fi
283
284 # Make sure we have all /mnt/* directories
285 while read p m f relax; do
286   case "$m" in *none*|*proc*|*sys*|'') continue ;; esac
287   if ! grep -q "$m" /proc/mounts ; then
288      if ! [ -d "$m" ] ; then
289         if mkdir -m 755 -p "$m" ; then
290           [ -r "$MNTFILE" ] && grep -q "$m" $MNTFILE || echo "$m" >> $MNTFILE
291         fi
292      fi
293   fi
294 done <<EOT
295 $(cat /etc/fstab | grep -v '^#')
296 EOT
297
298 # sort the file again...
299 if [ -r "$MNTFILE" ] ; then
300    grep '^/.*$' $MNTFILE | sort -u > $TMPFILE
301    cat $TMPFILE > $MNTFILE
302 fi
303
304 bailout 0
305
306 ## END OF FILE #################################################################