Skip to content

Commit

Permalink
- enabled ssh x11 forwarding by default
Browse files Browse the repository at this point in the history
- check if default bridge config exists
- fallback bridge network if default 192.168.122.0/24 is not usable
- moved libvirt check after help generation
- enabled nested virtualization by default
  • Loading branch information
wubbl0rz committed Apr 29, 2023
1 parent 33462c1 commit 634be83
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
37 changes: 29 additions & 8 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Helper
public static Process? ConnectViaSsh(string user, string ip)
{
var startInfo = new ProcessStartInfo("ssh");
startInfo.Arguments = $"-o StrictHostKeyChecking=off {user}@{ip}";
startInfo.Arguments = $"-Y -o StrictHostKeyChecking=off {user}@{ip}";

return Process.Start(startInfo);
}
Expand All @@ -33,23 +33,44 @@ public static bool CheckDefaultNetwork()
.Select(info => info.Address)
.Any();

if (defaultBridgeHasIp is true)
var bridgeFileExists = File.Exists("/etc/qemu/bridge.conf");

if (defaultBridgeHasIp is true && bridgeFileExists)
return true;

var script = "";

if (defaultBridge == null)
if (defaultBridge == null || !bridgeFileExists)
{
AnsiConsole.MarkupLine("[red]Default network bridge: virbr0 not found. " +
AnsiConsole.MarkupLine("[red]Default network bridge: virbr0 missing configuration. " +
"Some distributions do not setup a bridge automatically.[/]");

if (AnsiConsole.Ask<string>("Do you want to create one now and allow VMs to use it? (y/N)", "N").ToLower() != "y")
var hasFreeNetwork = NetworkInterface.GetAllNetworkInterfaces()
.SelectMany(i => i.GetIPProperties().UnicastAddresses)
.Any(i => i.ToString()?.Contains("192.168.122.") is true);

var thirdOctet = hasFreeNetwork ? "122" : "22";

var bridgeName = $"virbr0";
var bridgeIp = $"192.168.{thirdOctet}.1";
var bridgeMask = "255.255.255.0";
var bridgeDhcpStart = $"192.168.{thirdOctet}.128";
var bridgeDhcpEnd = $"192.168.{thirdOctet}.254";

AnsiConsole.WriteLine("👉 The following default network can be created: ");
AnsiConsole.WriteLine($"Name: {bridgeName}");
AnsiConsole.WriteLine($"IP: {bridgeIp}");
AnsiConsole.WriteLine($"Mask: {bridgeMask}");
AnsiConsole.WriteLine();

if (AnsiConsole
.Ask<string>("Do you want to create it now and allow VMs to use it? (y/N)", "N")
.ToLower() != "y")
{
return false;
}

AnsiConsole.WriteLine("👉 The following default network and bridge is going to be created: ");
AnsiConsole.WriteLine(Interop.DefaultNetworkDefiniton);
var bridgeXml = Interop.GenVirBridgeXml(bridgeName, bridgeIp, bridgeMask, bridgeDhcpStart, bridgeDhcpEnd);

script = $"""
if ! test -f /etc/qemu/bridge.conf; then
Expand All @@ -60,7 +81,7 @@ chmod u+s /usr/lib/qemu/qemu-bridge-helper

if ! virsh --connect qemu:///system net-start default &> /dev/null; then
virsh net-undefine default &> /dev/null
virsh --connect qemu:///system net-define <(echo \"{Interop.DefaultNetworkDefiniton}\") &> /dev/null
virsh --connect qemu:///system net-define <(echo \"{bridgeXml}\") &> /dev/null
virsh --connect qemu:///system net-destroy default &> /dev/null
fi
virsh --connect qemu:///system net-start default &> /dev/null
Expand Down
11 changes: 7 additions & 4 deletions Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ public static void DestroyVm(nint vmId, string vmName, string vmDir)
}
}

public static string DefaultNetworkDefiniton = """
public static string GenVirBridgeXml(string name, string ip, string mask, string dhcpStart, string dhcpEnd)
{
return $"""
<network>
<name>default</name>
<bridge name='virbr0'/>
<bridge name='{name}'/>
<forward/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<ip address='{ip}' netmask='{mask}'>
<dhcp>
<range start='192.168.122.128' end='192.168.122.254'/>
<range start='{dhcpStart}' end='{dhcpEnd}'/>
</dhcp>
</ip>
</network>
""";
}
}

public class LibvirtConnection : IDisposable
Expand Down
12 changes: 6 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
using Spectre.Console;
using VmChamp;

if (!Interop.IsLibvirtInstalled())
{
AnsiConsole.MarkupLine("[red]Libvirt not found.[/]");
return 1;
}

var appConfig = new AppConfig();
Directory.CreateDirectory(appConfig.CacheDir);
Directory.CreateDirectory(appConfig.DataDir);
Expand Down Expand Up @@ -84,4 +78,10 @@ compdef _VmChamp_zsh_complete vmchamp
return 0;
}

if (!Interop.IsLibvirtInstalled())
{
AnsiConsole.MarkupLine("[red]Libvirt not found.[/]");
return 1;
}

return await app.InvokeAsync(args);
2 changes: 1 addition & 1 deletion RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private string GenXml(string vmName, string diskImage, string initImage, double
<apic/>
<vmport state="off"/>
</features>
<cpu mode="host-model" check="partial"/>
<cpu mode="host-passthrough" check="partial"/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
Expand Down

0 comments on commit 634be83

Please sign in to comment.