I have somehow an rough idea of how the userspace and init-system (be it classic init sysV /upstart/ systemd) work at system shutdown. (Essentially there is an order succession of "Stop!", "Please stop now really", "Process I need to kill you to Stop" and waiting... things going on).
I am anyhow very unaware of how the system shutdown works in the kernel (where surely there is also lots of stuff to do)?
I tried to look into the kernel documentation https://www.kernel.org/doc/htmldocs/ and even used the NSA's pal search tool to give me a head start on finding out how it works.
Also I searched on SE U+L and found nothing (did I overlook it?)
Anyways the question, though potentially a bit challenging, would merit an answer in this Q&A network as I assume more people are interested to get a sketch for what happens in the linux kernel at shutdown.
Potentially there is also change to link to some more detailed explanations.
An answer might maybe include which system-calls and which kernal signals are used?
https://github.com/torvalds/linux/blob/b3a3a9c441e2c8f6b6760de9331023a7906a4ac6/arch/x86/kernel/reboot.c seems to be the x86 used file related to reboot (already close to shutdown, eh?)
maybe the snippet found here http://lxr.free-electrons.com/source/kernel/reboot.c#L176 can be used to give an explanation
176 void kernel_power_off(void)
177 {
178 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
179 if (pm_power_off_prepare)
180 pm_power_off_prepare();
181 migrate_to_reboot_cpu();
182 syscore_shutdown();
183 pr_emerg("Power down\n");
184 kmsg_dump(KMSG_DUMP_POWEROFF);
185 machine_power_off();
186 }
187 EXPORT_SYMBOL_GPL(kernel_power_off);
shutdown(8)i.e. the ___deprecated___-none which I think in old unix documentation used to read "shutdown the system ourselves - the core unit is ON FIRE!" effectively a messy system kill-switch which would/could leave bits scattered across the floor (or at least the file-systems in a corrupt state) - one imagines this would be used for a main-frame type system where someone has just caught their hand in a cooling fan. – SlySven Jul 13 '17 at 14:58