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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ xcuserdata/
DerivedData/
.swiftpm/
.netrc
.swiftpm
workdir/
installer/
.venv/
Expand Down
49 changes: 49 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import CompilerPluginSupport
import Foundation
import PackageDescription

let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
let gitTag = ProcessInfo.processInfo.environment["GIT_TAG"] ?? ""
let buildTime = ProcessInfo.processInfo.environment["BUILD_TIME"] ?? "unspecified"

let package = Package(
name: "containerization",
platforms: [.macOS("15.0")],
Expand All @@ -33,6 +37,7 @@ let package = Package(
.library(name: "ContainerizationOS", targets: ["ContainerizationOS"]),
.library(name: "ContainerizationExtras", targets: ["ContainerizationExtras"]),
.library(name: "ContainerizationArchive", targets: ["ContainerizationArchive"]),
.library(name: "VminitdCore", targets: ["VminitdCore", "Cgroup", "LCShim"]),
.executable(name: "cctl", targets: ["cctl"]),
],
dependencies: [
Expand Down Expand Up @@ -259,6 +264,50 @@ let package = Package(
.target(
name: "CShim"
),
.target(
name: "CVersion",
path: "vminitd/Sources/CVersion",
cSettings: [
.define("GIT_COMMIT", to: "\"\(gitCommit)\""),
.define("GIT_TAG", to: "\"\(gitTag)\""),
.define("BUILD_TIME", to: "\"\(buildTime)\""),
]
),
.target(
name: "LCShim",
path: "vminitd/Sources/LCShim"
),
.target(
name: "Cgroup",
Comment thread
dkovba marked this conversation as resolved.
dependencies: [
.product(name: "Logging", package: "swift-log"),
"ContainerizationOCI",
"ContainerizationOS",
.product(name: "SystemPackage", package: "swift-system"),
"LCShim",
],
path: "vminitd/Sources/Cgroup"
),
.target(
name: "VminitdCore",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
"Containerization",
"ContainerizationArchive",
"ContainerizationNetlink",
"ContainerizationIO",
"ContainerizationOS",
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "GRPCCore", package: "grpc-swift-2"),
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
"LCShim",
"CVersion",
"Cgroup",
],
path: "vminitd/Sources/VminitdCore"
),
]
)

Expand Down
55 changes: 2 additions & 53 deletions vminitd/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,29 @@

// The swift-tools-version declares the minimum version of Swift required to build this package.

import Foundation
import PackageDescription

let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
let gitTag = ProcessInfo.processInfo.environment["GIT_TAG"] ?? ""
let buildTime = ProcessInfo.processInfo.environment["BUILD_TIME"] ?? "unspecified"

let package = Package(
name: "swift-vminitd",
platforms: [.macOS("15")],
products: [
.library(name: "VminitdCore", targets: ["VminitdCore"]),
.executable(name: "vminitd", targets: ["vminitd"]),
.executable(name: "vmexec", targets: ["vmexec"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.10.1"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.36.0"),
.package(url: "https://github.com/apple/swift-system.git", from: "1.6.4"),
.package(url: "https://github.com/grpc/grpc-swift-2.git", from: "2.3.0"),
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", from: "2.4.4"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", from: "2.2.0"),
.package(name: "containerization", path: "../"),
],
targets: [
.target(
name: "CVersion",
cSettings: [
.define("GIT_COMMIT", to: "\"\(gitCommit)\""),
.define("GIT_TAG", to: "\"\(gitTag)\""),
.define("BUILD_TIME", to: "\"\(buildTime)\""),
]
),
.target(
name: "LCShim"
),
.target(
name: "Cgroup",
dependencies: [
.product(name: "Logging", package: "swift-log"),
.product(name: "ContainerizationOCI", package: "containerization"),
.product(name: "ContainerizationOS", package: "containerization"),
.product(name: "SystemPackage", package: "swift-system"),
"LCShim",
]
),
.target(
name: "VminitdCore",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
.product(name: "Containerization", package: "containerization"),
.product(name: "ContainerizationArchive", package: "containerization"),
.product(name: "ContainerizationNetlink", package: "containerization"),
.product(name: "ContainerizationIO", package: "containerization"),
.product(name: "ContainerizationOS", package: "containerization"),
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "GRPCCore", package: "grpc-swift-2"),
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
"LCShim",
"CVersion",
"Cgroup",
]
),
.executableTarget(
name: "vminitd",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "ContainerizationOS", package: "containerization"),
.product(name: "Logging", package: "swift-log"),
"VminitdCore",
.product(name: "VminitdCore", package: "containerization"),
]
),
.executableTarget(
Expand All @@ -100,8 +50,7 @@ let package = Package(
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "Containerization", package: "containerization"),
.product(name: "ContainerizationOS", package: "containerization"),
"LCShim",
"Cgroup",
.product(name: "VminitdCore", package: "containerization"),
]
),
]
Expand Down
14 changes: 7 additions & 7 deletions vminitd/Sources/Cgroup/Cgroup2Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

// NOTE: Ideally this should live in ContainerizationOS/Linux, or just ContainerizationCgroups
// or something similar, but it's not there yet. It does what we need, but it'd need a lot more
// features and testing before it's ready to be public.

#if os(Linux)

#if canImport(Musl)
import Musl
#elseif canImport(Glibc)
Expand All @@ -43,8 +43,8 @@ package enum Cgroup2Controller: String {

// Extremely simple cgroup manager. Our needs are simple for now, and this is
// reflected in the type.
package struct Cgroup2Manager: Sendable {
package static let defaultMountPoint = URL(filePath: "/sys/fs/cgroup")
public struct Cgroup2Manager: Sendable {
public static let defaultMountPoint = URL(filePath: "/sys/fs/cgroup")

private static let killFile = "cgroup.kill"
private static let procsFile = "cgroup.procs"
Expand All @@ -66,7 +66,7 @@ package struct Cgroup2Manager: Sendable {
self.logger = logger
}

package static func load(
public static func load(
mountPoint: URL = Self.defaultMountPoint,
group: URL,
logger: Logger? = nil
Expand Down Expand Up @@ -183,7 +183,7 @@ package struct Cgroup2Manager: Sendable {
}
}

package func addProcess(pid: Int32) throws {
public func addProcess(pid: Int32) throws {
self.logger?.debug(
"adding new proc to cgroup",
metadata: [
Expand All @@ -199,7 +199,7 @@ package struct Cgroup2Manager: Sendable {
)
}

package func applyResources(resources: ContainerizationOCI.LinuxResources) throws {
public func applyResources(resources: ContainerizationOCI.LinuxResources) throws {
self.logger?.debug(
"applying cgroup resources",
metadata: [
Expand Down
2 changes: 2 additions & 0 deletions vminitd/Sources/LCShim/include/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#define __SYSCALL_H

#include <sys/types.h>
#ifdef __linux__
#include <sys/vfs.h>
#endif

// CLONE_* flags
#ifndef CLONE_NEWNS
Expand Down
2 changes: 2 additions & 0 deletions vminitd/Sources/LCShim/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#ifdef __linux__
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
Expand Down Expand Up @@ -48,3 +49,4 @@ int CZ_setrlimit(int resource, unsigned long long soft,
limit.rlim_max = (rlim_t)hard;
return setrlimit(resource, &limit);
}
#endif
6 changes: 5 additions & 1 deletion vminitd/Sources/VminitdCore/AgentCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ArgumentParser
import CVersion
import Cgroup
Expand Down Expand Up @@ -44,7 +46,7 @@ public struct AgentCommand: AsyncParsableCommand {
private static let foregroundEnvVar = "FOREGROUND"
public static let vsockPort = 1024

@OptionGroup public var options: LogLevelOption
@OptionGroup var options: LogLevelOption

public init() {}

Expand Down Expand Up @@ -214,3 +216,5 @@ public struct AgentCommand: AsyncParsableCommand {
}
}
}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/CommandRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ContainerizationOS
import Foundation
import Synchronization
Expand Down Expand Up @@ -102,3 +104,5 @@ final class ReaperCommandRunner: CommandRunner, Sendable {
}
}
}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/ContainerProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ContainerizationOS
import Foundation

Expand Down Expand Up @@ -64,3 +66,5 @@ protocol ContainerProcess: Sendable {
/// Set the exit status of the process.
func setExit(_ status: Int32)
}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/HostStdio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

struct HostStdio: Sendable {
let stdin: UInt32?
let stdout: UInt32?
let stderr: UInt32?
let terminal: Bool
}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/IOCloser+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ContainerizationOS
import Foundation

Expand All @@ -26,3 +28,5 @@ extension Terminal: IOCloser {
}

extension FileHandle: IOCloser {}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/IOCloser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

protocol IOCloser: Sendable {
var fileDescriptor: Int32 { get }

Expand All @@ -31,3 +33,5 @@ struct UnownedIOCloser: IOCloser {

func close() throws {}
}

#endif
4 changes: 4 additions & 0 deletions vminitd/Sources/VminitdCore/IOPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ContainerizationError
import ContainerizationOS
import Foundation
Expand Down Expand Up @@ -183,3 +185,5 @@ final class IOPair: Sendable {
}
}
}

#endif
10 changes: 7 additions & 3 deletions vminitd/Sources/VminitdCore/InitCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(Linux)

import ArgumentParser
import ContainerizationOS
import LCShim
Expand Down Expand Up @@ -42,13 +44,13 @@ public struct InitCommand: ParsableCommand {
public init() {}

@Flag(name: .shortAndLong, help: "Send signals to the child's process group instead of just the child")
public var processGroup: Bool = false
var processGroup: Bool = false

@Argument(help: "The command to run")
public var command: String
var command: String

@Argument(parsing: .captureForPassthrough, help: "Arguments for the command")
public var arguments: [String] = []
var arguments: [String] = []

/// Signals that should NOT be forwarded to the child.
private static let ignoredSignals: Set<Int32> = [
Expand Down Expand Up @@ -107,3 +109,5 @@ public struct InitCommand: ParsableCommand {
_exit(childExitStatus ?? 1)
}
}

#endif
Loading
Loading