Skip to content

Commit 5268c48

Browse files
committed
Add LinuxContainer block I/O resources
1 parent f8a18e8 commit 5268c48

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

Sources/Containerization/LinuxContainer.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public final class LinuxContainer: Container, Sendable {
5656
public var cpus: Int = 4
5757
/// The memory in bytes to give to the container.
5858
public var memoryInBytes: UInt64 = 1024.mib()
59+
/// Optional block I/O resource limits for the container cgroup.
60+
public var blockIO: LinuxBlockIO?
5961
/// The hostname for the container.
6062
public var hostname: String?
6163
/// The system control options for the container.
@@ -94,6 +96,7 @@ public final class LinuxContainer: Container, Sendable {
9496
process: LinuxProcessConfiguration,
9597
cpus: Int = 4,
9698
memoryInBytes: UInt64 = 1024.mib(),
99+
blockIO: LinuxBlockIO? = nil,
97100
hostname: String? = nil,
98101
sysctl: [String: String] = [:],
99102
interfaces: [any Interface] = [],
@@ -111,6 +114,7 @@ public final class LinuxContainer: Container, Sendable {
111114
self.process = process
112115
self.cpus = cpus
113116
self.memoryInBytes = memoryInBytes
117+
self.blockIO = blockIO
114118
self.hostname = hostname
115119
self.sysctl = sysctl
116120
self.interfaces = interfaces
@@ -374,7 +378,7 @@ public final class LinuxContainer: Container, Sendable {
374378
)
375379
}
376380

377-
private func generateRuntimeSpec() -> Spec {
381+
func generateRuntimeSpec() -> Spec {
378382
var spec = Self.createDefaultRuntimeSpec(id)
379383

380384
// Process toggles.
@@ -409,7 +413,8 @@ public final class LinuxContainer: Container, Sendable {
409413
cpu: LinuxCPU(
410414
quota: Int64(config.cpus * 100_000),
411415
period: 100_000
412-
)
416+
),
417+
blockIO: config.blockIO
413418
)
414419

415420
spec.linux?.namespaces = [

Tests/ContainerizationTests/LinuxContainerTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,53 @@ struct LinuxContainerTests {
6666

6767
#expect(process.arguments == ["/bin/sh", "-c", "echo 'hello'", "&&", "sleep 10"])
6868
}
69+
70+
@Test func runtimeSpecIncludesConfiguredBlockIO() throws {
71+
let blockIO = LinuxBlockIO(
72+
weight: 500,
73+
leafWeight: 300,
74+
weightDevice: [
75+
LinuxWeightDevice(major: 8, minor: 0, weight: 700, leafWeight: 400)
76+
],
77+
throttleReadBpsDevice: [
78+
LinuxThrottleDevice(major: 8, minor: 16, rate: 1_048_576)
79+
],
80+
throttleWriteBpsDevice: [
81+
LinuxThrottleDevice(major: 8, minor: 32, rate: 2_097_152)
82+
],
83+
throttleReadIOPSDevice: [
84+
LinuxThrottleDevice(major: 8, minor: 48, rate: 1_000)
85+
],
86+
throttleWriteIOPSDevice: [
87+
LinuxThrottleDevice(major: 8, minor: 64, rate: 2_000)
88+
]
89+
)
90+
91+
let container = try LinuxContainer(
92+
"blkio-test",
93+
rootfs: .block(format: "ext4", source: "/tmp/rootfs.img", destination: "/"),
94+
vmm: StubVirtualMachineManager(),
95+
configuration: .init(process: .init(), blockIO: blockIO)
96+
)
97+
98+
let resources = try #require(container.generateRuntimeSpec().linux?.resources)
99+
let specBlockIO = try #require(resources.blockIO)
100+
101+
#expect(specBlockIO.weight == 500)
102+
#expect(specBlockIO.leafWeight == 300)
103+
#expect(specBlockIO.weightDevice.first?.major == 8)
104+
#expect(specBlockIO.weightDevice.first?.minor == 0)
105+
#expect(specBlockIO.weightDevice.first?.weight == 700)
106+
#expect(specBlockIO.weightDevice.first?.leafWeight == 400)
107+
#expect(specBlockIO.throttleReadBpsDevice.first?.rate == 1_048_576)
108+
#expect(specBlockIO.throttleWriteBpsDevice.first?.rate == 2_097_152)
109+
#expect(specBlockIO.throttleReadIOPSDevice.first?.rate == 1_000)
110+
#expect(specBlockIO.throttleWriteIOPSDevice.first?.rate == 2_000)
111+
}
112+
}
113+
114+
private struct StubVirtualMachineManager: VirtualMachineManager {
115+
func create(config: some VMCreationConfig) async throws -> any VirtualMachineInstance {
116+
fatalError("StubVirtualMachineManager.create should not be called by LinuxContainerTests")
117+
}
69118
}

0 commit comments

Comments
 (0)