Create a Native Development Environment on Kobo
This is a way to create your own native development environment on a Kobo eReader, courtesy of NiMa. It will help you set up Alpine Linux with gcc, g++ or clang to develop custom applications for your Kobo.
Disclaimer: NiMa is not responsible if you brick your Kobo with this technique, or if you lose files; although it should run well. (Running in a chroot jail in a disk image.)
[edit] Prerequisites:
- A Kobo with a fair amount of free space (ideally 3GB+). Preferably with kernel >3.0, but maybe it'll work with older versions too; we haven't tried yet. Tested with my Glo HD and Libra and both worked (Glo HD has 3.0.35, Libra has 4.1).
- A computer (or something you can ssh/telnet with).
- NiLuJe's stuff (not mandatory, but is very useful for SSH) https://www.mobileread.com/forums/showthread.php?t=254214
[edit] Instructions:
1. First, enable developer mode on your Kobo. Fairly easy; just search "devmodeon" in your Kobo's search bar. It will add some games to your "Beta features" menu, but more importantly, it will give us entire access to the Kobo via telnet.
2. Check what IP your Kobo is using by going to Settings > Device information.
3. Tap the WiFi icon at the top of the screen and leave the menu open. It will disable WiFi standby, which could disrupt your telnet access. Note: you can also set up usbnet from there and then telnet via USB, with no interruptions.
4. In a Linux terminal/Telnet-able machine, type "telnet <IP>". Example: telnet 192.168.1.68
Log in as root. There's no password.
5. Check your Linux kernel version by typing in "uname -r". If it is less than 3.0, you may encounter problems.
6. Change directory to /mnt/onboard. Type in:
cd /mnt/onboard
Here we will create a disk image. It's not possible to just dump a rootfs in the onboard partition because it is formatted in FAT32, and FAT32 doesn't like Linux very much (permission problems). So a disk image will help us circumvent this problem. Type in:
dd if=/dev/zero of=/mnt/onboard/alpine.img bs=1M count=2048
Note: the above command creates a 2G disk image. If your Kobo doesn't have the disk space required for that, you could change the "count" to 1024 or 512 for example, and you will get, respectively, a 1G and 512MB disk image.
7. Format the disk image. If your Kobo is from the future or has this capability, you *could* be able to format it in ext4 format natively by adding the "-t ext4" flag in the command line. However, mke2fs here only supports ext2, which is usable too, albeit older. So you could try to format a 2G image on your host system with an ext4 filesystem, then txz-compress it and extract it in your Kobo's storage. This didn't work on Glo HD though, but did on Libra. So we'll use the legacy way:
mke2fs -F /mnt/onboard/alpine.img
8. Mount the disk image. Here we'll use /mnt/user as a mountpoint, but you can mount it in another directory if you want.
mount /mnt/onboard/alpine.img /mnt/user
This should run without errors. Then, change directory to the mountpoint:
cd /mnt/user
and do a quick
df -h
to check if you see your mountpoint in the list, like this:
/dev/loop1 2047.7M 13.0K 2047.2M 0% /mnt/user
9. Ensure that your Kobo is connected to the Internet, then head over to https://alpinelinux.org/downloads/ and scroll down to the "Mini Root Filesystem" part, and right-click the green button with "armv7", then click on "Copy link location" or something like that. On your Kobo:
cd /mnt/user && wget https://dl-cdn.alpinelinux.org/alpine/v3.12/releases/armv7/alpine-minirootfs-3.12.3-armv7.tar.gz
The link should be replaced with the latest version. This should get the tar archive with the Alpine Linux filesystem on your disk image.
10. On your Kobo, we'll now get the tar archive extracted. Ensure you're still on your SSH/Telnet session and type:
cd /mnt/user && tar -xvf alpine-minirootfs*.tar.gz
Where the wildcard (*) should be replaced with the actual name of the tar archive.
11. Prepare the chroot. Type in:
mount -v --bind /dev /mnt/user/dev mount -v --bind /dev/pts /mnt/user/dev/pts mount -vt proc proc /mnt/user/proc mount -vt sysfs sysfs /mnt/user/sys mount -vt tmpfs tmpfs /mnt/user/run mkdir /mnt/user/dev/shm mount -vt tmpfs tmpfs /mnt/user/dev/shm cp /etc/resolv.conf /mnt/user/etc
This will bind your Kobo's existing /dev, /proc, /sys, /run contents to your Alpine Linux mounted disk image on /mnt/user. Therefore, you'll have greater flexibility when it comes to more hardware-based tasks or process monitoring. Also, we copy the resolv.conf file from the root filesystem to the Alpine disk image to be able to access the network from the chroot. You may need to re-copy this file later if your network info changes.
12. Chroot. Type in:
chroot /mnt/user /bin/sh
You should get into Alpine Linux.
13. Now, Alpine Linux comes with the holy grail: a package manager!, apk. First, try installing Nano:
apk add nano
If all goes well, you should be able to run it directly after, by simply typing in "nano"! :D
14. To add a compiler:
apk add gcc g++
or you can install clang instead if you prefer it to gcc/g++. You should now be able to compile! It's slow, but usable and it works well! If you want to go further, you could build gcc/g++ with gcc/g++, to obtain a fully optimized compiler (three-stage process: https://gcc.gnu.org/install/build.html), but the default binary works well.
Well, it's done! If you reboot your Kobo, you can think of adding the disk image to /etc/fstab so it is automatically mounted at startup, and set up an init script which mounts /dev, /sys, /run, etc. automatically.