Skip to content

Commit 26aae3e

Browse files
authored
Add rootfs override (#1323)
This PR adds an option in `ContainerCreateOption` to override the root filesystem of container. When `ContainerCreateOption.rootFsOverride` is set, container uses that as the root fs instead of the one cloned from image snapshot. ## Type of Change - [ ] Bug fix - [X] New feature - [ ] Breaking change - [ ] Documentation update ## Motivation and Context [Why is this change needed?] ## Testing - [X] Tested locally - [ ] Added/updated tests - [ ] Added/updated docs
1 parent 77ed6c5 commit 26aae3e

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

Sources/ContainerResource/Container/Bundle.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ extension Bundle {
110110
try bundle.write(filename: Self.containerConfigFilename, value: containerConfiguration)
111111
}
112112

113-
if let containerRootFilesystem {
113+
if let rootFsOverride = options?.rootFsOverride {
114+
try bundle.setContainerRootFs(fs: rootFsOverride)
115+
} else if let containerRootFilesystem {
114116
let readonly = containerConfiguration?.readOnly ?? false
115-
try bundle.setContainerRootFs(cloning: containerRootFilesystem, readonly: readonly)
117+
try bundle.cloneContainerRootFs(cloning: containerRootFilesystem, readonly: readonly)
116118
}
117119

118120
if let options {
@@ -133,14 +135,18 @@ extension Bundle {
133135
path.appendingPathComponent(name)
134136
}
135137

136-
public func setContainerRootFs(cloning fs: Filesystem, readonly: Bool = false) throws {
138+
public func setContainerRootFs(fs: Filesystem) throws {
139+
let fsData = try JSONEncoder().encode(fs)
140+
try fsData.write(to: self.containerRootfsConfig)
141+
}
142+
143+
public func cloneContainerRootFs(cloning fs: Filesystem, readonly: Bool = false) throws {
137144
var mutableFs = fs
138145
if readonly && !mutableFs.options.contains("ro") {
139146
mutableFs.options.append("ro")
140147
}
141148
let cloned = try mutableFs.clone(to: self.containerRootfsBlock.absolutePath())
142-
let fsData = try JSONEncoder().encode(cloned)
143-
try fsData.write(to: self.containerRootfsConfig)
149+
try setContainerRootFs(fs: cloned)
144150
}
145151

146152
/// Delete the bundle and all of the resources contained inside.

Sources/ContainerResource/Container/ContainerCreateOptions.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
public struct ContainerCreateOptions: Codable, Sendable {
18+
/// Remove the container and wipe out its data on container stop
1819
public let autoRemove: Bool
20+
/// Override the rootFs with this one other than the image-cloned version
21+
public let rootFsOverride: Filesystem?
1922

20-
public init(autoRemove: Bool) {
23+
public init(autoRemove: Bool, rootFsOverride: Filesystem? = nil) {
2124
self.autoRemove = autoRemove
25+
self.rootFsOverride = rootFsOverride
2226
}
2327

2428
public static let `default` = ContainerCreateOptions(autoRemove: false)

Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public actor ContainersService {
364364
"ref": "\(configuration.image.reference)",
365365
])
366366
let containerImage = ClientImage(description: configuration.image)
367-
let imageFs = try await containerImage.getCreateSnapshot(platform: configuration.platform)
367+
let imageFs = try await options.rootFsOverride == nil ? containerImage.getCreateSnapshot(platform: configuration.platform) : nil
368368

369369
self.log.debug(
370370
"configure runtime",

0 commit comments

Comments
 (0)