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