November 22, 2021

Tailscale on Kobo Sage

It's basically a rite of passage for Tailscale users to put Tailscale on the weirdest device they own.

Last month, Rakuten finally released a reasonably-sized Kobo with USB C, the Kobo Sage. One of the most fascinating things about the Kobo eReaders (compared to the Kindle eReaders) is that they run a user-modifiable version of Linux, meaning you're free to do all sorts of weird and wild things with them.

So I decided to put Tailscale on my Kobo Sage.

Tailscale is an easy-to-use VPN(ish) service that allows you to connect multiple devices over the internet to a secure, shared network. Since it's is distributed as statically-compiled binaries (thanks Go!), getting Tailscale downloaded and running on Kobo Sage was as easy as downloading the binaries, copying them to the onboard storage (since the root filesystem is only about 300 MB), symlinking them into a $PATH friendly folder, and running tailscaled.

tar -xzf tailscale_1.18.0_arm.tgz
mv tailscale_1.18.0_arm/tailscaled /mnt/onboard
mv tailscale_1.18.0_arm/tailscale /mnt/onboard
ln -s /mnt/onboard/tailscale /usr/bin/tailscale
ln -s /mnt/onboard/tailscaled /usr/bin/tailscaled
rm -rf tailscale_1.18.0_arm
rm tailscale_1.18.0_arm.tgz

However, if you were to do this, you'll be greeted with this:

wgengine.NewUserspaceEngine(tun "tailscale0") error: exec: "iptables": executable file not found in $PATH
wgengine.New: exec: "iptables": executable file not found in $PATH

While the Linux 4.9 kernel used by Kobo Sage supports iptables, the actual binary is nowhere to be found on the device. Thankfully, acquiring a suitable binary isn't too terribly difficult.

Getting iptables

Unfortunately, there's no officially distributed pre-compiled binaries of iptables. So, we can either compile from source (either on a 32-bit ARM device or via cross-compilation), or we can yank the binary out of an image of a 32-bit ARM Linux distro. We need a distro that's old enough to be using glibc version less than 2.19, so the best choice I came up with was the July 5, 2017 release of Raspbian, the last to be based on Debian 8 (codename jessie).

Using 7zip for Windows, I extracted the following files from the disk image:

  • /sbin/xtables-multi
  • /lib/
  • /lib/
  • /lib/

Note: You can also extract these files on Linux, but it's a bit more complicated.

Now that we had a binary, it was time to put it on the device.

Installing iptables

Out of the box, Kobo Sage doesn't support ssh, but it does support telnet after enabliing dev mode. Since I couldn't use scp to copy files to the device, I ran a web server on my computer, and downloaded the files with wget:

wget http://callisto.localdomain:8000/xtables-multi
wget http://callisto.localdomain:8000/
wget http://callisto.localdomain:8000/
wget http://callisto.localdomain:8000/

Once I had the files, I was able to move them into the appropriate locations:

mv xtables-multi /sbin
mv /lib
mv /lib
mv /lib

Since we need iptables in $PATH, I created a symlink in /sbin:

cd /sbin
ln -s xtables-multi iptables

We also need symlinks for the libraries, so I ran the following in /lib:

ln -s
ln -s
ln -s
chmod 755
chmod 755
chmod 755

Once I had the binary and libraries in the right spots, I was able to confirm that iptables worked:

iptables --version

Testing Tailscale

With the iptables binary working, confirming Tailscale works was the next step. By default tailscaled stores some state in /var/lib, which the Kobo doesn't like. So, for the purposes of testing, I just manually told tailscaled to store its state in a file at the root directory.

tailscaled --state=tailscaled.state

In another telnet session, I was able to bring Tailscale up with

tailscale up

After authenticating, the final step was to open a Tailscale-only URL in the Kobo's experimental web browser to confirm that I was, indeed, connected to my Tailscale network!

Kobo Sage displaying a webpage with the heading You're connected over Tailscale

Staying Connected to Tailscale

Getting Tailscale up and running wasn't as smooth as on a typical Linux device, but staying connected to Tailscale is an even trickier thing to do. Kobo Sage doesn't have systemd, so we can't use the included service definition provided by Tailscale. To further complicate matters, if we try to hook into the lower levels of the OS and make a mistake, we can end up with a bricked device that's unable to be recovered. Thankfully, there's been a ton of exploration in this area by other developers (shoutout to the Kobo Developer's Corner on the MobileRead Forums), and the generally agreed upon way of running things at boot is by using the udev system. This allows us not only to hook into a startup event, but also into events that are triggered when Kobo Sage connects to and disconnects from WiFi. This allows us to run tailscale up when we have an internet connection, and tailscale down when we lose the connection.

To start, I defined the udev rules in a file named 98-tailscale.rules in /etc/udev/rules.d

KERNEL=="loop0", RUN+="/usr/local/dstaley/"
KERNEL=="wlan*", ACTION=="add", RUN+="/usr/local/dstaley/"
KERNEL=="wlan*", ACTION=="remove", RUN+="/usr/local/dstaley/"

The KERNEL=="loop0" rule runs roughly at boot, so that's where I start tailscaled. The next two rules are for when the WiFi is brought up and torn down respectively.

The script for starting tailscaled is straightforward. The catch, though (and with Kobo there's always a catch), is that the OS will normally kill long-running tasks spawned by udev. To circumvent that, I used a combination of renice and setsid to ensure that tailscaled isn't killed off.

The script contains the following:


# Start by renicing ourselves to a neutral value, to avoid any mishap...
renice 0 -p $$

# Launch in the background, with a clean env, after a setsid call to make very very sure udev won't kill us ;).
env -i -- setsid /usr/local/dstaley/ &

# Done :)
exit 0

This script runs in the background. That script contains:


# make absolutely sure that iptables is in the PATH
export PATH=/usr/sbin:/usr/bin:$PATH

# make sure /mnt/onboard is mounted
timeout 5 sh -c "while ! grep /mnt/onboard /proc/mounts; do sleep 0.1; done"
if [[ $? -eq 143 ]]; then
    exit 1

case "$(pidof tailscaled | wc -w)" in
0) tailscaled --state=/tailscaled.state &> /tailscaled.log &

exit 0

Since we're not starting a long running process on connecting and disconnecting to WiFi, the scripts and only contain a single command: tailscale up and tailscale down respectively.

After placing these files in their new homes and rebooting, I can now access my Kobo Sage via my Tailscale network! This also means that I can access other devices on my network, which will come in handy in the future when I want to do things like backup my notebooks to my NAS at home.

Other Kobo Devices

Since Rakuten reuses a lot of the same software for all of their Kobo devices, there's a decent chance that these steps will work on any Kobo. If you do decide to go that route, you'll need to verify that your Kobo's kernel was compiled with iptables support, and that the included glibc version is at least equal to the glibc version used to compile the iptables binary.