836
THROW_HR_IF(E_INVALIDARG, !m_lunBitmap[Lun]);
837
838
m_lunBitmap[Lun] = false;
839
-}
\ No newline at end of file
839
+}
840
+
841
+namespace wsl::windows::service::wslc {
842
+
843
+WSLCVirtualMachineFactory::WSLCVirtualMachineFactory(_In_ const WSLCSessionSettings* Settings)
844
+{
845
+ THROW_HR_IF(E_POINTER, Settings == nullptr);
846
+
847
+ m_displayName = Settings->DisplayName ? Settings->DisplayName : L"";
848
+ m_storagePath = Settings->StoragePath ? Settings->StoragePath : L"";
849
+
850
+ if (Settings->RootVhdOverride != nullptr)
851
+ {
852
+ m_rootVhdOverride.emplace(Settings->RootVhdOverride);
853
+ }
854
+
855
+ if (Settings->RootVhdTypeOverride != nullptr)
856
+ {
857
+ m_rootVhdTypeOverride.emplace(Settings->RootVhdTypeOverride);
858
+ }
859
+
860
+ // Keep our own duplicate of the dmesg sink so recreated VMs can reuse it.
861
+ if (Settings->DmesgOutput.Handle.File != nullptr && Settings->DmesgOutput.Handle.File != INVALID_HANDLE_VALUE)
862
+ {
863
+ m_dmesgOutput.reset(wslutil::DuplicateHandle(wslutil::FromCOMInputHandle(Settings->DmesgOutput), GENERIC_WRITE | SYNCHRONIZE));
864
+ }
865
+
866
+ m_terminationCallback = Settings->TerminationCallback;
867
+ m_maximumStorageSizeMb = Settings->MaximumStorageSizeMb;
868
+ m_cpuCount = Settings->CpuCount;
869
+ m_memoryMb = Settings->MemoryMb;
870
+ m_bootTimeoutMs = Settings->BootTimeoutMs;
871
+ m_networkingMode = Settings->NetworkingMode;
872
+ m_featureFlags = Settings->FeatureFlags;
873
+ m_storageFlags = Settings->StorageFlags;
874
+}
875
+
876
+WSLCSessionSettings WSLCVirtualMachineFactory::BuildSettings()
877
+{
878
+ WSLCSessionSettings settings{};
879
+ settings.DisplayName = m_displayName.c_str();
880
+ settings.StoragePath = m_storagePath.empty() ? nullptr : m_storagePath.c_str();
881
+ settings.MaximumStorageSizeMb = m_maximumStorageSizeMb;
882
+ settings.CpuCount = m_cpuCount;
883
+ settings.MemoryMb = m_memoryMb;
884
+ settings.BootTimeoutMs = m_bootTimeoutMs;
885
+ settings.NetworkingMode = m_networkingMode;
886
+ settings.TerminationCallback = m_terminationCallback.get();
887
+ settings.FeatureFlags = m_featureFlags;
888
+ settings.StorageFlags = m_storageFlags;
889
+ settings.RootVhdOverride = m_rootVhdOverride ? m_rootVhdOverride->c_str() : nullptr;
890
+ settings.RootVhdTypeOverride = m_rootVhdTypeOverride ? m_rootVhdTypeOverride->c_str() : nullptr;
891
+
892
+ if (m_dmesgOutput)
893
+ {
894
+ settings.DmesgOutput = wslutil::ToCOMInputHandle(m_dmesgOutput.get());
895
+ }
896
+
897
+ return settings;
898
+}
899
+
900
+HRESULT WSLCVirtualMachineFactory::CreateVirtualMachine(_Out_ IWSLCVirtualMachine** Vm)
901
+try
902
+{
903
+ RETURN_HR_IF(E_POINTER, Vm == nullptr);
904
+ *Vm = nullptr;
905
+
906
+ const auto settings = BuildSettings();
907
+ auto vm = Microsoft::WRL::Make<HcsVirtualMachine>(&settings);
908
+ THROW_IF_NULL_ALLOC(vm);
909
+
910
+ *Vm = vm.Detach();
911
+ return S_OK;
912
+}
913
+CATCH_RETURN()
914
+
915
+} // namespace wsl::windows::service::wslc
\ No newline at end of file