This is a follow up to PR #41512
Remove the redundant restore cgroup namespace code.
Simplify the cgroup check logic by moving the retry inside the debug shell.
Add a new systemd boot timeout test.
When parsing the config file, non-ascii utf-16 values are casted into char. This will mangle non-ascii values in the config file. For example, a custom kernel path containing Chinese characters will cause WSL_E_CUSTOM_KERNEL_NOT_FOUND.
This PR removes the faulty static cast and stores the config value in wstring instead of string. So non-ascii values are preserved.
Before this change, an additional 10ms delay was added in the port tracker loop presumably to make the main loop slower than the worker loop. So, the worker result does not get super dated. This is not reliable. And the 10ms delay also slows down every consecutive bind call.
This PR refactors the thread synchronization method. So, the timing between those two loops is more deterministic. And removes the performance penalty for all bind calls.
In 2.9.8, the distro processes are separated into their own cgroups. But they remain in the same cgroup namespace. That caused compatibility issues with softwares that assume a fixed systemd cgroup layout. For example, rootless docker and nerdctl.
Fixes on Moby and nertctl are being worked on. However, to avoid issues with other software, WSL's cgroup handling should also be improved.
This PR creates new cgroup namespaces for the distros. So, to the non-critical distro processes, the systemd cgroup layout stays the same as before.
This PR also introduces a cgroup structure change to accomplish this. The systemd init is moved from wsl-user/distro-N/systemd to wsl-user/distro-N. And the initialization of the distro-N controllers is handled by systemd instead.
The processes are also moved into the non-systemd cgroup in systemdless distros. This makes sure that sub-group controllers can be enabled in the distro root.
Cgroup v2 is now enforced when distro isolation is enabled. Instead of constructing an unusable cgroup v1 layout when cgroup v1 and distro isolation are both enabled.
In mirrored mode, handle route update as add instead of replace
Remove usage of route update as it can overwrite routes on other interfaces. add tests that verify host routing changes are correctly reflected in Linux
---------
Co-authored-by: Catalin-Emil Fetoiu <cfetoiu@microsoft.com>
Currently the stdout and stderr relay tracks the output offset separately. And when redirected to the same file, the writes could overlap.
This PR uses the append mode for overlap io writes instead of the separate offsets in the relay. So, the file write offset is correctly tracked by the system.
The current logic uses a fixed 4KiB buffer for stdio relay on the Windows side. And uses an initial 4KiB buffer for stdio relay on the Linux side, which grows only if the message won't fit. This can severely limit the relay performance in some situations. For example, in #41572, when redirecting stdout to a SMB share.
This PR increases thedefault relay buffer size to 64KiB. Which shows significant performance improvements according to buffer size tests.
`case '\r': break;` made a backslash before a CR the only unrecognised
escape the parser accepts without a diagnostic. In a CRLF file that left
the value truncated at the backslash and the remainder parsed as its own
line, with nothing reported.
Letting it fall into the default case gives the same
MessageConfigInvalidEscape warning as any other bad escape. As with those,
the line is then discarded rather than kept truncated.
The current factory function for LxssUserSession and WSLCSessionManager translates the error code CO_E_SERVER_STOPPING to S_FALSE. Which combined with *ppCreated == NULL causes COM to treat this as an OOM. Leading to error messages like this when activating a stopping service:
Not enough memory resources are available to complete this operation.
Error code: Wsl/E_OUTOFMEMORY
This PR removes this conversion. So, COM actually retries when the service is stopping. And returns CO_E_SERVER_EXEC_FAILURE if the retry times out.
When the per-user plugin Plan9 server is no longer running, the previous instance was dropped without a Teardown call, so it could still hold the Plan9 port when the replacement tries to bind it.
Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0d028ad4-fe5e-4ef1-9832-18ee0e4825cc
Avoid selecting the guest IPv4 address as its synthesized default gateway when the host adapter does not expose one.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
Copilot-Session: ce168659-cb9d-4f0e-8fd1-2834d065ba9d
* Fix virtiofs bind mounts exposing files as root-owned (#40719)
Add the 'metadata' option to virtiofs shares created via
HcsVirtualMachine::AddShare (the WSLC/Docker container path). Without
this option, the virtiofs device host cannot persist per-file uid/gid
in NTFS extended attributes, so all files default to uid=0/gid=0
regardless of the creating user.
This matches the behavior of the regular distro mount path
(WslCoreVm::AddVirtioFsShare) which receives metadata/uid/gid options
from the Linux init's ConvertDrvfsMountOptionsToPlan9.
Also adds a regression test (WindowsMountsVirtioFsFileOwnership) that
verifies file ownership is preserved on virtiofs mounts.
Fixes#40719
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address code review feedback
- Add inline comment explaining why 'metadata' is required
- Use unique mount point (/virtiofs-ownership-test) to avoid test interference
- Use numeric UID (su '#65534') instead of username to avoid environment dependency
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* test: use 'nobody' user instead of numeric UID in virtiofs ownership test
The su command with numeric UID syntax ('#65534') requires the user to
exist in /etc/passwd. Use the 'nobody' account directly since it is
already available in the test VHD.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* fix: wrap long test command strings to satisfy clang-format 130-col limit
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* test: unmount inline with VERIFY_SUCCEEDED to match file convention
Replace the scope_exit unmount cleanup with an inline VERIFY_SUCCEEDED
call at the end of the test, matching every other mount test in this
file (e.g. WindowsMountsVirtioFsShareReuse). This also asserts the
unmount HRESULT rather than silently swallowing it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9213b7da-c5c8-4d9c-89ab-e80048d288a2
* Fix Windows mount test API calls
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Blue <OneBlue@users.noreply.github.com>
Copilot-Session: 9213b7da-c5c8-4d9c-89ab-e80048d288a2
* Host plugin Plan9 shares as the session user
Use a dedicated per-user Plan9 server for plugin folder mounts so host filesystem permissions are preserved without relying on HCS-managed share identity.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c20b1da0-c613-489d-92a3-9c27527c0c54
* Simplify plugin Plan9 port plumbing
Use the fixed plugin port directly in mini_init and mirror the existing per-user Plan9 server lifecycle.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c20b1da0-c613-489d-92a3-9c27527c0c54
* Recreate stopped plugin Plan9 servers
Recreate the per-user server before adding a share when its process is no longer running.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c20b1da0-c613-489d-92a3-9c27527c0c54
---------
Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
Copilot-Session: c20b1da0-c613-489d-92a3-9c27527c0c54
* init: fix dhcpcd option name in bridged-mode config
dhcpcd has no option named "broadcast"; option 28 is "broadcast_address".
dhcpcd 10 rejects the whole "option" line, so DNS servers, domain, search
list, hostname and MTU were never requested from the DHCP server, leaving
/mnt/wsl/resolv.conf without nameservers on servers that honour the PRL.
* init: apply clang-format to dhcpcd config string
The longer broadcast_address option name pushed the literal past the
130-column limit in .clang-format. Split it as clang-format does; the
concatenated value is unchanged.
#41129 introduces a regression where the ";ro" option is passed to the host for p9 shares. However, that option is not supported by the p9 server and causes the mount to fail.
This PR removes the special handling of "ro" in the common parser. And use MountParseFlags to add the required virtio option.