From 634be83d1be7fb2dd6abf54e6cf3fdd297ca0c0e Mon Sep 17 00:00:00 2001 From: wubbl0rz Date: Sat, 29 Apr 2023 17:45:39 +0200 Subject: [PATCH] - enabled ssh x11 forwarding by default - 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 --- Helper.cs | 37 +++++++++++++++++++++++++++++-------- Interop.cs | 11 +++++++---- Program.cs | 12 ++++++------ RunCommand.cs | 2 +- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/Helper.cs b/Helper.cs index 83d92aa..3af0b22 100644 --- a/Helper.cs +++ b/Helper.cs @@ -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); } @@ -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("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("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 @@ -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 diff --git a/Interop.cs b/Interop.cs index 3720496..856f4c6 100644 --- a/Interop.cs +++ b/Interop.cs @@ -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 $""" default - + - + - + """; + } } public class LibvirtConnection : IDisposable diff --git a/Program.cs b/Program.cs index eb16964..039f858 100644 --- a/Program.cs +++ b/Program.cs @@ -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); @@ -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); \ No newline at end of file diff --git a/RunCommand.cs b/RunCommand.cs index 4128a1a..749cd4f 100644 --- a/RunCommand.cs +++ b/RunCommand.cs @@ -250,7 +250,7 @@ private string GenXml(string vmName, string diskImage, string initImage, double - + destroy restart destroy