|
| 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` |
0 commit comments