Seshan's Blogkeyboard_backspaceBack to Posts
Adding a little ARM to my G5! (Pi Zero USB Gadget on a PowerMac G5)
June 10, 2020
As some of you know, the server running this website is a PowerMac G5. It’s a great little machine and I love it, but, it’s also a PowerPC machine. As much as that’s really cool, it also means that a lot of newer software doesn’t support it…
As it turns out, there is still plenty of support for the POWER ISA thanks to IBM still using it in servers and supercomputers, but those new POWER chips (POWER8+) are mostly ppc64le (little-endian), not big-endian like the G5 (a POWER4 CPU). Not to mention plenty of new instructions even in big-endian mode.
So what am I to do? I do have a really nice x86 server, but that’s more of a production machine (plus, I’m heading off to uni soon, I’ll be bringing the G5 with me!).
Plug a Raspberry Pi into it! See, I don’t want to just have a Raspberry Pi running as a separate server entirely, 1. because that’s boring, 2. because it would need to run on WiFi (which looking into the future in university, I don’t think that’ll be the greatest experience).
For those unaware, because the Pi Zero‘s Micro-B port has a direct connection to the SoC (with no hub in between), you can actually have it run as a USB device instead of a host. What that means is using the Linux kernel’s USB Gadget feature, the Pi Zero can make itself a USB Ethernet device (giving us a nice usb0 ethernet device on the G5)!
Adafruit has a nice tutorial on how to set it up, but here’s the gist of it:
- Flash a fresh copy of Raspbian on an SD Card
- Add “
dtoverlay=dwc2” to the bottom of config.txt (on the boot partition)
- Add “
modules-load=dwc2,g_ether” after “rootwait” in cmdline.txt
- (Optionally create a empty file named “ssh” to automatically enable ssh on first boot, just do “touch ssh” in the boot partition).
After that, just plug the Pi Zero’s OTG port into your computer, and it should pop up as a network interface (on Windows you may need additional drivers)! On macOS (and Linux with avahi), your computer and the Pi will automatically get a link-local ip (169.whatever, zeroconf networking is cool), and you can just do “ssh firstname.lastname@example.org”.
Now, you could leave it just like that and use the zeroconf network, but because I wanted the Pi Zero to be a permanent extension of my G5 server, I needed to setup a static IP, aswell as network sharing (so the Pi could access the internet).
First I added the following to “/etc/network/interfaces” on the Pi:
allow-hotplug usb0 iface usb0 inet static address 192.168.7.2 netmask 255.255.255.0 network 192.168.7.0 broadcast 192.168.7.255 gateway 192.168.7.1
Then, on the G5:
allow-hotplug usb0 iface usb0 inet static address 192.168.7.1 netmask 255.255.255.0 network 192.168.7.0 broadcast 192.168.7.255
This gives the Pi an IP of “192.168.7.2” and the G5 “192.168.7.1”. This is nice, but we still need to have a NAT between eth0 (the ethernet port of the G5) and usb0 (the Pi Zero). I found a nice article that explains how to do that, here are the steps:
net.ipv4.ip_forward=1in “/etc/sysctl.conf”, the do
sudo sysctl -p /etc/sysctl.conf
- Run the following commands (where eth0 is the WAN port, and usb0 is the Pi’s interface):
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -A FORWARD -i eth0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT
Your almost done, on the Pi you’ll want to disable dhcpcd (since we have a static IP), and edit “/etc/resolv.conf” to use some public DNS server (like 220.127.116.11). And that’s it! The Pi Zero should be able to access the internet.
Still one more tweak to go though. You can SSH into the Pi (or access any service) locally on the machine using “192.168.7.2”, but the Pi isn’t exposed outside of the server. If I was running, say, web services, I’d use NGINX as a reverse proxy. But for things like SSH, you need to basically port forward. Most people would add some additional iptables entries, but since I was messing around with Golang a bit while doing this project, I decided I’d write a simple TCP proxy to forward certain ports of the server to the Pi.
You can find that code on my GitHub, and basically it just parses a config file with a list of ports to forward. This way, services on the Pi appear alongside everything on the server, it’s great! Basically “ssh email@example.com” goes to the host OS, and “ssh firstname.lastname@example.org -p 2287” goes to the Pi. 🎉
And there you go! The SeshanXYZ server now has a little Pi companion! It works great, and I can run stuff that would otherwise not work on the G5 (like modern versions of NodeJS). That being said, a Pi zero isn’t the fastest thing in the world… but it’s the only Pi that can run in Gadget mode… or is it? As it turns out, the Pi 4’s USB-C port can actually do that same thing! Yes that’s right ladies and gentlemen, you can get this same experience but with 64-bit quad-core goodness and up to 8GB of RAM! I might want to get my hands on a Raspberry Pi 4…