nonlinux runtime start

This commit is contained in:
Nils André
2024-03-05 22:32:09 -08:00
parent 87246985ff
commit 680143929c
3 changed files with 60 additions and 31 deletions

View File

@@ -1,4 +1,7 @@
const std = @import("std");
const crun_content = @embedFile("src/tools/crun");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
@@ -18,7 +21,6 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
// const clap = b.addModule("clap", .{ .root_source_file = .{ .path = "lib/zig-clap/clap.zig" } });
const runtime = b.addExecutable(.{
.name = "runtime",
@@ -26,8 +28,6 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
.link_libc = true,
// necessary to link in bigger file
// .code_model = .medium,
});
const dockerc = b.addExecutable(.{
@@ -38,35 +38,35 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});
const init = b.addExecutable(.{
.name = "init",
.root_source_file = .{ .path = "src/init.zig" },
.target = target,
.optimize = optimize,
// for std.c.environ
.link_libc = true,
});
const cpio = b.addWriteFiles();
_ = cpio.addCopyFile(init.getEmittedBin(), "init");
_ = cpio.add("etc/resolv.conf", "nameserver 1.1.1.1\n");
_ = cpio.add("crun", crun_content);
const mkdir = b.addSystemCommand(&.{"mkdir", "dev", "bundle", "mnt", "proc", "run", "sys", "tmp"});
mkdir.setCwd(cpio.getDirectory());
const findCommand = b.addSystemCommand(&.{"find", ".", "-print0"});
findCommand.setCwd(cpio.getDirectory());
findCommand.step.dependOn(&mkdir.step);
const cpioCommand = b.addSystemCommand(&.{"cpio", "--null", "-ov", "--format=newc"});
cpioCommand.setCwd(cpio.getDirectory());
cpioCommand.setStdIn(.{.lazy_path=findCommand.captureStdOut()});
dockerc.root_module.addAnonymousImport("runtime", .{ .root_source_file = runtime.getEmittedBin() });
dockerc.root_module.addAnonymousImport("cpio", .{ .root_source_file = cpioCommand.captureStdOut() });
b.installArtifact(dockerc);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
// b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
// const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
// run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
// if (b.args) |args| {
// run_cmd.addArgs(args);
// }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
// const run_step = b.step("run", "Run the app");
// run_step.dependOn(&run_cmd.step);
b.installArtifact(init);
}

24
src/init.zig Normal file
View File

@@ -0,0 +1,24 @@
const std = @import("std");
pub fn main() !void {
// TODO: check the return value of mount
_ = std.posix.linux.mount("proc", "/proc", "proc", 0, 0);
_ = std.posix.linux.mount("sysfs", "/sys", "sysfs", 0, 0);
_ = std.posix.linux.mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, 0);
_ = std.posix.linux.mount("tmpfs", "/run", "tmpfs", 0, 0);
try std.fs.makeDirAbsolute("/run/upper");
try std.fs.makeDirAbsolute("/run/work");
_ = std.posix.linux.mount("dev", "/dev", "devtmpfs", 0, 0);
_ = std.posix.linux.mount("/dev/sda", "/bundle", "squashfs", std.posix.linux.MS.RDONLY, 0);
_ = std.posix.linux.mount("overlay", "/mnt", "overlay", std.posix.linux.MS.RDONLY, @intFromPtr("lowerdir=/bundle,upperdir=/run/upper,workdir=/run/work"));
const argv = &[_:null]?[*:0]const u8{
"/crun", "run", "-b", "/mnt", "--no-pivot", "crun_docker_c_id", null,
};
// TODO: make sure this never returns
_ = std.posix.linux.execve("/crun", argv, std.c.environ);
}

5
src/nonlinux_runtime.zig Normal file
View File

@@ -0,0 +1,5 @@
pub fn main() !void {
}