diff --git a/docs/book/src/running_crosvm/example_usage.md b/docs/book/src/running_crosvm/example_usage.md index d6c8556cc..166f027f0 100644 --- a/docs/book/src/running_crosvm/example_usage.md +++ b/docs/book/src/running_crosvm/example_usage.md @@ -34,6 +34,24 @@ 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. + +```bash +{{#include ../../../../tools/examples/example_simple:kvm}} +``` + +You can check if the user is in the kvm group or not with the following command: + +```bash +groups | grep kvm +``` + +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: diff --git a/tools/examples/example_simple b/tools/examples/example_simple index 9852d232d..a95a1b62d 100755 --- a/tools/examples/example_simple +++ b/tools/examples/example_simple @@ -27,6 +27,15 @@ if ! [ -f rootfs ]; then # ANCHOR_END: kernel fi +if [ "$(groups | grep kvm -c)" -eq 0 ]; then + echo "Adding user $USER to the kvm group to grant access to /dev/kvm" + # ANCHOR: kvm + sudo adduser "$USER" kvm + # ANCHOR_END: kvm + echo "Please logout and log back in to reflect the kvm group." + exit 1 +fi + # ANCHOR: run # Create `/var/empty` where crosvm can do chroot for jailing each virtio device. # Devices can't be jailed if /var/empty doesn't exist.