Skip to content

Latest commit

 

History

History
48 lines (42 loc) · 1.31 KB

File metadata and controls

48 lines (42 loc) · 1.31 KB

Networking on cromvm on android

The official guide for crosvm includes instructions for setting up a NAT network, but those are intended for Desktop Linux and require some additional configuration to work on Android.

#!/system/bin/sh
cd /data/local/tmp
ifname=crosvm_tap
if [ ! -d /sys/class/net/$ifname ]; then
ip tuntap add mode tap vnet_hdr $ifname
ip addr add 192.168.8.1/24 dev $ifname
ip link set $ifname up
ip r a table wlan0 192.168.8.0/24 via 192.168.8.1 dev $ifname
iptables -D INPUT -j ACCEPT -i $ifname
iptables -D OUTPUT -j ACCEPT -o $ifname
iptables -I INPUT -j ACCEPT -i $ifname
iptables -I OUTPUT -j ACCEPT -o $ifname
iptables -t nat -D POSTROUTING -j MASQUERADE -o wlan0 -s 192.168.8.0/24
iptables -t nat -I POSTROUTING -j MASQUERADE -o wlan0 -s 192.168.8.0/24
sysctl -w net.ipv4.ip_forward=1
ip rule add from all fwmark 0/0x1ffff iif wlan0 lookup wlan0
ip rule add iif $ifname lookup wlan0
iptables -j ACCEPT -D FORWARD -i $ifname -o wlan0
iptables -j ACCEPT -D FORWARD -m state --state ESTABLISHED,RELATED -i wlan0 -o $ifname
iptables -j ACCEPT -D FORWARD -m state --state ESTABLISHED,RELATED -o wlan0 -i $ifname
iptables -j ACCEPT -I FORWARD -i $ifname -o wlan0
iptables -j ACCEPT -I FORWARD -m state --state ESTABLISHED,RELATED -i wlan0 -o $ifname
iptables -j ACCEPT -I FORWARD -m state --state ESTABLISHED,RELATED -o wlan0 -i $ifname
fi
ulimit -l unlimited
LD_PRELOAD=./libbinder_ndk.so:./libbinder.so /data/local/tmp/crosvm-a16 --log-level debug run \
--disable-sandbox --no-balloon --protected-vm-without-firmware --swiotlb 64 \
--params 'root=/dev/vda' --mem 4096 --cpus 4 \
--net tap-name=$ifname \
--rwdisk root_part /data/local/tmp/kernel

Copy and paste them or download with wget like this:

# wget https://raw.githubusercontent.com/polygraphene/gunyah-on-sd-guide/refs/heads/main/run-crosvm-net.sh

Then edit /etc/netplan/90-default.yaml in the VM like this:

network:
    version: 2
    ethernets:
        all-en:
            match:
                name: en*
            dhcp4: false

            addresses:
              - 192.168.8.2/24
            routes:
              - to: default
                via: 192.168.8.1
            nameservers:
                  addresses: [8.8.8.8]
            dhcp6: true
            dhcp6-overrides:
                use-domains: true
        all-eth:
            match:
                name: eth*
            dhcp4: true
            dhcp4-overrides:
                use-domains: true
            dhcp6: true
            dhcp6-overrides:
                use-domains: true

Then run

# netplan apply
# ping www.google.com
...