Path: blob/main/docs/book/src/running_crosvm/example_usage.md
5394 views
Example Usage
This section will explain how to use a prebuilt Ubuntu image as the guest OS. If you want to prepare a kernel and rootfs by yourself, please see Custom Kernel / Rootfs.
The example code for this guide is available in tools/examples
Run a simple Guest OS (using virt-builder)
To run a VM with crosvm, we need two things: A kernel binary and a rootfs. You can build those yourself or use prebuilt cloud/vm images that some linux distributions provide.
Preparing the guest OS image
One of the more convenient ways to customize these VM images is to use virt-builder from the libguestfs-tools package.
Extract the Kernel (And initrd)
Crosvm directly runs the kernel instead of using the bootloader. So we need to extract the kernel binary from the image. virt-builder has a tool for that:
The kernel binary is going to be saved in the same directory.
Note: Most distributions use an init ramdisk, which is extracted at the same time and needs to be passed to crosvm as well.
Add the user to the kvm group
To run crosvm without sudo, the user should be added to the kvm group in order to obtain the access to the /dev/kvm file. If the user is already in the kvm group, skip this part. Otherwise, execute the command below.
You can check if the user is in the kvm group or not with the following command:
After executing the adduser command above, please logout and log back in to reflect the kvm group.
Launch the VM
With all the files in place, crosvm can be run:
The full source for this example can be executed directly:
The login username will be the username on the host, and it will prompt to decide the password on the first login in the VM.
Add Networking Support
Networking support is easiest set up with a TAP device on the host, which can be done with:
The script will create a TAP device called crosvm_tap and sets up routing. For details, see the instructions for network devices.
With the crosvm_tap in place we can use it when running crosvm:
To use the network device in the guest, we need to assign it a static IP address. In our example guest this can be done via a netplan config:
First, create a guest directory and the netplan config:
Then edit guest/01-netcfg.yaml and add the following contents:
The netplan config can be installed when building the VM image:
This also allows us to use SSH to access the VM. The script above will install your ~/.ssh/id_rsa.pub into the VM, so you'll be able to SSH from the host to the guest with no password:
WARNING: If you are on a gLinux machine, then you will need to disable Corp SSH Helper:
The full source for this example can be executed directly:
Add GUI support
First you'll want to add some desktop environment to the VM image:
Then you can use the --gpu argument to specify how gpu output of the VM should be handled. In this example we are using the virglrenderer backend and output into an X11 window on the host.

The full source for this example can be executed directly (Note, you may want to run setup_networking first):