Typo
[linux_from_scratch_hints.git] / OLD / mtab-pre-lfs-4.1.txt
blob13e3be8b62dd9d3e0268133fd59e71125e05d01b
1 TITLE:          How to get rid of the /etc/mtab -> /proc/mounts symlink
2 LFS VERSION:    LFS-3.2-RC1 to LFS CVS 2003-02-02 (pre LFS 4.1)
3 AUTHOR:         Oliver Brakmann <obrakmann@gmx.net>
5 LAST CHANGES:   2003-02-27: Moved to old/
6                 2003-01-02: Minor stylistic changes
7                 2003-01-02: Added a note for glibc 2.3.1 users
8                 2002-11-14: Minor stylistic changes (thanks to DJ Lucas)
9                 2002-08-06: Restructured the whole hint
10                 2002-08-04: Corrected a few technical details
11                 2002-08-04: Minor stylistic changes
12                 2002-07-30: Added a note for those who do not want to
13                             recompile glibc
14                 2002-07-30: rephrased some parts to prevent misunderstanding
15                 2002-07-29: fixed a couple of typos
17 SYNOPSIS:
18         A default LFS install sets up /etc/mtab as a symlink to /proc/mounts,
19         which breaks (u)mount's behaviour.  This hints tells you how to
20         solve this problem elegantly, even on a read-only root partition.
22 HINT:
24 !!      Important note: Since 2003-02-03, the LFS Book uses a real /etc/mtab
25         file again.  The first public release including that change will be
26         LFS 4.1.  Because of that, this hint has become obsolete in parts. 
27         For an mtab hint that applies to recent LFS versions, take a look at
28         the new hint at <http://hints.linuxfromscratch.org>.
31 CONTENTS
32 ========
34 1. INTRODUCTION
35 2. MOTIVATION
36 3. WRITEABLE ROOT
37 4. READ-ONLY ROOT
38 5. CONCLUSION
41 1. INTRODUCTION
42 ===============
44 This hint really covers two issues:
45 1. getting rid of that annoying symlink
46 2. doing so on a read-only root partition.
48 Because the instructions to achieve the second goal (discussed in chapter 4)
49 include those from chapter 3, albeit in slightly modified form, you will find
50 that a lot of information is more or less duplicated in this hint.  I did not
51 know how to solve this any other way without this hint becoming way too
52 confusing.
54 OK, as a final note before we begin: keep in mind that I take no responsibility
55 whatsoever for any damage done to your computer.  Be careful doing this, you
56 might badly screw your system.
59 2. MOTIVATION
60 =============
62 If /etc/mtab is a link to /proc/mounts, the following issues with the
63 mount/umount programs show up:
65         - the fstab `user' option does not work
66           (you have to specify `users' as a workaround)
67         - loop devices are not freed properly when unmounting
68           (you have to use `umount -d' or run `losetup -d' manually)
69         - certain kernel versions under certain circumstances list /dev/root
70           instead of the real root device in /proc/mounts.
72 The reasons one might want to have /etc/mtab as a link to /proc/mounts
73 are the following:
75         - if, for some reason, the system crashes and you have to do
76           a hard reboot, /etc/mtab will not contain inconsistent data upon
77           the next boot.
78         - you can mount your root partition read-only.
80 However, those two points can also be dealt with without the need for
81 the symlink.
84 3. WRITEABLE ROOT
85 =================
87 Follow these instructions if your / and /var directories are on the same
88 partition.  If they are not, but your / is writeable nevertheless, go to
89 chapter 4 anyway.  If you want to have a read-only / one day, you do not
90 need to start all over again with this.
93         1. Switch to single-user mode
95                 # telinit 1
97         2. Remove /etc/mtab
99                 # rm /etc/mtab
101            Be careful: from this point on, you lose all information about the
102            currently mounted filesystems (that umount can work with, at least)!
103            If you have any filesystems mounted that do not get mounted at boot-
104            time (ie. by the /etc/rc.d/init.d/mountfs script), unmount them
105            _before_ this step! If you are not sure what you have mounted, you
106            can still take a look at /proc/mounts.
108         3. Create an empty /etc/mtab
110                 # touch /etc/mtab
111                 # chown root:root /etc/mtab
112                 # chmod 644 /etc/mtab
114         4. Now you have to modify your init scripts so that they basically
115            do this:
117                 1. mount -n /proc       # this is in /etc/rc.d/init.d/mountproc
118                 2. fsck                 # this is in /etc/rc.d/init.d/checkfs
119                 3. remount -n / rw      # so that we can write to /
120                 4. > /etc/mtab          # clear /etc/mtab
121                 5. mount -f /           # update mtab with the already
122                 6. mount -f /proc       # mounted partitions.
123                 7. mount -a             # mount all remaining partitions
125            
126            The first step requires a change in /etc/rc.d/init.d/mountproc,
127            whereas the second one does not require any changes.
128            The remainder is handled in /etc/rc.d/init.d/mountfs:
130 ,---[ /etc/rc.d/init.d/mountfs ]------->8--------------------------------------
131 #!/bin/bash
132 # Begin $rc_base/init.d/mountfs - File System Mount Script
134 # Based on mountfs script from LFS-3.1 and earlier.
135 # Rewritten by Gerard Beekmans  - gerard@linuxfromscratch.org
137 source /etc/sysconfig/rc
138 source $rc_functions
140 case "$1" in
141         start)
142                 echo "Remounting root file system in read-write mode..."
143                 mount -n -o remount,rw /
144                 evaluate_retval
146                 echo "Cleaning /etc/mtab..."
147                 > /etc/mtab &&
148                 mount -f -o remount,rw / &&
149                 mount -f /proc
150                 evaluate_retval
152                 # The following mount command will mount all file systems.  If
153                 # you have other (network based) file system that should not
154                 # be or cannot be mounted at this time, add them to the NO_FS
155                 # variable below.  All file systems that are added to the
156                 # variable in the form of no<filesystem> will be skipped.
158                 NO_FS="nonfs,nosmbfs,noproc"
159                 echo "Mounting remaining file systems..."
160                 mount -a -t $NO_FS
161                 evaluate_retval
162                 ;;
164 [...remainder of the script omitted...]
165 `-------------------------------------->8--------------------------------------
167            If you use another set of init scripts, you are surely competent
168            enough to know how to implement these changes in your scripts.
170         5. Modify your /etc/fstab so that /proc does not get mounted by
171            mount -a:
173                 proc                    /proc proc noauto       0 0
175            The important part here is the `noauto' keyword.
176            While this has nothing to do with the task at hand and is not
177            really necessary with the lfs-bootscripts, it is good to have
178            though, in case you ever run `mount -a' from outside of any
179            bootscripts (like in the following step :-).
181         6. If you have not unmounted any other partitions during the process,
182            all those listed in /etc/fstab should still be mounted.  Check out
183            /proc/mounts to be sure.  The userland tools do not know all this
184            anymore, so we have to tell them:
186                 # mount -f /
187                 # mount -f /proc
188                 # mount -af
190            Be sure that /etc/mtab and /proc/mounts contain consistent data
191            after this step.
193         7. You can telinit back to your favourite runlevel now.  Enjoy!
197 4. READ-ONLY ROOT
198 =================
200 Follow these instructions if you have your / and /var directories on
201 separate partitions.
204         1. Switch to single-user mode
206                 # telinit 1
208         2. Remount / read-write
210                 # mount -o remount,rw /
212         3. Rebuild glibc with the following patch applied:
213            (or just do the change by hand, which is probably faster *g*)
215            Please note that this patch works with both glibc 2.2.5 and 2.3.1.
217 ,---[ glibc.mtab-patch ]--------------->8--------------------------------------
218 diff -Naur glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h.orig glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h
219 --- glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h.orig    2000-11-16 00:06:47.000000000 +0100
220 +++ glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h 2003-01-02 13:37:12.000000000 +0100
221 @@ -51,7 +51,7 @@
222  #define        _PATH_MAN       "/usr/share/man"
223  #define        _PATH_MEM       "/dev/mem"
224  #define        _PATH_MNTTAB    "/etc/fstab"
225 -#define        _PATH_MOUNTED   "/etc/mtab"
226 +#define        _PATH_MOUNTED   "/var/lib/misc/mtab"
227  #define        _PATH_NOLOGIN   "/etc/nologin"
228  #define        _PATH_PRESERVE  "/var/lib"
229  #define        _PATH_RWHODIR   "/var/spool/rwho"
230 `-------------------------------------->8--------------------------------------
232         Look up the installation instructions for glibc in the LFS Book.
234         This patch makes newly compiled programs look for the mtab file
235         in /var/lib/misc/mtab.
237         4. Now unmount /var
239                 # umount /var
241         5. Create /var/lib/misc/mtab.  This file should contain only
242            one line that lists your root partition mounted read-only.
243            On my system, using devfs, it looked like this:
245                 # mkdir -p /var/lib/misc
246                 # echo "/dev/discs/disc0/part2 / ext3 ro 0 0" > /var/lib/misc/mtab
247                 # chown root:root /var/lib/misc/mtab
248                 # chmod 644 /var/lib/misc/mtab
250            Substitute your root device and file system according to your setup.
252         6. To prevent scripts and broken code from failing, create
253            /etc/mtab as a link to /var/lib/misc/mtab
255                 # ln -sf ../var/lib/misc/mtab /etc/mtab
257            Be careful: from this point on, you lose all information about the
258            currently mounted filesystems (that umount can work with, at least)!
259            If you have any filesystems mounted that do not get mounted at boot-
260            time (ie. by the /etc/rc.d/init.d/mountfs script), unmount them
261            _before_ this step! If you are not sure what you have mounted, you
262            can still take a look at /proc/mounts.
264         7. At this point you have to mount /var again
266                 # mount -n /var
268         8. Create an empty /var/lib/misc/mtab
270                 # touch /var/lib/misc/mtab
271                 # chown root:root /var/lib/misc/mtab
272                 # chmod 644 /var/lib/misc/mtab
274         9. Now you have to modify your init scripts so that they basically
275            do this:
277                 1. mount -n /proc       # this is in /etc/rc.d/init.d/mountproc
278                 2. fsck                 # this is in /etc/rc.d/init.d/checkfs
279                 3. mount -n /var        # so that /var/lib/misc/mtab shows up
280                 4. > /var/lib/misc/mtab # clear /var/lib/misc/mtab
281                 5. mount -f /           # update mtab with the already
282                 6. mount -f /proc       # mounted partitions.
283                 7. mount -f /var        #
284                 8. mount -a             # mount all remaining partitions
286            The first step requires a change in /etc/rc.d/init.d/mountproc,
287            whereas the second one does not require any changes.
288            The remainder is handled in /etc/rc.d/init.d/mountfs:
290 ,---[ /etc/rc.d/init.d/mountfs ]------->8--------------------------------------
291 #!/bin/bash
292 # Begin $rc_base/init.d/mountfs - File System Mount Script
294 # Based on mountfs script from LFS-3.1 and earlier.
295 # Rewritten by Gerard Beekmans  - gerard@linuxfromscratch.org
297 source /etc/sysconfig/rc
298 source $rc_functions
300 case "$1" in
301         start)
302                 # Uncomment the following three lines if you do not have
303                 # a read only root filesystem.
305                 # echo "Remounting root file system in read-write mode..."
306                 # mount -n -o remount,rw /
307                 # evaluate_retval
309                 echo "Cleaning /var/lib/misc/mtab..."
310                 mount -n /var &&
311                 > /var/lib/misc/mtab &&
312                 mount -f / &&
313                 mount -f /proc &&
314                 mount -f /var
315                 evaluate_retval
317                 # The following mount command will mount all file systems.  If
318                 # you have other (network based) file system that should not
319                 # be or cannot be mounted at this time, add them to the NO_FS
320                 # variable below.  All file systems that are added to the
321                 # variable in the form of no<filesystem> will be skipped.
323                 NO_FS="nonfs,nosmbfs,noproc"
324                 echo "Mounting remaining file systems..."
325                 mount -a -t $NO_FS
326                 evaluate_retval
327                 ;;
329 [...remainder of the script omitted...]
330 `-------------------------------------->8--------------------------------------
332             If you use another set of init scripts, you are surely competent
333             enough to know how to implement these changes in your scripts.
335         10. Modify your /etc/fstab so that /proc and /var do not get mounted by
336             mount -a:
338                 /dev/discs/disc0/part7  /var  xfs  noauto       0 0
339                 proc                    /proc proc noauto       0 0
341             The important part here is the `noauto' keyword.
342             This is, again, what it looks like on my system.  Change it
343             so that it fits your system.
344             Note that with the lfs-bootscripts, /proc does not really need
345             the `noauto' option.  It is good to have it though, in case you
346             ever run `mount -a' from outside of any bootscripts (like in
347             step 13 :-).  So while you are at it...
349         11. Rebuild util-linux so that mount, umount and friends are
350             aware of mtab's new location.  There are no patches required,
351             just look up the instructions in the LFS Book.
353             Other packages you might want to rebuild are the GNU fileutils
354             (df) and other programs that might access /etc/mtab.
355             As long as these programs do not write to that file, it is not
356             absolutely required, though.  That is why we set up that symlink.
358         12. If you have a read-only root, remount / read-only now, since 
359             we are not changing any more files.
361                 # mount -n -o remount,ro /
363         13. If you have not unmounted any other partitions during the process,
364             all those listed in /etc/fstab should still be mounted.  Check out
365             /proc/mounts to be sure.  The userland tools do not know all this
366             anymore, so we have to tell them:
368                 # mount -f /
369                 # mount -f /proc
370                 # mount -f /var
371                 # mount -af
373             Be sure that /etc/mtab and /proc/mounts contain consistent data
374             after this step.
376         14. Finally done! You can telinit back to your favourite run-level now.
380 CONCLUSION
381 ==========
383 Credits are due to Seth W. Klein for teaching me through the process in the
384 first place and further corrections and suggestions.  Also thanks to DJ Lucas,
385 Jesse Tie-Ten-Quee, Richard Lightman and Wouter Vanwalleghem for additional
386 feedback.
388 If you have any questions, comments or suggestions regarding this hint, write
389 to me at <obrakmann@gmx.net> or to the blfs-support mailing list.
391 I hope this helps some people out there.
393 Oliver Brakmann