-
Notifications
You must be signed in to change notification settings - Fork 35
Rust helloworld app #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BVRazvan
wants to merge
9
commits into
unikraft:main
Choose a base branch
from
BVRazvan:main-rust-app
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
201ceab
added rs project + makefiles + setup
BVRazvan a50a236
modified app in a no-std manner
BVRazvan 957f3f8
removed makefile.uk from gitignore
BVRazvan 6b94bf4
changed output message
BVRazvan eac90af
added readme
BVRazvan aad1e17
fixed bugs regarding Rust target and ignore hash suffix in object file
BVRazvan fc3b46f
updated README
BVRazvan 5d0fe08
add Customize section in README
BVRazvan d2c3367
made sure stable toolchain channel is installed
BVRazvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| [build] | ||
| target = ["x86_64-unknown-none"] |
This file contains hidden or 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,2 @@ | ||
| /workdir/ | ||
| /target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,12 @@ | ||
| [package] | ||
| name = "rs-hello" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [profile.dev] | ||
| panic = "abort" | ||
|
|
||
| [profile.release] | ||
| panic = "abort" | ||
|
|
||
| [dependencies] |
This file contains hidden or 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,13 @@ | ||
| UK_ROOT ?= $(PWD)/workdir/unikraft | ||
| UK_BUILD ?= $(PWD)/workdir/build | ||
| UK_APP ?= $(PWD) | ||
| LIBS_BASE = $(PWD)/workdir/libs | ||
| UK_LIBS ?= | ||
|
|
||
| .PHONY: all | ||
|
|
||
| all: | ||
| @$(MAKE) -C $(UK_ROOT) L=$(UK_LIBS) A=$(UK_APP) O=$(UK_BUILD) | ||
|
|
||
| $(MAKECMDGOALS): | ||
| @$(MAKE) -C $(UK_ROOT) L=$(UK_LIBS) A=$(UK_APP) O=$(UK_BUILD) $(MAKECMDGOALS) |
This file contains hidden or 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,3 @@ | ||
| $(eval $(call addlib,apprshello)) | ||
|
|
||
| APPRSHELLO_OBJS-y += $(APPRSHELLO_BASE)/target/x86_64-unknown-none/debug/deps/rs_hello-*.o |
This file contains hidden or 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,195 @@ | ||
| # Rust Hello on Unikraft | ||
|
|
||
| Build and run a `std` independent Rust Hello program on Unikraft. | ||
| Follow the instructions below to set up, configure, build and run Rust Hello. | ||
| Make sure you installed the [requirements](../README.md#requirements) and the [Rust toolchain channel through Rustup](https://www.rust-lang.org/tools/install). | ||
|
|
||
| ## Quick Setup (aka TLDR) | ||
|
|
||
| For a quick setup, run the commands below. | ||
| Note that you still need to install the [requirements](../README.md#requirements) and the [Rust toolchain](https://www.rust-lang.org/tools/install). | ||
| Before everything, make sure you run the [top-level `setup.sh` script](../setup.sh). | ||
|
|
||
| To build and run the application for `x86_64`, use the commands below: | ||
|
|
||
| ```console | ||
| ./setup.sh | ||
| make distclean | ||
| rustup target add x86_64-unknown-none | ||
| cargo +stable rustc -- --emit=obj | ||
| wget -O /tmp/defconfig https://raw.githubusercontent.com/unikraft/catalog-core/refs/heads/scripts/rs-hello/scripts/defconfig/qemu.x86_64 | ||
| UK_DEFCONFIG=/tmp/defconfig make defconfig | ||
| make -j $(nproc) | ||
| qemu-system-x86_64 -nographic -m 8 -cpu max -kernel workdir/build/rs-hello_qemu-x86_64 | ||
| ``` | ||
|
|
||
| This will configure, build and run the application, resulting in a `Hello from Unikraft!` message being printed. | ||
|
|
||
| Information about every step is detailed below. | ||
|
|
||
| ## Set Up | ||
|
|
||
| Set up the required repositories. | ||
| For this, you have two options: | ||
|
|
||
| 1. Use the `setup.sh` script: | ||
|
|
||
| ```console | ||
| ./setup.sh | ||
| ``` | ||
|
|
||
| It will create symbolic links to the required repositories in `../repos/`. | ||
| Be sure to run the [top-level `setup.sh` script](../setup.sh). | ||
|
|
||
| If you want use a custom variant of repositories (e.g. apply your own patch, make modifications), update it accordingly in the `../repos/` directory. | ||
|
|
||
| 1. Have your custom setup of repositories in the `workdir/` directory. | ||
| Clone, update and customize repositories to your own needs. | ||
|
|
||
| ## Clean | ||
|
|
||
| While not strictly required, it is safest to clean the previous build artifacts: | ||
|
|
||
| ```console | ||
| make distclean | ||
| ``` | ||
|
|
||
| ## Compile Rust source file | ||
|
|
||
| To generate the object file of the Rust source file, use: | ||
|
|
||
| ``` | ||
| rustup target add x86_64-unknown-none | ||
| cargo +stable rustc -- --emit=obj | ||
| ``` | ||
|
|
||
| ## Configure | ||
|
|
||
| To configure the kernel, use: | ||
|
|
||
| ```console | ||
| make menuconfig | ||
| ``` | ||
|
|
||
| In the console menu interface, choose the target architecture (x86_64) and platform (KVM/QEMU). | ||
|
|
||
| The end result will be the creation of the `.config` configuration file. | ||
|
|
||
| ## Build | ||
|
|
||
| Build the application for the current configuration: | ||
|
|
||
| ```console | ||
| make -j $(nproc) | ||
| ``` | ||
|
|
||
| This results in the creation of the `workdir/build/` directory storing the build artifacts. | ||
| The unikernel application image file is `workdir/build/rs-hello_<plat>-<arch>`, where `<plat>` is the platform name (`qemu`), and `<arch>` is the architecture (`x86_64`). | ||
|
|
||
| ### Use a Different Compiler | ||
|
|
||
| If you want to use a different compiler, such as a Clang or a different GCC version, pass the `CC` variable to `make`. | ||
|
|
||
| To build with Clang, use the commands below: | ||
|
|
||
| ```console | ||
| make properclean | ||
| make CC=clang -j $(nproc) | ||
| ``` | ||
|
|
||
| Note that Clang >= 14 is required to build Unikraft. | ||
|
|
||
| To build with another GCC version, use the commands below: | ||
|
|
||
| ```console | ||
| make properclean | ||
| make CC=gcc-<version> -j $(nproc) | ||
| ``` | ||
|
|
||
| where `<version>` is the GCC version, such as `11`, `12`. | ||
|
|
||
| Note that GCC >= 8 is required to build Unikraft. | ||
|
|
||
| ## Run | ||
|
|
||
| Run the resulting image using the corresponding platform tool. | ||
|
|
||
| A successful run will show a message such as the one below: | ||
|
|
||
| ```text | ||
| Booting from ROM..Powered by | ||
| o. .o _ _ __ _ | ||
| Oo Oo ___ (_) | __ __ __ _ ' _) :_ | ||
| oO oO ' _ `| | |/ / _)' _` | |_| _) | ||
| oOo oOO| | | | | (| | | (_) | _) :_ | ||
| OoOoO ._, ._:_:_,\_._, .__,_:_, \___) | ||
| Helene 0.18.0~12072b5f | ||
| Hello from Unikraft! | ||
| ``` | ||
|
|
||
| ### Run on QEMU/x86_64 | ||
|
|
||
| ```console | ||
| qemu-system-x86_64 -nographic -m 8 -cpu max -kernel workdir/build/rs-hello_qemu-x86_64 | ||
| ``` | ||
|
|
||
| ## Clean Up | ||
|
|
||
| Doing a new configuration, or a new build may require cleaning up the configuration and build artifacts. | ||
|
|
||
| In order to remove the build artifacts, use: | ||
|
|
||
| ```console | ||
| make clean | ||
| ``` | ||
|
|
||
| In order to remove fetched files also, that is the removal of the `workdir/build/` directory, use: | ||
|
|
||
| ```console | ||
| make properclean | ||
| ``` | ||
|
|
||
| In order to remove the generated `.config` file as well, use: | ||
|
|
||
| ```console | ||
| make distclean | ||
| ``` | ||
|
|
||
| In order to remove the generated files by Cargo in the `target` directory, use: | ||
|
|
||
| ```console | ||
| cargo clean | ||
| ``` | ||
| ## Customize | ||
|
|
||
| Rust Hello is the simplest application to be run with Unikraft. | ||
| This makes it ideal as a minimal testing ground for new features: it builds fast, it doesn't have dependencies. | ||
|
|
||
| ### Update the Unikraft Core Code | ||
|
|
||
| If updating the Unikraft core code in the `./workdir/unikraft/` directory, you then go through the [configure](#configure), [build](#build) and [run](#run) steps. | ||
|
|
||
| ### Add Other Object Files | ||
|
|
||
| The current configuration use a object file. | ||
| If looking to add another file to the build, update the [`Makefile.uk`](Makefile.uk) file. | ||
|
|
||
| For example, to add a new object file `support.o` to the build, update the [`Makefile.uk`](Makefile.uk) file to: | ||
|
|
||
| ```make | ||
| $(eval $(call addlib,apprshello)) | ||
|
|
||
| APPRSHELLO_OBJS-y += $(APPRSHELLO_BASE)/target/x86_64-unknown-none/debug/deps/rs_hello-*.o | ||
| APPRSHELLO_OBJS-y += $(APPRSHELLO_BASE)/support.o | ||
| ``` | ||
|
|
||
| To include a Rust library, such as `librs_hello.rlib`, update the [`Makefile.uk`](Makefile.uk) file to: | ||
|
|
||
| ```make | ||
| $(eval $(call addlib,apprshello)) | ||
|
|
||
| APPRSHELLO_OBJS-y += $(APPRSHELLO_BASE)/target/x86_64-unknown-none/debug/deps/rs_hello-*.o | ||
| APPRSHELLO_ALIBS-y += $(APPRSHELLO_BASE)/librs_hello.rlib | ||
| ``` | ||
|
|
||
| Then go through the [configure](#configure), [build](#build) and [run](#run) steps. | ||
This file contains hidden or 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,32 @@ | ||
| #!/bin/sh | ||
|
|
||
| check_exists_and_create_symlink() | ||
| { | ||
| path="$1" | ||
|
|
||
| if ! test -d workdir/"$path"; then | ||
| if ! test -d ../repos/"$path"; then | ||
| echo "No directory ../repos/$path. Run the top-level setup.sh script first." 1>&2 | ||
| exit 1 | ||
| fi | ||
| depth=$(echo "$path" | awk -F / '{ print NF }') | ||
| if test "$depth" -eq 1; then | ||
| ln -sfn ../../repos/"$path" workdir/"$path" | ||
| elif test "$depth" -eq 2; then | ||
| ln -sfn ../../../repos/"$path" workdir/"$path" | ||
| else | ||
| echo "Unknown depth of path $path." 1>&2 | ||
| exit 1 | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| if ! test -d workdir; then | ||
| mkdir workdir | ||
| fi | ||
|
|
||
| if ! test -d workdir/libs; then | ||
| mkdir workdir/libs | ||
| fi | ||
|
|
||
| check_exists_and_create_symlink "unikraft" |
This file contains hidden or 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,22 @@ | ||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use core::panic::PanicInfo; | ||
|
|
||
| extern "C" { | ||
| fn uk_console_out(s: *const u8, len: usize); | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "C" fn main() -> () { | ||
| let msg = "Hello from Unikraft!\n"; | ||
| unsafe { | ||
| uk_console_out(msg.as_ptr(), msg.len()); | ||
| } | ||
| } | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_info: &PanicInfo) -> ! { | ||
| loop {} | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get an error here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, can you paste here your output of
rustup showcommand in cli?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so you did a custom installation probably, otherwise stable channel is default (I will explicitly mention it in the readme file) .
As a side note, I matched your toolchain (on local as well as on a vanilla ubuntu image) and it seems that the stable channel gets installed automatically, but I won't take it for granted.
I will squash the commits when everything is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @StefanJum. Did you manage to run the app after my last commit?