From 9ac2725ea06a5b1771452ab32e848106e3efe477 Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sat, 29 Oct 2022 18:57:10 -0500 Subject: [PATCH 1/6] Bump version to 0.11. --- Makefile | 2 +- PKGBUILD | 2 +- binsrc/imp/__main__.py | 2 +- debian/changelog | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b441f00..5afa7bd 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # # Bottle-Imp version -IMPVERSION = 0.10 +IMPVERSION = 0.11 # Determine this makefile's path. # Be sure to place this BEFORE `include` directives, if any. diff --git a/PKGBUILD b/PKGBUILD index b5154bc..1faac79 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: Alistair Young pkgname=bottle-imp -pkgver=0.10 +pkgver=0.11 pkgrel=1 pkgdesc="A helper for WSL's native systemd support." arch=('x86_64') diff --git a/binsrc/imp/__main__.py b/binsrc/imp/__main__.py index 26a3cac..4e23434 100644 --- a/binsrc/imp/__main__.py +++ b/binsrc/imp/__main__.py @@ -10,7 +10,7 @@ import helpers # Global variables -version = "0.10" +version = "0.11" verbose = False login = None diff --git a/debian/changelog b/debian/changelog index 3d6f62e..f6efe72 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +bottle-imp (0.11) buster bullseye bookworm sid focal jammy; urgency=medium + + * bump + + -- Alistair Young Sat, 29 Oct 2022 19:00:01 -0500 + bottle-imp (0.10) buster bullseye bookworm sid focal jammy; urgency=medium * Added fix for /dev/shm / /run/shm inversion. From e2f435a110dec75e181397b76711cc44fbe610b0 Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sun, 30 Oct 2022 19:18:15 -0500 Subject: [PATCH 2/6] Added envars to the passthrough list. --- binsrc/imp/__main__.py | 3 ++- debian/changelog | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/binsrc/imp/__main__.py b/binsrc/imp/__main__.py index 4e23434..f703d64 100644 --- a/binsrc/imp/__main__.py +++ b/binsrc/imp/__main__.py @@ -113,7 +113,8 @@ def do_initialize(): wait_for_systemd() # Update the base environment with interop-fu. - subprocess.run(['systemctl', 'import-environment', 'WSL_INTEROP']) + subprocess.run(['systemctl', 'import-environment', + 'WSL_INTEROP', 'WSL2_GUI_APPS_ENABLED', 'WSL_DISTRO_NAME', 'WSLENV', 'NAME', 'HOSTTYPE']) # Run wait-forever subprocess. subprocess.Popen(['/usr/lib/bottle-imp/wait-forever.sh'], diff --git a/debian/changelog b/debian/changelog index f6efe72..f61af96 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,6 @@ bottle-imp (0.11) buster bullseye bookworm sid focal jammy; urgency=medium - * bump + * Added various environment variables to the passthrough list. -- Alistair Young Sat, 29 Oct 2022 19:00:01 -0500 From 1a18bf24bb79a15b44fd674926f21e04545240a1 Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sun, 30 Oct 2022 19:42:49 -0500 Subject: [PATCH 3/6] Pass Windows Terminal variables through to user session. --- binsrc/imp/__main__.py | 21 +++++++++++++++++++-- binsrc/imp/helpers.py | 9 +++++++++ debian/changelog | 1 + 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/binsrc/imp/__main__.py b/binsrc/imp/__main__.py index f703d64..8a029ed 100644 --- a/binsrc/imp/__main__.py +++ b/binsrc/imp/__main__.py @@ -129,6 +129,7 @@ def do_initialize(): # Exit sys.exit(0) + def do_login(): """Start a systemd login prompt.""" wait_for_systemd() @@ -142,6 +143,7 @@ def do_login(): os.execv ('/usr/bin/machinectl', ['machinectl', 'login', '.host']) # never get here + def do_shell(): """Start/connect to a systemd user session with a shell.""" wait_for_systemd() @@ -152,9 +154,17 @@ def do_shell(): if verbose: print("imp: starting shell") - os.execv ('/usr/bin/machinectl', ['machinectl', 'shell', '-q', login + '@.host']) + if helpers.get_in_windows_terminal(): + os.execv ('/usr/bin/machinectl', ['machinectl', + '-E', 'WT_SESSION', '-E', 'WT_PROFILE_ID', + 'shell', '-q', login + '@.host']) + else: + os.execv ('/usr/bin/machinectl', ['machinectl', + 'shell', '-q', login + '@.host']) + # never get here + def do_command(commandline): """Start/connect to a systemd user session with a command.""" wait_for_systemd() @@ -168,10 +178,17 @@ def do_command(commandline): if len(commandline) == 0: sys.exit("imp: no command specified") - command = ['machinectl', 'shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()] + commandline; + if helpers.get_in_windows_terminal(): + command = ['machinectl', + '-E', 'WT_SESSION', '-E', 'WT_PROFILE_ID', + 'shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()] + commandline; + else: + command = ['machinectl', + 'shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()] + commandline; os.execv ('/usr/bin/machinectl', command) + # Entrypoint def entrypoint(): """Entrypoint of the application.""" diff --git a/binsrc/imp/helpers.py b/binsrc/imp/helpers.py index d09f86b..2f1cc10 100644 --- a/binsrc/imp/helpers.py +++ b/binsrc/imp/helpers.py @@ -6,6 +6,15 @@ import psutil import pwd + +def get_in_windows_terminal(): + """Are we inside a Windows Terminal session?""" + if 'WT_SESSION' in os.environ: + return True + else: + return False + + def get_login_session_user(): """Get the user logged into the current session, pre-setuid.""" # This environment variable is set by the setuid wrapper. diff --git a/debian/changelog b/debian/changelog index f61af96..4d2355a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ 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. -- Alistair Young Sat, 29 Oct 2022 19:00:01 -0500 From 5e0a6b4c116bbcded6bd2a484ea77609638a58ef Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sun, 30 Oct 2022 23:06:02 -0500 Subject: [PATCH 4/6] Added BUGS section to documentation. --- othersrc/docs/README.md | 8 ++++++++ othersrc/docs/imp.8 | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/othersrc/docs/README.md b/othersrc/docs/README.md index 08238a5..9b11cd1 100644 --- a/othersrc/docs/README.md +++ b/othersrc/docs/README.md @@ -118,3 +118,11 @@ _imp -c [command]_ runs _command_ inside a _systemd_ login session, then exits. With either of the above, the _imp -a [user]_ option may be used to specify a particular user to start a shell for, or to run a command as, rather than using the currently logged-in user. For example, _imp -a bongo -s_ would start a shell as the user _bongo_. _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. + +## 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. + +2. While the Windows Terminal environment variables, WT_SESSION and WT_PROFILE_ID, will be passed through to shell and command prompt invocations of _imp_, they will not be passed through to login sessions created with _imp -l_, due to a limitation in _machinectl_. + +3. _imp_ requires the python package _psutil_, which due to technical limitations of _zipapp_ can't be wrapped into the _imp_ executable. As such, _imp_ depends on this package for the system _python_. If you are inside another python environment, _imp_ may fail unless you install the _psutil_ package into this environment also. diff --git a/othersrc/docs/imp.8 b/othersrc/docs/imp.8 index 24f6caa..c367723 100644 --- a/othersrc/docs/imp.8 +++ b/othersrc/docs/imp.8 @@ -86,6 +86,43 @@ undefined. .Xr bootup 7 , .Xr systemd-machined 8 , .Sh BUGS +1. Using +.Nm +to create a session is required for the user login session (and its concomitants, such as a user +.Xr systemd 1 +instance and a session dbus) to be created properly. Simply starting a process with +.Ar wsl +(or using a Linux GUI app shortcut) does not do this, although the problem is less serious than with +.Ar genie +, since the process will still be started with +.Xr systemd 1 +as pid 1. +.Pp +2. While the Windows Terminal environment variables, WT_SESSION and WT_PROFILE_ID, will be passed +through to shell and command prompt invocations of +.Nm +, they will not be passed through to login sessions created with +.Ar imp -l +, due to a limitation in +.Xr machinectl 1 +. +.Pp +3. +.Nm +requires the Python package +.Ar psutil +, which due to technical limitations of +.Ar zipapp +can't be wrapped into the +.Nm +executable. As such, +.Nm +depends on this package for the system Python. If you are inside another Python environment, +.Nm +may fail unless you install the +.Ar psutil +package into this environment also. +.Pp If you feel you have found a bug in .Nm please submit a bug report at From e13c6284c754e9bdcf8680fbfeda10dd48343aca Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sun, 30 Oct 2022 23:07:48 -0500 Subject: [PATCH 5/6] Added VSC remote workaround to readme. --- othersrc/docs/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/othersrc/docs/README.md b/othersrc/docs/README.md index 9b11cd1..672aedb 100644 --- a/othersrc/docs/README.md +++ b/othersrc/docs/README.md @@ -123,6 +123,8 @@ _imp -l_ opens a login prompt. This permits you to log in to the WSL distributio 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. +For information about starting Visual Studio Code remote sessions in login sessions, see https://github.com/arkane-systems/bottle-imp/discussions/19 . + 2. While the Windows Terminal environment variables, WT_SESSION and WT_PROFILE_ID, will be passed through to shell and command prompt invocations of _imp_, they will not be passed through to login sessions created with _imp -l_, due to a limitation in _machinectl_. 3. _imp_ requires the python package _psutil_, which due to technical limitations of _zipapp_ can't be wrapped into the _imp_ executable. As such, _imp_ depends on this package for the system _python_. If you are inside another python environment, _imp_ may fail unless you install the _psutil_ package into this environment also. From 868398561f7a72c29f260c48445d41c752d1d0a6 Mon Sep 17 00:00:00 2001 From: Alistair Young Date: Sun, 30 Oct 2022 23:27:49 -0500 Subject: [PATCH 6/6] Added shutdown helper command. --- binsrc/imp/__main__.py | 14 ++++++++++++++ debian/changelog | 2 ++ othersrc/docs/README.md | 6 ++++++ othersrc/docs/imp.8 | 16 ++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/binsrc/imp/__main__.py b/binsrc/imp/__main__.py index 8a029ed..ab67a55 100644 --- a/binsrc/imp/__main__.py +++ b/binsrc/imp/__main__.py @@ -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() @@ -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.""" @@ -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: diff --git a/debian/changelog b/debian/changelog index 4d2355a..442f92f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -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 Sat, 29 Oct 2022 19:00:01 -0500 diff --git a/othersrc/docs/README.md b/othersrc/docs/README.md index 672aedb..ff89299 100644 --- a/othersrc/docs/README.md +++ b/othersrc/docs/README.md @@ -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/ ``` @@ -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. diff --git a/othersrc/docs/imp.8 b/othersrc/docs/imp.8 index c367723..7446f7a 100644 --- a/othersrc/docs/imp.8 +++ b/othersrc/docs/imp.8 @@ -14,6 +14,7 @@ .Op -i .Op -s .Op -l +.Op -u .Op -c .Ar command... .Sh DESCRIPTION @@ -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