Skip to content

Commit 7567bce

Browse files
committed
WASM Hello World application
build scripts are to be added in another branch Signed-off-by: sarah-kamall <[email protected]>
1 parent b356723 commit 7567bce

File tree

13 files changed

+421
-0
lines changed

13 files changed

+421
-0
lines changed

firecracker-x86_64

2.59 MB
Binary file not shown.

wasm-hello/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/workdir/

wasm-hello/Config.uk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Configure WASM Hello application
2+
config APPWASMHELLO
3+
bool "WASM Hello World Application"
4+
default y
5+
select LIBWAMR
6+
select CONFIG_LIBWAMR_MAIN_FUNCTION
7+
select LIBUKDEBUG
8+
select LIBUKLIBPARAM
9+
select LIBMUSL
10+
select LIBLWIP

wasm-hello/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
UK_ROOT ?= $(PWD)/workdir/unikraft
2+
UK_LIBS ?= $(PWD)/workdir/libs
3+
UK_BUILD ?= $(PWD)/workdir/build
4+
LIBS := $(UK_LIBS)/musl:$(UK_LIBS)/lwip:$(UK_LIBS)/wamr
5+
6+
all:
7+
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) O=$(UK_BUILD)
8+
9+
$(MAKECMDGOALS):
10+
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) O=$(UK_BUILD) $(MAKECMDGOALS)

wasm-hello/Makefile.uk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(eval $(call addlib,appwasmhello))
2+
3+
4+
APPWASMHELLO_WASM-y += $(APPWASMHELLO_BASE)/main.wasm

wasm-hello/README.md

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
# WASM Hello World on Unikraft
2+
3+
Build and run a WebAssembly (Wasm) hello world program on Unikraft.
4+
This program runs inside an iwasm Intel runtime environment, which is a lightweight WebAssembly runtime designed for efficient execution of WebAssembly modules.
5+
Follow the instructions below to set up, configure, build and run the program.
6+
Make sure you installed the [requirements](../README.md#requirements).
7+
8+
## Quick Setup (aka TLDR)
9+
10+
For a quick setup, run the commands below.
11+
Note that you still need to install the [requirements](../README.md#requirements).
12+
Before everything, make sure you run the [top-level `setup.sh` script](../setup.sh).
13+
14+
To build and run the application for `x86_64`, use the commands below:
15+
16+
```console
17+
./setup.sh
18+
make distclean
19+
wget -O /tmp/defconfig https://raw.githubusercontent.com/unikraft/catalog-core/refs/heads/scripts/wasm-hello/scripts/defconfig/qemu.x86_64
20+
UK_DEFCONFIG=/tmp/defconfig make defconfig
21+
make -j $(nproc)
22+
rm -f initrd.cpio
23+
./workdir/unikraft/support/scripts/mkcpio initrd.cpio ./rootfs/
24+
qemu-system-x86_64 \
25+
-nographic \
26+
-m 8 \
27+
-cpu max \
28+
-kernel workdir/build/wasm-hello-x86_64 \
29+
-append "wasm-hello-x86_64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- main.wasm" \
30+
-initrd ./initrd.cpio
31+
```
32+
33+
This will configure, build and run the application, resulting in a
34+
`Hello world!
35+
buf ptr: 0x400002b0
36+
buf: 1234`
37+
message being printed.
38+
39+
To do the same for `AArch64`, run the commands below:
40+
41+
```console
42+
./setup.sh
43+
make distclean
44+
wget -O /tmp/defconfig https://raw.githubusercontent.com/unikraft/catalog-core/refs/heads/scripts/wasm-hello/scripts/defconfig/qemu.arm64
45+
UK_DEFCONFIG=/tmp/defconfig make defconfig
46+
make -j $(nproc)
47+
rm -f initrd.cpio
48+
./workdir/unikraft/support/scripts/mkcpio initrd.cpio ./rootfs/
49+
qemu-system-aarch64 \
50+
-nographic \
51+
-machine virt \
52+
-m 8 \
53+
-cpu max \
54+
-kernel workdir/build/wasm-hello-arm64 \
55+
-append "wasm-hello-arm64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- main.wasm" \
56+
-initrd ./initrd.cpio
57+
```
58+
59+
Similar to the `x86_64` build, this will result in a
60+
61+
```text
62+
Powered by
63+
o. .o _ _ __ _
64+
Oo Oo ___ (_) | __ __ __ _ ' _) :_
65+
oO oO ' _ `| | |/ / _)' _` | |_| _)
66+
oOo oOO| | | | | (| | | (_) | _) :_
67+
OoOoO ._, ._:_:_,\_._, .__,_:_, \___)
68+
Helene 0.18.0
69+
Hello world!
70+
buf ptr: 0x400002b0
71+
buf: 1234
72+
```
73+
74+
Information about every step and about other types of builds is detailed below.
75+
76+
## Set Up
77+
78+
Set up the required repositories.
79+
For this, you have two options:
80+
81+
1. Use the `setup.sh` script:
82+
83+
```console
84+
./setup.sh
85+
```
86+
87+
It will create symbolic links to the required repositories in `../repos/`.
88+
Be sure to run the [top-level `setup.sh` script](../setup.sh).
89+
90+
If you want use a custom variant of repositories (e.g. apply your own patch, make modifications), update it accordingly in the `../repos/` directory.
91+
92+
1. Have your custom setup of repositories in the `workdir/` directory.
93+
Clone, update and customize repositories to your own needs.
94+
95+
## Clean
96+
97+
While not strictly required, it is safest to clean the previous build artifacts:
98+
99+
```console
100+
make distclean
101+
```
102+
103+
## Configure
104+
105+
To configure the kernel, use:
106+
107+
```console
108+
make menuconfig
109+
```
110+
111+
In the console menu interface, choose the target architecture (x86_64 or ARMv8 or ARMv7) and platform (Xen or KVM/QEMU or KVM/Firecracker).
112+
113+
The end result will be the creation of the `.config` configuration file.
114+
115+
## Build
116+
117+
Build the application for the current configuration:
118+
119+
```console
120+
make -j $(nproc)
121+
```
122+
123+
This results in the creation of the `workdir/build/` directory storing the build artifacts.
124+
The unikernel application image file is `workdir/build/wasm-hello_<plat>-<arch>`, where `<plat>` is the platform name (`qemu`, `fc`, `xen`), and `<arch>` is the architecture (`x86_64` or `arm64`).
125+
126+
```console
127+
$(nproc)
128+
```
129+
130+
this uses all availble cpu cores to run the build process.
131+
132+
### Build the Filesystem
133+
134+
The filesystem is to be packed into `initrd.cpio`, an initial ramdisk CPIO file.
135+
Use the command below for that:
136+
137+
```console
138+
rm -f initrd.cpio
139+
./workdir/unikraft/support/scripts/mkcpio initrd.cpio ./rootfs/
140+
```
141+
142+
## Run
143+
144+
Run the resulting image using the corresponding platform tool.
145+
Firecracker requires KVM support.
146+
147+
Xen requires a system with Xen installed.
148+
149+
A successful run will show a message such as the one below:
150+
151+
```text
152+
Booting from ROM..Powered by
153+
o. .o _ _ __ _
154+
Oo Oo ___ (_) | __ __ __ _ ' _) :_
155+
oO oO ' _ `| | |/ / _)' _` | |_| _)
156+
oOo oOO| | | | | (| | | (_) | _) :_
157+
OoOoO ._, ._:_:_,\_._, .__,_:_, \___)
158+
Helene 0.18.0
159+
Hello world!
160+
buf ptr: 0x400002b0
161+
buf: 1234
162+
```
163+
164+
### Run on QEMU/x86_64
165+
166+
```console
167+
qemu-system-x86_64 \
168+
-nographic \
169+
-m 8 \
170+
-cpu max \
171+
-kernel workdir/build/wasm-hello-x86_64 \
172+
-append "wasm-hello-x86_64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- main.wasm" \
173+
-initrd ./initrd.cpio
174+
```
175+
176+
### Run on QEMU/ARM64
177+
178+
```console
179+
qemu-system-aarch64 \
180+
-nographic \
181+
-machine virt \
182+
-m 8 \
183+
-cpu max \
184+
-kernel workdir/build/wasm-hello-arm64 \
185+
-append "wasm-hello-x86_64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- main.wasm" \
186+
-initrd ./initrd.cpio
187+
```
188+
189+
### Run on Firecracker/x86_64
190+
191+
To run with firecraker use this command. this is run with debugging in mind. Feel free to run without it.
192+
193+
the command below is used for dedugging perposes, feel free to omit.
194+
195+
```console
196+
rm -f /tmp/firecracker.log
197+
> /tmp/firecracker.log
198+
```
199+
200+
```console
201+
rm -f /tmp/firecracker.socket
202+
firecracker-x86_64 \
203+
--api-sock /tmp/firecracker.socket \
204+
--config-file ./fc.x86_64.json
205+
```
206+
207+
The user running the above command must be able to use KVM.
208+
Typically this means being part of the `kvm` group.
209+
Otherwise, run the command above as root or prefixed by `sudo`.
210+
211+
### Run on Firecracker/ARM64
212+
213+
the command below is used for dedugging perposes, feel free to omit.
214+
215+
```console
216+
rm -f /tmp/firecracker.log
217+
> /tmp/firecracker.log
218+
```
219+
220+
```console
221+
rm -f firecracker.socket
222+
firecracker-aarch64 --config-file fc.arm64.json --api-sock firecracker.socket
223+
```
224+
225+
The user running the above command must be able to use KVM.
226+
Typically this means being part of the `kvm` group.
227+
Otherwise, run the command above as the `root` account or prefixed by `sudo`.
228+
229+
### Run on Xen/x86_64
230+
231+
```console
232+
sudo xl create -c xen.x86_64.cfg
233+
```
234+
235+
You need use `sudo` or the `root` account to run Xen.
236+
237+
### Run on Xen/ARM64
238+
239+
```console
240+
sudo xl create -c xen.arm64.cfg
241+
```
242+
243+
You need use `sudo` or the `root` account to run Xen.
244+
245+
## Clean Up
246+
247+
Doing a new configuration, or a new build may require cleaning up the configuration and build artifacts.
248+
249+
In order to remove the build artifacts, use:
250+
251+
```console
252+
make clean
253+
```
254+
255+
In order to remove fetched files also, that is the removal of the `workdir/build/` directory, use:
256+
257+
```console
258+
make properclean
259+
```
260+
261+
In order to remove the generated `.config` file as well, use:
262+
263+
```console
264+
make distclean
265+
```
266+
267+
## Customize
268+
269+
WASM Hello is the simplest application to be run with Unikraft in web assembly.
270+
271+
### Update the Unikraft Core Code
272+
273+
If updating the Unikraft core code in the `./workdir/unikraft/` directory, you then go through the [configure](#configure), [build](#build) and [run](#run) steps.
274+
275+
### Enable Debug Messages
276+
277+
You can customize the ELF Loader build debug messages.
278+
For that, use the ["Configure" step](#configure) to add debug printing option from the [`ukdebug` library](https://github.com/unikraft/unikraft/tree/staging/lib/ukdebug).
279+
Then, build and run again.
280+
You can also enable debug configurations in `/tmp/defconfig`

wasm-hello/defconfigoo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CONFIG_PLAT_KVM=y
2+
CONFIG_KVM_VMM_QEMU=y
3+
CONFIG_ARCH_X86_64=y
4+
CONFIG_LIBVFSCORE=y
5+
CONFIG_LIBVFSCORE_AUTOMOUNT_UP=y
6+
CONFIG_LIBRAMFS=y
7+
CONFIG_LIBUKCPIO=y
8+
# Uncomment for debugging.
9+
#CONFIG_LIBUKDEBUG_PRINTD=y
10+
#CONFIG_LIBUKDEBUG_PRINTK_INFO=y
11+
CONFIG_LIBMUSL=y
12+
CONFIG_LIBLWIP=y
13+
CONFIG_LIBWAMR=y
14+
CONFIG_LIBWAMR_MAIN_FUNCTION=y

wasm-hello/fc.arm64.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"boot-source": {
3+
"kernel_image_path": "workdir/build/wasm-hello_fc-arm64",
4+
"boot_args": "wasm-hello_qemu-x86_64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- main.wasm",
5+
"initrd_path": "initrd.cpio"
6+
},
7+
"drives": [],
8+
"machine-config": {
9+
"vcpu_count": 1,
10+
"mem_size_mib": 32,
11+
"smt": false,
12+
"track_dirty_pages": false
13+
},
14+
"cpu-config": null,
15+
"balloon": null,
16+
"vsock": null,
17+
"logger": {
18+
"log_path": "/tmp/firecracker.log",
19+
"level": "Debug",
20+
"show_level": true,
21+
"show_log_origin": true
22+
},
23+
"metrics": null,
24+
"mmds-config": null,
25+
"entropy": null
26+
}

wasm-hello/fc.x86_64.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"boot-source": {
3+
"kernel_image_path": "workdir/build/wasm-hello_fc-x86_64",
4+
"boot_args": "wasm-hello_qemu-x86_64 vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- /main.wasm",
5+
"initrd_path": "initrd.cpio"
6+
},
7+
"drives": [],
8+
"machine-config": {
9+
"vcpu_count": 1,
10+
"mem_size_mib": 32,
11+
"smt": false,
12+
"track_dirty_pages": false
13+
},
14+
"cpu-config": null,
15+
"balloon": null,
16+
"vsock": null,
17+
"logger": {
18+
"log_path": "/tmp/firecracker.log",
19+
"level": "Debug",
20+
"show_level": true,
21+
"show_log_origin": true
22+
},
23+
"metrics": null,
24+
"mmds-config": null,
25+
"entropy": null
26+
}

wasm-hello/rootfs/main.wasm

1.36 KB
Binary file not shown.

0 commit comments

Comments
 (0)