Skip to content

Commit 07a2f0c

Browse files
committed
refactor!: Migrate zig from library to example
Since Zig is a compile-time language and the entry contains simply a "Hello, world!" application it does not make sense to include it as a standalone entry in the library. This better serves as an example therefore. Additional changes clean up the example. Signed-off-by: Alexander Jung <alex@unikraft.io>
1 parent 4132395 commit 07a2f0c

File tree

7 files changed

+96
-186
lines changed

7 files changed

+96
-186
lines changed

examples/helloworld-zig/Dockerfile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM gcc:13.2.0-bookworm AS zig
2+
3+
RUN set -xe; \
4+
apt-get update; \
5+
apt-get install -y --no-install-recommends \
6+
ca-certificates \
7+
curl \
8+
xz-utils;
9+
10+
WORKDIR /zig
11+
12+
ARG ZIG_VERSION=0.11.0
13+
14+
RUN set -xe; \
15+
curl -o /zig.tar.xz https://ziglang.org/download/0.11.0/zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz; \
16+
tar -xf /zig.tar.xz --strip-components 1 -C /zig; \
17+
mv /zig/zig /usr/bin/zig; \
18+
mv /zig/lib /usr/lib/zig
19+
20+
FROM zig AS build
21+
22+
WORKDIR /src
23+
24+
COPY . /src
25+
26+
RUN set -xe; \
27+
zig build-exe /src/helloworld.zig -fPIE -static
28+
29+
FROM scratch
30+
31+
COPY --from=build /src/helloworld /helloworld

examples/helloworld-zig/Kraftfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spec: v0.6
2+
3+
runtime: unikraft.org/base:latest
4+
5+
rootfs: ./Dockerfile
6+
7+
args: ["/helloworld"]

examples/helloworld-zig/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Zig "Hello, world!"
2+
3+
This directory contains a Zig-based "Hello, world!" example running on Unikraft.
4+
5+
## Set Up
6+
7+
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.
8+
9+
## Run and Use
10+
11+
Use `kraft` to run the image and start a Unikraft instance:
12+
13+
```bash
14+
kraft run --rm --plat qemu --arch x86_64 .
15+
```
16+
17+
If the `--plat` argument is left out, it defaults to `qemu`.
18+
If the `--arch` argument is left out, it defaults to your system's CPU architecture.
19+
20+
Once executed, you should see a "Bye, World!" message.
21+
22+
## Inspect and Close
23+
24+
To list information about the Unikraft instance, use:
25+
26+
```bash
27+
kraft ps
28+
```
29+
30+
```text
31+
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
32+
elastic_goblin oci://unikraft.org/base:latest /helloworld 9 seconds ago running 64M qemu/x86_64
33+
```
34+
35+
The instance name is `elastic_goblin`.
36+
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run:
37+
38+
```bash
39+
kraft rm elastic_goblin
40+
```
41+
42+
Note that depending on how you modify this example your instance **may** need more memory to run.
43+
To do so, use the `kraft run`'s `-M` flag, for example:
44+
45+
```bash
46+
kraft run --rm --plat qemu --arch x86_64 -M 256M .
47+
```
48+
49+
## `kraft` and `sudo`
50+
51+
Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior.
52+
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless).
53+
54+
## Learn More
55+
56+
- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
57+
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const std = @import("std");
22

33
pub fn main() !void {
4-
std.debug.print("Hello, World!\n", .{});
4+
std.debug.print("Hello, world!\n", .{});
55
}

library/zig/0.11.0/Dockerfile

-21
This file was deleted.

library/zig/0.11.0/Kraftfile

-143
This file was deleted.

library/zig/0.11.0/README.md

-21
This file was deleted.

0 commit comments

Comments
 (0)