-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: Migrate zig from library to example (#158)
Reviewed-by: Cezar Craciunoiu <[email protected]> Approved-by: Cezar Craciunoiu <[email protected]>
- Loading branch information
Showing
7 changed files
with
96 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
2 changes: 1 addition & 1 deletion
2
library/zig/0.11.0/helloworld.zig → examples/helloworld-zig/helloworld.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.