Why does the Linux kernel support “umount /”?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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










share|improve this question



















  • 1




    First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
    – Luciano Andress Martini
    Sep 3 at 16:21











  • @LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
    – sourcejedi
    Sep 3 at 18:36











  • It will be a honor! Thank you!
    – Luciano Andress Martini
    Sep 3 at 19:37















up vote
1
down vote

favorite












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










share|improve this question



















  • 1




    First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
    – Luciano Andress Martini
    Sep 3 at 16:21











  • @LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
    – sourcejedi
    Sep 3 at 18:36











  • It will be a honor! Thank you!
    – Luciano Andress Martini
    Sep 3 at 19:37













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







mount linux-kernel compatibility






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 3 at 21:45

























asked Sep 3 at 9:51









sourcejedi

20.3k42887




20.3k42887







  • 1




    First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
    – Luciano Andress Martini
    Sep 3 at 16:21











  • @LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
    – sourcejedi
    Sep 3 at 18:36











  • It will be a honor! Thank you!
    – Luciano Andress Martini
    Sep 3 at 19:37













  • 1




    First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
    – Luciano Andress Martini
    Sep 3 at 16:21











  • @LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
    – sourcejedi
    Sep 3 at 18:36











  • It will be a honor! Thank you!
    – Luciano Andress Martini
    Sep 3 at 19:37








1




1




First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
– Luciano Andress Martini
Sep 3 at 16:21





First time i have a problem in a filesystem in linux i received a message by fsck like "/dev/hda2 is mounted read-write" in that epoch(1999), i did not understand what that means i am 11 y.o. the only thing that come to my mind was: umount /, and it works (as it remounted read-only), maybe because a lot of people tried to that when do not want to maintencance without damage the /, they tried umount /, and the kernel started to support that as a facility.
– Luciano Andress Martini
Sep 3 at 16:21













@LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
– sourcejedi
Sep 3 at 18:36





@LucianoAndressMartini thanks, that's awesome :-D. If you don't even know that there is a command to remount readonly, it makes sense that you would try umount thinking it might do something useful. I have stolen your example for my answer, and sneaked in a reference to a floppy disk device for extra nostalgia,
– sourcejedi
Sep 3 at 18:36













It will be a honor! Thank you!
– Luciano Andress Martini
Sep 3 at 19:37





It will be a honor! Thank you!
– Luciano Andress Martini
Sep 3 at 19:37











1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Luciano Andress Martini points out:




First time I have a problem in a filesystem in linux I received a message by fsck like "/dev/hda2 is mounted read-write". In that epoch (1999), I did not understand what that means. I am 11 y.o. The only thing that come to my mind was: umount /, and it works (as it remounted read-only).




(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 the fsck 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 a fsck 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) or umount(8). So the current man page implies that umount -a will always show an error, but this is not the case. I suspect that umount -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 how for loops and array indexes work in C :-).



The old scripts that rely on umount -a contrast with current scripts for SysVinit, for example in Debian. /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 old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.



#! /bin/sh
#
# brc This file is executed by init(8) when the system is being
# shutdown (i.e. set to run at level 0). It usually takes
# care of un-mounting al unneeded file systems.
#
# Version: @(#)/etc/brc 2.01 02/17/93
#
# Authors: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

PATH=/bin:/etc:/usr/bin
echo Unmounting file systems.....
umount -a
echo Done.





share|improve this answer






















  • You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
    – JdeBP
    Sep 3 at 11:07











  • @JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
    – sourcejedi
    Sep 3 at 11:28











  • Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
    – JdeBP
    Sep 3 at 12:05










  • @JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
    – sourcejedi
    Sep 3 at 12:15










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466535%2fwhy-does-the-linux-kernel-support-umount%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Luciano Andress Martini points out:




First time I have a problem in a filesystem in linux I received a message by fsck like "/dev/hda2 is mounted read-write". In that epoch (1999), I did not understand what that means. I am 11 y.o. The only thing that come to my mind was: umount /, and it works (as it remounted read-only).




(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 the fsck 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 a fsck 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) or umount(8). So the current man page implies that umount -a will always show an error, but this is not the case. I suspect that umount -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 how for loops and array indexes work in C :-).



The old scripts that rely on umount -a contrast with current scripts for SysVinit, for example in Debian. /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 old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.



#! /bin/sh
#
# brc This file is executed by init(8) when the system is being
# shutdown (i.e. set to run at level 0). It usually takes
# care of un-mounting al unneeded file systems.
#
# Version: @(#)/etc/brc 2.01 02/17/93
#
# Authors: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

PATH=/bin:/etc:/usr/bin
echo Unmounting file systems.....
umount -a
echo Done.





share|improve this answer






















  • You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
    – JdeBP
    Sep 3 at 11:07











  • @JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
    – sourcejedi
    Sep 3 at 11:28











  • Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
    – JdeBP
    Sep 3 at 12:05










  • @JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
    – sourcejedi
    Sep 3 at 12:15














up vote
3
down vote



accepted










Luciano Andress Martini points out:




First time I have a problem in a filesystem in linux I received a message by fsck like "/dev/hda2 is mounted read-write". In that epoch (1999), I did not understand what that means. I am 11 y.o. The only thing that come to my mind was: umount /, and it works (as it remounted read-only).




(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 the fsck 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 a fsck 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) or umount(8). So the current man page implies that umount -a will always show an error, but this is not the case. I suspect that umount -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 how for loops and array indexes work in C :-).



The old scripts that rely on umount -a contrast with current scripts for SysVinit, for example in Debian. /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 old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.



#! /bin/sh
#
# brc This file is executed by init(8) when the system is being
# shutdown (i.e. set to run at level 0). It usually takes
# care of un-mounting al unneeded file systems.
#
# Version: @(#)/etc/brc 2.01 02/17/93
#
# Authors: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

PATH=/bin:/etc:/usr/bin
echo Unmounting file systems.....
umount -a
echo Done.





share|improve this answer






















  • You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
    – JdeBP
    Sep 3 at 11:07











  • @JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
    – sourcejedi
    Sep 3 at 11:28











  • Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
    – JdeBP
    Sep 3 at 12:05










  • @JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
    – sourcejedi
    Sep 3 at 12:15












up vote
3
down vote



accepted







up vote
3
down vote



accepted






Luciano Andress Martini points out:




First time I have a problem in a filesystem in linux I received a message by fsck like "/dev/hda2 is mounted read-write". In that epoch (1999), I did not understand what that means. I am 11 y.o. The only thing that come to my mind was: umount /, and it works (as it remounted read-only).




(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 the fsck 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 a fsck 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) or umount(8). So the current man page implies that umount -a will always show an error, but this is not the case. I suspect that umount -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 how for loops and array indexes work in C :-).



The old scripts that rely on umount -a contrast with current scripts for SysVinit, for example in Debian. /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 old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.



#! /bin/sh
#
# brc This file is executed by init(8) when the system is being
# shutdown (i.e. set to run at level 0). It usually takes
# care of un-mounting al unneeded file systems.
#
# Version: @(#)/etc/brc 2.01 02/17/93
#
# Authors: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

PATH=/bin:/etc:/usr/bin
echo Unmounting file systems.....
umount -a
echo Done.





share|improve this answer














Luciano Andress Martini points out:




First time I have a problem in a filesystem in linux I received a message by fsck like "/dev/hda2 is mounted read-write". In that epoch (1999), I did not understand what that means. I am 11 y.o. The only thing that come to my mind was: umount /, and it works (as it remounted read-only).




(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 the fsck 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 a fsck 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) or umount(8). So the current man page implies that umount -a will always show an error, but this is not the case. I suspect that umount -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 how for loops and array indexes work in C :-).



The old scripts that rely on umount -a contrast with current scripts for SysVinit, for example in Debian. /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 old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.



#! /bin/sh
#
# brc This file is executed by init(8) when the system is being
# shutdown (i.e. set to run at level 0). It usually takes
# care of un-mounting al unneeded file systems.
#
# Version: @(#)/etc/brc 2.01 02/17/93
#
# Authors: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

PATH=/bin:/etc:/usr/bin
echo Unmounting file systems.....
umount -a
echo Done.






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 3 at 23:42

























answered Sep 3 at 9:51









sourcejedi

20.3k42887




20.3k42887











  • You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
    – JdeBP
    Sep 3 at 11:07











  • @JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
    – sourcejedi
    Sep 3 at 11:28











  • Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
    – JdeBP
    Sep 3 at 12:05










  • @JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
    – sourcejedi
    Sep 3 at 12:15
















  • You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
    – JdeBP
    Sep 3 at 11:07











  • @JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
    – sourcejedi
    Sep 3 at 11:28











  • Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
    – JdeBP
    Sep 3 at 12:05










  • @JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
    – sourcejedi
    Sep 3 at 12:15















You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
– JdeBP
Sep 3 at 11:07





You have the van Smoorenburg /etc/init.d conflated with the Upstart /etc/init. And "current" is a misnomer given that all three of those van Smoorenburg scripts have been masked for systemd for years, now, and not used in the default install in the past two major versions of Debian. On that score, you should probably consider what happens when stopping systemd's -.mount too. (The nosh toolset's mount@- service bundle is explicitly special cased to be a read-only remount by convert-fstab-services and write-volume-service-bundle, for comparison.)
– JdeBP
Sep 3 at 11:07













@JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
– sourcejedi
Sep 3 at 11:28





@JdeBP -.mount is "perpetual". I hadn't noticed this reason for it before. github.com/systemd/systemd/commit/… I've clarified "current init scripts".
– sourcejedi
Sep 3 at 11:28













Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
– JdeBP
Sep 3 at 12:05




Pointing to code that makes system.slice perpetual does not demonstrate that. (-: You still have van Smoorenburg rc (it not really being init) and Upstart conflated.
– JdeBP
Sep 3 at 12:05












@JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
– sourcejedi
Sep 3 at 12:15




@JdeBP you could have just said I typo'ed /etc/init/ for /etc/init.d/ :-). Fixed, thanks for you help.
– sourcejedi
Sep 3 at 12:15

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466535%2fwhy-does-the-linux-kernel-support-umount%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?