Skip to content

Commit b61b932

Browse files
authored
Adds --add-file to allow adding files to initfs. (#302)
- Motivation is to be able to add the `swift-backtrace-static` binary as needed. - Use singlular `--add-file` and `--label` options since both accept multiple invocations with single values each. Example usage: ```bash ./bin/cctl rootfs create \ --vminitd vminitd/bin/vminitd \ --vmexec vminitd/bin/vmexec \ --add-file /Users/john/.swiftpm/swift-sdks/swift-6.2-RELEASE_static-linux-0.0.1.artifactbundle/swift-6.2-RELEASE_static-linux-0.0.1/swift-linux-musl/musl-1.2.5.sdk/aarch64/usr/libexec/swift/linux-static/swift-backtrace-static:sbin/swift-backtrace \ --label org.opencontainers.image.source=https://github.com/apple/containerization \ --image vminit:latest \ bin/init.rootfs.tar.gz ```
1 parent 82f2d88 commit b61b932

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ init: containerization vminitd
6262
@rm -f bin/init.rootfs.tar.gz bin/init.block
6363
@./bin/cctl rootfs create \
6464
--vminitd vminitd/bin/vminitd \
65-
--labels org.opencontainers.image.source=https://github.com/apple/containerization \
6665
--vmexec vminitd/bin/vmexec \
66+
--label org.opencontainers.image.source=https://github.com/apple/containerization \
6767
--image vminit:latest \
6868
bin/init.rootfs.tar.gz
6969

Sources/cctl/RootfsCommand.swift

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,26 @@ extension Application {
3434
)
3535

3636
struct Create: AsyncParsableCommand {
37-
@Option(name: .long, help: "Path to vminitd")
38-
var vminitd: String
37+
@Option(name: [.short, .customLong("add-file")], help: "Additional file to add (format src-path:dst-path)")
38+
var addFiles: [String] = []
3939

40-
@Option(name: .long, help: "Path to vmexec")
41-
var vmexec: String
40+
@Option(name: .customLong("ext4"), help: "The path to an ext4 image to create.")
41+
var ext4File: String?
4242

43-
@Option(name: .long, help: "Platform of the built binaries being packaged into the block")
44-
var platformString: String = Platform.current.description
43+
@Option(name: .customLong("image"), help: "The name of the image to produce.")
44+
var imageName: String?
4545

46-
@Option(name: .long, help: "Labels to add to the built image of the form <key1>=<value1>, [<key2>=<value2>,...]")
46+
@Option(name: .customLong("label"), help: "Label to add to the image (format: key=value)")
4747
var labels: [String] = []
4848

49-
@Option(name: .customLong("image"), help: "The name of the image to produce.")
50-
var imageName: String?
49+
@Option(name: .long, help: "Platform of the built binaries being packaged into the block")
50+
var platformString: String = Platform.current.description
5151

52-
@Option(name: .customLong("ext4"), help: "The path to an ext4 image to create.")
53-
var ext4File: String?
52+
@Option(name: .long, help: "Path to vmexec")
53+
var vmexec: String
54+
55+
@Option(name: .long, help: "Path to vminitd")
56+
var vminitd: String
5457

5558
// The path where the intermediate tar archive is created.
5659
@Argument var tarPath: String
@@ -90,18 +93,17 @@ extension Application {
9093

9194
private func outputExt4(archive: URL, to path: URL) async throws {
9295
let unpacker = EXT4Unpacker(blockSizeInBytes: 256.mib())
93-
9496
try unpacker.unpack(archive: archive, compression: .gzip, at: path)
9597
}
9698

9799
private func outputImage(path: URL, reference: String) async throws {
98100
let p = try Platform(from: platformString)
99-
let labels = Application.parseKeyValuePairs(from: labels)
101+
let parsedLabels = Application.parseKeyValuePairs(from: labels)
100102
_ = try await InitImage.create(
101103
reference: reference,
102104
rootfs: path,
103105
platform: p,
104-
labels: labels,
106+
labels: parsedLabels,
105107
imageStore: Application.imageStore,
106108
contentStore: Application.contentStore
107109
)
@@ -142,6 +144,18 @@ extension Application {
142144
entry.size = Int64(data.count)
143145
try writer.writeEntry(entry: entry, data: data)
144146

147+
for addFile in addFiles {
148+
let paths = addFile.components(separatedBy: ":")
149+
guard paths.count == 2 else {
150+
throw ContainerizationError(.invalidArgument, message: "use src-path:dst-path for --add-file")
151+
}
152+
src = URL(fileURLWithPath: paths[0])
153+
data = try Data(contentsOf: src)
154+
entry.path = paths[1]
155+
entry.size = Int64(data.count)
156+
try writer.writeEntry(entry: entry, data: data)
157+
}
158+
145159
entry.fileType = .symbolicLink
146160
entry.path = "proc/self/exe"
147161
entry.symlinkTarget = "sbin/vminitd"

0 commit comments

Comments
 (0)