Why does Linux support this:
umount /
Why would anyone write that, instead of this:
mount / -oremount,ro
I'm looking at the kernel code here:
if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
/*
* Special case for "unmounting" root ...
* we just try to remount it readonly.
*/
if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
return -EPERM;
down_write(&sb->s_umount);
if (!sb_rdonly(sb))
retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
https://elixir.bootlin.com/linux/v4.18/source/fs/namespace.c#L1612
Best Answer
Luciano Andress Martini
points out:(This requires there are no files open for writing. E.g. it could work when the system is running in single-user mode. Note that after running fsck, to repair a filesystem which is still mounted in read-only mode, you must always reboot for safety reasons).
In other words, if you don't even know there is a command to remount a filesystem read-only, you can try the same commands as if you needed to fsck (repair)
/dev/fd0
or your/home
filesystem. The special case allows this to work, even though thefsck
command is on the filesystem you apparently unmounted :-). It's nice that Linux can be helpful like this, when you try to repair a corrupted system.There is one other use of this special case:
umount -a
, used in old shutdown scripts. This is defined to simply unmount all filesystems in reverse order, finishing with the root filesystem. It makes sure all filesystems are in a consistent state on the disk, so they do not require afsck
on the next boot. The Linux kernel does not shut down any filesystem automatically; you need to have some shutdown program or "init system" that does this.I'm not certain why this special case is in the kernel, rather than in the
umount
command. One reason might be that old kernels accepted the name of the mounted device, instead of the directory the filesystem was mounted at. Perhaps this made it seem simpler or more reliable to put this code in the kernel.The special case is not documented in the current man pages for
umount(2)
orumount(8)
. So the current man page implies thatumount -a
will always show an error, but this is not the case. I suspect thatumount -a
is not very widely used nowadays.There is a very similar code comment in early versions of Linux including 0.99.10 (1993).
This does not seem to be a standard for traditional UNIX. The FreeBSD kernel returns an error instead. I'm not sure why there's a specific error check for this case, separate from the general error check for unmounting a filesystem that is currently in use. The FreeBSD equivalent of
umount -a
is aware of this issue, and stops before unmounting the first filesystem i.e. the root. (The code is here, but you need to understand howfor
loops and array indexes work in C :-).The old scripts that rely on
umount -a
contrast with more recent scripts for SysVinit, which are still available in Debian for example./etc/init.d/umount_root
explicitly remounts/
as readonly. The rest of the mounts are processed individually, by/etc/init.d/umountfs
and/etc/init.d/umountnfs.sh
.umount -a
is not ideal on modern systems. It is simpler to leave a/proc
filesystem mounted so that/proc/mounts
can still be used. And/dev
is usually a separate mounted filesystem, which will show an unmount error because/dev/console
is still open.For an example of an old shutdown script, see the reference script
etc/rc.d/rc.0
in the oldSysVinit-2.4.tar.z
/SysVinit-2.4.tar.gz
.