Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean shutdown of Linux from Windows? #5661

Closed
derinsh0 opened this issue Jul 27, 2020 · 7 comments
Closed

Clean shutdown of Linux from Windows? #5661

derinsh0 opened this issue Jul 27, 2020 · 7 comments

Comments

@derinsh0
Copy link

I seem to have issues with initiating a clean shutdown of the WSL system without needing to issue it from within. For example, I run the genie program that emulates systemd. I have services that mount certain filesystems and I want to unmount them cleanly before shutdown. The command wsl.exe --shutdown does not give Linux time to stop services, in journalctl it looks like the worst kind of shutdown, it just gets blank. From cmd I can do wsl.exe sudo systemctl stop srv && wsl.exe sudo systemctl poweroff but I’m trying to automate this behavior. I have tried a Windows shutdown script by policies, where I enter the commands above along with a timeout of 3s, but it does not work. How do I even know if the script is executed? Event viewer does not give me clues, and I have enabled verbose status messages. Is there no native WSL support for clean shutdowns? Thanks for any help.

@therealkenc
Copy link
Collaborator

Systemctl is #1579 which is really #994. This ask is effectively #3253 (message) redux. Newspeak is wsl.exe --terminate <distro>. The --shutdown command is more analogous big red switch.

@derinsh0
Copy link
Author

derinsh0 commented Jul 28, 2020

I read some of those posts myself, the last one specifically, but the terminate command is not much different from shutdown, in terms of giving systemctl any notice. I started up the samba daemon, did --terminate arch right after from cmd (then restarted the distro), and in journalctl the sequence is:

Jul 28 19:53:41 PSISYS-wsl systemd[1]: Started Samba SMB Daemon.
Jul 28 19:53:57 PSISYS-wsl kernel: Linux version 4.19.104-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GC>
Jul 28 19:53:57 PSISYS-wsl kernel: Command line: initrd=\initrd.img panic=-1 pty.legacy_count=0 nr_cpus=12
...

Compare that to poweroff instead of wsl --terminate :

Jul 28 20:01:32 PSISYS-wsl systemd[1]: Started Samba SMB Daemon.
Jul 28 20:01:42 PSISYS-wsl sudo:     root : TTY=pts/1 ; PWD=/mnt/c/Users/Psi ; USER=root ; COMMAND=/usr/sbin/systemct>
Jul 28 20:01:42 PSISYS-wsl sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 28 20:01:42 PSISYS-wsl systemd-logind[45]: System is powering down.
Jul 28 20:01:42 PSISYS-wsl sudo: pam_unix(sudo:session): session closed for user root
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopping Session c1 of user root.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Removed slice system-modprobe.slice.
Jul 28 20:01:42 PSISYS-wsl runuser[90]: pam_unix(runuser-l:session): session closed for user root
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped target Graphical Interface.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped target Multi-User System.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped target Login Prompts.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped target Timers.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: cache.timer: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Clear cache.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: man-db.timer: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Daily man-db regeneration.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: shadow.timer: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Daily verification of password and group files.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: systemd-tmpfiles-clean.timer: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Daily Cleanup of Temporary Directories.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopping Console Getty...
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopping Getty on tty1...
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopping Samba SMB Daemon...
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Starting Server Unit...
Jul 28 20:01:42 PSISYS-wsl systemd[1]: console-getty.service: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Console Getty.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: getty@tty1.service: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Getty on tty1.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: smb.service: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped Samba SMB Daemon.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Removed slice system-getty.slice.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Stopped target Network is Online.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: srvstop.service: Succeeded.
Jul 28 20:01:42 PSISYS-wsl systemd[1]: Finished Server Unit.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: session-c1.scope: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Session c1 of user root.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping Login Service...
Jul 28 20:01:44 PSISYS-wsl systemd-logind[45]: Session c1 logged out. Waiting for processes to exit.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping User Manager for UID 0...
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Stopped target Main User Target.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Stopped target Basic System.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Stopped target Paths.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Stopped target Sockets.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Stopped target Timers.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: dbus.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed D-Bus User Message Bus Socket.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: dirmngr.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-logind.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed GnuPG network certificate management daemon.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: gpg-agent-browser.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed GnuPG cryptographic agent and passphrase cache (access for web browser>
Jul 28 20:01:44 PSISYS-wsl systemd[93]: gpg-agent-extra.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed GnuPG cryptographic agent and passphrase cache (restricted).
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Login Service.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: gpg-agent-ssh.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed GnuPG cryptographic agent (ssh-agent emulation).
Jul 28 20:01:44 PSISYS-wsl systemd[93]: gpg-agent.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed GnuPG cryptographic agent and passphrase cache.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: p11-kit-server.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Closed p11-kit server.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Reached target Shutdown.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: systemd-exit.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Finished Exit the Session.
Jul 28 20:01:44 PSISYS-wsl systemd[93]: Reached target Exit the Session.
Jul 28 20:01:44 PSISYS-wsl systemd[94]: pam_warn(systemd-user:setcred): function=[pam_sm_setcred] flags=0x8004 servic>
Jul 28 20:01:44 PSISYS-wsl systemd[1]: user@0.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped User Manager for UID 0.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping User Runtime Directory /run/user/0...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: run-user-0.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /run/user/0.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: user-runtime-dir@0.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped User Runtime Directory /run/user/0.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Removed slice User Slice of UID 0.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping D-Bus System Message Bus...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping Permit User Sessions...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: dbus.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped D-Bus System Message Bus.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-user-sessions.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Permit User Sessions.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Basic System.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Paths.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Remote File Systems.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Slices.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Removed slice User and Session Slice.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Sockets.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: dbus.socket: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Closed D-Bus System Message Bus Socket.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target System Initialization.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Local Encrypted Volumes.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-ask-password-console.path: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Dispatch Password Requests to Console Directory Watch.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-ask-password-wall.path: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Forward Password Requests to Wall Directory Watch.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-sysctl.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Apply Kernel Variables.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopping Update UTMP about System Boot/Shutdown...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-update-utmp.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Update UTMP about System Boot/Shutdown.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-tmpfiles-setup.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Create Volatile Files and Directories.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Local File Systems.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /etc/hostname...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /init...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/c...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/d...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/e...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/f...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/p...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /mnt/wsl...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /run/lock...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /run/shm...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting /run/user...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounting Temporary Directory (/tmp)...
Jul 28 20:01:44 PSISYS-wsl systemd[1]: etc-hostname.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /etc/hostname.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: init.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /init.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-c.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/c.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-d.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/d.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-e.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/e.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-f.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/f.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-p.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/p.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: mnt-wsl.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /mnt/wsl.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: run-lock.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /run/lock.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: run-shm.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /run/shm.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: run-user.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted /run/user.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: tmp.mount: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Unmounted Temporary Directory (/tmp).
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Local File Systems (Pre).
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped target Swap.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Reached target Unmount All Filesystems.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-remount-fs.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Remount Root and Kernel File Systems.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-tmpfiles-setup-dev.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Stopped Create Static Device Nodes in /dev.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Reached target Shutdown.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Reached target Final Step.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: systemd-poweroff.service: Succeeded.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Finished Power-Off.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Reached target Power-Off.
Jul 28 20:01:44 PSISYS-wsl systemd[1]: Shutting down.
Jul 28 20:01:44 PSISYS-wsl systemd-journald[23]: Journal stopped
-- Reboot --
Jul 28 20:02:04 PSISYS-wsl kernel: Linux version 4.19.104-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GC>
Jul 28 20:02:04 PSISYS-wsl kernel: Command line: initrd=\initrd.img panic=-1 pty.legacy_count=0 nr_cpus=12
...

There is a whole lot of user-level jobs that is left out by letting WSL shut it down, I think the big red power switch applies to all of its commands.

I know my question about Windows shutdown scripts is a bit off-topic, but the MS forums can get a bit dizzying. If someone knows how to automate the command poweroff at Windows shutdown that would be a good solution.

@therealkenc
Copy link
Collaborator

therealkenc commented Jul 28, 2020

the terminate command is not much different from shutdown

You are right, it isn't. If you've spun your own systemd (it look like you have) that's ongoing #994 and probably best taken over there. Long short if you wanted to roll your own, you'd have to do something more elaborate than the fire-and-forget daemonize hack being pasted around. You'd need the parent to wait on the child systemd inside the namespace to exit. And if you wanted to monitor that exit from the Windows side, build some kind of RPC mechanism to communicate the exit happened.

Having shutdown(8) support for house brand /init is a legit enough ask, so this can be morphed into a feature request (#3253 redux). One could imagine a wsl.exe --shutdown-nicely <distro> that blocks. Whether that would get implemented in the foreseeable I couldn't tell ya. It seems more likely #994 flips status in some sanctioned way. But never say never.

@derinsh0
Copy link
Author

@therealkenc
Alright, I understand it more now. And there seems to be a smarter bunch than me trying to figure this thing out. I hope the developers see the benefit in managing services easily, something Windows inherently lacks. In the meanwhile, a logoff script helped me, and systemd is appropriately shutting down.

@therealkenc
Copy link
Collaborator

Alright. I am going to drop the feature tag, because the ask really is /dupe #994, or more generally, support for third-party init(8). The probability of WSL-brand /init death marching to managing Linux daemons, which it fundamentally cannot without sacrificing being distribution agnostic, is "small". Open #2530 can serve as the landing zone to the contrary. Whatever features Windows proper "inherently lacks" would be a feedback hub thing.

@ghost
Copy link

ghost commented Jul 29, 2020

Hi! We've identified this issue as a duplicate of another one that already exists in this repository. This specific instance is being closed in favor of tracking the concern over on the referenced thread.

Thanks for your report!

@ghost ghost added the duplicate label Jul 29, 2020
@cerebrate
Copy link

cerebrate commented Aug 2, 2020

@psisis Just a quick note - I saw this issue and added shutdown support (using systemctl poweroff) to genie in 1.26. Seems to work pretty well for most purposes, including waiting on wsl genie -u from the Windows side to ensure shutdown is complete.

(Disclaimer: It is not, principally for mount reasons, perfectly idempotent, and so I wouldn't recommend starting a new genie bottle (or indeed, carrying on using Linux) without terminating and restarting the distro in question first.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants