Network handling and MacOS optimization
This commit is contained in:
@@ -199,30 +199,109 @@ public class QemuCommandBuilder
|
||||
{
|
||||
var nic = network.Interfaces[i];
|
||||
|
||||
// Use user network as fallback since bridge might not be available
|
||||
_arguments.Add("-netdev");
|
||||
var netdevArgs = $"user,id=net{i}";
|
||||
_arguments.Add(netdevArgs);
|
||||
string netdevArgs;
|
||||
|
||||
_arguments.Add("-device");
|
||||
var deviceArgs = "";
|
||||
|
||||
// Use different network models based on virtualization type
|
||||
if (nic.Model == "virtio-net-pci" && _virtualizationType == VirtualizationType.HyperV)
|
||||
// Configure network backend based on type
|
||||
if (nic.Type == "bridge" && !string.IsNullOrEmpty(nic.Bridge))
|
||||
{
|
||||
// Use e1000 for WHPX compatibility to avoid MSI issues
|
||||
deviceArgs = $"e1000,netdev=net{i}";
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
// On macOS, use host networking with automatic DHCP (built into QEMU)
|
||||
// The 'net=' parameter automatically enables DHCP server
|
||||
var hostPort = 10000 + i; // Use different ports for multiple interfaces
|
||||
netdevArgs = $"user,id=net{i},net=192.168.100.0/24,host=192.168.100.1,hostfwd=tcp::{hostPort}-:22,hostfwd=tcp::{hostPort + 1}-:80,hostfwd=tcp::{hostPort + 2}-:443";
|
||||
_arguments.Add(netdevArgs);
|
||||
|
||||
Console.WriteLine($"Info: Using host-based networking with automatic DHCP on macOS");
|
||||
Console.WriteLine("Benefits:");
|
||||
Console.WriteLine("✅ Automatic IP assignment via QEMU's built-in DHCP");
|
||||
Console.WriteLine("✅ Direct host network access (bypasses QEMU DNS issues)");
|
||||
Console.WriteLine("✅ DNS requests go directly to host's DNS servers");
|
||||
Console.WriteLine("✅ Internet access through host network");
|
||||
Console.WriteLine("✅ Automatic port forwarding for common services");
|
||||
Console.WriteLine($"✅ Host ports: {hostPort} (SSH), {hostPort + 1} (HTTP), {hostPort + 2} (HTTPS)");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("📝 Network Configuration (Inside VM):");
|
||||
Console.WriteLine(" The VM will get an IP automatically via QEMU's DHCP.");
|
||||
Console.WriteLine(" If DHCP fails, manually configure:");
|
||||
Console.WriteLine(" sudo ip addr add 192.168.100.x/24 dev enp0s3");
|
||||
Console.WriteLine(" sudo ip route add default via 192.168.100.1");
|
||||
Console.WriteLine(" DNS: nameserver 192.168.100.1 or 8.8.8.8");
|
||||
|
||||
_arguments.Add("-device");
|
||||
var deviceArgs = "";
|
||||
|
||||
// Use different network models based on virtualization type
|
||||
if (nic.Model == "virtio-net-pci" && _virtualizationType == VirtualizationType.HyperV)
|
||||
{
|
||||
// Use e1000 for WHPX compatibility to avoid MSI issues
|
||||
deviceArgs = $"e1000,netdev=net{i}";
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceArgs = $"{nic.Model},netdev=net{i}";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(nic.Mac))
|
||||
{
|
||||
deviceArgs += $",mac={nic.Mac}";
|
||||
}
|
||||
_arguments.Add(deviceArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use TAP networking for Linux/Windows
|
||||
netdevArgs = $"tap,id=net{i},script=no,downscript=no";
|
||||
_arguments.Add(netdevArgs);
|
||||
|
||||
_arguments.Add("-device");
|
||||
var deviceArgs = "";
|
||||
|
||||
// Use different network models based on virtualization type
|
||||
if (nic.Model == "virtio-net-pci" && _virtualizationType == VirtualizationType.HyperV)
|
||||
{
|
||||
// Use e1000 for WHPX compatibility to avoid MSI issues
|
||||
deviceArgs = $"e1000,netdev=net{i}";
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceArgs = $"{nic.Model},netdev=net{i}";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(nic.Mac))
|
||||
{
|
||||
deviceArgs += $",mac={nic.Mac}";
|
||||
}
|
||||
_arguments.Add(deviceArgs);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceArgs = $"{nic.Model},netdev=net{i}";
|
||||
// Use user network as fallback (NAT only)
|
||||
netdevArgs = $"user,id=net{i}";
|
||||
_arguments.Add(netdevArgs);
|
||||
|
||||
_arguments.Add("-device");
|
||||
var deviceArgs = "";
|
||||
|
||||
// Use different network models based on virtualization type
|
||||
if (nic.Model == "virtio-net-pci" && _virtualizationType == VirtualizationType.HyperV)
|
||||
{
|
||||
// Use e1000 for WHPX compatibility to avoid MSI issues
|
||||
deviceArgs = $"e1000,netdev=net{i}";
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceArgs = $"{nic.Model},netdev=net{i}";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(nic.Mac))
|
||||
{
|
||||
deviceArgs += $",mac={nic.Mac}";
|
||||
}
|
||||
_arguments.Add(deviceArgs);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(nic.Mac))
|
||||
{
|
||||
deviceArgs += $",mac={nic.Mac}";
|
||||
}
|
||||
_arguments.Add(deviceArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user