-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathHcsVirtualMachine.h
More file actions
106 lines (79 loc) · 3.4 KB
/
HcsVirtualMachine.h
File metadata and controls
106 lines (79 loc) · 3.4 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
HcsVirtualMachine.h
Abstract:
Implementation of IWSLCVirtualMachine - represents a single HCS-based VM instance.
This class encapsulates a VM and all operations on it.
--*/
#pragma once
#include "wslc.h"
#include "hcs.hpp"
#include "GuestDeviceManager.h"
#include "Dmesg.h"
#include "INetworkingEngine.h"
#include "WslCoreConfig.h"
#include <filesystem>
#include <map>
#define MAX_VHD_COUNT 254
namespace wsl::windows::service::wslc {
class HcsVirtualMachine
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>, IWSLCVirtualMachine, IFastRundown>
{
public:
HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings);
~HcsVirtualMachine();
// IWSLCVirtualMachine implementation
IFACEMETHOD(GetId)(_Out_ GUID* VmId) override;
IFACEMETHOD(AcceptConnection)(_Out_ HANDLE* Socket) override;
IFACEMETHOD(ConfigureNetworking)(_In_ HANDLE GnsSocket, _In_opt_ HANDLE* DnsSocket) override;
IFACEMETHOD(AttachDisk)(_In_ LPCWSTR Path, _In_ BOOL ReadOnly, _Out_ ULONG* Lun) override;
IFACEMETHOD(DetachDisk)(_In_ ULONG Lun) override;
IFACEMETHOD(AddShare)(_In_ LPCWSTR WindowsPath, _In_ BOOL ReadOnly, _Out_ GUID* ShareId) override;
IFACEMETHOD(RemoveShare)(_In_ REFGUID ShareId) override;
IFACEMETHOD(MapVirtioNetPort)
(_In_ USHORT HostPort, _In_ USHORT GuestPort, _In_ int Protocol, _In_ LPCSTR ListenAddress, _Out_ USHORT* AllocatedHostPort) override;
IFACEMETHOD(UnmapVirtioNetPort)
(_In_ USHORT HostPort, _In_ USHORT GuestPort, _In_ int Protocol, _In_ LPCSTR ListenAddress) override;
private:
struct DiskInfo
{
std::wstring Path;
bool AccessGranted = false;
};
bool FeatureEnabled(WSLCFeatureFlags Value) const;
static void CALLBACK OnVmExitCallback(HCS_EVENT* Event, void* Context);
void OnExit(const HCS_EVENT* Event);
void OnCrash(const HCS_EVENT* Event);
std::filesystem::path GetCrashDumpFolder();
void CreateVmSavedStateFile(HANDLE UserToken);
void EnforceVmSavedStateFileLimit();
void WriteCrashLog(const std::wstring& crashLog);
ULONG AllocateLun();
void FreeLun(ULONG Lun);
std::recursive_mutex m_lock;
wsl::windows::common::hcs::unique_hcs_system m_computeSystem;
GUID m_vmId{};
std::wstring m_vmIdString;
ULONG m_bootTimeoutMs{};
wil::shared_handle m_userToken;
GUID m_virtioFsClassId{};
WSLCFeatureFlags m_featureFlags{};
WSLCNetworkingMode m_networkingMode{};
wil::unique_socket m_listenSocket;
std::shared_ptr<DmesgCollector> m_dmesgCollector;
std::shared_ptr<GuestDeviceManager> m_guestDeviceManager;
std::optional<wsl::core::Config> m_natConfig;
std::unique_ptr<wsl::core::INetworkingEngine> m_networkEngine;
wil::unique_event m_vmExitEvent{wil::EventOptions::ManualReset};
std::map<ULONG, DiskInfo> m_attachedDisks;
std::bitset<MAX_VHD_COUNT> m_lunBitmap;
// Shares: key is ShareId, value is nullopt for Plan9 or DeviceInstanceId for VirtioFS
std::map<GUID, std::optional<GUID>, wsl::windows::common::helpers::GuidLess> m_shares;
std::filesystem::path m_vmSavedStateFile;
std::filesystem::path m_crashDumpFolder;
bool m_vmSavedStateCaptured = false;
bool m_crashLogCaptured = false;
wil::com_ptr<ITerminationCallback> m_terminationCallback;
};
} // namespace wsl::windows::service::wslc