Tizen 2.1 base
[platform/upstream/sysvinit.git] / debian / src / initscripts / etc / init.d / mtab.sh
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          mtab
4 # Required-Start:    checkroot
5 # Required-Stop:
6 # Default-Start:     S
7 # Default-Stop:
8 # Short-Description: Update mtab file.
9 # Description:       Update the mount program's mtab file after
10 #                    all local filesystems have been mounted.
11 ### END INIT INFO
12
13 #
14 # The main purpose of this script is to update the mtab file to reflect
15 # the fact that virtual filesystems were mounted early on, before mtab
16 # was writable.
17 #
18
19 ## We don't need this on SLP platform
20 [ -e /etc/init.d/.slp ] && exit 0
21
22 PATH=/sbin:/bin
23 . /lib/init/vars.sh
24
25 TTYGRP=5
26 TTYMODE=620
27 [ -f /etc/default/devpts ] && . /etc/default/devpts
28
29 TMPFS_SIZE=
30 [ -f /etc/default/tmpfs ] && . /etc/default/tmpfs
31
32 KERNEL="$(uname -s)"
33
34 . /lib/lsb/init-functions
35 . /lib/init/mount-functions.sh
36
37 # $1 - fstype
38 # $2 - mount point
39 # $3 - mount name/device
40 # $4 - mount options
41 domtab ()
42 {
43         # Directory present?
44         if [ ! -d $2 ]
45         then
46                 return
47         fi
48
49         # Not mounted?
50         if ! mountpoint -q $2 < /dev/null
51         then
52                 return
53         fi
54
55         if [ -n "$3" ]
56         then
57                 NAME="$3"
58         else
59                 NAME="$1"
60         fi
61
62         # Already recorded?
63         if ! grep -E -sq "^([^ ]+) +$2 +" /etc/mtab < /dev/null
64         then
65                 mount -f -t $1 $OPTS $4 $NAME $2 < /dev/null
66         fi
67 }
68
69 do_start () {
70         DO_MTAB=""
71         MTAB_PATH="$(readlink -f /etc/mtab || :)"
72         case "$MTAB_PATH" in
73           /proc/*)
74                 # Assume that /proc/ is not writable
75                 ;;
76           /*)
77                 # Only update mtab if it is known to be writable
78                 # Note that the touch program is in /usr/bin
79                 #if ! touch "$MTAB_PATH" >/dev/null 2>&1
80                 #then
81                 #       return
82                 #fi
83                 ;;
84           "")
85                 [ -L /etc/mtab ] && MTAB_PATH="$(readlink /etc/mtab)"
86                 if [ "$MTAB_PATH" ]
87                 then
88                         log_failure_msg "Cannot initialize ${MTAB_PATH}."
89                 else
90                         log_failure_msg "Cannot initialize /etc/mtab."
91                 fi
92                 ;;
93           *)
94                 log_failure_msg "Illegal mtab location '${MTAB_PATH}'."
95                 ;;
96         esac
97
98         #
99         # Initialize mtab file if necessary
100         #
101         if [ ! -f /etc/mtab ]
102         then
103                 :> /etc/mtab
104                 chmod 644 /etc/mtab
105         fi
106         if selinux_enabled && [ -x /sbin/restorecon ] && [ -r /etc/mtab ]
107         then
108                 restorecon /etc/mtab
109         fi
110
111         # S02mountkernfs.sh
112         RW_OPT=
113         [ "${RW_SIZE:=$TMPFS_SIZE}" ] && RW_OPT=",size=$RW_SIZE"
114         domtab tmpfs /lib/init/rw tmpfs -omode=0755,nosuid$RW_OPT
115
116         domtab proc /proc "proc" -onodev,noexec,nosuid
117         if grep -E -qs "sysfs\$" /proc/filesystems
118         then
119                 domtab sysfs /sys sysfs -onodev,noexec,nosuid
120         fi
121         if [ yes = "$RAMRUN" ] ; then
122                 RUN_OPT=
123                 [ "${RUN_SIZE:=$TMPFS_SIZE}" ] && RUN_OPT=",size=$RUN_SIZE"
124                 domtab tmpfs /var/run "varrun" -omode=0755,nosuid$RUN_OPT
125         fi
126         if [ yes = "$RAMLOCK" ] ; then
127                 LOCK_OPT=
128                 [ "${LOCK_SIZE:=$TMPFS_SIZE}" ] && LOCK_OPT=",size=$LOCK_SIZE"
129                 domtab tmpfs /var/lock "varlock" -omode=1777,nodev,noexec,nosuid$LOCK_OPT
130         fi
131         if [ -d /proc/bus/usb ]
132         then
133                 domtab usbfs /proc/bus/usb "procbususb"
134         fi
135
136         # S03udev
137         domtab tmpfs /dev "udev" -omode=0755
138
139         # S04mountdevsubfs
140         SHM_OPT=
141         [ "${SHM_SIZE:=$TMPFS_SIZE}" ] && SHM_OPT=",size=$SHM_SIZE"
142         domtab tmpfs /dev/shm tmpfs -onosuid,nodev$SHM_OPT
143         domtab devpts /dev/pts "devpts" -onoexec,nosuid,gid=$TTYGRP,mode=$TTYMODE
144
145         # Add everything else in /proc/mounts into /etc/mtab, with
146         # special exceptions.
147         exec 9<&0 0</proc/mounts
148         while read FDEV FDIR FTYPE FOPTS REST
149         do
150                 case "$FDIR" in
151                         /lib/modules/*/volatile)
152                                 FDEV="lrm"
153                                 ;;
154                         /dev/.static/dev)
155                                 # Not really useful to show in 'df',
156                                 # and it isn't accessible for non-root
157                                 # users.
158                                 continue
159                                 ;;
160                 esac
161                 domtab "$FTYPE" "$FDIR" "$FDEV" "-o$FOPTS"
162         done
163         exec 0<&9 9<&-
164 }
165
166 case "$1" in
167   start|"")
168         do_start
169         ;;
170   restart|reload|force-reload)
171         echo "Error: argument '$1' not supported" >&2
172         exit 3
173         ;;
174   stop)
175         # No-op
176         ;;
177   *)
178         echo "Usage: mountall-mtab.sh [start|stop]" >&2
179         exit 3
180         ;;
181 esac
182
183 :