Skip to content

Commit

Permalink
Merge pull request #93 from aesteves900/dev/AEST/46-validate-hyperv-o…
Browse files Browse the repository at this point in the history
…n-windows

feat: Add validation for Hyper-V on Windows
  • Loading branch information
aesteves900 authored Sep 7, 2022
2 parents d5c3c99 + bef5bfc commit 0eb5ad7
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
68 changes: 68 additions & 0 deletions UnoCheck/Checkups/HyperVCheckup.cs
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;
}
}
}
3 changes: 2 additions & 1 deletion UnoCheck/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ static Task<int> Main(string[] args)
new GTK3Checkup(),
new WindowsPhytonInstallationCheckup(),
new WindowsLongPathCheckup(),
new LinuxNinjaPresenceCheckup()
new LinuxNinjaPresenceCheckup(),
new HyperVCheckup()
);

var app = new CommandApp();
Expand Down
54 changes: 54 additions & 0 deletions UnoCheck/Solutions/HyperVActivationSolution.cs
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;
}
}
2 changes: 1 addition & 1 deletion UnoCheck/UnoCheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="NuGet.Packaging" Version="5.10.0" />
<PackageReference Include="NuGet.Protocol" Version="5.10.0" />
<PackageReference Include="NuGet.Versioning" Version="5.10.0" />
Expand Down

0 comments on commit 0eb5ad7

Please sign in to comment.