Skip to content

Commit ab37e57

Browse files
authored
ContainerManager: Make root/imagestore suppliable (#228)
We should be able to supply an ImageStore if the user wants a different ContentStore, or a different path.
1 parent d512fcb commit ab37e57

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

Sources/Containerization/ContainerManager.swift

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,37 @@ public struct ContainerManager: Sendable {
192192
}
193193
}
194194

195-
/// Create a new manager with the provided kernel and initfs mount.
195+
/// Create a new manager with the provided kernel, initfs mount, image store
196+
/// and optional network implementation.
196197
public init(
197198
kernel: Kernel,
198199
initfs: Mount,
200+
imageStore: ImageStore,
199201
network: Network? = nil
200202
) throws {
201-
self.imageStore = ImageStore.default
203+
self.imageStore = imageStore
204+
self.network = network
205+
try Self.createRootDirectory(path: self.imageStore.path)
206+
self.vmm = VZVirtualMachineManager(
207+
kernel: kernel,
208+
initialFilesystem: initfs,
209+
bootlog: self.imageStore.path.appendingPathComponent("bootlog.log").absolutePath()
210+
)
211+
}
212+
213+
/// Create a new manager with the provided kernel, initfs mount, root state
214+
/// directory and optional network implementation.
215+
public init(
216+
kernel: Kernel,
217+
initfs: Mount,
218+
root: URL? = nil,
219+
network: Network? = nil
220+
) throws {
221+
if let root {
222+
self.imageStore = try ImageStore(path: root)
223+
} else {
224+
self.imageStore = ImageStore.default
225+
}
202226
self.network = network
203227
try Self.createRootDirectory(path: self.imageStore.path)
204228
self.vmm = VZVirtualMachineManager(
@@ -208,13 +232,55 @@ public struct ContainerManager: Sendable {
208232
)
209233
}
210234

235+
/// Create a new manager with the provided kernel, initfs reference, image store
236+
/// and optional network implementation.
237+
public init(
238+
kernel: Kernel,
239+
initfsReference: String,
240+
imageStore: ImageStore,
241+
network: Network? = nil
242+
) async throws {
243+
self.imageStore = imageStore
244+
self.network = network
245+
try Self.createRootDirectory(path: self.imageStore.path)
246+
247+
let initPath = self.imageStore.path.appendingPathComponent("initfs.ext4")
248+
let initImage = try await self.imageStore.getInitImage(reference: initfsReference)
249+
let initfs = try await {
250+
do {
251+
return try await initImage.initBlock(at: initPath, for: .linuxArm)
252+
} catch let err as ContainerizationError {
253+
guard err.code == .exists else {
254+
throw err
255+
}
256+
return .block(
257+
format: "ext4",
258+
source: initPath.absolutePath(),
259+
destination: "/",
260+
options: ["ro"]
261+
)
262+
}
263+
}()
264+
265+
self.vmm = VZVirtualMachineManager(
266+
kernel: kernel,
267+
initialFilesystem: initfs,
268+
bootlog: self.imageStore.path.appendingPathComponent("bootlog.log").absolutePath()
269+
)
270+
}
271+
211272
/// Create a new manager with the provided kernel and image reference for the initfs.
212273
public init(
213274
kernel: Kernel,
214275
initfsReference: String,
276+
root: URL? = nil,
215277
network: Network? = nil
216278
) async throws {
217-
self.imageStore = ImageStore.default
279+
if let root {
280+
self.imageStore = try ImageStore(path: root)
281+
} else {
282+
self.imageStore = ImageStore.default
283+
}
218284
self.network = network
219285
try Self.createRootDirectory(path: self.imageStore.path)
220286

0 commit comments

Comments
 (0)