1# Building Crosvm on Linux 2 3This page describes how to build and develop crosvm on linux. If you are targeting ChromeOS, please 4see [Integration](../integration/index.md) 5 6## Checking out 7 8Obtain the source code via git clone. 9 10```sh 11git clone https://chromium.googlesource.com/crosvm/crosvm 12``` 13 14## Setting up the development environment 15 16Crosvm uses submodules to manage external dependencies. Initialize them via: 17 18```sh 19git submodule update --init 20``` 21 22It is recommended to enable automatic recursive operations to keep the submodules in sync with the 23main repository (but do not push them, as that can conflict with `repo`): 24 25```sh 26git config submodule.recurse true 27git config push.recurseSubmodules no 28``` 29 30Crosvm development best works on Debian derivatives. We provide a script to install the necessary 31packages on Debian, Ubuntu or gLinux: 32 33```sh 34./tools/install-deps 35``` 36 37For other systems, please see below for instructions on 38[Using the development container](#using-the-development-container). 39 40### Using the development container 41 42We provide a Debian container with the required packages installed. With 43[Podman](https://podman.io/getting-started/installation) or 44[Docker](https://docs.docker.com/get-docker/) installed, it can be started with: 45 46```sh 47./tools/dev_container 48``` 49 50The container image is big and may take a while to download when first used. **Once started, you can 51follow all instructions in this document within the container shell.** 52 53Instead of using the interactive shell, commands to execute can be provided directly: 54 55```sh 56./tools/dev_container cargo build 57``` 58 59Note: The container and build artifacts are preserved between calls to `./tools/dev_container`. If 60you wish to start fresh, use the `--clean` flag. 61 62## Building a binary 63 64If you simply want to try crosvm, run `cargo build`. Then the binary is generated at 65`./target/debug/crosvm`. Now you can move to [Example Usage](../running_crosvm/example_usage.md). 66 67If you want to enable [additional features](../running_crosvm/features.md), use the `--features` 68flag. (e.g. `cargo build --features=gdb`) 69 70## Development 71 72### Running all tests 73 74Crosvm's integration tests have special requirements for execution (see 75[Testing](../testing/index.md)), so we use a special test runner. By default it will only execute 76unit tests: 77 78```sh 79./tools/run_tests 80``` 81 82To execute integration tests as well, you need to specify a device-under-test (DUT). The most 83reliable option is to use the built-in VM for testing: 84 85```sh 86./tools/run_tests --dut=vm 87``` 88 89However, you can also use your local host directly. Your mileage may vary depending on your host 90kernel version and permissions. 91 92```sh 93./tools/run_tests --dut=host 94``` 95 96Since we have some architecture-dependent code, we also have the option of running unit tests for 97aarch64, armhf, riscv64, and windows (mingw64). These will use an emulator to execute (QEMU or 98wine): 99 100```sh 101./tools/run_tests --platform=aarch64 102./tools/run_tests --platform=armhf 103./tools/run_tests --platform=riscv64 104./tools/run_tests --platform=mingw64 105``` 106 107When working on a machine that does not support cross-compilation (e.g. gLinux), you can use the dev 108container to build and run the tests. 109 110```sh 111./tools/dev_container ./tools/run_tests --platform=aarch64 112``` 113 114### Presubmit checks 115 116To verify changes before submitting, use the `presubmit` script. To ensure the toolchains for all 117platforms are available, it is recommended to run it inside the dev container. 118 119```sh 120./tools/dev_container ./tools/presubmit 121``` 122 123This will run clippy, formatters and runs all tests for all platforms. The same checks will also be 124run by our CI system before changes are merged into `main`. 125 126See `tools/presumit -h` for details on various options for selecting which checks should be run to 127trade off speed and accuracy. 128 129## Cross-compilation 130 131Crosvm is built and tested on x86, aarch64, armhf, and riscv64. Your system needs some setup work to 132be able to cross-comple for other architectures, hence it is recommended to use the 133[development container](#using-the-development-container), which will have everything configured. 134 135Note: Cross-compilation is **not supported on gLinux**. Please use the development container. 136 137### Enable foreign architectures 138 139Your host needs to be set up to allow installation of foreign architecture packages. 140 141On Debian this is as easy as: 142 143```sh 144sudo dpkg --add-architecture arm64 145sudo dpkg --add-architecture armhf 146sudo dpkg --add-architecture riscv64 147sudo apt update 148``` 149 150On ubuntu this is a little harder and needs some 151[manual modifications](https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library) 152of APT sources. 153 154With that enabled, the following scripts will install the needed packages: 155 156```sh 157./tools/install-aarch64-deps 158./tools/install-armhf-deps 159./tools/install-riscv64-deps 160``` 161 162### Configuring wine and mingw64 163 164Crosvm is also compiled and tested on windows. Some limited testing can be done with mingw64 and 165wine on linux machines. Use the provided setup script to install the needed dependencies. 166 167```sh 168./tools/install-mingw64-deps 169``` 170 171### Configure cargo for cross-compilation 172 173Cargo requries additional configuration to support cross-compilation. You can copy the provided 174example config to your cargo configuration: 175 176```sh 177cat .cargo/config.debian.toml >> ${CARGO_HOME:-~/.cargo}/config.toml 178``` 179 180## Known issues 181 182- Devices can't be jailed if `/var/empty` doesn't exist. `sudo mkdir -p /var/empty` to work around 183 this for now. 184- You need read/write permissions for `/dev/kvm` to run tests or other crosvm instances. Usually 185 it's owned by the `kvm` group, so `sudo usermod -a -G kvm $USER` and then log out and back in 186 again to fix this. 187- Some other features (networking) require `CAP_NET_ADMIN` so those usually need to be run as root. 188