-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from aesteves900/dev/AEST/46-validate-hyperv-o…
…n-windows feat: Add validation for Hyper-V on Windows
- Loading branch information
Showing
4 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#nullable enable | ||
|
||
using DotNetCheck.Models; | ||
using DotNetCheck.Solutions; | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotNetCheck.Checkups | ||
{ | ||
public class HyperVCheckup : Checkup | ||
{ | ||
public override string Id => "windowshyperv"; | ||
|
||
public override string Title => $"Windows Hyper-V Checkup"; | ||
|
||
public override bool IsPlatformSupported(Platform platform) => platform == Platform.Windows; | ||
|
||
public override Task<DiagnosticResult> Examine(SharedState history) | ||
{ | ||
if (!this.GetHyperVStatus()) | ||
{ | ||
return Task.FromResult(new DiagnosticResult( | ||
Status.Warning, | ||
this, | ||
new Suggestion($"Activate Hyper-V on this computer to benefit from faster Android Emulators", | ||
new HyperVActivationSolution()))); | ||
} | ||
else | ||
{ | ||
ReportStatus($"Hyper-V is configured", Status.Ok); | ||
|
||
return Task.FromResult(DiagnosticResult.Ok(this)); | ||
} | ||
} | ||
|
||
private bool GetHyperVStatus() | ||
{ | ||
var startInfo = new ProcessStartInfo | ||
{ | ||
Arguments = "/enum {current}", | ||
CreateNoWindow = true, | ||
FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "bcdedit.exe"), | ||
RedirectStandardOutput = true, | ||
UseShellExecute = false | ||
}; | ||
using (var process = Process.Start(startInfo)) | ||
{ | ||
process.WaitForExit(); | ||
|
||
while (!process.StandardOutput.EndOfStream) | ||
{ | ||
string? line = process.StandardOutput.ReadLine(); | ||
|
||
if (!string.IsNullOrEmpty(line)) | ||
{ | ||
if (line.StartsWith("hypervisorlaunchtype ", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return line.IndexOf(" off", StringComparison.OrdinalIgnoreCase) == -1; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using DotNetCheck.Models; | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotNetCheck.Solutions | ||
{ | ||
public class HyperVActivationSolution : Solution | ||
{ | ||
public HyperVActivationSolution() | ||
{ | ||
} | ||
|
||
public override Task Implement(SharedState sharedState, CancellationToken cancellationToken) | ||
{ | ||
if (this.ActivateHyperV()) | ||
{ | ||
ReportStatus("Hyper-V activated. Please, restart your computer."); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("Please, restart your computer to complete the Hyper-V activation process."); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private bool ActivateHyperV() | ||
{ | ||
string dsimPath = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "dism.exe"); | ||
|
||
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) | ||
{ | ||
// For 32-bit processes on 64-bit systems, %windir%\system32 folder | ||
// can only be accessed by specifying %windir%\sysnative folder. | ||
dsimPath = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative", "dism.exe"); | ||
} | ||
|
||
var dism = ShellProcessRunner.Run(dsimPath, "/Online /Enable-Feature /Quiet /NoRestart /All /FeatureName:Microsoft-Hyper-V"); | ||
|
||
if(dism.ExitCode == RebootRequired) | ||
{ | ||
ReportStatus("Please, restart your computer to complete the Hyper-V activation process."); | ||
|
||
return false; | ||
} | ||
|
||
return dism.ExitCode == 0; | ||
} | ||
|
||
private const int RebootRequired = 3010; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters