xref: /cloud-hypervisor/docs/building.md (revision aaf67c9ae4ae5249ae05247f060fd98f3ded5b7f)
1ffc222b2SRob Bradford- [Building Cloud Hypervisor](#building-cloud-hypervisor)
2ffc222b2SRob Bradford  - [Preparation](#preparation)
3ffc222b2SRob Bradford  - [Install prerequisites](#install-prerequisites)
4ffc222b2SRob Bradford  - [Clone and build](#clone-and-build)
5ffc222b2SRob Bradford    - [Containerized builds and tests](#containerized-builds-and-tests)
6ffc222b2SRob Bradford
7ffc222b2SRob Bradford# Building Cloud Hypervisor
8ffc222b2SRob Bradford
9ffc222b2SRob BradfordWe recommend users use the pre-built binaries that are mentioned in the README.md file in the root of the repository. Building from source is only necessary if you wish to make modifications.
10ffc222b2SRob Bradford
11ffc222b2SRob Bradford## Preparation
12ffc222b2SRob Bradford
13ffc222b2SRob BradfordWe create a folder to build and run `cloud-hypervisor` at `$HOME/cloud-hypervisor`
14ffc222b2SRob Bradford
15ffc222b2SRob Bradford```shell
16ffc222b2SRob Bradford$ export CLOUDH=$HOME/cloud-hypervisor
17ffc222b2SRob Bradford$ mkdir $CLOUDH
18ffc222b2SRob Bradford```
19ffc222b2SRob Bradford
20ffc222b2SRob Bradford## Install prerequisites
21ffc222b2SRob Bradford
22ffc222b2SRob BradfordYou need to install some prerequisite packages in order to build and test Cloud
23ffc222b2SRob BradfordHypervisor. Here, all the steps are based on Ubuntu, for other Linux
24ffc222b2SRob Bradforddistributions please replace the package manager and package name.
25ffc222b2SRob Bradford
26ffc222b2SRob Bradford```shell
276835dfa5SHenry Wang# Install basic packages needed. For a package list targeting for more
286835dfa5SHenry Wang# functionalities for example the test, please see resources/Dockerfile.
296835dfa5SHenry Wang$ sudo apt-get update
30*aaf67c9aSRavi kumar Veeramally$ sudo apt install git build-essential m4 bison flex uuid-dev qemu-utils musl-tools
31ffc222b2SRob Bradford# Install rust tool chain
32ffc222b2SRob Bradford$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
33ffc222b2SRob Bradford# If you want to build statically linked binary please add musl target
346835dfa5SHenry Wang$ rustup target add x86_64-unknown-linux-musl  # x86-64
356835dfa5SHenry Wang$ rustup target add aarch64-unknown-linux-musl # AArch64
36ffc222b2SRob Bradford```
37ffc222b2SRob Bradford
38ffc222b2SRob Bradford## Clone and build
39ffc222b2SRob Bradford
40ffc222b2SRob BradfordFirst you need to clone and build the Cloud Hypervisor repository:
41ffc222b2SRob Bradford
42ffc222b2SRob Bradford```shell
43ffc222b2SRob Bradford$ pushd $CLOUDH
44ffc222b2SRob Bradford$ git clone https://github.com/cloud-hypervisor/cloud-hypervisor.git
45ffc222b2SRob Bradford$ cd cloud-hypervisor
46ffc222b2SRob Bradford$ cargo build --release
47ffc222b2SRob Bradford
48ffc222b2SRob Bradford# We need to give the cloud-hypervisor binary the NET_ADMIN capabilities for it to set TAP interfaces up on the host.
49ffc222b2SRob Bradford$ sudo setcap cap_net_admin+ep ./target/release/cloud-hypervisor
50ffc222b2SRob Bradford
51ffc222b2SRob Bradford# If you want to build statically linked binary
526835dfa5SHenry Wang$ cargo build --release --target=x86_64-unknown-linux-musl --all  # x86-64
536835dfa5SHenry Wang$ cargo build --release --target=aarch64-unknown-linux-musl --all # AArch64
54ffc222b2SRob Bradford$ popd
55ffc222b2SRob Bradford```
56ffc222b2SRob Bradford
57ffc222b2SRob BradfordThis will build a `cloud-hypervisor` binary under
58ffc222b2SRob Bradford`$CLOUDH/cloud-hypervisor/target/release/cloud-hypervisor`.
59ffc222b2SRob Bradford
60ffc222b2SRob Bradford### Containerized builds and tests
61ffc222b2SRob Bradford
62ffc222b2SRob BradfordIf you want to build and test Cloud Hypervisor without having to install all the
63ffc222b2SRob Bradfordrequired dependencies (The rust toolchain, cargo tools, etc), you can also use
64ffc222b2SRob BradfordCloud Hypervisor's development script: `dev_cli.sh`. Please note that upon its
65ffc222b2SRob Bradfordfirst invocation, this script will pull a fairly large container image.
66ffc222b2SRob Bradford
67ffc222b2SRob BradfordFor example, to build the Cloud Hypervisor release binary:
68ffc222b2SRob Bradford
69ffc222b2SRob Bradford```shell
70ffc222b2SRob Bradford$ pushd $CLOUDH
71ffc222b2SRob Bradford$ cd cloud-hypervisor
72ffc222b2SRob Bradford$ ./scripts/dev_cli.sh build --release
73ffc222b2SRob Bradford```
74ffc222b2SRob Bradford
75ffc222b2SRob BradfordWith `dev_cli.sh`, one can also run the Cloud Hypervisor CI locally. This can be
76ffc222b2SRob Bradfordvery convenient for debugging CI errors without having to fully rely on the
77ffc222b2SRob BradfordCloud Hypervisor CI infrastructure.
78ffc222b2SRob Bradford
79ffc222b2SRob BradfordFor example, to run the Cloud Hypervisor unit tests:
80ffc222b2SRob Bradford
81ffc222b2SRob Bradford```shell
82ffc222b2SRob Bradford$ ./scripts/dev_cli.sh tests --unit
83ffc222b2SRob Bradford```
84ffc222b2SRob Bradford
85ffc222b2SRob BradfordRun the `./scripts/dev_cli.sh --help` command to view all the supported
86ffc222b2SRob Bradforddevelopment script commands and their related options.
87