-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathHcsVirtualMachine.cpp
More file actions
742 lines (609 loc) · 24.6 KB
/
HcsVirtualMachine.cpp
File metadata and controls
742 lines (609 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
HcsVirtualMachine.cpp
Abstract:
Implementation of IWSLCVirtualMachine - represents a single HCS-based VM instance.
--*/
#include "HcsVirtualMachine.h"
#include <format>
#include "hcs_schema.h"
#include "VirtioNetworking.h"
#include "NatNetworking.h"
#include "wslsecurity.h"
#include "wslutil.h"
#include "lxinitshared.h"
#include "DnsResolver.h"
using namespace wsl::windows::common;
using helpers::WindowsBuildNumbers;
using wsl::windows::service::wslc::HcsVirtualMachine;
constexpr auto MAX_VM_CRASH_FILES = 3;
constexpr auto SAVED_STATE_FILE_EXTENSION = L".vmrs";
constexpr auto SAVED_STATE_FILE_PREFIX = L"saved-state-";
HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
{
THROW_HR_IF(E_POINTER, Settings == nullptr);
// Store the user token.
m_userToken = wil::shared_handle{wsl::windows::common::security::GetUserToken(TokenImpersonation).release()};
m_virtioFsClassId = wsl::windows::common::security::IsTokenElevated(m_userToken.get()) ? VIRTIO_FS_ADMIN_CLASS_ID : VIRTIO_FS_CLASS_ID;
m_crashDumpFolder = GetCrashDumpFolder();
std::lock_guard lock(m_lock);
THROW_IF_FAILED(CoCreateGuid(&m_vmId));
m_vmIdString = wsl::shared::string::GuidToString<wchar_t>(m_vmId, wsl::shared::string::GuidToStringFlags::Uppercase);
m_featureFlags = Settings->FeatureFlags;
m_networkingMode = Settings->NetworkingMode;
m_bootTimeoutMs = Settings->BootTimeoutMs;
// Build HCS settings
hcs::ComputeSystem systemSettings{};
systemSettings.Owner = L"WSL";
systemSettings.ShouldTerminateOnLastHandleClosed = true;
// Determine which schema version to use based on the Windows version. Windows 10 does not support
// newer schema versions and some features may be disabled as a result.
if (wsl::windows::common::helpers::IsWindows11OrAbove())
{
systemSettings.SchemaVersion.Major = 2;
systemSettings.SchemaVersion.Minor = 7;
}
else
{
systemSettings.SchemaVersion.Major = 2;
systemSettings.SchemaVersion.Minor = 3;
}
hcs::VirtualMachine vmSettings{};
vmSettings.StopOnReset = true;
vmSettings.Chipset.UseUtc = true;
// Ensure the 2MB granularity enforced by HCS.
vmSettings.ComputeTopology.Memory.SizeInMB = Settings->MemoryMb & ~0x1;
vmSettings.ComputeTopology.Memory.AllowOvercommit = true;
vmSettings.ComputeTopology.Memory.EnableDeferredCommit = true;
vmSettings.ComputeTopology.Memory.EnableColdDiscardHint = true;
vmSettings.ComputeTopology.Processor.Count = Settings->CpuCount;
// Configure backing page size, fault cluster shift size, and cold discard hint size to favor density (lower vmmem usage).
//
// N.B. Cold discard hint size should be a multiple of the fault cluster shift size.
const auto windowsVersion = wsl::windows::common::helpers::GetWindowsVersion();
if (windowsVersion.BuildNumber >= WindowsBuildNumbers::Germanium)
{
vmSettings.ComputeTopology.Memory.BackingPageSize = hcs::MemoryBackingPageSize::Small;
vmSettings.ComputeTopology.Memory.FaultClusterSizeShift = 4;
vmSettings.ComputeTopology.Memory.DirectMapFaultClusterSizeShift = 4;
}
if (helpers::IsVmemmSuffixSupported() && Settings->DisplayName)
{
vmSettings.ComputeTopology.Memory.HostingProcessNameSuffix = Settings->DisplayName;
}
#ifdef _AMD64_
HV_X64_HYPERVISOR_HARDWARE_FEATURES hardwareFeatures{};
__cpuid(reinterpret_cast<int*>(&hardwareFeatures), HvCpuIdFunctionMsHvHardwareFeatures);
vmSettings.ComputeTopology.Processor.EnablePerfmonPmu = hardwareFeatures.ChildPerfmonPmuSupported != 0;
vmSettings.ComputeTopology.Processor.EnablePerfmonLbr = hardwareFeatures.ChildPerfmonLbrSupported != 0;
#endif
// Initialize kernel command line.
std::wstring kernelCmdLine = L"initrd=\\" LXSS_VM_MODE_INITRD_NAME L" " TEXT(WSLC_ROOT_INIT_ENV) L"=1 panic=-1";
kernelCmdLine += std::format(L" nr_cpus={}", Settings->CpuCount);
// Enable timesync workaround to sync on resume from sleep in modern standby.
kernelCmdLine += L" hv_utils.timesync_implicit=1";
// Setup dmesg collector with optional DmesgOutput handle.
// TODO: move dmesg collector to user session process.
// N.B. 'DmesgOutput' needs to be duplicated since COM will close it when this call completes.
wil::unique_handle dmesgOutputHandle;
if (Settings->DmesgOutput.Handle.File != nullptr && Settings->DmesgOutput.Handle.File != INVALID_HANDLE_VALUE)
{
dmesgOutputHandle.reset(wslutil::DuplicateHandle(wslutil::FromCOMInputHandle(Settings->DmesgOutput), GENERIC_WRITE | SYNCHRONIZE));
}
m_dmesgCollector = DmesgCollector::Create(
m_vmId, m_vmExitEvent, true, false, L"", FeatureEnabled(WslcFeatureFlagsEarlyBootDmesg), std::move(dmesgOutputHandle));
if (FeatureEnabled(WslcFeatureFlagsEarlyBootDmesg))
{
kernelCmdLine += L" earlycon=uart8250,io,0x3f8,115200";
vmSettings.Devices.ComPorts["0"] = hcs::ComPort{m_dmesgCollector->EarlyConsoleName()};
}
if (helpers::IsVirtioSerialConsoleSupported())
{
kernelCmdLine += L" console=hvc0 debug";
vmSettings.Devices.VirtioSerial.emplace();
hcs::VirtioSerialPort virtioPort{};
virtioPort.Name = L"hvc0";
virtioPort.NamedPipe = m_dmesgCollector->VirtioConsoleName();
virtioPort.ConsoleSupport = true;
vmSettings.Devices.VirtioSerial->Ports["0"] = std::move(virtioPort);
}
// Set up boot params.
//
// N.B. Linux kernel direct boot is not yet supported on ARM64.
auto basePath = wslutil::GetBasePath();
#ifdef WSL_KERNEL_PATH
auto kernelPath = std::filesystem::path(WSL_KERNEL_PATH);
#else
auto kernelPath = std::filesystem::path(basePath) / L"tools" / LXSS_VM_MODE_KERNEL_NAME;
#endif
if constexpr (!wsl::shared::Arm64)
{
vmSettings.Chipset.LinuxKernelDirect.emplace();
vmSettings.Chipset.LinuxKernelDirect->KernelFilePath = kernelPath.wstring();
vmSettings.Chipset.LinuxKernelDirect->InitRdPath = (basePath / L"tools" / LXSS_VM_MODE_INITRD_NAME).c_str();
vmSettings.Chipset.LinuxKernelDirect->KernelCmdLine = kernelCmdLine;
}
else
{
auto bootThis = hcs::UefiBootEntry{};
bootThis.DeviceType = hcs::UefiBootDevice::VmbFs;
bootThis.VmbFsRootPath = (basePath / L"tools").c_str();
bootThis.DevicePath = L"\\" LXSS_VM_MODE_KERNEL_NAME;
bootThis.OptionalData = kernelCmdLine;
hcs::Uefi uefiSettings{};
uefiSettings.BootThis = std::move(bootThis);
vmSettings.Chipset.Uefi = std::move(uefiSettings);
}
#ifdef WSL_KERNEL_MODULES_PATH
auto kernelModulesPath = std::filesystem::path(TEXT(WSL_KERNEL_MODULES_PATH));
#else
auto kernelModulesPath = basePath / L"tools" / L"modules.vhd";
#endif
// Get root VHD path
std::filesystem::path rootVhdPath;
if (Settings->RootVhdOverride != nullptr)
{
rootVhdPath = Settings->RootVhdOverride;
}
else
{
#ifdef WSL_SYSTEM_DISTRO_PATH
rootVhdPath = TEXT(WSL_SYSTEM_DISTRO_PATH);
#else
rootVhdPath = std::filesystem::path(wslutil::GetMsiPackagePath().value()) / L"system.vhd";
#endif
}
// Setup boot VHDs
hcs::Scsi scsiController{};
auto attachScsiDisk = [&](PCWSTR path) {
const ULONG lun = AllocateLun();
hcs::Attachment disk{};
disk.Type = hcs::AttachmentType::VirtualDisk;
disk.Path = path;
disk.ReadOnly = true;
disk.SupportCompressedVolumes = true;
disk.AlwaysAllowSparseFiles = true;
disk.SupportEncryptedFiles = true;
scsiController.Attachments[std::to_string(lun)] = std::move(disk);
DiskInfo diskInfo{path};
m_attachedDisks.emplace(lun, std::move(diskInfo));
};
attachScsiDisk(rootVhdPath.c_str());
attachScsiDisk(kernelModulesPath.c_str());
vmSettings.Devices.Scsi["0"] = std::move(scsiController);
// Setup HvSocket security
auto tokenUser = wil::get_token_information<TOKEN_USER>(m_userToken.get());
wil::unique_hlocal_string userSidString;
THROW_LAST_ERROR_IF(!ConvertSidToStringSidW(tokenUser->User.Sid, &userSidString));
std::wstring securityDescriptor = std::format(L"D:P(A;;FA;;;SY)(A;;FA;;;{})", userSidString.get());
hcs::HvSocket hvSocketConfig{};
hvSocketConfig.HvSocketConfig.DefaultBindSecurityDescriptor = securityDescriptor;
hvSocketConfig.HvSocketConfig.DefaultConnectSecurityDescriptor = securityDescriptor;
vmSettings.Devices.HvSocket = std::move(hvSocketConfig);
// Enable .vmrs dump collection if supported.
if (wsl::windows::common::helpers::IsWindows11OrAbove())
{
CreateVmSavedStateFile(m_userToken.get());
if (!m_vmSavedStateFile.empty())
{
hcs::DebugOptions debugOptions{};
debugOptions.BugcheckSavedStateFileName = m_vmSavedStateFile;
vmSettings.DebugOptions = std::move(debugOptions);
}
}
systemSettings.VirtualMachine = std::move(vmSettings);
auto json = wsl::shared::ToJsonW(systemSettings);
WSL_LOG("CreateWSLCVirtualMachine", TraceLoggingValue(json.c_str(), "json"));
// Create and start compute system
m_computeSystem = hcs::CreateComputeSystem(m_vmIdString.c_str(), json.c_str());
if (FeatureEnabled(WslcFeatureFlagsVirtioFs) || m_networkingMode == WSLCNetworkingModeVirtioProxy)
{
m_guestDeviceManager = std::make_shared<::GuestDeviceManager>(m_vmIdString, m_vmId);
}
// Configure termination callback
if (Settings->TerminationCallback)
{
m_terminationCallback = Settings->TerminationCallback;
}
hcs::RegisterCallback(m_computeSystem.get(), &HcsVirtualMachine::OnVmExitCallback, this);
// Create a listening socket for mini_init to connect to once the VM is running.
m_listenSocket = wsl::windows::common::hvsocket::Listen(m_vmId, LX_INIT_UTILITY_VM_INIT_PORT);
// Start the virtual machine
hcs::StartComputeSystem(m_computeSystem.get(), json.c_str());
// Add GPU to the VM if requested
if (FeatureEnabled(WslcFeatureFlagsGPU))
{
hcs::ModifySettingRequest<hcs::GpuConfiguration> gpuRequest{};
gpuRequest.ResourcePath = L"VirtualMachine/ComputeTopology/Gpu";
gpuRequest.RequestType = hcs::ModifyRequestType::Update;
gpuRequest.Settings.AssignmentMode = hcs::GpuAssignmentMode::Mirror;
gpuRequest.Settings.AllowVendorExtension = true;
if (wsl::windows::common::hcs::IsDisableVgpuSettingsSupported())
{
gpuRequest.Settings.DisableGdiAcceleration = true;
gpuRequest.Settings.DisablePresentation = true;
}
hcs::ModifyComputeSystem(m_computeSystem.get(), wsl::shared::ToJsonW(gpuRequest).c_str());
}
}
HcsVirtualMachine::~HcsVirtualMachine()
{
std::lock_guard lock(m_lock);
// Wait up to 5 seconds for the VM to terminate gracefully.
bool forceTerminate = false;
if (!m_vmExitEvent.wait(5000))
{
forceTerminate = true;
try
{
hcs::TerminateComputeSystem(m_computeSystem.get());
}
CATCH_LOG()
}
WSL_LOG("WSLCTerminateVm", TraceLoggingValue(forceTerminate, "forced"));
// N.B. Destruction order matters: the networking engine and device manager must be torn down
// before the compute system handle is closed. The networking engine holds a shared_ptr to
// GuestDeviceManager, so it must be released first for the device manager reset to be effective.
m_networkEngine.reset();
m_guestDeviceManager.reset();
m_computeSystem.reset();
// Revoke VM access for attached disks
for (const auto& e : m_attachedDisks)
{
try
{
if (e.second.AccessGranted)
{
hcs::RevokeVmAccess(m_vmIdString.c_str(), e.second.Path.c_str());
}
}
CATCH_LOG()
}
// If the VM did not crash, the saved state file should be empty, so we can remove it.
if (!m_vmSavedStateFile.empty() && !m_vmSavedStateCaptured)
{
try
{
WI_ASSERT(std::filesystem::is_empty(m_vmSavedStateFile));
std::filesystem::remove(m_vmSavedStateFile);
}
CATCH_LOG()
}
}
bool HcsVirtualMachine::FeatureEnabled(WSLCFeatureFlags Value) const
{
return static_cast<ULONG>(m_featureFlags) & static_cast<ULONG>(Value);
}
HRESULT HcsVirtualMachine::GetId(_Out_ GUID* VmId)
try
{
*VmId = m_vmId;
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::AcceptConnection(_Out_ HANDLE* Socket)
try
{
auto socket = wsl::windows::common::hvsocket::CancellableAccept(m_listenSocket.get(), m_bootTimeoutMs, m_vmExitEvent.get());
THROW_HR_IF(E_ABORT, !socket.has_value());
*Socket = reinterpret_cast<HANDLE>(socket->release());
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::ConfigureNetworking(_In_ HANDLE GnsSocket, _In_opt_ HANDLE* DnsSocket)
try
{
std::lock_guard lock(m_lock);
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED), m_networkEngine != nullptr);
if (m_networkingMode == WSLCNetworkingModeNone)
{
return S_OK;
}
// Duplicate the socket handles - COM manages the lifetime of the marshalled handles,
// so we need our own copies to take ownership.
wil::unique_socket gnsSocketHandle{reinterpret_cast<SOCKET>(wslutil::DuplicateHandle(GnsSocket))};
wil::unique_socket dnsSocketHandle;
if (FeatureEnabled(WslcFeatureFlagsDnsTunneling))
{
THROW_HR_IF(E_INVALIDARG, DnsSocket == nullptr);
const auto result = wsl::core::networking::DnsResolver::LoadDnsResolverMethods();
if (FAILED(result))
{
LOG_HR_MSG(result, "Failed to load DNS resolver methods, DNS tunneling will be disabled");
WI_ClearFlag(m_featureFlags, WslcFeatureFlagsDnsTunneling);
}
else
{
dnsSocketHandle.reset(reinterpret_cast<SOCKET>(wslutil::DuplicateHandle(*DnsSocket)));
}
}
else
{
THROW_HR_IF(E_INVALIDARG, DnsSocket != nullptr);
}
if (m_networkingMode == WSLCNetworkingModeNAT)
{
// TODO: refactor this to avoid using wsl config
m_natConfig.emplace(nullptr);
if (!wsl::core::NatNetworking::IsHyperVFirewallSupported(*m_natConfig))
{
m_natConfig->FirewallConfig.reset();
}
// Enable DNS tunneling if a DNS socket was provided
if (FeatureEnabled(WslcFeatureFlagsDnsTunneling))
{
WI_ASSERT(dnsSocketHandle);
m_natConfig->EnableDnsTunneling = true;
in_addr address{};
WI_VERIFY(inet_pton(AF_INET, LX_INIT_DNS_TUNNELING_IP_ADDRESS, &address) == 1);
m_natConfig->DnsTunnelingIpAddress = address.S_un.S_addr;
}
m_networkEngine = std::make_unique<wsl::core::NatNetworking>(
m_computeSystem.get(),
wsl::core::NatNetworking::CreateNetwork(*m_natConfig),
wsl::core::GnsChannel(std::move(gnsSocketHandle)),
*m_natConfig,
std::move(dnsSocketHandle),
nullptr);
}
else if (m_networkingMode == WSLCNetworkingModeVirtioProxy)
{
wsl::core::VirtioNetworkingFlags flags = wsl::core::VirtioNetworkingFlags::Ipv6;
if (FeatureEnabled(WslcFeatureFlagsDnsTunneling))
{
WI_SetFlag(flags, wsl::core::VirtioNetworkingFlags::DnsTunnelingSocket);
}
m_networkEngine = std::make_unique<wsl::core::VirtioNetworking>(
wsl::core::GnsChannel(std::move(gnsSocketHandle)), flags, nullptr, m_guestDeviceManager, m_userToken, std::move(dnsSocketHandle));
}
else
{
THROW_HR_MSG(E_INVALIDARG, "Invalid networking mode: %lu", m_networkingMode);
}
m_networkEngine->Initialize();
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::AttachDisk(_In_ LPCWSTR Path, _In_ BOOL ReadOnly, _Out_ ULONG* Lun)
try
{
RETURN_HR_IF(E_POINTER, Path == nullptr || Lun == nullptr);
std::lock_guard lock(m_lock);
DiskInfo disk{Path};
const ULONG allocatedLun = AllocateLun();
auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() {
if (disk.AccessGranted)
{
hcs::RevokeVmAccess(m_vmIdString.c_str(), disk.Path.c_str());
}
FreeLun(allocatedLun);
});
auto grantDiskAccess = [&]() {
auto runAsUser = wil::impersonate_token(m_userToken.get());
hcs::GrantVmAccess(m_vmIdString.c_str(), Path);
disk.AccessGranted = true;
};
if (!ReadOnly)
{
grantDiskAccess();
}
auto result = wil::ResultFromException([&]() { hcs::AddVhd(m_computeSystem.get(), Path, allocatedLun, ReadOnly); });
if (result == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) && !disk.AccessGranted)
{
grantDiskAccess();
hcs::AddVhd(m_computeSystem.get(), Path, allocatedLun, ReadOnly);
}
else
{
THROW_IF_FAILED(result);
}
m_attachedDisks.emplace(allocatedLun, std::move(disk));
cleanup.release();
*Lun = allocatedLun;
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::DetachDisk(_In_ ULONG Lun)
try
{
std::lock_guard lock(m_lock);
auto it = m_attachedDisks.find(Lun);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), it == m_attachedDisks.end());
hcs::RemoveScsiDisk(m_computeSystem.get(), Lun);
FreeLun(Lun);
if (it->second.AccessGranted)
{
hcs::RevokeVmAccess(m_vmIdString.c_str(), it->second.Path.c_str());
}
m_attachedDisks.erase(it);
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::AddShare(_In_ LPCWSTR WindowsPath, _In_ BOOL ReadOnly, _Out_ GUID* ShareId)
try
{
RETURN_HR_IF(E_POINTER, WindowsPath == nullptr || ShareId == nullptr);
std::lock_guard lock(m_lock);
GUID shareIdLocal;
THROW_IF_FAILED(CoCreateGuid(&shareIdLocal));
auto shareName = wsl::shared::string::GuidToString<wchar_t>(shareIdLocal, wsl::shared::string::None);
// Add the share entry upfront so the emplace cannot fail after the device is created.
auto it = m_shares.emplace(shareIdLocal, std::nullopt).first;
auto cleanup = wil::scope_exit([&]() { m_shares.erase(it); });
if (!FeatureEnabled(WslcFeatureFlagsVirtioFs))
{
auto flags = hcs::Plan9ShareFlags::AllowOptions;
WI_SetFlagIf(flags, hcs::Plan9ShareFlags::ReadOnly, ReadOnly);
hcs::AddPlan9Share(
m_computeSystem.get(),
shareName.c_str(),
shareName.c_str(),
WindowsPath,
LX_INIT_UTILITY_VM_PLAN9_PORT,
flags,
m_userToken.get());
}
else
{
it->second = m_guestDeviceManager->AddGuestDevice(
VIRTIO_FS_DEVICE_ID,
m_virtioFsClassId,
shareName.c_str(),
ReadOnly ? L"ro" : L"",
WindowsPath,
VIRTIO_FS_FLAGS_TYPE_FILES,
m_userToken.get());
}
cleanup.release();
*ShareId = shareIdLocal;
return S_OK;
}
CATCH_RETURN()
HRESULT HcsVirtualMachine::RemoveShare(_In_ REFGUID ShareId)
try
{
std::lock_guard lock(m_lock);
auto it = m_shares.find(ShareId);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), it == m_shares.end());
if (!it->second.has_value())
{
auto shareName = wsl::shared::string::GuidToString<wchar_t>(it->first, wsl::shared::string::None);
hcs::RemovePlan9Share(m_computeSystem.get(), shareName.c_str(), LX_INIT_UTILITY_VM_PLAN9_PORT);
}
else
{
m_guestDeviceManager->RemoveGuestDevice(VIRTIO_FS_DEVICE_ID, it->second.value());
}
m_shares.erase(it);
return S_OK;
}
CATCH_RETURN()
void CALLBACK HcsVirtualMachine::OnVmExitCallback(HCS_EVENT* Event, void* Context)
try
{
WSL_LOG(
"OnVmExitCallback",
TraceLoggingValue(Event->EventData, "details"),
TraceLoggingValue(static_cast<int>(Event->Type), "type"));
auto* vm = reinterpret_cast<HcsVirtualMachine*>(Context);
if (Event->Type == HcsEventSystemExited)
{
vm->OnExit(Event);
}
else if (Event->Type == HcsEventSystemCrashInitiated || Event->Type == HcsEventSystemCrashReport)
{
vm->OnCrash(Event);
}
}
CATCH_LOG()
void HcsVirtualMachine::OnExit(const HCS_EVENT* Event)
{
m_vmExitEvent.SetEvent();
const auto exitStatus = wsl::shared::FromJson<wsl::windows::common::hcs::SystemExitStatus>(Event->EventData);
auto reason = WSLCVirtualMachineTerminationReasonUnknown;
if (exitStatus.ExitType.has_value())
{
switch (exitStatus.ExitType.value())
{
case hcs::NotificationType::ForcedExit:
case hcs::NotificationType::GracefulExit:
reason = WSLCVirtualMachineTerminationReasonShutdown;
break;
case hcs::NotificationType::UnexpectedExit:
reason = WSLCVirtualMachineTerminationReasonCrashed;
break;
default:
reason = WSLCVirtualMachineTerminationReasonUnknown;
break;
}
}
if (m_terminationCallback)
{
LOG_IF_FAILED(m_terminationCallback->OnTermination(reason, Event->EventData));
}
}
void HcsVirtualMachine::OnCrash(const HCS_EVENT* Event)
{
if (m_crashLogCaptured && m_vmSavedStateCaptured)
{
return;
}
const auto crashReport = wsl::shared::FromJson<wsl::windows::common::hcs::CrashReport>(Event->EventData);
if (crashReport.GuestCrashSaveInfo.has_value() && crashReport.GuestCrashSaveInfo->SaveStateFile.has_value())
{
m_vmSavedStateCaptured = true;
EnforceVmSavedStateFileLimit();
}
if (!m_crashLogCaptured && !crashReport.CrashLog.empty())
{
WriteCrashLog(crashReport.CrashLog);
}
}
std::filesystem::path HcsVirtualMachine::GetCrashDumpFolder()
{
auto tempPath = wsl::windows::common::filesystem::GetTempFolderPath(m_userToken.get());
return tempPath / L"wslc-crashes";
}
void HcsVirtualMachine::CreateVmSavedStateFile(HANDLE InUserToken)
{
auto runAsUser = wil::impersonate_token(InUserToken);
const auto filename = std::format(L"saved-state-{}-{}.vmrs", std::time(nullptr), m_vmIdString);
auto savedStateFile = m_crashDumpFolder / filename;
wsl::windows::common::filesystem::EnsureDirectory(m_crashDumpFolder.c_str());
wil::unique_handle file{CreateFileW(savedStateFile.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, nullptr)};
THROW_LAST_ERROR_IF(!file);
hcs::GrantVmAccess(m_vmIdString.c_str(), savedStateFile.c_str());
m_vmSavedStateFile = savedStateFile;
}
void HcsVirtualMachine::EnforceVmSavedStateFileLimit()
{
auto runAsUser = wil::impersonate_token(m_userToken.get());
auto pred = [](const auto& e) {
return WI_IsFlagSet(GetFileAttributes(e.path().c_str()), FILE_ATTRIBUTE_TEMPORARY) && e.path().has_extension() &&
e.path().extension() == SAVED_STATE_FILE_EXTENSION && e.path().has_filename() &&
e.path().filename().wstring().find(SAVED_STATE_FILE_PREFIX) == 0 && e.file_size() > 0;
};
wsl::windows::common::wslutil::EnforceFileLimit(m_crashDumpFolder.c_str(), MAX_VM_CRASH_FILES + 1, pred);
}
void HcsVirtualMachine::WriteCrashLog(const std::wstring& crashLog)
{
auto runAsUser = wil::impersonate_token(m_userToken.get());
constexpr auto c_extension = L".txt";
constexpr auto c_prefix = L"kernel-panic-";
const auto filename = std::format(L"{}{}-{}{}", c_prefix, std::time(nullptr), m_vmIdString, c_extension);
auto filePath = m_crashDumpFolder / filename;
WI_ASSERT(std::filesystem::exists(m_crashDumpFolder));
WI_ASSERT(std::filesystem::is_directory(m_crashDumpFolder));
auto pred = [&c_extension, &c_prefix](const auto& e) {
return WI_IsFlagSet(GetFileAttributes(e.path().c_str()), FILE_ATTRIBUTE_TEMPORARY) && e.path().has_extension() &&
e.path().extension() == c_extension && e.path().has_filename() && e.path().filename().wstring().find(c_prefix) == 0;
};
wsl::windows::common::wslutil::EnforceFileLimit(m_crashDumpFolder.c_str(), MAX_VM_CRASH_FILES, pred);
{
std::wofstream outputFile(filePath.wstring());
THROW_HR_IF(E_UNEXPECTED, !outputFile.is_open());
outputFile << crashLog;
THROW_HR_IF(E_UNEXPECTED, outputFile.fail());
}
THROW_IF_WIN32_BOOL_FALSE(SetFileAttributesW(filePath.c_str(), FILE_ATTRIBUTE_TEMPORARY));
m_crashLogCaptured = true;
}
ULONG HcsVirtualMachine::AllocateLun()
{
for (ULONG index = 0; index < gsl::narrow_cast<ULONG>(m_lunBitmap.size()); index += 1)
{
if (!m_lunBitmap[index])
{
m_lunBitmap[index] = true;
return index;
}
}
THROW_HR(WSL_E_TOO_MANY_DISKS_ATTACHED);
}
void HcsVirtualMachine::FreeLun(ULONG Lun)
{
THROW_HR_IF(E_BOUNDS, Lun >= m_lunBitmap.size());
THROW_HR_IF(E_INVALIDARG, !m_lunBitmap[Lun]);
m_lunBitmap[Lun] = false;
}