From ab3d88e71f2829aac85bd78956e108c854dc0fe2 Mon Sep 17 00:00:00 2001 From: Joe Hattori Date: Wed, 9 Aug 2023 06:14:16 +0000 Subject: [PATCH] docs: make sure user is added to the kvm group in running_crosvm/example_simple.md. At present, the `tools/examples/example_simple` script fails with an access error when the user is not added to the kvm group. This commit makes sure the user is added to the kvm group when trying to run crosvm in running_crosvm/example_simple.md. Tested on my local environment and checked the script works regardless of the user being in the kvm group. BUG=b:294970555 TEST=./tools/presubmit TEST=./tools/examples/example_simple works regardless of user being in the kvm group TEST=shellcheck Change-Id: I19e42af048774f68c7e0a93afa89a7acd183ba82 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4763917 Reviewed-by: Takaya Saeki Reviewed-by: Keiichi Watanabe Commit-Queue: Joe Hattori --- docs/book/src/running_crosvm/example_usage.md | 18 ++++++++++++++++++ tools/examples/example_simple | 9 +++++++++ 2 files changed, 27 insertions(+) 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.