Skip to content

Commit

Permalink
Added shutdown helper command.
Browse files Browse the repository at this point in the history
  • Loading branch information
cerebrate committed Oct 31, 2022
1 parent e13c628 commit 8683985
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions binsrc/imp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def parse_command_line():
help='open a login prompt for a systemd user session')
group.add_argument(
'-c', '--command', help='open or connect to a systemd user session, and run the specified command in it\n(preserves working directory)', nargs=argparse.REMAINDER)
group.add_argument('-u', '--shutdown', action='store_true',
help='shut down systemd and the WSL instance')

return parser.parse_args()

Expand Down Expand Up @@ -189,6 +191,16 @@ def do_command(commandline):
os.execv ('/usr/bin/machinectl', command)


def do_shutdown():
"""Shut down systemd and the WSL instance."""
wait_for_systemd()

if verbose:
print ("imp: shutting down WSL instance")

os.execv('/usr/bin/systemctl', ['systemctl', 'poweroff'])


# Entrypoint
def entrypoint():
"""Entrypoint of the application."""
Expand Down Expand Up @@ -225,6 +237,8 @@ def entrypoint():
do_shell()
elif arguments.login:
do_login()
elif arguments.shutdown:
do_shutdown()
elif arguments.command is not None:
do_command(arguments.command)
else:
Expand Down
2 changes: 2 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ bottle-imp (0.11) buster bullseye bookworm sid focal jammy; urgency=medium

* Added various environment variables to the passthrough list.
* Pass through Windows Terminal variables on shell/command.
* Add shutdown helper command.
* Added bugs section to documentation.

-- Alistair Young <avatar@arkane-systems.net> Sat, 29 Oct 2022 19:00:01 -0500

Expand Down
6 changes: 6 additions & 0 deletions othersrc/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ commands:
-l, --login open a login prompt for a systemd user session
-c ..., --command ...
open or connect to a systemd user session, and run the specified command within it (preserves working directory)
-u, --shutdown shut down systemd and the WSL instance
For more information, see https://github.com/arkane-systems/bottle-imp/
```
Expand All @@ -119,6 +120,11 @@ With either of the above, the _imp -a [user]_ option may be used to specify a pa

_imp -l_ opens a login prompt. This permits you to log in to the WSL distribution via _systemd_ as any user. The login prompt will return when you log out; to terminate the session, press `^]` three times within one second. It follows _login_ semantics, and as such does not preserve the current working directory.

_imp -u_ will shut down _systemd_ cleanly and exit the WSL instance. This uses the _systemctl poweroff_ command to
simulate a normal Linux system shutting down. It is suggested that this be used before shutting down the Windows machine or force-terminating WSL to ensure a clean shutdown of _systemd_ services.

Shutting down the WSL instance in this way causes it to exit completely. You should wait for the instance to show as stopped before attempting to restart it or execute further commands inside it.

## BUGS

1. Using _imp_ to create a session is required for the user login session (and its concomitants, such as a user _systemd_ instance and a session dbus) to be created properly. Simply starting a process with _wsl_ (or using a Linux GUI app shortcut) does not do this, although the problem is less serious than with _genie_, since the process will still be started with _systemd_ as pid 1.
Expand Down
16 changes: 16 additions & 0 deletions othersrc/docs/imp.8
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
.Op -i
.Op -s
.Op -l
.Op -u
.Op -c
.Ar command...
.Sh DESCRIPTION
Expand Down Expand Up @@ -64,6 +65,21 @@ distribution with
installed.
.Pp
Unlike the other options, this preserves the current working directory.
.It Fl u, -shutdown
Shuts down
.Xr systemd 1
cleanly and exits the WSL instance. This uses the
.Ar systemctl poweroff
command to simulate a normal Linux system shutting down. It is suggested that
this be used before shutting down the Windows machine or force-terminating WSL
to ensure a clean shutdown of
.Xr systemd 1
services.
.Pp
Shutting down the WSL instance in this way causes it to exit
.Ar completely.
You should wait for the instance to show as stopped before attempting to restart it
or execute further commands inside it.
.El
.Sh EXIT STATUS
.Nm
Expand Down

0 comments on commit 8683985

Please sign in to comment.