mirror of
https://github.com/microsoft/WSL.git
synced 2025-12-10 17:47:59 -06:00
Expose the VM ID inside user distros (#13212)
* Add --vm-id to wslinfo usage string * Pass the VM id to init This change ensures that we pass the vm id to an instances init. The id is then set as an environment variable and can be accessed at runtime. * Expose VM id to wslinfo Add a new argument --vm-id to wslinfo so that the caller can retrieve the VM id by calling the binary. Although it is an environment variable, it can be useful here too to save additional string parsing from the caller.
This commit is contained in:
parent
e563689b88
commit
4547e2a6f2
@ -321,6 +321,9 @@ Please enable the "Windows Subsystem for Linux" optional component to use WSL1.<
|
||||
--msal-proxy-path
|
||||
Display the path to the MSAL proxy application.
|
||||
|
||||
--vm-id
|
||||
Display the WSL VM ID.
|
||||
|
||||
--version
|
||||
Display the version of the WSL package.
|
||||
|
||||
@ -328,6 +331,7 @@ Please enable the "Windows Subsystem for Linux" optional component to use WSL1.<
|
||||
Do not print a newline.</value>
|
||||
<comment>{Locked="--networking-mode
|
||||
"}{Locked="--msal-proxy-path
|
||||
"}{Locked="--vm-id
|
||||
"}{Locked="--version
|
||||
"}Command line arguments, file names and string inserts should not be translated</comment>
|
||||
</data>
|
||||
|
||||
@ -2582,7 +2582,7 @@ void ProcessLaunchInitMessage(
|
||||
DISTRO_PATH,
|
||||
enableGuiApps,
|
||||
Config,
|
||||
nullptr,
|
||||
wsl::shared::string::FromSpan(Buffer, Message->VmIdOffset),
|
||||
wsl::shared::string::FromSpan(Buffer, Message->DistributionNameOffset),
|
||||
nullptr,
|
||||
wsl::shared::string::FromSpan(Buffer, Message->InstallPathOffset),
|
||||
|
||||
@ -23,12 +23,14 @@ Abstract:
|
||||
#include "defs.h"
|
||||
#include "Localization.h"
|
||||
#include "CommandLine.h"
|
||||
#include "../../shared/inc/lxinitshared.h"
|
||||
|
||||
enum class WslInfoMode
|
||||
{
|
||||
GetNetworkingMode,
|
||||
MsalProxyPath,
|
||||
WslVersion
|
||||
WslVersion,
|
||||
VMId
|
||||
};
|
||||
|
||||
int WslInfoEntry(int Argc, char* Argv[])
|
||||
@ -65,6 +67,7 @@ Return Value:
|
||||
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::MsalProxyPath>{Mode, Usage}, WSLINFO_MSAL_PROXY_PATH);
|
||||
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION);
|
||||
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION_LEGACY);
|
||||
parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::VMId>{Mode, Usage}, WSLINFO_WSL_VMID);
|
||||
parser.AddArgument(NoOp{}, WSLINFO_WSL_HELP);
|
||||
parser.AddArgument(noNewLine, nullptr, WSLINFO_NO_NEWLINE);
|
||||
|
||||
@ -144,6 +147,22 @@ Return Value:
|
||||
{
|
||||
std::cout << WSL_PACKAGE_VERSION;
|
||||
}
|
||||
else if (Mode.value() == WslInfoMode::VMId)
|
||||
{
|
||||
auto value = UtilGetEnvironmentVariable(LX_WSL2_VM_ID_ENV);
|
||||
if (value.empty())
|
||||
{
|
||||
std::cerr << Localization::MessageNoValueFound() << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << value;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false && "Unknown WslInfoMode");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!noNewLine)
|
||||
{
|
||||
|
||||
@ -20,6 +20,7 @@ Abstract:
|
||||
#define WSLINFO_NETWORKING_MODE "--networking-mode"
|
||||
#define WSLINFO_WSL_VERSION "--version"
|
||||
#define WSLINFO_WSL_VERSION_LEGACY "--wsl-version"
|
||||
#define WSLINFO_WSL_VMID "--vm-id"
|
||||
#define WSLINFO_WSL_HELP "--help"
|
||||
#define WSLINFO_NO_NEWLINE 'n'
|
||||
|
||||
|
||||
@ -842,6 +842,18 @@ class UnitTests
|
||||
L"Invalid command line argument: --invalid\nPlease use 'wslinfo --help' to get a list of supported "
|
||||
L"arguments.\n");
|
||||
}
|
||||
|
||||
if (LxsstuVmMode())
|
||||
{
|
||||
// Get the VM ID from the distro and validate that it not null.
|
||||
auto [vmId, vmIdErr] = LxsstuLaunchWslAndCaptureOutput(L"env | grep 'WSL2_VM_ID' | awk -F= '{print $2}'");
|
||||
VERIFY_ARE_NOT_EQUAL(vmId, L"");
|
||||
|
||||
// Ensure that the response from wslinfo matches the VM id from the distros environment
|
||||
auto [out, err] = LxsstuLaunchWslAndCaptureOutput(L"wslinfo --vm-id");
|
||||
VERIFY_ARE_EQUAL(out, std::format(L"{}", vmId));
|
||||
VERIFY_ARE_EQUAL(err, L"");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_METHOD(WslPath)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user