crosvm/tools/examples/setup_network
Dennis Kempin df640d068c Add functional crosvm examples for running on linux
Two examples are added. A simple one and one with network
configured.

The examples are written in a way they can be integrated
with the mdbook to form a tutorial. This will ensure the
tutorial code snippets stay up to date.

The documentation for networking is added after
https://crrev.com/c/3237468 is merged.

BUG=b:214104901
TEST=./tools/examples/example_simple

Change-Id: I33682878858d8a0324fbb6a87e33cd55b29811b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3388063
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Tested-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-01-18 23:24:00 +00:00

38 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
# Copyright 2022 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Set up networking on the host using a TAP device. This probably works on
# many ubuntu or debian machines, but highly depends on the existing network
# configuration.
TAP_USER=$USER
HOST_DEV=$(ip route get 8.8.8.8 | awk -- '{printf $5}')
SUBNET=192.168.10.1/24
setup_network() {
sudo ip tuntap add mode tap user "$TAP_USER" vnet_hdr crosvm_tap
sudo ip addr add "$SUBNET" dev crosvm_tap
sudo ip link set crosvm_tap up
sudo sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o "${HOST_DEV}" -j MASQUERADE
sudo iptables -A FORWARD -i "${HOST_DEV}" -o crosvm_tap -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i crosvm_tap -o "${HOST_DEV}" -j ACCEPT
}
echo "This will set up a tap device 'crosvm_tap' using the $SUBNET"
echo "network range, routed through /dev/${HOST_DEV}."
echo
echo "It will run the following commands:"
echo
type setup_network | sed '1,3d;$d'
echo
read -p "Continue [y/N]? " -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
set -ex
setup_network