mtab hint

Oliver Brakmann obrakmann at
Tue Jul 30 07:28:54 PDT 2002


updated the mtab hint: fixed a couple of typos and
rewrote some parts for better understanding.

/"\   ASCII Ribbon   | Reg. Linux User #198843  
\ /    Campaign -    | Reg.  LFS  User  #3082
 x    Against HTML   |
/ \ in mail and news | NP: Love like Blood - Slow motion
-------------- next part --------------
TITLE:		mtab
AUTHOR:		Oliver Brakmann <obrakmann at>

LAST CHANGES:	2002-07-30: Added a note for those who don't want to
			    recompile glibc
		2002-07-30: rephrased some parts to prevent misunderstanding
		2002-07-29: fixed a couple of typos

	A default LFS install sets up mtab as a symlink to /proc/mounts,
	which breaks (u)mount's behaviour. This hints tells you how to
	solve this problem elegantly.

	If /etc/mtab is a link to /proc/mounts, the following issues with
the mount/umount programs show up:

	- the fstab 'user' option doesn't work
	  (you have to specify 'users' as a workaround)
	- loop devices aren't freed properly when umounting
	  (you have to run 'losetup -d' manually)
	- there was some talk about a wrong root device listed in /etc/mtab,
	  though I never noticed that. Maybe someone would like to shed
	  some light on this for me.

The reasons one might want to have /etc/mtab as a link to /proc/mounts are
the following:

	- if, for some reason, the system crashes and you have to do
	  a hard reboot, /etc/mtab will not contain inconsistent data upon
	  the next boot.
	- you can mount your root partition read-only.

However, those two points can also be dealt with without the need for
the symlink.

The goal is to have a read-only root partition and an mtab file that isn't
a symlink to somewhere. To achieve this, do the following steps:

	1. switch to single-user mode

		$ telinit 1

	2. re-mount / read-write

		$ mount / -o remount,rw

	3. rebuild glibc with the following patch applied:
	   (or just do the change by hand, which is probably faster *g*)

--- glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h.orig    Sun Jul  7 12:22:31 2002
+++ glibc-2.2.5/sysdeps/unix/sysv/linux/paths.h Sun Jul  7 12:22:53 2002
@@ -51,7 +51,7 @@
#define        _PATH_MAN       "/usr/share/man"
#define        _PATH_MEM       "/dev/mem"
#define        _PATH_MNTTAB    "/etc/fstab"
-#define        _PATH_MOUNTED   "/etc/mtab"
+#define        _PATH_MOUNTED   "/var/lib/misc/mtab"
#define        _PATH_NOLOGIN   "/etc/nologin"
#define        _PATH_PRESERVE  "/var/lib"
#define        _PATH_RWHODIR   "/var/spool/rwho"

	Look up the installation instructions for glibc in the LFS Book.

	This patch makes newly compiled programs look for the mtab file
	in /var/lib/misc/mtab.

	If you absolutely don't want to rebuild glibc, you can probably just
	patch /usr/include/paths.h instead of this step.
	This is, however, untested.

	4. now umount /var

		$ umount /var

	5. create /var/lib/misc/mtab. This file should contain only
	   one line that lists your root partition mounted read-only.
	   On my system, using devfs, it looked like this:

		$ mkdir -p /var/lib/misc
		$ echo "/dev/discs/disc0/part2 / ext3 ro 0 0" > /var/lib/misc/mtab
		$ chown root:root /var/lib/misc/mtab
		$ chmod 644 /var/lib/misc/mtab

	   Substitute your root device and file system according to your setup.

	6. to prevent scripts and broken code from failing, create
	   /etc/mtab as a link to /var/lib/misc/mtab

		$ ln -sf ../var/lib/misc/mtab /etc/mtab

	   Be careful: from this point on, you lose all information about the
	   currently mounted partitions (that umount can work with, at least)!
	   If you have anything mounted that does not get mounted at boot-time
	   (ie. by the /etc/rc.d/init.d/mountfs script), umount it _before_
	   this step!

	7. at this point you have to mount /var again

		$ mount -n /var

	8. create an empty /var/lib/misc/mtab

		$ touch /var/lib/misc/mtab
		$ chown root:root /var/lib/misc/mtab
		$ chmod 644 /var/lib/misc/mtab

	9. now you have to modify your init scripts so that they basically
	   do this:

		1. fsck
		2. mount -n /var	# so that /var/lib/misc/mtab shows up
		3. > /var/lib/misc/mtab # clear /var/lib/misc/mtab
		4. mount -f /		# update mtab with the already
		5. mount -f /var	# mounted partitions.
		6. mount -a		# mount all remaining partitions

	When you're done, it should look like this:

# Begin $rc_base/init.d/mountfs - File System Mount Script

# Based on mountfs script from LFS-3.1 and earlier.
# Rewritten by Gerard Beekmans  - gerard at

source /etc/sysconfig/rc
source $rc_functions

case "$1" in
                mount -n /var
                echo "Clearing /var/lib/misc/mtab..."
                > /var/lib/misc/mtab
                mount -f /
                mount -f /var

                # The following mount command will mount all file systems. If
                # you have other (network based) file system that should not
                # be or cannot be mounted at this time, add them to the NO_FS
                # variable below. All file systems that are added to the
		# variable in the form of no<filesystem> will be skipped.

                echo "Mounting remaining file systems..."
                mount -a -t $NO_FS

[...remainder of the script omitted...]

	10. modify your /etc/fstab so that /var doesn't get mounted by
	    mount -a:

		/dev/discs/disc0/part7  /var  xfs  defaults,noauto  0 0

	    The important part here is the 'noauto' keyword.
	    This is, again, what it looks like on my system. Change that
	    line so that it fits your system.

	11. rebuild util-linux so that mount, umount and friends are
	    aware of mtab's new location. There are no patches required,
	    just look up the instructions in the LFS Book.

	12. remount / read-only now, since we're not changing any more files.

		$ mount -n / -o remount,ro

	13. if you haven't umounted any other parititions during the process,
	    all those listed in /etc/fstab should still be mounted.
	    Your system doesn't know that anymore, so we have to tell it
	    about that:

		$ mount -f /
		$ mount -f /var
		$ mount -af

	Finally done! You can telinit back to your favourite run-level now.

OK, as a final note: keep in mind that I take no responsibility whatsoever for
any damage done to your computer. Be careful doing this, you might badly screw
your system. This hint has 13 steps for a reason!

Credits are due to Seth W. Klein for teaching me through the process
in the first place.

If you have any questions, comments or suggestions regarding this hint, write
to me at <obrakmann at> or to the blfs-support mailing list.

I hope this helps some people out there.

Oliver Brakmann

More information about the hints mailing list