Setting Up an OpenWRT Dev Environment on a Raspberry Pi 3 B+

It’s been a while since I last tinkered with something fun, but I finally got around to setting up an OpenWRT development environment on my Raspberry Pi 3 B+. This time, it’s for version 2 of my VPN profile switcher script, which you can find on my GitHub repo.

Note: this post is about setting up a shell script dev environment on OpenWRT, not an actual OpenWRT development environment. For that, please follow the official developer’s guide

Flashing OpenWRT

To start, I obtained the most recent OpenWRT image for the Raspberry Pi and flashed it onto an SD card. The process was simple - download the image, use Balena Etcher, and you’re ready to proceed. Afterward, I powered on the Pi, set a password, and added my SSH keys from my main machine. It’s important to prioritize security and accessibility, isn’t it?

To make sure I had enough space for everything, I followed OpenWRT’s guide to expand the filesystem for ext4. This step is crucial, especially if you plan on installing packages or running scripts.

For some reason, I had to also install e2fsck as well, so my process is as follows:

opkg update
opkg install parted tune2fs resize2fs e2fsck

parted
p
resizepart 2 8GB # or whatever size you want
q

mount -o remount,ro /                  #Remount root as Read Only
tune2fs -O^resize_inode /dev/mmcblk0p2    #Remove reserved GDT blocks
# This failed, so I had to run e2fsck
e2fsck -f /dev/mmcblk0p2                #Answer yes to all questions
tune2fs -O^resize_inode /dev/mmcblk0p2    #Remove reserved GDT blocks
fsck.ext4 /dev/mmcblk0p2                  #Fix part, answer yes to remove GDT blocks remnants
reboot

# After reboot
resize2fs /dev/mmcblk0p2

WiFi Hotspot and WAN Setup

Next, I turned the Pi into a WiFi hotspot and set the ethernet port as WAN. This means the Pi can share its internet connection wirelessly while staying wired to my main router.

I actually tried to set up the wireless interface on the pi as a STA (client) + AP (master), but it didn’t work out. I ended up using the USB WiFi adapter as a WWAN instead. You can find some threads on the topic here, and some more info on the travelmate support thread. Apperantly it is possible w/ Raspoberry pi OS, here’s a how-to.

Adding a WiFi WWAN

Now, for a bit of fun – I had a spare TL-822N USB WiFi adapter and decided to add a WiFi WWAN. I followed what worked for someone on the OpenWRT forum, but ran into some issues with the adapter repeatedly failing to connect. After some head-scratching, I figured it might be a power issue. Swapped out the power supply for a beefier 3W one - and the problem was solved.

So, assuming you have a beefy enough power supply, install the following packages:

opkg update
opkg install kmod-usb2 rtl8192eu-firmware kmod-rtl8xxxu

Why the WWAN?

You might wonder why go through the hassle of adding a WWAN. Well, I wanted to keep the ethernet port on my main router free and have a ready-to-use travel router for future adventures.

Getting OpenVPN Running

With the networking sorted, it was time to get OpenVPN up and running with NordVPN. I followed my own instructions from the repo’s README – always nice when past me makes things easier for future me.

SSH Convenience

To make my life easier, I ensured my SSH connection was rock-solid and installed nano on the Pi. On my main machine, I set up the SSH FS extension in VS Code and installed the openssh-sftp-server package on the Pi, following OpenWRT’s documentation. This setup lets me edit files on the Pi directly from VS Code, which provides some useful quality of life features, such as switching between files easily, syntax highlighting, and Copilot.

Final Touches

Lastly, I installed git and git-http packages on the Pi so I could clone and manage repos directly. With everything in place, my OpenWRT dev environment was ready to roll.

Edit: 2024-07-24

When trying to work on the script on the Pi, I had a bit of trouble pushing the changes to the repo. I decided to set up a new SSH key on the Pi and add it to my GitHub account. This way, I could push changes without any issues. Sicne the default SSH agentt on tthe standard OpenWRT is dropbear, I followed this how-to on the OpenWRT wiki. Here’s how I did it:

mkdir ~/.ssh
dropbearkey -t ed25519 -f ~/.ssh/id_dropbear
dropbearkey -y -f ~/.ssh/id_dropbear

I then copied the public key to my GitHub account and added the private key to my SSH agent. This way, I could push changes to the repo without any issues.

I also set the username and email for the git config:

git config --global user.name "Your Name"
git config --global user.email "yourname@your.domain"

I had trouble working with source control from my remote sshfs connection, so I decided to clone the repo directly to the Pi. This way, I could work on the script without any issues. That’s a bit of a bummer, but it’s a small price to pay for a stable development environment.

Wrapping Up

So there you have it – a solid OpenWRT development environment on a Raspberry Pi 3 B+. Perfect for freeing up your main router’s ethernet connection or having a travel-ready router. Happy tinkering!

Cheers, Uri