cloud-hypervisor - tycoon3
https://github.com/cloud-hypervisor/cloud-hypervisor#containerized-builds-and-tests
apt install rustc -y
Command \'rustup\' not found, but can be installed with:
root@cloud:~/hyper# rustup target add aarch64-unknown-linux-musl Command \'rustup\' not found, but can be installed with: snap install rustup root@cloud:~/hyper# snap install rustup error: This revision of snap "rustup" was published using classic confinement and thus may perform arbitrary system changes outside of the security sandbox that snaps are usually confined to, which may put your system at risk. If you understand and want to proceed repeat the command including --classic. root@cloud:~/hyper# rustup target add aarch64-unknown-linux-musl Command \'rustup\' not found, but can be installed with: snap install rustup root@cloud:~/hyper# ls $HOME/.cargo/bin cargo cargo-clippy cargo-fmt cargo-miri clippy-driver rls rustc rustdoc rustfmt rust-gdb rust-lldb rustup root@cloud:~/hyper#
root@cloud:~/hyper# cat ~/.profile # ~/.profile: executed by Bourne-compatible login shells. if [ "$BASH" ]; then if [ -f ~/.bashrc ]; then . ~/.bashrc fi fi mesg n || true export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig export PATH="$HOME/.cargo/bin:$PATH" root@cloud:~/hyper#
保存后,使用命令source .bashrc生效。 #source .bashrc 注意: /etc/profile和/etc/bashrc针对所有用户、.bashrc和.profile针对当前用户 profile登录有效、bashrc执行bash命令行时有效
root@cloud:~/hyper# rustc -V rustc 1.43.0 root@cloud:~/hyper# cargo -V cargo 1.43.0 root@cloud:~/hyper#
重新登录可以解决
Command \'rustup\' not found, but can be installed with:
在树莓派上玩转 Rust :交叉编译
https://zhuanlan.zhihu.com/p/92330365
[root@bogon cloud-hypervisor]# cargo build --release bash: cargo: command not found... [root@bogon cloud-hypervisor]# cd .. [root@bogon hyper]# curl --proto \'=https\' --tlsv1.2 -sSf https://sh.rustup.rs | sh
[root@bogon ~]# rustc -V rustc 1.47.0 (18bf6b4f0 2020-10-07) [root@bogon ~]# c
[root@bogon ~]# rustup target add aarch64-linux-musl-gcc $<2>error: $<2>toolchain \'stable-aarch64-unknown-linux-gnu\' does not contain component \'rust-std\' for target \'aarch64-linux-musl-gcc\' [root@bogon ~]# rustup target add aarch64-unknown-linux-musl $<2>info: $<2>downloading component \'rust-std\' for \'aarch64-unknown-linux-musl\' $<2>info: $<2>Retrying download for \'https://static.rust-lang.org/dist/2020-10-08/rust-std-1.47.0-aarch64-unknown-linux-musl.tar.xz\' 15.8 MiB / 15.8 MiB (100 %) 3.6 MiB/s in 10s ETA: 0s $<2>info: $<2>installing component \'rust-std\' for \'aarch64-unknown-linux-musl\' $<2>info: $<2>Defaulting to 500.0 MiB unpack ram 15.8 MiB / 15.8 MiB (100 %) 9.2 MiB/s in 1s ETA: 0s [root@bogon ~]#
yum -y install libfdt-devel.aarch64
[root@bogon cloud-hypervisor]# cargo build --no-default-features --features mmio,kvm error: Package `cloud-hypervisor v0.10.0 (/data2/hyper/cloud-hypervisor)` does not have these features: `mmio` [root@bogon cloud-hypervisor]# cargo build --no-default-features --features kvm
[root@bogon cloud-hypervisor]# cargo build --no-default-features --features kvm Compiling cloud-hypervisor v0.10.0 (/data2/hyper/cloud-hypervisor) Finished dev [unoptimized + debuginfo] target(s) in 5.68s [root@bogon cloud-hypervisor]#
[root@bogon cloud-hypervisor]# ./scripts/dev_cli.sh build --release Unable to find image \'cloudhypervisor/dev:latest\' locally latest: Pulling from cloudhypervisor/dev 296c9ad75bee: Pull complete c0533d139302: Pull complete 3c11bb34abc8: Pull complete e7770175a944: Pull complete a7bbebc0ffb0: Pull complete 661ced4a1441: Pull complete 03cd8484d4f9: Pull complete 90bdf7d261d0: Pull complete 041a536ee0d2: Pull complete Digest: sha256:9ca26cba7039b48a1e40bc1274034f29dc206775f608b7ce1f0082a4e84061f4 Status: Downloaded newer image for cloudhypervisor/dev:latest docker: Error response from daemon: OCI runtime create failed: unable to retrieve OCI runtime error (open /run/containerd/io.containerd.runtime.v1.linux/moby/ee50598f82402162a297f6ea565b43be23f529ee45b2af1cd717c35380d92cf4/log.json: no such file or directory): fork/exec /usr/local/bin/kata-runtime: no such file or directory: unknown. [root@bogon cloud-hypervisor]#
root@cloud:~/hyper/cloud-hypervisor# ./scripts/dev_cli.sh build --release Unable to find image \'cloudhypervisor/dev:latest\' locally latest: Pulling from cloudhypervisor/dev 296c9ad75bee: Already exists c0533d139302: Already exists 3c11bb34abc8: Already exists e7770175a944: Pull complete a7bbebc0ffb0: Pull complete 661ced4a1441: Pull complete 03cd8484d4f9: Pull complete 90bdf7d261d0: Pull complete 041a536ee0d2: Pull complete Digest: sha256:9ca26cba7039b48a1e40bc1274034f29dc206775f608b7ce1f0082a4e84061f4 Status: Downloaded newer image for cloudhypervisor/dev:latest Updating git repository `https://github.com/cloud-hypervisor/kvm-ioctls` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Updating git repository `https://github.com/cloud-hypervisor/kvm-bindings` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Updating git repository `https://github.com/cloud-hypervisor/vm-memory` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Updating git repository `https://github.com/cloud-hypervisor/vhost` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Updating crates.io index Updating git repository `https://github.com/rust-vmm/linux-loader` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Updating git repository `https://github.com/cloud-hypervisor/vfio-ioctls` Updating git repository `https://github.com/firecracker-microvm/firecracker` Updating git repository `https://github.com/firecracker-microvm/micro-http` Updating git submodule `https://github.com/rust-vmm/rust-vmm-ci.git` Downloading crates ... Downloaded rand v0.4.6 Downloaded thiserror-impl v1.0.21 Downloaded url v2.1.1 Downloaded rand_chacha v0.2.2 Downloaded proc-macro-nested v0.1.6 Downloaded syn v1.0.48 Downloaded ryu v1.0.5 Downloaded lazy_static v1.4.0 Downloaded strsim v0.8.0 Downloaded signal-hook-registry v1.2.1 Downloaded proc-macro-hack v0.5.19 Downloaded ansi_term v0.11.0 Downloaded futures-task v0.3.7 Downloaded futures-macro v0.3.7 Downloaded futures-util v0.3.7 Downloaded pin-utils v0.1.0 Downloaded textwrap v0.11.0 Downloaded unicode-width v0.1.8 Downloaded futures-core v0.3.7 Downloaded atty v0.2.14 Downloaded thiserror v1.0.21 Downloaded remove_dir_all v0.5.3 Downloaded tempfile v3.1.0 Downloaded unicode-xid v0.2.1 Downloaded vec_map v0.8.2 Downloaded tinyvec v0.3.4 Downloaded vmm-sys-util v0.6.1 Downloaded serde_derive v1.0.117 Downloaded unicode-bidi v0.3.4 Downloaded epoll v4.3.1 Downloaded anyhow v1.0.33 Downloaded futures-sink v0.3.7 Downloaded futures v0.3.7 Downloaded byteorder v1.3.4 Downloaded bitflags v1.2.1 Downloaded term_size v0.3.2 Downloaded unicode-normalization v0.1.13 Downloaded arc-swap v0.4.7 Downloaded tempdir v0.3.7 Downloaded rand_core v0.5.1 Downloaded rand v0.7.3 Downloaded quote v1.0.7 Downloaded proc-macro2 v1.0.24 Downloaded pin-project-internal v1.0.1 Downloaded num_cpus v1.13.0 Downloaded slab v0.4.2 Downloaded signal-hook v0.1.16 Downloaded once_cell v1.4.1 Downloaded libc v0.2.80 Downloaded clap v2.33.3 Downloaded itoa v0.4.6 Downloaded futures-io v0.3.7 Downloaded futures-executor v0.3.7 Downloaded serde_json v1.0.59 Downloaded idna v0.2.0 Downloaded serde v1.0.117 Downloaded pin-project v1.0.1 Downloaded log v0.4.11 Downloaded cfg-if v0.1.10 Downloaded futures-channel v0.3.7 Downloaded ppv-lite86 v0.2.9 Downloaded percent-encoding v2.1.0 Downloaded memchr v2.3.3 Downloaded matches v0.1.8 Downloaded getrandom v0.1.15 Downloaded vfio-bindings v0.2.0 Downloaded remain v0.2.2 Downloaded io-uring v0.4.0 Downloaded virtio-bindings v0.1.0 Compiling libc v0.2.80 Compiling proc-macro2 v1.0.24 Compiling unicode-xid v0.2.1 Compiling syn v1.0.48 Compiling bitflags v1.2.1 Compiling serde v1.0.117 Compiling serde_derive v1.0.117 Compiling cfg-if v0.1.10 Compiling arc-swap v0.4.7 Compiling ryu v1.0.5 Compiling log v0.4.11 Compiling serde_json v1.0.59 Compiling itoa v0.4.6 Compiling byteorder v1.3.4 Compiling anyhow v1.0.33 Compiling getrandom v0.1.15 Compiling virtio-bindings v0.1.0 Compiling ppv-lite86 v0.2.9 Compiling io-uring v0.4.0 Compiling unicode-width v0.1.8 Compiling strsim v0.8.0 Compiling vec_map v0.8.2 Compiling ansi_term v0.11.0 Compiling arch_gen v0.1.0 (/cloud-hypervisor/arch_gen) Compiling remove_dir_all v0.5.3 Compiling proc-macro-hack v0.5.19 Compiling proc-macro-nested v0.1.6 Compiling memchr v2.3.3 Compiling option_parser v0.1.0 (/cloud-hypervisor/option_parser) Compiling futures-core v0.3.7 Compiling tinyvec v0.3.4 Compiling once_cell v1.4.1 Compiling futures-sink v0.3.7 Compiling matches v0.1.8 Compiling futures-io v0.3.7 Compiling pin-utils v0.1.0 Compiling slab v0.4.2 Compiling percent-encoding v2.1.0 Compiling lazy_static v1.4.0 Compiling api_client v0.1.0 (/cloud-hypervisor/api_client) Compiling unicode-bidi v0.3.4 Compiling futures-channel v0.3.7 Compiling futures-task v0.3.7 Compiling unicode-normalization v0.1.13 Compiling quote v1.0.7 Compiling term_size v0.3.2 Compiling atty v0.2.14 Compiling textwrap v0.11.0 Compiling vm-memory v0.3.0 (https://github.com/cloud-hypervisor/vm-memory?branch=ch#6f2f7107) Compiling epoll v4.3.1 Compiling seccomp v0.1.0 (https://github.com/firecracker-microvm/firecracker?tag=v0.22.0#cc538763) Compiling num_cpus v1.13.0 Compiling signal-hook-registry v1.2.1 Compiling rand v0.4.6 Compiling clap v2.33.3 Compiling micro_http v0.1.0 (https://github.com/firecracker-microvm/micro-http#40309b72) Compiling signal-hook v0.1.16 Compiling rand_core v0.5.1 Compiling idna v0.2.0 Compiling rand_chacha v0.2.2 Compiling rand v0.7.3 Compiling linux-loader v0.2.0 (https://github.com/rust-vmm/linux-loader#3cf96c53) Compiling acpi_tables v0.1.0 (/cloud-hypervisor/acpi_tables) Compiling tempdir v0.3.7 Compiling url v2.1.1 Compiling tempfile v3.1.0 Compiling cloud-hypervisor v0.10.0 (/cloud-hypervisor) Compiling thiserror-impl v1.0.21 Compiling pin-project-internal v1.0.1 Compiling remain v0.2.2 Compiling futures-macro v0.3.7 Compiling thiserror v1.0.21 Compiling pin-project v1.0.1 Compiling futures-util v0.3.7 Compiling futures-executor v0.3.7 Compiling futures v0.3.7 Compiling vmm-sys-util v0.6.1 Compiling vm-virtio v0.1.0 (/cloud-hypervisor/vm-virtio) Compiling vm-migration v0.1.0 (/cloud-hypervisor/vm-migration) Compiling kvm-bindings v0.2.0 (https://github.com/cloud-hypervisor/kvm-bindings?branch=ch#2164129f) Compiling vhost v0.1.0 (https://github.com/cloud-hypervisor/vhost?branch=ch#6fc980bf) Compiling net_gen v0.1.0 (/cloud-hypervisor/net_gen) Compiling vm-device v0.1.0 (/cloud-hypervisor/vm-device) Compiling block_util v0.1.0 (/cloud-hypervisor/block_util) Compiling vfio-bindings v0.2.0 Compiling qcow v0.1.0 (/cloud-hypervisor/qcow) Compiling devices v0.1.0 (/cloud-hypervisor/devices) Compiling net_util v0.1.0 (/cloud-hypervisor/net_util) Compiling vhost_user_backend v0.1.0 (/cloud-hypervisor/vhost_user_backend) Compiling kvm-ioctls v0.5.0 (https://github.com/cloud-hypervisor/kvm-ioctls?branch=ch#64427395) Compiling vhost_user_block v0.1.0 (/cloud-hypervisor/vhost_user_block) Compiling vhost_user_net v0.1.0 (/cloud-hypervisor/vhost_user_net) Compiling hypervisor v0.1.0 (/cloud-hypervisor/hypervisor) Compiling vfio-ioctls v0.1.0 (https://github.com/cloud-hypervisor/vfio-ioctls?branch=ch#49cc3626) Compiling arch v0.1.0 (/cloud-hypervisor/arch) Compiling vm-allocator v0.1.0 (/cloud-hypervisor/vm-allocator) Compiling pci v0.1.0 (/cloud-hypervisor/pci) Compiling virtio-devices v0.1.0 (/cloud-hypervisor/virtio-devices) Compiling vmm v0.1.0 (/cloud-hypervisor/vmm) Compiling vhost_user_fs v0.1.0 (/cloud-hypervisor/vhost_user_fs) Finished release [optimized] target(s) in 5m 32s [Cloud Hypervisor] Binaries placed under /root/hyper/cloud-hypervisor/build/cargo_target/aarch64-unknown-linux-gnu/release
先安装kata 运行时
[root@bogon cloud-hypervisor]# ./scripts/dev_cli.sh build --release docker: Error response from daemon: OCI runtime create failed: /usr/share/defaults/kata-containers/configuration-clh.toml: file /usr/bin/cloud-hypervisor does not exist: unknown. [root@bogon cloud-hypervisor]# cd /usr/share/defaults/kata-containers/
[root@bogon hyper]# find ./ -name cloud-hypervisor
./cloud-hypervisor
./cloud-hypervisor/target/debug/cloud-hypervisor
[root@bogon hyper]# ln -s cloud-hypervisor/target/debug/cloud-hypervisor /usr/bin/cloud-hypervisor
[root@bogon hyper]#
[root@bogon defaults]# kata-runtime kata-check ERRO[0000] /usr/share/defaults/kata-containers/configuration-clh.toml: file /usr/share/kata-containers/vmlinux-virtiofs.container does not exist arch=arm64 name= pid=13877 source=runtime /usr/share/defaults/kata-containers/configuration-clh.toml: file /usr/share/kata-containers/vmlinux-virtiofs.container does not exist [root@bogon defaults]#
https://github.com/cloud-hypervisor/linux
[root@bogon hyper]# git clone –depth 1 https://github.com/cloud-hypervisor/linux.git -b virtio-fs-virtio-iommu-5.8-rc4 linux-cloud-hypervisor
Cloning into \’linux-cloud-hypervisor\’…
[root@bogon linux-cloud-hypervisor]# find ../cloud-hypervisor -name linux-config* ../cloud-hypervisor/resources/linux-config-aarch64 ../cloud-hypervisor/resources/linux-config-x86_64 [root@bogon linux-cloud-hypervisor]#
= note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-nostartfiles" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o" "-L" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "-L" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained" "/root/hyper/cloud-hypervisor/target/aarch64-unknown-linux-musl/release/deps/vhost_user_block-bd3dc1d0902e68d3.vhost_user_block.1oaasaby-cgu.2.rcgu.o" "-o" "/root/hyper/cloud-hypervisor/target/aarch64-unknown-linux-musl/release/deps/vhost_user_block-bd3dc1d0902e68d3" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/root/hyper/cloud-hypervisor/target/aarch64-unknown-linux-musl/release/deps" "-L" "/root/hyper/cloud-hypervisor/target/release/deps" "-L" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/tmp/rustcPhPvHA/libunwind-7a7db0d68fb707fa.rlib" "/tmp/rustcPhPvHA/liblibc-f904a5ce7e6fe846.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-64ce58124784d85c.rlib" "-Wl,-Bdynamic" "/root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtn.o" = note: /tmp/rustcPhPvHA/liblibc-f904a5ce7e6fe846.rlib(vfprintf.lo): In function `fmt_fp\': /build/musl-1.1.24/src/stdio/vfprintf.c:210: undefined reference to `__addtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:225: undefined reference to `__multf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:228: undefined reference to `__subtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:229: undefined reference to `__addtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:232: undefined reference to `__addtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:233: undefined reference to `__subtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:246: undefined reference to `__subtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:246: undefined reference to `__multf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:268: undefined reference to `__multf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:275: undefined reference to `__subtf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:275: undefined reference to `__multf3\' /build/musl-1.1.24/src/stdio/vfprintf.c:331: undefined reference to `__addtf3\' /tmp/rustcPhPvHA/liblibc-f904a5ce7e6fe846.rlib(frexpl.lo): In function `frexpl\': /build/musl-1.1.24/src/math/frexpl.c:16: undefined reference to `__multf3\' collect2: error: ld returned 1 exit status error: aborting due to previous error error: build failed root@cloud:~/hyper/cloud-hypervisor#
“Hello world”几乎是学习任何语言的第一个程序了,学习Rust页不例外。在开始之前,请你先确保在自己电脑上安装了Rust。如果你还没有安装的话,我们一起安装一遍,不过,我使用的是Linux操作系统,其他系统可能略有不同,这个你需要注意。Rust 使用 Rustup 安装,你也可以查看更详细的文档:Rustup。
首先,打开一个终端,输入:
$ curl https://sh.rustup.rs -sSf | sh
这行代码会帮助我们下载一个小脚本并运行它,由于网络的原因,你需要耐心等待,多尝试几次, 直到出现类似的字样:
……
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
到这里,离成功就不远了。接着,跟据提示,输入“1”并按下回车,就会开始下载一些组件了,你会看到如下字样:
info: syncing channel updates for \'stable-x86_64-unknown-linux-gnu\'
info: latest update on 2018-08-02, rust version 1.28.0 (9634041f0 2018-07-30)
info: downloading component \'rustc\'
20.1 MiB / 68.6 MiB ( 29 %) 493.4 KiB/s ETA: 101 s
由于网络原因,你还需要等待一阵子,或者可以考虑其他办法。直到你看到如下输出:
Rust is installed now. Great!
To get started you need Cargo\'s bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done automatically.
https://doc.rust-lang.org/cargo/index.html
To configure your current shell run source $HOME/.cargo/env
恭喜你,Rust 已经安装完成了,包括自带的 Cargo 包管理器。我们还需要将$HOME/.cargo/bin
添加到环境变量“PATH”中,编辑 $HOME/.profile
文件,在最后面追加export PATH="$HOME/.cargo/bin:$PATH"
就可以了,不过,如果是Ubuntu等系统,Rustup会自动给你添加环境变量。
添加完环境变量后,你需要重启一下系统,或者注销后重新登录。我们打开一个终端并输入:
$ rustc -V
rustc 1.28.0 (9634041f0 2018-07-30)
那说明没有什么问题。
我们进行第一个程序。用你喜欢的编辑器创建一个文件:
$ vim hello_world.rs
然后在文件中输入:
fn main() {
println!("hello world!");
}
编译并运行:
$ rustc hello_world.rs
$ ./hello_world
hello world!
也可以使用Cargo:
$ cargo new hello_world --bin
Created binary (application) `hello_world` project
$ cd hello_world
注意后面的 --bin
参数,这将告诉Cargo创建一个可执行程序,而不是库。
我们可以看到,cargo 自动创建了 src
目录和Cargo.toml
文件(类似于nodejs的package.js文件),实际上,cargo还初始化了Git仓库并创建了.gitignore
文件。因此在这之前,你应该先安装并配置好Git。
我们打开Cargo.toml
文件:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["xxx <xxx@xxx.com>"]
[dependencies]
[package]
片段中,包含了项目的名称、版本和从Git配置中读取的作者和作者邮箱。[dependencies]
片段中包含了项目的依赖。今后,我们还需要修改Cargo.toml
文件。你还可以去查看 Cargo 的文档。
我们进入src
目录中的main.rs
文件:
cd src
vim main.rs
Cargo 已经帮我们写好了“hello world!”, 我们可以直接运行:
$ cargo run // 编译并运行
Compiling hello_world v0.1.0 (file:///home/xxx/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
Running `/home/xxx/hello_world/target/debug/hello_world`
Hello, world!
或者也可以先编译,后运行:
$ cargo build // 编译
Compiling hello_world v0.1.0 (file:///home/xxx/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s
$ ./target/debug/hello_world 运行
Hello, world!
我们会发现,项目目录下又多出了一个Cargo.lock
文件和target
目录,Cargo.lock
文件由Cargo维护,不需要手动修改,target
目录用于存放可执行文件和一些临时文件,也不需要我们手动去维护。部分临时文件是可以复用的,以提高下次编译时的编译速度。
我们执行cargo run
或cargo build
时,默认生成的可执行文件存放在target/debug
目录下,虽然编译速度更快,但运行较缓慢,还包含了一些调试信息,导致生成的可执行文件体积也稍大。我们可以添加--release
参数来开启编译器优化,会在target/release
目录下生成可执行文件,这时候编译速度较慢,但是程序的运行速度会更快。
还有一个常用命令是 cargo check
,只检查代码是否可以编译而不生成可执行文件,因而速度比cargo build
还要快。这里要说明的是,Rust的编译器非常强大,大部分情况下,只要能够编译通过的程序,运行是没有什么问题的。
再来解释以下这段代码:
fn main() {
println!("hello world!");
}
第一行,fn
是Rust中的一个关键字,用于定义一个函数,main
是Rust的入口函数。要注意,函数体必须包裹在{}
中,不可省略。对换行和缩进也没有要求;
第二行,println!
调用了一个“宏”而不是函数,注意“!”,调用函数不需要“!”,当看到“!”的时候就意味着调用的是一个“宏”而不是普通的函数,在后面会介绍“宏”。”hello world!”是一个字符串,作为参数传递给println!
,println!
负责将“hello world!”打印到屏幕上。
我们的目的达到了没有?然而并没有,我们需要在浏览器上输出“hello world!”,不仅仅是在终端上。不管你在此之前是否听说或了解过HTTP协议和TCP协议,目前主流浏览器通常是先跟HTTP Server建立TCP连接,再通过TCP向HTTP Server发送HTTP请求。HTTPS又在TCP和HTTP中间加了一层SSL协议,在发送HTTP请求之前,还需要进行SSL协议“握手”,我们暂且不谈HTTPS。
Rust标准库中没有HTTP Server的实现,(Golang的同学请不要笑,Golang的标准库杂而不精,等后面你就知道Rust是多么好用了),没关系,接下来我们实现一个玩具级别的。
请看下一节!
Cloud-hypervisor on Raspberry Pi 4B
Cloud-hypervisor is a rising star among Virtual Machine Monitors (VMM).
As its name indicates, Cloud-hypervisor aims to be running in computing centers to bear cloud workloads. Typically it should be running on servers. So Cloud-hypervisor is not interested in supporting all sorts of legacy devices (like what QEMU has done), but focuses on the most prevailing virtual devices — Virtio.
Since version 0.8.0, Cloud-hypervisor begins to support Arm64. In that version, Arm64 support was in very early stage, only a small set of features were enabled. A virtual machine on Arm64 can use only very limited sorts of devices, and they have to be MMIO.
When this article is written, Arm64 support is quite better. PCI is enabled, so is Vhost-user things like Vhost-user-block and Vhost-user-net.
If you are interested in playing Cloud-hypervisor on Arm64 server, the easiest way is using a Raspberry Pi 4B.
Raspberry Pi 4B is 64-bit, the memory extends up to 4GiB (depending on your choice when you bought it). That’s enough to run a small virtual machine.
Before everything, you need to equip your Pi with a 64-bit operating system. Although the hardware of your Pi supports 64-bit, but the official RaspBian system is a 32-bit one, it doesn’t work with Cloud-hypervisor. Ubuntu Server is a good choice, and it supports Pi.
Preparation
Beside some very basic development packages like build-essential and git, Cloud-hypervisor requires an extra package: libfdt. Install it like:
sudo apt-get update
sudo apt-get install libfdt-dev
Install Cargo
Cloud-hypervisor is written in Rust. To build Rust source code you need to install Cargo.
Cargo is the Rust package manager. Besides compiling your code, Cargo also downloads your Rust package’s dependencies, makes distributable packages and etc.
Follow this link to know more, or simply use the command to install:
curl https://sh.rustup.rs -sSf | sh
Build
For Virtio devices, you can choose MMIO or PCI as transport option. But on Raspberry Pi 4B, MMIO is the only option. Because PCI devices requires GICv3-ITS for MSI messaging. GICv3-ITS is very common in modern servers, on RPi4b only GICv2 is available.
cargo build — no-default-features — features mmio,kvm
Containerized build
If you want to build and test Cloud Hypervisor without having to install all the required dependencies, you can also turn to the development script: scripts/dev_cli.sh.
To build Cloud-hypervisor in the container: (The default option for Virtio transport is MMIO.)
./scripts/dev_cli.sh build
It will take quite a several minutes if it’s the first time you run this command. A container image will be built from a local Dockerfile. A lot of things will be installed and configured in the image.
Kernel & Root-FS
Download kernel binary and rootfs image.
wget https://s3.amazonaws.com/spec.ccfc.min/img/aarch64/ubuntu_with_ssh/fsfiles/xenial.rootfs.ext4 -O rootfs.ext4
wget https://s3.amazonaws.com/spec.ccfc.min/img/aarch64/ubuntu_with_ssh/kernel/vmlinux.bin -O kernel.bin
The kernel and rootfs are originally made for Firecracker, another VMM for very small VMs. The kernel was carefully tailored, the rootfs was also very small.
Run
Assuming you have built Cloud-hypervisor with the development container, a VM can be started with command:
sudo build/cargo_target/aarch64-unknown-linux-gnu/debug/cloud-hypervisor --kernel kernel.bin --disk path=rootfs.ext4 --cmdline "keep_bootcon console=hvc0 reboot=k panic=1 pci=off root=/dev/vda rw" --cpus boot=4 --memory size=512M --serial file=serial.log --log-file log.log -vvv
If the build was done out of the container, replace the binary path with target/debug/cloud-hypervisor.
Login
The default user/passwd is root/root. Login and enjoy the small virtual machine.