Skip to content

Commit

Permalink
refactor!: Migrate zig from library to example (#158)
Browse files Browse the repository at this point in the history
Reviewed-by: Cezar Craciunoiu <[email protected]>
Approved-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc authored Feb 12, 2025
2 parents 14ac2a0 + 5e92d29 commit 20800b7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 186 deletions.
31 changes: 31 additions & 0 deletions examples/helloworld-zig/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM gcc:13.2.0-bookworm AS zig

RUN set -xe; \
apt-get update; \
apt-get install -y --no-install-recommends \
ca-certificates \
curl \
xz-utils;

WORKDIR /zig

ARG ZIG_VERSION=0.11.0

RUN set -xe; \
curl -o /zig.tar.xz https://ziglang.org/download/${ZIG_VERSION}/zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz; \
tar -xf /zig.tar.xz --strip-components 1 -C /zig; \
mv /zig/zig /usr/bin/zig; \
mv /zig/lib /usr/lib/zig

FROM zig AS build

WORKDIR /src

COPY . /src

RUN set -xe; \
zig build-exe /src/helloworld.zig -fPIE -static

FROM scratch

COPY --from=build /src/helloworld /helloworld
7 changes: 7 additions & 0 deletions examples/helloworld-zig/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spec: v0.6

runtime: unikraft.org/base:latest

rootfs: ./Dockerfile

args: ["/helloworld"]
57 changes: 57 additions & 0 deletions examples/helloworld-zig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Zig "Hello, world!"

This directory contains a Zig-based "Hello, world!" example running on Unikraft.

## Set Up

To run this example, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli), clone this repository and `cd` into this directory.

## Run and Use

Use `kraft` to run the image and start a Unikraft instance:

```bash
kraft run --rm --plat qemu --arch x86_64 .
```

If the `--plat` argument is left out, it defaults to `qemu`.
If the `--arch` argument is left out, it defaults to your system's CPU architecture.

Once executed, you should see a "Bye, World!" message.

## Inspect and Close

To list information about the Unikraft instance, use:

```bash
kraft ps
```

```text
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
elastic_goblin oci://unikraft.org/base:latest /helloworld 9 seconds ago running 64M qemu/x86_64
```

The instance name is `elastic_goblin`.
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run:

```bash
kraft rm elastic_goblin
```

Note that depending on how you modify this example your instance **may** need more memory to run.
To do so, use the `kraft run`'s `-M` flag, for example:

```bash
kraft run --rm --plat qemu --arch x86_64 -M 256M .
```

## `kraft` and `sudo`

Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior.
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless).

## Learn More

- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("Hello, World!\n", .{});
std.debug.print("Hello, world!\n", .{});
}
21 changes: 0 additions & 21 deletions library/zig/0.11.0/Dockerfile

This file was deleted.

143 changes: 0 additions & 143 deletions library/zig/0.11.0/Kraftfile

This file was deleted.

21 changes: 0 additions & 21 deletions library/zig/0.11.0/README.md

This file was deleted.

0 comments on commit 20800b7

Please sign in to comment.