Skip to content

Commit dc4284a

Browse files
authored
Introduce new experimental LinuxPod type (#343)
Closes #319 Introduces a new type capable of running > 1 container in the guest. The API mostly follows LinuxContainer, and each individual container can be addressed via any of the methods that require you to pass the containerID as the first param. Today there's no support for namespace sharing, but that shouldn't be terrible to support.
1 parent 3d7bb7b commit dc4284a

File tree

4 files changed

+1533
-6
lines changed

4 files changed

+1533
-6
lines changed

Sources/Containerization/LinuxContainer.swift

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final class LinuxContainer: Container, Sendable {
3939
/// The configuration for the LinuxContainer.
4040
public struct Configuration: Sendable {
4141
/// Configuration for the init process of the container.
42-
public var process = LinuxProcessConfiguration.init()
42+
public var process = LinuxProcessConfiguration()
4343
/// The amount of cpus for the container.
4444
public var cpus: Int = 4
4545
/// The memory in bytes to give to the container.
@@ -380,7 +380,7 @@ extension LinuxContainer {
380380
let containerMounts = createdState.vm.mounts[self.id] ?? []
381381
spec.mounts = containerMounts.dropFirst().map { $0.to }
382382

383-
let stdio = Self.setupIO(
383+
let stdio = IOUtil.setup(
384384
portAllocator: self.hostVsockPorts,
385385
stdin: self.config.process.stdin,
386386
stdout: self.config.process.stdout,
@@ -577,7 +577,7 @@ extension LinuxContainer {
577577
try configuration(&config)
578578
spec.process = config.toOCI()
579579

580-
let stdio = Self.setupIO(
580+
let stdio = IOUtil.setup(
581581
portAllocator: self.hostVsockPorts,
582582
stdin: config.stdin,
583583
stdout: config.stdout,
@@ -606,7 +606,7 @@ extension LinuxContainer {
606606
var spec = self.generateRuntimeSpec()
607607
spec.process = configuration.toOCI()
608608

609-
let stdio = Self.setupIO(
609+
let stdio = IOUtil.setup(
610610
portAllocator: self.hostVsockPorts,
611611
stdin: configuration.stdin,
612612
stdout: configuration.stdout,
@@ -710,7 +710,7 @@ extension LinuxContainer {
710710
extension VirtualMachineInstance {
711711
/// Scoped access to an agent instance to ensure the resources are always freed (mostly close(2)'ing
712712
/// the vsock fd)
713-
fileprivate func withAgent<T>(fn: @Sendable (VirtualMachineAgent) async throws -> T) async throws -> T {
713+
func withAgent<T>(fn: @Sendable (VirtualMachineAgent) async throws -> T) async throws -> T {
714714
let agent = try await self.dialAgent()
715715
do {
716716
let result = try await fn(agent)
@@ -724,7 +724,7 @@ extension VirtualMachineInstance {
724724
}
725725

726726
extension AttachedFilesystem {
727-
fileprivate var to: ContainerizationOCI.Mount {
727+
var to: ContainerizationOCI.Mount {
728728
.init(
729729
type: self.type,
730730
source: self.source,
@@ -734,4 +734,46 @@ extension AttachedFilesystem {
734734
}
735735
}
736736

737+
struct IOUtil {
738+
static func setup(
739+
portAllocator: borrowing Atomic<UInt32>,
740+
stdin: ReaderStream?,
741+
stdout: Writer?,
742+
stderr: Writer?
743+
) -> LinuxProcess.Stdio {
744+
var stdinSetup: LinuxProcess.StdioReaderSetup? = nil
745+
if let reader = stdin {
746+
let ret = portAllocator.wrappingAdd(1, ordering: .relaxed)
747+
stdinSetup = .init(
748+
port: ret.oldValue,
749+
reader: reader
750+
)
751+
}
752+
753+
var stdoutSetup: LinuxProcess.StdioSetup? = nil
754+
if let writer = stdout {
755+
let ret = portAllocator.wrappingAdd(1, ordering: .relaxed)
756+
stdoutSetup = LinuxProcess.StdioSetup(
757+
port: ret.oldValue,
758+
writer: writer
759+
)
760+
}
761+
762+
var stderrSetup: LinuxProcess.StdioSetup? = nil
763+
if let writer = stderr {
764+
let ret = portAllocator.wrappingAdd(1, ordering: .relaxed)
765+
stderrSetup = LinuxProcess.StdioSetup(
766+
port: ret.oldValue,
767+
writer: writer
768+
)
769+
}
770+
771+
return LinuxProcess.Stdio(
772+
stdin: stdinSetup,
773+
stdout: stdoutSetup,
774+
stderr: stderrSetup
775+
)
776+
}
777+
}
778+
737779
#endif

0 commit comments

Comments
 (0)