Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if let path = ProcessInfo.processInfo.environment["CONTAINERIZATION_PATH"] {
scDependency = .package(path: path)
scVersion = "latest"
} else {
scVersion = "0.2.0"
scVersion = "0.3.0"
scDependency = .package(url: "https://github.com/apple/containerization.git", exact: Version(stringLiteral: scVersion))
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Helpers/Images/ImagesHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ extension ImagesHelper {
.appendingPathComponent("com.apple.container")
}()

private static let unpackStrategy = SnapshotStore.defaultUnpackStrategy

func run() async throws {
let commandName = ImagesHelper._commandName
let log = setupLogger()
Expand Down Expand Up @@ -89,7 +91,7 @@ extension ImagesHelper {
private func initializeImagesService(root: URL, log: Logger, routes: inout [String: XPCServer.RouteHandler]) throws {
let contentStore = RemoteContentStoreClient()
let imageStore = try ImageStore(path: root, contentStore: contentStore)
let snapshotStore = try SnapshotStore(path: root)
let snapshotStore = try SnapshotStore(path: root, unpackStrategy: Self.unpackStrategy, log: log)
let service = try ImagesService(contentStore: contentStore, imageStore: imageStore, snapshotStore: snapshotStore, log: log)
let harness = ImagesServiceHarness(service: service, log: log)

Expand Down
35 changes: 25 additions & 10 deletions Sources/Services/ContainerImagesService/Server/SnapshotStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,41 @@ import ContainerizationExtras
import ContainerizationOCI
import ContainerizationOS
import Foundation
import Logging
import TerminalProgress

public actor SnapshotStore {
private static let snapshotFileName = "snapshot"
private static let snapshotInfoFileName = "snapshot-info"
private static let ingestDirName = "ingest"

/// Return the Unpacker to use for a given image.
/// If the given platform for the image cannot be unpacked return `nil`.
public typealias UnpackStrategy = @Sendable (Containerization.Image, Platform) async throws -> Unpacker?

public static let defaultUnpackStrategy: UnpackStrategy = { image, platform in
guard platform.os == "linux" else {
return nil
}
var minBlockSize = 512.gib()
Copy link
Contributor

@jglogan jglogan Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a disk/filesystem size, or a block size? Is the blockSizeInBytes parameter to EXT4Unpacker a bit misleading?

if image.reference == ClientDefaults.get(key: .defaultInitImage) {
minBlockSize = 512.mib()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if we can move this to a UserDefault in a followup so that end users can customize this size.

}
return EXT4Unpacker(blockSizeInBytes: minBlockSize)
}

let path: URL
let fm = FileManager.default
let ingestDir: URL
let unpackStrategy: UnpackStrategy
let log: Logger?

public init(path: URL) throws {
public init(path: URL, unpackStrategy: @escaping UnpackStrategy, log: Logger?) throws {
let root = path.appendingPathComponent("snapshots")
self.path = root
self.ingestDir = self.path.appendingPathComponent(Self.ingestDirName)
self.unpackStrategy = unpackStrategy
self.log = log
try self.fm.createDirectory(at: root, withIntermediateDirectories: true)
try self.fm.createDirectory(at: self.ingestDir, withIntermediateDirectories: true)
}
Expand All @@ -62,7 +82,8 @@ public actor SnapshotStore {
guard let platform = desc.platform else {
throw ContainerizationError(.internalError, message: "Missing platform for descriptor \(desc.digest)")
}
guard Self.shouldUnpackPlatform(platform) else {
guard let unpacker = try await self.unpackStrategy(image, platform) else {
self.log?.warning("Skipping unpack for \(image.reference) for platform \(platform.description). No unpacker configured.")
continue
}
let currentSubTask = await taskManager.startTask()
Expand All @@ -79,7 +100,8 @@ public actor SnapshotStore {
let tempSnapshotPath = tempDir.appendingPathComponent(Self.snapshotFileName, isDirectory: false)
let infoPath = tempDir.appendingPathComponent(Self.snapshotInfoFileName, isDirectory: false)
do {
let mount = try await image.unpack(for: platform, at: tempSnapshotPath, progress: ContainerizationProgressAdapter.handler(from: taskUpdateProgress))
let progress = ContainerizationProgressAdapter.handler(from: taskUpdateProgress)
let mount = try await unpacker.unpack(image, for: platform, at: tempSnapshotPath, progress: progress)
let fs = Filesystem.block(
format: mount.type,
source: self.snapshotPath(desc).absolutePath(),
Expand Down Expand Up @@ -186,13 +208,6 @@ public actor SnapshotStore {
try self.fm.createDirectory(at: uniqueDirectoryURL, withIntermediateDirectories: true, attributes: nil)
return uniqueDirectoryURL
}

private static func shouldUnpackPlatform(_ platform: Platform) -> Bool {
guard platform.os == "linux" else {
return false
}
return true
}
}

extension FileManager {
Expand Down