example_network 1.5 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 internet access and sshd
  6. set -e
  7. sudo mkdir -p /var/empty
  8. SRC=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
  9. mkdir -p "$SRC/images/network" && cd "$_"
  10. if ! [ -f rootfs ]; then
  11. # ANCHOR: build
  12. builder_args=(
  13. # Create user with no password.
  14. --run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER"
  15. # Configure network via netplan config in 01-netcfg.yaml
  16. --hostname crosvm-test
  17. # $SRC=/path/to/crosvm
  18. --copy-in "$SRC/guest/01-netcfg.yaml:/etc/netplan/"
  19. # Install sshd.
  20. --install openssh-server
  21. -o rootfs
  22. )
  23. # Inject authorized key for the user.
  24. # If the SSH RSA public key file is missing, you will need to login to
  25. # the VM the first time and change passwords before you can login via SSH.
  26. ID_RSA_PUB="$HOME/.ssh/id_rsa.pub"
  27. if [ -r "${ID_RSA_PUB}" ]; then
  28. builder_args+=("--ssh-inject" "${USER}:file:${ID_RSA_PUB}")
  29. fi
  30. virt-builder ubuntu-20.04 "${builder_args[@]}"
  31. # ANCHOR_END: build
  32. virt-builder --get-kernel ./rootfs -o .
  33. fi
  34. # ANCHOR: run
  35. # Use the previously configured crosvm_tap device for networking.
  36. cargo run -- run \
  37. --rwdisk ./rootfs \
  38. --initrd ./initrd.img-* \
  39. --net tap-name=crosvm_tap \
  40. -p "root=/dev/vda5" \
  41. ./vmlinuz-*
  42. # ANCHOR_END: run