Skip to content

Commit

Permalink
fixed #9 ubuntu and added debian12
Browse files Browse the repository at this point in the history
  • Loading branch information
wubbl0rz committed Apr 28, 2023
1 parent c1597bd commit 51e4df9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CleanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class CleanCommand : Command
public CleanCommand(AppConfig appConfig) : base("clean", "delete all vms and images")
{
_appConfig = appConfig;

this.AddAlias("purge");

var allVmDirectories = Directory.GetDirectories(appConfig.DataDir);

Expand Down
10 changes: 10 additions & 0 deletions DistroInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ public class DistroInfo
{
public static IEnumerable<DistroInfo> Distros { get; set; } = new List<DistroInfo>()
{
new()
{
Name = "Debian12",
Family = "Debian",
ImageName = "debian-12-genericcloud-amd64-daily.qcow2",
Url = "https://cloud.debian.org/images/cloud/bookworm/daily/latest/",
Aliases = new[] { "Bookworm" },
ChecksumFile = "SHA512SUMS",
ChecksumType = "sha512"
},
new()
{
Name = "Debian11",
Expand Down
10 changes: 8 additions & 2 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static bool CheckDefaultNetwork()
if (defaultBridge == null)
{
AnsiConsole.MarkupLine("[red]Default network bridge: virbr0 not found. " +
"Normally libvirt should have created a bridge after installation.[/]");
"Some distributions do not setup a bridge automatically.[/]");

if (AnsiConsole.Ask<string>("Do you want to create one now? (y/N)", "N").ToLower() != "y")
if (AnsiConsole.Ask<string>("Do you want to create one now and allow VMs to use it? (y/N)", "N").ToLower() != "y")
{
return false;
}
Expand All @@ -52,6 +52,12 @@ public static bool CheckDefaultNetwork()
AnsiConsole.WriteLine(Interop.DefaultNetworkDefiniton);

script = $"""
if ! test -f /etc/qemu/bridge.conf; then
mkdir -p /etc/qemu &> /dev/null
echo \"allow virbr0\" > /etc/qemu/bridge.conf
chmod u+s /usr/lib/qemu/qemu-bridge-helper
fi

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
Expand Down
9 changes: 8 additions & 1 deletion Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ public static extern nint virDomainCreateXML(IntPtr conn,
var ip = Marshal.PtrToStringUTF8((nint)virDomainIpAddress.addr);
return ip;
}


public static bool IsLibvirtInstalled()
{
NativeLibrary.TryLoad("libvirt.so.0", out var handle);

return handle != IntPtr.Zero;
}

public static void DestroyVm(nint vmId, string vmName, string vmDir)
{
AnsiConsole.MarkupLine($"[yellow]💀 Removing VM: {vmName}[/]");
Expand Down
7 changes: 7 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
using System.CommandLine.Parsing;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
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
4 changes: 2 additions & 2 deletions RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private string GenXml(string vmName, string diskImage, string initImage, double
<memory unit="B">{memSizeInBytes}</memory>
<vcpu placement="static">{cpuCount}</vcpu>
<os>
<type arch="x86_64" machine="pc-q35-7.2">hvm</type>
<type arch="x86_64" machine="q35">hvm</type>
<boot dev="hd"/>
</os>
<features>
Expand Down Expand Up @@ -296,7 +296,7 @@ private string GenXml(string vmName, string diskImage, string initImage, double
<redirdev bus="usb" type="spicevmc">
<address type="usb" bus="0" port="3"/>
</redirdev>
<watchdog model="itco" action="reset"/>
<watchdog model="i6300esb" action="reset"/>
<memballoon model="virtio">
<address type="pci" domain="0x0000" bus="0x06" slot="0x00" function="0x0"/>
</memballoon>
Expand Down

0 comments on commit 51e4df9

Please sign in to comment.