Files
container/Sources/Containerization/VZVirtualMachineManager.swift
T
Danny Canter 636099970e LinuxContainer/LinuxProcess: Rework supplying configuration (#219)
This is a fairly large reworking, but it gets rid of something that has
plagued this since release which is the properties needing to be locked
to be Sendable compliant. This was somewhat of a copout because we
mostly know there's not a great deal of ways to have misused the setup
today, but alas we'd need to either mark the type as `@unchecked` or
just find a different route for setting the configuration. This change:

Exposes the underlying Configuration type that today only housed things
that aren't on the OCI spec. I'd love to just expose the OCI spec, but
we don't (and possibly never will) support everything on the spec, so
exposing it to be freely modified would be a bit odd. Now everything
related to the container is configured on this type, and the same goes
for execs.
2025-07-22 12:14:57 -04:00

71 lines
2.4 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
//
// 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.
//===----------------------------------------------------------------------===//
#if os(macOS)
import ContainerizationError
import ContainerizationOCI
import Foundation
import Logging
/// A virtualization.framework backed `VirtualMachineManager` implementation.
public struct VZVirtualMachineManager: VirtualMachineManager {
private let kernel: Kernel
private let bootlog: String?
private let initialFilesystem: Mount
private let logger: Logger?
public init(
kernel: Kernel,
initialFilesystem: Mount,
bootlog: String?,
logger: Logger? = nil
) {
self.kernel = kernel
self.bootlog = bootlog
self.initialFilesystem = initialFilesystem
self.logger = logger
}
public func create(container: Container) throws -> any VirtualMachineInstance {
guard let c = container as? LinuxContainer else {
throw ContainerizationError(
.invalidArgument,
message: "provided container is not a LinuxContainer"
)
}
return try VZVirtualMachineInstance(
logger: self.logger,
with: { config in
config.cpus = container.cpus
config.memoryInBytes = container.memoryInBytes
config.kernel = self.kernel
config.initialFilesystem = self.initialFilesystem
config.interfaces = container.interfaces
if let bootlog {
config.bootlog = URL(filePath: bootlog)
}
config.rosetta = c.config.rosetta
config.nestedVirtualization = c.config.virtualization
config.mounts = [c.rootfs] + c.config.mounts
})
}
}
#endif