Milk-V Duo. USB Internet configuration without RJ45

Hi everyone! I just finished commiting my first open source code for Milk-V Duo
Recently I bought Milkv-V Duo 64M board without Ethernet adapter with RJ45. And installed official OS image

Than I connected my MilkV to the Raspberry Pi 4 (RPi4).

ISSUE
The issue what I got - that I can communicate with board by ssh connection throw USB Type-C, but without Internet on this board, that was challange for me.
So how I’ve solve this issue. On the board we have just pure Linux - Linux Core, without any additional packages. But really this is not full Linux Core - many of standard applications is absent.

SOLUTION
So I decided to write two shell scripts that will help me, and I hope also many peoples.

You have to run two scripts, for one script by each machine:
MilkV-Duo-internet-throw-USB--MilkV.sh on MilkV
– and MilkV-Duo-internet-throw-USB--RPi4.sh on the host)

  • The file MilkV-Duo-internet-throw-USB–MilkV.sh contains next instrusctions


### Add rc.local file
echo -e “#!/bin/sh
echo Adding route for 192.168.42.21
sleep 2
route add default gw 192.168.42.21
echo Add default finished
exit 0” >> /etc/rc.local
chmod +x /etc/rc.local

##
echo -e "

### Run user scripts
::sysinit:/etc/rc.local" >> /etc/inittab

## Instead above movigs with /etc/rc.local and /etc/inittab you can call below script, but it works juft until you not reboot the board
#route add default gw 192.168.42.21

### Add nameservers
echo -e “nameserver 8.8.8.8” >> /etc/resolv.conf.head

### Reboot
reboot

  • The file MilkV-Duo-internet-throw-USB–RPi4.sh contains next instrusctions


### Network interfaces Forwarding btw eth0->usb0

sudo sysctl net.ipv4.ip_forward
sudo sysctl -w net.ipv4.ip_forward=1

sysctl_conf=/etc/sysctl.conf
sysctl_conf_row_commented=#net.ipv4.ip_forward=1
sysctl_conf_row_uncommented=net.ipv4.ip_forward=1

grep -q sysctl_conf_row_commented sysctl_conf && (sed -i ‘s/sysctl_conf_row_commented/sysctl_conf_row_uncommented/’ sysctl_conf || echo “sysctl_conf_row_uncommented” >> sysctl_conf)

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install iptables-persistent
sudo netfilter-persistent save

### Make static IP for Rpi4 with address 192.168.42.21
echo -e "
interface usb0
static ip_address=192.168.42.21/24
static routers=192.168.42.1
static domain_name_servers=192.168.42.1" >> /etc/dhcpcd.conf

sudo systemctl restart dhcpcd

# Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1

# Set up port forwarding
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 2222 -j DNAT --to-destination 192.168.42.1:22
sudo iptables -A FORWARD -i eth0 -o usb0 -p tcp --dport 22 -d 192.168.42.1 -j ACCEPT
sudo iptables -A FORWARD -i usb0 -o eth0 -p tcp --sport 22 -s 192.168.42.1 -j ACCEPT

You dont need to copy-past above code, just copy one file to the Milkv and run it
scp MilkV-Duo-internet-throw-USB--MilkV.sh root@192.168.42.1:~/
also run MilkV-Duo-internet-throw-USB–RPi4.sh on host machine

All detail description you can find in README file

Screenshot 2024-01-11 053712