Original documentation 1 suggests setting up network for CH guest using virtio-net deivces, host TAP and bridge interfaces.
This note is an alternative way of achieving a the Internet connectivity without setting up a bridge--spoiler alert--with iptables. (Similar setup used by smoltcp 2)
ip tuntap add dev chtap0 mode tap
ip addr add 192.168.12.1/24 dev chtap0
ip link set chtap0 up # later
iptables -t nat -A POSTROUTING -s 192.168.12.0/24 -j MASQUERADE
sysctl net.ipv4.ip_forward=1
iptables -A FORWARD -i chtap0 -s 192.168.12.0/24 -j ACCEPT
iptables -A FORWARD -o chtap0 -d 192.168.12.0/24 -j ACCEPT
$ ./cloud-hypervisor/target/release/cloud-hypervisor \
--kernel ./hypervisor-fw \
--disk path=focal-server-cloudimg-amd64.raw \
--cpus boot=4 \
--memory size=1024M \
--net "tap=chtap0,mac=,ip=192.168.12.100,mask=" \
--rng
Note the tap name and the IP address.
ip addr add 192.168.12.100/24 dev ens3 # interface may have a different name
ip route add default via 192.168.12.1
ping 1.1.1.1
For exmaple, add nameserver 1.1.1.1
to /etc/resolv.conf
and test nslookup google.com
.