What

With an Android device running an aftermarket operating system (see Part 1 of this series) and root capabilities we can configure it as a low-cost low-power Linux home server.

Contents

When

With an abundant amount of used devices the thought of repurposing comes to mind.

Why

As described in the previous part of the series an Android device is essentially a low-cost arm-based computer, with a backup battery and on-board peripherals. They are also run relatively stable, are readily available and generally have more performance especially when compared to Raspberry Pis. Low-level I/O is limited, but that isn’t a concern for a small home-server - though disk space may.

Background

In Part 1 of the series I’ve shown how to install a custom recovery on a Samsung Galaxy Tablet, add a Custom OS, gain root and a Linux shell. This time we’ll install an SSH-Server and set-up timelapse photography - primarily to test battery life and reliability. With all that configured other server software can be added easily.

How

In Part 1 we’ve already installed Termux. With it we can install further software.

Install an SSH-Server

To access the device remotely via SSH instead of USB with adb we need to install an SSH-Server.

For this, from Linux with the device connected and developer mode active

1) bring up Termux

adb shell am start -n com.termux/com.termux.app.TermuxActivity

2) setup storage on the device

adb shell "input text 'termux-setup-storage' && input keyevent KEYCODE_ENTER"

accept on the touch screen

3) close termux and reopen

adb shell am force-stop com.termux
adb shell am start -n com.termux/com.termux.app.TermuxActivity

4) install the ssh server in termux

Update all packages first otherwise you might run into all sorts of errors due to incompatible libraries (e.g. “sshd cannot link libcrypto”)

adb shell "input text 'yes | pkg update' && input keyevent KEYCODE_ENTER"
adb shell "input text 'yes | pkg install openssh' && input keyevent KEYCODE_ENTER"

5) push the ssh identity file from your Linux PC to the device

adb push ~/.ssh/id_rsa.pub /sdcard/Download/id_rsa.pub
adb shell "input text 'cat ~/storage/downloads/id_rsa.pub >> ~/.ssh/authorized_keys' && input keyevent KEYCODE_ENTER"

6) create host key files

adb shell "ssh-keygen -A && input keyevent KEYCODE_ENTER"

6) start the ssh server

adb shell "input text 'sshd' && input keyevent KEYCODE_ENTER"

7) connect for the first time

get the IP-Address

adb shell "input text 'ifconfig' && input keyevent KEYCODE_ENTER"

connect

ssh root@<Device IP-Address> -p 8022

Keep Termux running when screen is locked

By default Android will disable the server when the screen is locked. To avoid this we need to run

termux-wake-lock

Prevent Wifi from disconnecting after 10-15 minutes

Android also disables Wifi after some time.

To combat this:

Some useful tools to install in Termux

yes | pkg install vim wget

Note that due to the way vim is configured in Termux you’ll need to hold the shift key down to copy & paste using the mouse right-click menu.

Timelapse Photos

An interesting use-case for the rooted phone is to do timelapse photography.

For this we need to get the Termux-Api packages. It can be downloaded by

wget https://f-droid.org/repo/com.termux.api_51.apk

and then installed on the device by the UI or by using adb install.

For this to work the complementary package is installed within Termux

yes | pkg install termux-api

Now you can take photos by issuing

termux-camera-photo -c 0 $HOME/storage/dcim/photo.jpg

Note that:

For convenience you can create a script for this:

vim timelapse.sh
#!/bin/bash
termux-camera-photo -c0 $HOME/storage/dcim/`date +'timelapse_%Y-%m-%d_%H-%M.jpg'`

and make it executable

chmod +x timelapse.sh

See the next step to make this script automatically run.

To get the timelapse photos from the device, on your Linux machine you can

mkdir $HOME/timelapse && cd $HOME/timelapse

scp -P8022 root@192.168.2.148:/data/data/com.termux/files/home/storage/dcim/timelapse* $HOME/timelapse

and with ffmpeg installed make a video out of them

cd $HOME/timelapse/
ffmpeg -framerate 10 -pattern_type glob -i '*.jpg' -c:v libx264 -pix_fmt yuv420p -vf "transpose=1" timelapse.mkv

Note

Run timed scripts

With termux in wake-lock “cron” can be used to run scripts at timed intervals.

We can use this for the timelapse script mentioned above.

For this install cronie and termux-services.

pkg install cronie termux-services

Enable the cron service

sv-enable crond

Edit the cron configuration

crontab -e

And for example add

*/5 * * * * /data/data/com.termux/files/home/timelapse.sh

to take a photo every 5 minutes.

At 1,6 MB per photo and 16 GB of disk space, with half of that likely consumed by the operating system, we get 8000 MB / 1,6 MB = 5000 photos. At a photo every 5 minutes we get ca. 25.000 minutes = 416 hours = 17 days or total runtime.

Auto-Start

So far we can keep Termux running with the screen locked, keep the device conected to the Wifi and run scripts at set intervals.

We’re missing an autostart feature in order for the scripts to start after the device was powered off and back on.

For this we need to install Termux:Boot.

wget https://f-droid.org/repo/com.termux.boot_7.apk

and install that.

It has to be run once.

After this, following the insruction it displays, we need to put scripts we want to automatically start in

mkdir $HOME/.termux/boot/

So for example in

vim $HOME/.termux/boot/start.sh

we can add

#!/bin/bash
termux-wake-lock
sshd
sv-enable crond
sv up crond

and make it executable

$HOME/.termux/boot/start.sh

Now the ssh server and cron will start on boot and the device won’t switch of the Wifi or close Termux when the screen is off.

Progress

The tablet ran on battery for 3 days straight taking timelapse photos while staying connected to the WiFi. The battery life is massive since the screen is turned off most of the time. Power cycling the device automatically starts the SSH-Server and cronie.

All in all this works exceptionally well and it’s clear we can set up our own cloud. We can easily install something like NextCloud+MariaDB/MySql that allows for file sharing (NextCloud Smartphone App), calendar/contacts synchronisation (caldav/carddav with the DavX5 Smartphone app) and multiuser expense tracking (CoSpend NextCloud Plugin and MoneyBuster App).

It might also be interesting to install PiHole, a DNS Sinkhole and run DNS-Queries through it to effectively block Ads and tracking services on your home network.