Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions localization/strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,14 @@ On first run, creates the file with all settings commented out at their defaults
<data name="WSLCCLI_RemoveArgDescription" xml:space="preserve">
<value>Remove the container after it stops</value>
</data>
<data name="WSLCCLI_StopSignalArgDescription" xml:space="preserve">
<value>Signal to stop the container (default: {})</value>
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
</data>
<data name="WSLCCLI_ShmSizeArgDescription" xml:space="preserve">
<value>Size of /dev/shm (e.g. 64m, 1g)</value>
Comment thread
AmelBawa-msft marked this conversation as resolved.
<comment>{Locked="/dev/shm"}</comment>
</data>
<data name="WSLCCLI_SessionIdPositionalArgDescription" xml:space="preserve">
<value>Session ID</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions src/windows/common/WSLCContainerLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ void WSLCContainerLauncher::SetDefaultStopSignal(WSLCSignal Signal)
m_stopSignal = Signal;
}

void WSLCContainerLauncher::SetShmSize(ULONGLONG ShmSize)
{
m_shmSize = ShmSize;
}

void WSLCContainerLauncher::SetEntrypoint(std::vector<std::string>&& entrypoint)
{
m_entrypoint = std::move(entrypoint);
Expand Down Expand Up @@ -254,6 +259,7 @@ std::pair<HRESULT, std::optional<RunningWSLCContainer>> WSLCContainerLauncher::C
options.PortsCount = static_cast<ULONG>(m_ports.size());
options.StopSignal = m_stopSignal;
options.Flags = m_containerFlags;
options.ShmSize = m_shmSize;

if (!entrypointStorage.empty())
{
Expand Down
2 changes: 2 additions & 0 deletions src/windows/common/WSLCContainerLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class WSLCContainerLauncher : private WSLCProcessLauncher
void SetName(std::string&& Name);
void SetEntrypoint(std::vector<std::string>&& entrypoint);
void SetDefaultStopSignal(WSLCSignal Signal);
void SetShmSize(ULONGLONG ShmSize);
void SetContainerFlags(WSLCContainerFlags Flags);
void SetHostname(std::string&& Hostname);
void SetDomainname(std::string&& Domainame);
Expand All @@ -95,6 +96,7 @@ class WSLCContainerLauncher : private WSLCProcessLauncher
WSLCContainerNetworkType m_containerNetworkType;
std::vector<std::string> m_entrypoint;
WSLCSignal m_stopSignal = WSLCSignalNone;
ULONGLONG m_shmSize = 0;
WSLCContainerFlags m_containerFlags = WSLCContainerFlagsNone;
std::string m_hostname;
std::string m_domainname;
Expand Down
8 changes: 5 additions & 3 deletions src/windows/wslc/arguments/ArgumentDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ _(Follow, "follow", L"f", Kind::Flag, L
_(Format, "format", NO_ALIAS, Kind::Value, Localization::WSLCCLI_FormatArgDescription()) \
_(ForwardArgs, "arguments", NO_ALIAS, Kind::Forward, Localization::WSLCCLI_ForwardArgsDescription()) \
/*_(GroupId, "groupid", NO_ALIAS, Kind::Value, Localization::WSLCCLI_GroupIdArgDescription())*/ \
_(Help, "help", WSLC_CLI_HELP_ARG, Kind::Flag, Localization::WSLCCLI_HelpArgDescription()) \
_(Help, "help", WSLC_CLI_HELP_ARG, Kind::Flag, Localization::WSLCCLI_HelpArgDescription())\
_(Hostname, "hostname", L"h", Kind::Value, Localization::WSLCCLI_HostnameArgDescription()) \
_(ImageForce, "force", L"f", Kind::Flag, Localization::WSLCCLI_ImageForceArgDescription()) \
_(ImageId, "image", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_ImageIdArgDescription()) \
_(Input, "input", L"i", Kind::Value, Localization::WSLCCLI_InputArgDescription()) \
_(Input, "input", L"i", Kind::Value, Localization::WSLCCLI_InputArgDescription())\
_(Interactive, "interactive", L"i", Kind::Flag, Localization::WSLCCLI_InteractiveArgDescription()) \
_(Label, "label", NO_ALIAS, Kind::Value, L"Volume metadata setting") \
_(Name, "name", NO_ALIAS, Kind::Value, Localization::WSLCCLI_NameArgDescription()) \
Expand All @@ -85,9 +85,11 @@ _(Remove, "rm", NO_ALIAS, Kind::Flag, L
_(Server, "server", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_LoginServerArgDescription()) \
_(Session, "session", NO_ALIAS, Kind::Value, Localization::WSLCCLI_SessionIdArgDescription()) \
_(SessionId, "session-id", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_SessionIdPositionalArgDescription()) \
_(StoragePath, "storage-path", NO_ALIAS, Kind::Positional, L"Path to the session storage directory") \
_(ShmSize, "shm-size", NO_ALIAS, Kind::Value, Localization::WSLCCLI_ShmSizeArgDescription()) \
_(Signal, "signal", L"s", Kind::Value, Localization::WSLCCLI_SignalArgDescription(L"SIGKILL")) \
_(Source, "source", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_SourceArgDescription()) \
_(StoragePath, "storage-path", NO_ALIAS, Kind::Positional, L"Path to the session storage directory") \
_(StopSignal, "stop-signal", NO_ALIAS, Kind::Value, Localization::WSLCCLI_StopSignalArgDescription(L"SIGTERM")) \
_(Tag, "tag", L"t", Kind::Value, Localization::WSLCCLI_TagArgDescription()) \
_(Target, "target", NO_ALIAS, Kind::Positional, Localization::WSLCCLI_TargetArgDescription()) \
_(Time, "time", L"t", Kind::Value, Localization::WSLCCLI_TimeArgDescription()) \
Expand Down
28 changes: 28 additions & 0 deletions src/windows/wslc/arguments/ArgumentValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ void Argument::Validate(const ArgMap& execArgs) const
validation::ValidateWSLCSignalFromString(execArgs.GetAll<ArgType::Signal>(), m_name);
break;

case ArgType::StopSignal:
validation::ValidateWSLCSignalFromString(execArgs.GetAll<ArgType::StopSignal>(), m_name);
break;

case ArgType::ShmSize:
validation::ValidateMemorySize(execArgs.GetAll<ArgType::ShmSize>(), m_name);
break;

case ArgType::Time:
validation::ValidateIntegerFromString<LONGLONG>(execArgs.GetAll<ArgType::Time>(), m_name);
break;
Expand Down Expand Up @@ -191,4 +199,24 @@ InspectType GetInspectTypeFromString(const std::wstring& input, const std::wstri
}
}

void ValidateMemorySize(const std::vector<std::wstring>& values, const std::wstring& argName)
{
for (const auto& value : values)
{
std::ignore = GetMemorySizeFromString(value, argName);
}
}

ULONGLONG GetMemorySizeFromString(const std::wstring& input, const std::wstring& argName)
{
auto narrowInput = wsl::windows::common::string::WideToMultiByte(input);
auto parsed = wsl::shared::string::ParseMemorySize(narrowInput.c_str());
if (!parsed.has_value())
{
throw ArgumentException(std::format(L"Invalid {} argument value: '{}'. Expected a memory size (e.g. 64m, 256m, 1g)", argName, input));
}
Comment thread
AmelBawa-msft marked this conversation as resolved.

return parsed.value();
}

} // namespace wsl::windows::wslc::validation
3 changes: 3 additions & 0 deletions src/windows/wslc/arguments/ArgumentValidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ T GetIntegerFromString(const std::wstring& value, const std::wstring& argName =
void ValidateWSLCSignalFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName = {});

void ValidateMemorySize(const std::vector<std::wstring>& values, const std::wstring& argName);
ULONGLONG GetMemorySizeFromString(const std::wstring& input, const std::wstring& argName = {});

void ValidateFormatTypeFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName = {});

Expand Down
2 changes: 2 additions & 0 deletions src/windows/wslc/commands/ContainerCreateCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ std::vector<Argument> ContainerCreateCommand::GetArguments() const
Argument::Create(ArgType::Remove),
// Argument::Create(ArgType::Scheme),
Argument::Create(ArgType::Session),
Argument::Create(ArgType::ShmSize),
Argument::Create(ArgType::StopSignal),
Argument::Create(ArgType::TMPFS, false, NO_LIMIT),
Argument::Create(ArgType::TTY),
Argument::Create(ArgType::User),
Expand Down
2 changes: 2 additions & 0 deletions src/windows/wslc/commands/ContainerRunCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ std::vector<Argument> ContainerRunCommand::GetArguments() const
Argument::Create(ArgType::Remove),
// Argument::Create(ArgType::Scheme),
Argument::Create(ArgType::Session),
Argument::Create(ArgType::ShmSize),
Argument::Create(ArgType::StopSignal),
Argument::Create(ArgType::TMPFS, false, NO_LIMIT),
Argument::Create(ArgType::TTY),
Argument::Create(ArgType::User),
Expand Down
2 changes: 2 additions & 0 deletions src/windows/wslc/services/ContainerModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct ContainerOptions
bool Remove = false;
bool TTY = false;
bool PublishAll = false;
WSLCSignal StopSignal = WSLCSignalNone;
ULONGLONG ShmSize = 0;
std::vector<std::string> Ports;
std::vector<std::wstring> Volumes;
std::string WorkingDirectory;
Expand Down
10 changes: 10 additions & 0 deletions src/windows/wslc/services/ContainerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ static wsl::windows::common::RunningWSLCContainer CreateInternal(Session& sessio

containerLauncher.SetContainerFlags(containerFlags);

if (options.StopSignal != WSLCSignalNone)
{
containerLauncher.SetDefaultStopSignal(options.StopSignal);
}

if (options.ShmSize > 0)
{
containerLauncher.SetShmSize(options.ShmSize);
}

if (!options.Entrypoint.empty())
{
auto entrypoints = options.Entrypoint;
Expand Down
10 changes: 10 additions & 0 deletions src/windows/wslc/tasks/ContainerTasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ void SetContainerOptionsFromArgs(CLIExecutionContext& context)
options.Remove = true;
}

if (context.Args.Contains(ArgType::StopSignal))
{
options.StopSignal = validation::GetWSLCSignalFromString(context.Args.Get<ArgType::StopSignal>());
}

if (context.Args.Contains(ArgType::ShmSize))
{
options.ShmSize = validation::GetMemorySizeFromString(context.Args.Get<ArgType::ShmSize>());
}

if (context.Args.Contains(ArgType::Command))
{
options.Arguments.emplace_back(WideToMultiByte(context.Args.Get<ArgType::Command>()));
Expand Down
22 changes: 22 additions & 0 deletions test/windows/wslc/e2e/WSLCE2EContainerCreateTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,26 @@ class WSLCE2EContainerCreateTests
VERIFY_IS_TRUE(result.Stdout->find(L"options ndots:5 timeout:3") != std::wstring::npos);
}

WSLC_TEST_METHOD(WSLCE2E_Container_Create_StopSignal)
{
auto result = RunWslc(std::format(
L"container create --stop-signal SIGUSR1 --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
std::wstring containerId = result.GetStdoutOneLine();
VerifyContainerIsListed(containerId, L"created");
}

WSLC_TEST_METHOD(WSLCE2E_Container_Create_ShmSize)
{
auto result = RunWslc(
std::format(L"container create --shm-size 128m --name {} {} df -h /dev/shm", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});

result = RunWslc(std::format(L"container start -a {}", WslcContainerName));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"128M") != std::wstring::npos);
Comment thread
AmelBawa-msft marked this conversation as resolved.
}

private:
// Test container name
const std::wstring WslcContainerName = L"wslc-test-container";
Expand Down Expand Up @@ -724,6 +744,8 @@ class WSLCE2EContainerCreateTests
<< L" -P,--publish-all Publish all exposed ports to random host ports\r\n"
<< L" --rm Remove the container after it stops\r\n"
<< L" --session Specify the session to use\r\n"
<< L" --shm-size Size of /dev/shm (e.g. 64m, 1g)\r\n"
<< L" --stop-signal Signal to stop the container (default: SIGTERM)\r\n"
<< L" --tmpfs Mount tmpfs to the container at the given path\r\n"
<< L" -t,--tty Open a TTY with the container process.\r\n"
<< L" -u,--user User ID for the process (name|uid|uid:gid)\r\n"
Expand Down
18 changes: 18 additions & 0 deletions test/windows/wslc/e2e/WSLCE2EContainerRunTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,22 @@ class WSLCE2EContainerRunTests
result.Verify({.Stderr = std::format(L"Volume not found: '{}'\r\nError code: WSLC_E_VOLUME_NOT_FOUND\r\n", WslcVolumeName), .ExitCode = 1});
}

WSLC_TEST_METHOD(WSLCE2E_Container_Run_StopSignal)
{
auto result = RunWslc(std::format(
L"container run -d --stop-signal SIGUSR1 --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
auto containerId = result.GetStdoutOneLine();
VerifyContainerIsListed(containerId, L"running");
}

WSLC_TEST_METHOD(WSLCE2E_Container_Run_ShmSize)
{
auto result = RunWslc(std::format(L"container run --rm --shm-size 128m {} df -h /dev/shm", DebianImage.NameAndTag()));
Comment thread
AmelBawa-msft marked this conversation as resolved.
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"128M") != std::wstring::npos);
}

private:
// Test container name
const std::wstring WslcContainerName = L"wslc-test-container";
Expand Down Expand Up @@ -720,6 +736,8 @@ class WSLCE2EContainerRunTests
<< L" -P,--publish-all Publish all exposed ports to random host ports\r\n"
<< L" --rm Remove the container after it stops\r\n"
<< L" --session Specify the session to use\r\n"
<< L" --shm-size Size of /dev/shm (e.g. 64m, 1g)\r\n"
<< L" --stop-signal Signal to stop the container (default: SIGTERM)\r\n"
<< L" --tmpfs Mount tmpfs to the container at the given path\r\n"
<< L" -t,--tty Open a TTY with the container process.\r\n"
<< L" -u,--user User ID for the process (name|uid|uid:gid)\r\n"
Expand Down
Loading