crosvm/tools/examples/example_simple
Joe Hattori ab3d88e71f 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 <takayas@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Joe Hattori <hattorij@google.com>
2023-08-09 10:37:25 +00:00

52 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Example VM with a simple ubuntu guest OS but no UI, audio or networking.
set -e
SRC=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
mkdir -p "$SRC/images/simple" && cd "$_"
if ! [ -f rootfs ]; then
# ANCHOR: build
# Build a simple ubuntu image and create a user with no password.
virt-builder ubuntu-20.04 \
--run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER" \
-o ./rootfs
# Packages can be pre-installed to the image using
# --install PACKAGE_NAME
# Ex: virt-builder ubuntu-20.04 ... --install openssh-server,ncat
# In this example, the ubuntu image will come pre-installed with OpenSSH-server and with Ncat.
# ANCHOR_END: build
# ANCHOR: kernel
virt-builder --get-kernel ./rootfs -o .
# 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.
# You can change this directory(/var/empty) by setting the environment variable: DEFAULT_PIVOT_ROOT
sudo mkdir -p /var/empty
# Run crosvm.
# The rootfs is an image of a partitioned hard drive, so we need to tell
# the kernel which partition to use (vda5 in case of ubuntu-20.04).
cargo run --no-default-features -- run \
--rwdisk ./rootfs \
--initrd ./initrd.img-* \
-p "root=/dev/vda5" \
./vmlinuz-*
# ANCHOR_END: run