-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathLinuxPod.swift
More file actions
1374 lines (1219 loc) · 57.7 KB
/
Copy pathLinuxPod.swift
File metadata and controls
1374 lines (1219 loc) · 57.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
// Copyright © 2025-2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerizationError
import ContainerizationExtras
import ContainerizationOCI
import Foundation
import Logging
import Synchronization
import struct ContainerizationOS.Terminal
/// NOTE: Experimental API
///
/// `LinuxPod` allows managing multiple Linux containers within a single
/// virtual machine. Each container has its own rootfs and process, but
/// shares the VM's resources (CPU, memory, network).
public final class LinuxPod: Sendable {
static let maxIDLength = 64
/// The identifier of the pod.
public let id: String
/// Configuration for the pod.
public let config: Configuration
/// The configuration for the LinuxPod.
public struct Configuration: Sendable {
/// The amount of cpus for the pod's VM.
public var cpus: Int = 4
/// The memory in bytes to give to the pod's VM.
public var memoryInBytes: UInt64 = 1024.mib()
/// The network interfaces for the pod.
public var interfaces: [any Interface] = []
/// Whether nested virtualization should be turned on for the pod.
public var virtualization: Bool = false
/// Optional file path to store serial boot logs.
public var bootLog: BootLog?
/// Whether containers in the pod should share a PID namespace.
/// When enabled, all containers can see each other's processes.
public var shareProcessNamespace: Bool = false
/// The default hostname for all containers in the pod.
/// Individual containers can override this by setting their own `hostname` configuration.
public var hostname: String?
/// The default DNS configuration for all containers in the pod.
/// Individual containers can override this by setting their own `dns` configuration.
public var dns: DNS?
/// The default hosts file configuration for all containers in the pod.
/// Individual containers can override this by setting their own `hosts` configuration.
public var hosts: Hosts?
/// Volumes attached to the pod. Can be shared with multiple containers.
public var volumes: [PodVolume] = []
/// Extension objects that participate in the VM instance lifecycle.
public var extensions: [any Sendable] = []
public init() {}
}
/// Configuration for a container within the pod.
public struct ContainerConfiguration: Sendable {
/// Configuration for the init process of the container.
public var process = LinuxProcessConfiguration()
/// Optional per-container CPU limit (can exceed pod total for oversubscription).
public var cpus: Int?
/// Optional per-container memory limit in bytes (can exceed pod total for oversubscription).
public var memoryInBytes: UInt64?
/// The hostname for the container.
public var hostname: String?
/// The system control options for the container.
public var sysctl: [String: String] = [:]
/// The mounts for the container.
public var mounts: [Mount] = LinuxContainer.defaultMounts()
/// Paths inside the container that vmexec hides from the workload.
/// Defaults to the OCI standard set (``LinuxContainer/defaultMaskedPaths()``),
/// matching the restricted capability baseline. Set to `[]` to opt out,
/// or append to extend it.
public var maskedPaths: [String] = LinuxContainer.defaultMaskedPaths()
/// Paths inside the container that vmexec marks read-only.
/// Defaults to the OCI standard set (``LinuxContainer/defaultReadonlyPaths()``),
/// matching the restricted capability baseline. Set to `[]` to opt out,
/// or append to extend it.
public var readonlyPaths: [String] = LinuxContainer.defaultReadonlyPaths()
/// The Unix domain socket relays to setup for the container.
public var sockets: [UnixSocketConfiguration] = []
/// The DNS configuration for the container.
public var dns: DNS?
/// The hosts file configuration for the container.
public var hosts: Hosts?
/// Run the container with a minimal init process that handles signal
/// forwarding and zombie reaping.
public var useInit: Bool = false
public init() {}
}
/// A volume that is attached at the pod level and can be shared by multiple containers.
public struct PodVolume: Sendable {
/// Describes the backing storage for the volume.
public enum Source: Sendable {
/// A network block device (NBD) volume.
case nbd(url: URL, timeout: TimeInterval? = nil, readOnly: Bool = false)
/// A disk-image file on the host, attached as a virtio-block device.
case diskImage(path: URL, readOnly: Bool = false)
/// An in-memory (tmpfs) volume mounted inside the guest.
case tmpfs(sizeBytes: UInt64? = nil)
}
/// The logical name of this volume. Containers reference this name
/// via `Mount.sharedMount(name:destination:)` in their mounts.
public var name: String
/// The backing storage source for this volume.
public var source: Source
/// The filesystem format on the volume.
public var format: String
public init(name: String, source: Source, format: String) {
self.name = name
self.source = source
self.format = format
}
func toMount() -> Mount {
switch source {
case .nbd(let url, let timeout, let readOnly):
var runtimeOptions: [String] = []
if let timeout {
runtimeOptions.append("vzTimeout=\(timeout)")
}
return Mount.block(
format: self.format,
source: url.absoluteString,
destination: LinuxPod.guestVolumePath(name),
options: readOnly ? ["ro"] : [],
runtimeOptions: runtimeOptions
)
case .diskImage(let path, let readOnly):
return Mount.block(
format: self.format,
source: path.absolutePath(),
destination: LinuxPod.guestVolumePath(name),
options: readOnly ? ["ro"] : []
)
case .tmpfs(let sizeBytes):
return Mount.any(
type: "tmpfs",
source: "tmpfs",
destination: LinuxPod.guestVolumePath(name),
options: sizeBytes.map { ["size=\($0)"] } ?? []
)
}
}
}
private struct PodContainer: Sendable {
let id: String
let rootfs: Mount
let config: ContainerConfiguration
var state: ContainerState
var process: LinuxProcess?
var fileMountContext: FileMountContext
enum ContainerState: Sendable {
case registered
case created
case started
case stopped
case errored
}
}
private let state: AsyncMutex<State>
// Ports to be allocated from for stdio and for
// unix socket relays that are sharing a guest
// uds to the host.
private let hostVsockPorts: Atomic<UInt32>
// Ports we request the guest to allocate for unix socket relays from
// the host.
private let guestVsockPorts: Atomic<UInt32>
private struct State: Sendable {
var phase: Phase
var containers: [String: PodContainer]
var pauseProcess: LinuxProcess?
// Whether the unified virtiofs share is mounted at `/run/virtiofs` in the guest
var unifiedVirtiofsMounted: Bool = false
}
private enum Phase: Sendable {
/// The pod has been created but no live resources are running.
case initialized
/// The pod's virtual machine has been setup and the runtime environment has been configured.
case created(CreatedState)
/// An error occurred during the lifetime of this class.
case errored(Swift.Error)
struct CreatedState: Sendable {
let vm: any VirtualMachineInstance
let relayManager: UnixSocketRelayManager
}
func createdState(_ operation: String) throws -> CreatedState {
switch self {
case .created(let state):
return state
case .errored(let err):
throw err
default:
throw ContainerizationError(
.invalidState,
message: "failed to \(operation): pod must be created"
)
}
}
mutating func validateForCreate() throws {
switch self {
case .initialized:
break
case .errored(let err):
throw err
default:
throw ContainerizationError(
.invalidState,
message: "pod must be in initialized state to create"
)
}
}
mutating func setErrored(error: Swift.Error) {
self = .errored(error)
}
}
private let vmm: VirtualMachineManager
private let logger: Logger?
/// Create a new `LinuxPod`. A `VirtualMachineManager` instance must be
/// provided that will handle launching the virtual machine the containers
/// will execute inside of.
public init(
_ id: String,
vmm: VirtualMachineManager,
logger: Logger? = nil,
configuration: (inout Configuration) throws -> Void
) throws {
guard id.count <= Self.maxIDLength else {
throw ContainerizationError(
.invalidArgument,
message: "pod id length \(id.count) exceeds maximum of \(Self.maxIDLength) characters"
)
}
self.id = id
self.vmm = vmm
self.hostVsockPorts = Atomic<UInt32>(0x1000_0000)
self.guestVsockPorts = Atomic<UInt32>(0x1000_0000)
self.logger = logger
var config = Configuration()
try configuration(&config)
self.config = config
self.state = AsyncMutex(State(phase: .initialized, containers: [:], pauseProcess: nil))
}
private static func createDefaultRuntimeSpec(_ containerID: String, podID: String) -> Spec {
.init(
process: .init(),
hostname: containerID,
root: .init(
path: Self.guestRootfsPath(containerID),
readonly: false
),
linux: .init(
resources: .init(),
cgroupsPath: "/container/pod/\(podID)/\(containerID)"
)
)
}
private func generateRuntimeSpec(containerID: String, config: ContainerConfiguration, rootfs: Mount) -> Spec {
var spec = Self.createDefaultRuntimeSpec(containerID, podID: self.id)
// Process configuration
spec.process = config.process.toOCI()
// Wrap with init process if requested.
if config.useInit {
let originalArgs = spec.process?.args ?? []
spec.process?.args = ["/.cz-init", "--"] + originalArgs
}
// General toggles
// Container-level hostname takes precedence; fall back to pod-level hostname.
if let hostname = config.hostname ?? self.config.hostname {
spec.hostname = hostname
}
// Linux toggles
spec.linux?.sysctl = config.sysctl
spec.linux?.maskedPaths = config.maskedPaths
spec.linux?.readonlyPaths = config.readonlyPaths
// If the rootfs was requested as read-only, set it in the OCI spec.
// We let the OCI runtime remount as ro, instead of doing it originally.
spec.root?.readonly = rootfs.options.contains("ro")
// Resource limits (if specified)
if let cpus = config.cpus, cpus > 0 {
spec.linux?.resources?.cpu = LinuxCPU(
quota: Int64(cpus * 100_000),
period: 100_000
)
}
if let memoryInBytes = config.memoryInBytes, memoryInBytes > 0 {
spec.linux?.resources?.memory = LinuxMemory(
limit: Int64(memoryInBytes)
)
}
return spec
}
static func guestRootfsPath(_ containerID: String) -> String {
"/run/container/\(containerID)/rootfs"
}
static func guestSocketStagingPath(_ socketID: String) -> String {
"/run/sockets/\(socketID).sock"
}
private static func guestVolumePath(_ volumeName: String) -> String {
"/run/volumes/\(volumeName)"
}
}
extension LinuxPod {
/// Number of CPU cores allocated to the pod's VM.
public var cpus: Int {
config.cpus
}
/// Amount of memory in bytes allocated for the pod's VM.
public var memoryInBytes: UInt64 {
config.memoryInBytes
}
/// Network interfaces of the pod.
public var interfaces: [any Interface] {
config.interfaces
}
/// Add a container to the pod.
///
/// When called before `create()`, the container is registered for setup during VM creation.
/// When called after `create()`, the container is hotplugged into the running VM.
/// If the underlying VMM does not support hotplug, an error is thrown.
public func addContainer(
_ id: String,
rootfs: Mount,
configuration: @Sendable @escaping (inout ContainerConfiguration) throws -> Void
) async throws {
guard id.count <= Self.maxIDLength else {
throw ContainerizationError(
.invalidArgument,
message: "container id length \(id.count) exceeds maximum of \(Self.maxIDLength) characters"
)
}
try await self.state.withLock { state in
guard state.containers[id] == nil else {
throw ContainerizationError(
.invalidArgument,
message: "container with id \(id) already exists in pod"
)
}
var config = ContainerConfiguration()
try configuration(&config)
let fileMountContext = try FileMountContext.prepare(mounts: config.mounts)
switch state.phase {
case .initialized:
state.containers[id] = PodContainer(
id: id,
rootfs: rootfs,
config: config,
state: .registered,
process: nil,
fileMountContext: fileMountContext
)
case .created(let createdState):
let vm = createdState.vm
var modifiedRootfs = rootfs
modifiedRootfs.options.removeAll(where: { $0 == "ro" })
let attachment = try await vm.hotplug(modifiedRootfs, id: id)
var updatedFileMountContext = fileMountContext
do {
let virtioFSMounts = fileMountContext.transformedMounts.filter {
if case .virtiofs(_) = $0.runtimeOptions { return true }
return false
}
if !virtioFSMounts.isEmpty {
try await vm.hotplugVirtioFS(virtioFSMounts, id: id)
}
let agent = try await vm.dialAgent()
do {
var mount = attachment.to
mount.destination = Self.guestRootfsPath(id)
try await agent.mount(mount)
// Filter out shared mounts — those are handled separately as
// pod volume bind mounts. Without it here, a container added to an
// already-created would add a duplicated mount into the shared VM.
let nonSharedMounts = fileMountContext.transformedMounts.filter {
if case .shared = $0.runtimeOptions { return false }
return true
}
try vm.registerMounts(
id: id,
rootfs: attachment,
additionalMounts: nonSharedMounts
)
// Mount this container's additional virtiofs shares in the
// guest. create() does this for boot-time containers (the
// /run/virtiofs loop); the hotplug path must do the same or
// the container's bind mounts from /run/virtiofs/<tag> fail
// with ENOENT.
//
// Derive the tags from the additional mounts directly rather
// than from vm.mounts[id], so this is independent of the
// rootfs (which may be virtiofs or virtio-blk) and of mount
// ordering. The rootfs is mounted at /run/container/<id>/rootfs
// and is never consumed from /run/virtiofs.
let newVirtiofsTags = try virtioFSMounts.map { try hashFilePath(path: $0.source) }
if !newVirtiofsTags.isEmpty {
try await agent.mkdir(path: "/run/virtiofs", all: true, perms: 0o755)
if vm.virtiofsLayout == .perTag {
// Tags already mounted in the guest at boot or by a
// prior hotplug (i.e. present on another container).
let alreadyMounted = Set(
vm.mounts
.filter { $0.key != id }
.values.flatMap { $0 }
.filter { $0.type == "virtiofs" }
.map { $0.source }
)
var seen: Set<String> = []
for tag in newVirtiofsTags
where !alreadyMounted.contains(tag) && seen.insert(tag).inserted {
let dest = "/run/virtiofs/\(tag)"
try await agent.mkdir(path: dest, all: true, perms: 0o755)
try await agent.mount(
ContainerizationOCI.Mount(
type: "virtiofs",
source: tag,
destination: dest,
options: []
))
}
} else if !state.unifiedVirtiofsMounted && vm.virtiofsLayout == .unified {
// Unified layout: one /run/virtiofs mount for the
// VM's lifetime, so mount it only if nothing has
// mounted it at boot or on an earlier hotplug.
try await agent.mount(
ContainerizationOCI.Mount(
type: "virtiofs",
source: "virtiofs",
destination: "/run/virtiofs",
options: []
))
state.unifiedVirtiofsMounted = true
}
}
if fileMountContext.hasFileMounts {
let containerMounts = vm.mounts[id] ?? []
try await updatedFileMountContext.mountHoldingDirectories(
vmMounts: containerMounts,
agent: agent
)
}
if let dns = config.dns ?? self.config.dns {
try await agent.configureDNS(
config: dns,
location: Self.guestRootfsPath(id)
)
}
if let hosts = config.hosts ?? self.config.hosts {
try await agent.configureHosts(
config: hosts,
location: Self.guestRootfsPath(id)
)
}
for socket in config.sockets {
try await self.relayUnixSocket(
socket: socket,
containerID: id,
relayManager: createdState.relayManager,
agent: agent
)
}
try await agent.close()
} catch {
try? await agent.umount(path: Self.guestRootfsPath(id), flags: 0)
try? await agent.close()
throw error
}
state.containers[id] = PodContainer(
id: id,
rootfs: rootfs,
config: config,
state: .created,
process: nil,
fileMountContext: updatedFileMountContext
)
} catch {
try? await vm.releaseHotplug(id: id)
try? await vm.releaseVirtioFS(id: id)
throw error
}
case .errored(let err):
throw err
}
}
}
/// Create and start the underlying pod's virtual machine and set up
/// the runtime environment. All registered containers will have their
/// rootfs mounted, but no init processes will be running.
public func create() async throws {
try await self.state.withLock { state in
try state.phase.validateForCreate()
// Build mountsByID for all containers.
// Strip "ro" from rootfs options - we handle readonly via the OCI spec's
// root.readonly field and remount in vmexec after setup is complete.
// Use transformedMounts from fileMountContext (file mounts become directory shares).
var mountsByID: [String: [Mount]] = [:]
for (id, container) in state.containers {
var modifiedRootfs = container.rootfs
modifiedRootfs.options.removeAll(where: { $0 == "ro" })
// Filter out shared mounts — those are handled separately as pod volume bind mounts.
let containerMounts = container.fileMountContext.transformedMounts.filter {
if case .shared = $0.runtimeOptions { return false }
return true
}
mountsByID[id] = [modifiedRootfs] + containerMounts
}
// Validate pod volume names are unique.
var volumeNames = Set<String>()
for volume in self.config.volumes {
guard volumeNames.insert(volume.name).inserted else {
throw ContainerizationError(
.invalidArgument,
message: "duplicate pod volume name \"\(volume.name)\""
)
}
}
// Validate that all shared mounts reference valid pod volume names.
for (id, container) in state.containers {
for mount in container.config.mounts {
if case .shared = mount.runtimeOptions {
guard volumeNames.contains(mount.source) else {
throw ContainerizationError(
.invalidArgument,
message: "container \(id) references unknown pod volume \"\(mount.source)\""
)
}
}
}
}
let podVolumeMounts = self.config.volumes.map { $0.toMount() }
if !podVolumeMounts.isEmpty {
mountsByID[self.id] = podVolumeMounts
}
// Capture into an immutable `let` so the value is safely usable
// from the concurrent `withAgent` closure below. The container
// path makes the same decision in LinuxContainer.create — CH
// only attaches a virtiofs device when shares are configured,
// so mounting an unbacked /run/virtiofs would fail with EINVAL
// on the CH backend.
let hasVirtiofsMount = mountsByID.values.contains { mounts in
mounts.contains { mount in
if case .virtiofs = mount.runtimeOptions { return true }
return false
}
}
var vmConfig = VMConfiguration(
cpus: self.config.cpus,
memoryInBytes: self.config.memoryInBytes,
interfaces: self.config.interfaces,
mountsByID: mountsByID,
bootLog: self.config.bootLog,
nestedVirtualization: self.config.virtualization
)
vmConfig.extensions = self.config.extensions
let creationConfig = StandardVMConfig(configuration: vmConfig)
let vm = try await self.vmm.create(config: creationConfig)
let relayManager = UnixSocketRelayManager(vm: vm)
try await vm.start()
do {
let containers = state.containers
let shareProcessNamespace = self.config.shareProcessNamespace
let pauseProcessHolder = Mutex<LinuxProcess?>(nil)
let fileMountContextUpdates = Mutex<[String: FileMountContext]>([:])
try await vm.withAgent { agent in
try await agent.standardSetup()
// Mount the unified virtiofs share at /run/virtiofs only
// when at least one container has a virtiofs mount. VZ
// tolerates the unbacked mount; CH does not.
if hasVirtiofsMount {
try await agent.mkdir(path: "/run/virtiofs", all: true, perms: 0o755)
if vm.virtiofsLayout == .perTag {
// CH backend: one virtio-fs device per source-hash
// tag, so mount each tag separately at
// /run/virtiofs/<tag>. See LinuxContainer for the
// VZ vs. CH model split.
var seenTags: Set<String> = []
for (_, attached) in vm.mounts {
for entry in attached where entry.type == "virtiofs" {
guard seenTags.insert(entry.source).inserted else { continue }
let dest = "/run/virtiofs/\(entry.source)"
try await agent.mkdir(path: dest, all: true, perms: 0o755)
try await agent.mount(
ContainerizationOCI.Mount(
type: "virtiofs",
source: entry.source,
destination: dest,
options: []
))
}
}
} else {
try await agent.mount(
ContainerizationOCI.Mount(
type: "virtiofs",
source: "virtiofs",
destination: "/run/virtiofs",
options: []
))
}
}
// Create pause container if PID namespace sharing is enabled
if shareProcessNamespace {
let pauseID = "pause-\(self.id)"
let pauseRootfsPath = "/run/container/\(pauseID)/rootfs"
// Bind mount /sbin into the pause container rootfs.
// This is where the guest agent lives.
try await agent.mount(
ContainerizationOCI.Mount(
type: "",
source: "/sbin",
destination: "\(pauseRootfsPath)/sbin",
options: ["bind"]
))
var pauseSpec = Self.createDefaultRuntimeSpec(pauseID, podID: self.id)
pauseSpec.process?.args = ["/sbin/vminitd", "pause"]
pauseSpec.hostname = ""
pauseSpec.mounts = LinuxContainer.defaultMounts().map {
ContainerizationOCI.Mount(
type: $0.type,
source: $0.source,
destination: $0.destination,
options: $0.options
)
}
pauseSpec.linux?.namespaces = [
LinuxNamespace(type: .cgroup),
LinuxNamespace(type: .ipc),
LinuxNamespace(type: .mount),
LinuxNamespace(type: .pid),
LinuxNamespace(type: .uts),
]
// Create LinuxProcess for pause container
let process = LinuxProcess(
pauseID,
containerID: pauseID,
spec: pauseSpec,
io: LinuxProcess.Stdio(stdin: nil, stdout: nil, stderr: nil),
ociRuntimePath: nil,
agent: agent,
vm: vm,
logger: self.logger
)
try await process.start()
pauseProcessHolder.withLock { $0 = process }
self.logger?.debug("Pause container started", metadata: ["pid": "\(process.pid)"])
}
// Mount all container rootfs
for (_, container) in containers {
guard let attachments = vm.mounts[container.id], let rootfsAttachment = attachments.first else {
throw ContainerizationError(.notFound, message: "rootfs mount not found for container \(container.id)")
}
var rootfs = rootfsAttachment.to
rootfs.destination = Self.guestRootfsPath(container.id)
try await agent.mount(rootfs)
}
// Mount file mount holding directories under /run for each container.
for (id, container) in containers {
if container.fileMountContext.hasFileMounts {
var ctx = container.fileMountContext
let containerMounts = vm.mounts[id] ?? []
try await ctx.mountHoldingDirectories(
vmMounts: containerMounts,
agent: agent
)
fileMountContextUpdates.withLock { $0[id] = ctx }
}
}
// Mount pod-level volumes.
let podVolumeAttachments = vm.mounts[self.id] ?? []
for (index, volume) in self.config.volumes.enumerated() {
guard index < podVolumeAttachments.count else {
throw ContainerizationError(
.notFound,
message: "attached filesystem not found for pod volume \"\(volume.name)\""
)
}
let attachment = podVolumeAttachments[index]
let guestPath = Self.guestVolumePath(volume.name)
try await agent.mount(
ContainerizationOCI.Mount(
type: volume.format,
source: attachment.source,
destination: guestPath,
options: attachment.options
))
}
// Start up unix socket relays for each container
for (_, container) in containers {
for socket in container.config.sockets {
try await self.relayUnixSocket(
socket: socket,
containerID: container.id,
relayManager: relayManager,
agent: agent
)
}
}
// For every interface asked for:
// 1. Add the address requested
// 2. Online the adapter
// 3. For the first interface, add the default route
var defaultRouteSet = false
for (index, i) in self.interfaces.enumerated() {
let name = "eth\(index)"
try await agent.setupInterface(
i,
name: name,
setDefaultRoute: !defaultRouteSet,
logger: self.logger
)
defaultRouteSet = true
}
// Setup /etc/resolv.conf and /etc/hosts for each container.
// Container-level config takes precedence over pod-level config.
for (_, container) in containers {
if let dns = container.config.dns ?? self.config.dns {
try await agent.configureDNS(
config: dns,
location: Self.guestRootfsPath(container.id)
)
}
if let hosts = container.config.hosts ?? self.config.hosts {
try await agent.configureHosts(
config: hosts,
location: Self.guestRootfsPath(container.id)
)
}
}
}
state.pauseProcess = pauseProcessHolder.withLock { $0 }
state.unifiedVirtiofsMounted = hasVirtiofsMount && vm.virtiofsLayout == .unified
// Apply file mount context updates.
let updates = fileMountContextUpdates.withLock { $0 }
for (id, ctx) in updates {
state.containers[id]?.fileMountContext = ctx
}
// Transition all containers to created state
for id in state.containers.keys {
state.containers[id]?.state = .created
}
state.phase = .created(.init(vm: vm, relayManager: relayManager))
} catch {
try? await relayManager.stopAll()
try? await vm.stop()
state.phase.setErrored(error: error)
throw error
}
}
}
/// Start a container's initial process.
public func startContainer(_ containerID: String) async throws {
try await self.state.withLock { state in
let createdState = try state.phase.createdState("startContainer")
guard var container = state.containers[containerID] else {
throw ContainerizationError(
.notFound,
message: "container \(containerID) not found in pod"
)
}
guard container.state == .created else {
throw ContainerizationError(
.invalidState,
message: "container \(containerID) must be in created state to start"
)
}
let agent = try await createdState.vm.dialAgent()
do {
var spec = self.generateRuntimeSpec(containerID: containerID, config: container.config, rootfs: container.rootfs)
// We don't need the rootfs, nor do OCI runtimes want it included.
// Also filter out file mount holding directories - we mount those separately under /run.
// Transform virtiofs mounts to bind mounts from /run/virtiofs/{tag}
let containerMounts = createdState.vm.mounts[containerID] ?? []
let holdingTags = container.fileMountContext.holdingDirectoryTags
var mounts: [ContainerizationOCI.Mount] =
containerMounts.dropFirst()
.filter { !holdingTags.contains($0.source) }
.map { attached -> ContainerizationOCI.Mount in
if attached.type == "virtiofs" {
// Transform to bind mount from holding directory
return ContainerizationOCI.Mount(
type: "none",
source: "/run/virtiofs/\(attached.source)",
destination: attached.destination,
options: ["bind"] + attached.options
)
}
return attached.to
}
+ container.fileMountContext.ociBindMounts()
// When useInit is enabled, bind mount vminitd from the VM's filesystem
// into the container so it can be executed.
if container.config.useInit {
mounts.append(
ContainerizationOCI.Mount(
type: "bind",
source: "/sbin/vminitd",
destination: "/.cz-init",
options: ["bind", "ro"]
))
}
// Bind mount staged sockets into the container. Sockets relayed
// .into the container are created in a staging directory outside
// the rootfs to avoid symlink traversal and mount shadowing.
for socket in container.config.sockets where socket.direction == .into {
mounts.append(
ContainerizationOCI.Mount(
type: "bind",
source: Self.guestSocketStagingPath(socket.id),
destination: socket.destination.path,
options: ["bind"]
))
}
// Bind mount pod volumes into the container.
for mount in container.config.mounts {
if case .shared = mount.runtimeOptions {
mounts.append(
ContainerizationOCI.Mount(
type: "none",
source: Self.guestVolumePath(mount.source),
destination: mount.destination,
options: ["bind"] + mount.options
))
}
}
spec.mounts = cleanAndSortMounts(mounts)
// Configure namespaces for the container
var namespaces: [LinuxNamespace] = [
LinuxNamespace(type: .cgroup),
LinuxNamespace(type: .ipc),
LinuxNamespace(type: .mount),
LinuxNamespace(type: .uts),
]
// Either join pause container's pid ns or create a new one
if self.config.shareProcessNamespace, let pausePID = state.pauseProcess?.pid {
let nsPath = "/proc/\(pausePID)/ns/pid"
self.logger?.debug(
"Container joining pause PID namespace",
metadata: [
"container": "\(containerID)",
"pausePID": "\(pausePID)",
"nsPath": "\(nsPath)",
])
namespaces.append(LinuxNamespace(type: .pid, path: nsPath))
} else {
namespaces.append(LinuxNamespace(type: .pid))
}
spec.linux?.namespaces = namespaces
let stdio = IOUtil.setup(
portAllocator: self.hostVsockPorts,
stdin: container.config.process.stdin,
stdout: container.config.process.stdout,
stderr: container.config.process.stderr
)
let process = LinuxProcess(
containerID,
containerID: containerID,
spec: spec,
io: stdio,
ociRuntimePath: nil,
agent: agent,
vm: createdState.vm,
logger: self.logger
)
try await process.start()
container.process = process
container.state = .started
state.containers[containerID] = container
} catch {
try? await agent.close()
throw error
}
}
}
/// Stop a container from executing.
public func stopContainer(_ containerID: String) async throws {
try await self.state.withLock { state in
let createdState = try state.phase.createdState("stopContainer")
guard var container = state.containers[containerID] else {
throw ContainerizationError(
.notFound,
message: "container \(containerID) not found in pod"
)
}
// Allow stop to be called multiple times
if container.state == .stopped {
return
}
// Handle containers that were hotplugged but never started
if container.state == .created {