Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions Sources/cctl/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ extension Application {
@Option(name: .customLong("mount"), help: "Directory to share into the container (Example: /foo:/bar)")
var mounts: [String] = []

@Option(name: .long, help: "IP address with subnet")
var ip: String?

@Option(name: .long, help: "Gateway address")
var gateway: String?

@Option(name: .customLong("ns"), help: "Nameserver addresses")
var nameservers: [String] = []

Expand All @@ -79,9 +73,19 @@ extension Application {
path: URL(fileURLWithPath: kernel),
platform: .linuxArm
)

// Choose network implementation based on macOS version
let network: ContainerManager.Network?
if #available(macOS 26, *) {
network = try ContainerManager.VmnetNetwork()
} else {
network = nil
}

var manager = try await ContainerManager(
kernel: kernel,
initfsReference: "vminit:latest",
network: network,
rosetta: rosetta
)
let sigwinchStream = AsyncSignalHandler.create(notify: [SIGWINCH])
Expand Down Expand Up @@ -119,21 +123,25 @@ extension Application {
}

var hosts = Hosts.default
if let ip {
guard let gateway else {
throw ContainerizationError(.invalidArgument, message: "gateway must be specified")
}
config.interfaces.append(NATInterface(address: ip, gateway: gateway))
config.dns = .init(nameservers: [gateway])
if nameservers.count > 0 {
config.dns = .init(nameservers: nameservers)
if !nameservers.isEmpty {
if #available(macOS 26, *) {
config.dns = DNS(nameservers: nameservers)
} else {
print("Warning: Networking not supported on macOS < 26, ignoring DNS configuration")
}
}

// Add host entry for the container using just the IP (not CIDR)
if #available(macOS 26, *), !config.interfaces.isEmpty {
let interface = config.interfaces[0]
let ipOnly = interface.address.components(separatedBy: "/")[0] // Strip /24 suffix
hosts.entries.append(
Hosts.Entry(
ipAddress: ip,
ipAddress: ipOnly,
hostnames: [id]
))
}

config.hosts = hosts
if let ociRuntimePath {
config.ociRuntimePath = ociRuntimePath
Expand Down