example_simple 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. # Copyright 2022 The ChromiumOS Authors
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # Example VM with a simple ubuntu guest OS but no UI, audio or networking.
  6. set -e
  7. SRC=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
  8. mkdir -p "$SRC/images/simple" && cd "$_"
  9. if ! [ -f rootfs ]; then
  10. # ANCHOR: build
  11. # Build a simple ubuntu image and create a user with no password.
  12. virt-builder ubuntu-20.04 \
  13. --run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER" \
  14. -o ./rootfs
  15. # Packages can be pre-installed to the image using
  16. # --install PACKAGE_NAME
  17. # Ex: virt-builder ubuntu-20.04 ... --install openssh-server,ncat
  18. # In this example, the ubuntu image will come pre-installed with OpenSSH-server and with Ncat.
  19. # ANCHOR_END: build
  20. # ANCHOR: kernel
  21. virt-builder --get-kernel ./rootfs -o .
  22. # ANCHOR_END: kernel
  23. fi
  24. if [ "$(groups | grep kvm -c)" -eq 0 ]; then
  25. echo "Adding user $USER to the kvm group to grant access to /dev/kvm"
  26. # ANCHOR: kvm
  27. sudo adduser "$USER" kvm
  28. # ANCHOR_END: kvm
  29. echo "Please logout and log back in to reflect the kvm group."
  30. exit 1
  31. fi
  32. # ANCHOR: run
  33. # Create `/var/empty` where crosvm can do chroot for jailing each virtio device.
  34. # Devices can't be jailed if /var/empty doesn't exist.
  35. # You can change this directory(/var/empty) by setting the environment variable: DEFAULT_PIVOT_ROOT
  36. sudo mkdir -p /var/empty
  37. # Run crosvm.
  38. # The rootfs is an image of a partitioned hard drive, so we need to tell
  39. # the kernel which partition to use (vda5 in case of ubuntu-20.04).
  40. cargo run --no-default-features -- run \
  41. --rwdisk ./rootfs \
  42. --initrd ./initrd.img-* \
  43. -p "root=/dev/vda5" \
  44. ./vmlinuz-*
  45. # ANCHOR_END: run