1

Sometimes a hard reboot is required when a computer gets itself into a bad state, in any operating system. In linux say, if a drive cannot be unmounted, even by force, how does a shutdown command manage to do it? What has shutdown got access to for force unmounting etc. that is not accessible from the terminal?

J Collins
  • 558
  • 5
  • 23
  • What OS are we talking about? What will happen automatically depends on the OS. What requires a user to intervene will depend on the OS. – Ramhound Nov 30 '17 at 17:46
  • At the moment, Linux, but I am interested in both answers. The problem is I don't know how an OS can forcefully exit a process in, as the first answer, a 'too bad' case, to shut down, yet no user requested kill can kill it. It happens in windows and linux both that a file handle might lock things, and the only solution is a restart. – J Collins Nov 30 '17 at 17:48
  • In most cases commands only attempt to do something, after that period of time, specific or unknown behavior can happen. – Ramhound Nov 30 '17 at 17:49
  • keep in mind, a "mount point" is a memory structure within the kernel memory structure that is overseen by kernel and userspace software. regardless of what the rest of the software thinks about it, the kernel can always destroy that memory structure. it just won;t under most circumstances to protect the integrity of the filesystem from bad events caused by bad software or bad users. Its not like a process exists beyond the point where the kernel wants it to exist. its all just state management. – Frank Thomas Nov 30 '17 at 18:06

1 Answers1

2

Shutdown will cause the system to try to gracefully close all the applications or services that are loaded. Applications will get notified that this is happening and given a short grace period to "make good their escape".

The next part of the shutdown process will trigger the operating system to tell the filesystem drivers to flush their stored data to the storage device. This should cause the filesystem driver to sanitize the filesystem prior to shutdown.

It will also then tell the drive that the filesystem is on to flush whatever hardware caches they have to disk.

If, after all that, there is still data locking the disk then it is tough luck as the system will simply shut down. If an application has refused to save its data or release it correctly then it is simply lost.

Mokubai
  • 89,133
  • 25
  • 207
  • 233
  • So the question is in the final 'too bad' scenario, is it possible to do that on a process by process, service by service etc. case? – J Collins Nov 30 '17 at 17:44
  • Well you can manually go through every process in the list and ask them to terminate, but it wont help for a process that crashed or was badly written and left an orphan lock on something. In which case chances are letting everything tidy up properly and reboot is probably the best thing you can do. – Mokubai Nov 30 '17 at 17:47
  • @JCollins - Forcefully ending a process does clean up handles to files that might have opened up. So it depends on the processs. – Ramhound Nov 30 '17 at 17:48
  • IIRC all processes get killed after `init` exits. However, it is entirely possible for files to remain locked due to strange circumstances and/or kernel bugs. – Daniel B Nov 30 '17 at 17:49
  • Ignoring kernel bugs, I have seen cases where for instance `smb` processes did not end well and `init` (or `systemd` in my CentOS) became the process owner shown in `pstree`. When all variants of `kill` fail, what options are left before a reboot, noting that it is impossible to kill the parent process. – J Collins Dec 12 '17 at 17:31