petergarner.net::blog

Travels with a Pi

In Pursuit of Smaller Things With The Pi Zero

A little background

For some time now I've been a huge fan of the ubiquitous Raspberry Pi computer in all of its various forms. I have a number of them here and each has a server role to play. Most of the time they work extremely well and I'm safe in the knowledge that the small amount of power they consume is keeping the bills down.

If you've visited here before, you may have read my account of how I migrated my desktop computing to a Pi 3. This worked well for quite a while, but I finally had to accept that editing large graphics and multimedia was a problem, so I replaced it with an Intel NUC. The hankering for Pi-experimentation was still there though, and I decided to do a "what-if" exercise and see if it could survive on the road. And that's when the Pi Zero (W) was dragged out of retirement from my spares box.

Motivation

"But why would I want to do this? Surely the trend is to travel with as powerful a device as possible?"

Well, it's like this. Last year, my employer issued a decree that in order to conform to Security Policy we would no longer be able to check-in laptops as hold luggage, and not long after, the US Gov decided to summarily ban carrying laptops in hand baggage to and from certain countries, y'know, for security. So how do we get around that one? The sensible option would be to simply not travel with a laptop and use a hot-desk-style spare at the destination. After all, everything is in the cloud now, right? Or, simply carry your important, commercially-sensitive data on a CD/DVD or memory stick, but only if it's encrypted to a standard and your employer's data handling policy allows that.

The problem is multi-faceted though: what if your role is such that you need to be able to fix software/systems on-the-go and you don't always have access to a "spare" laptop? I know that on the occasions I travel (by train) I have to lug my laptop with me, and it's a pain over the course of a day. Then there's the loss/theft/damage problem. Laptops can be easy targets and can get left on trains, or you could be mugged. Or there's the "Evil Maid" scenario in which someone interferes with your device without your knowledge. The list goes on...

Finally, if you're unfortunate enough to lose/damage your "disk", you can easily create another by downloading your 2Gb image from a secure location in the cloud and burning it to a new card. Try doing that with a normal laptop!

So here's what you can achieve with a Pi Zero portable computer:

Small enough to fit in hand baggage, or your pocket.

Cheap enough at $8 to buy another if yours gets lost/stolen/damaged.

The entire OS and data is held on a "disk" that is as small as a fingernail, is cheap and easily bought in a wide variety of retail outlets. If need be you can create a new one from a borrowed card from a phone.

A full development environment with the ability to work offline or online. Also has the ability to act as an SSH server so that more than one person can use it at once.

Safe storage: if you were paranoid, or travelling with certain airlines you could remove the "disk" and store it in your wallet or on your person. If your computer was stolen in transit, go and buy another one, off the shelf: you're already set up with the OS.

Network-tolerant: there are country-specific WiFi frequencies worldwide and a simple text-file change enables you to be compliant within minutes.

Keyboard independent: you can use a compliant(!) Bluetooth keyboard but when need to do something more demanding, just plug any USB keyboard into the spare USB connector using an OTG cable.

Power supply tolerant: my 3300mAh powerbank can run it for about 8 hours, but if all else fails you can use the TV USB connector to power it. Generally speaking, if a TV has HDMI it wil have a USB socket and the Zero will only draw about 120mA. Or use someone's phone charger!

That's motivation enough for me!



The Finished Product - beer mat for scale

Enter the Pi Zero W

The cheap-as-chips Pi Zero has always been a bit of an odd beast in my opinion. It features a single core CPU and has a frugal 512Mb of memory, some of which it has to share with the onboard video. The Raspbian OS currently based on Debian Stretch is touted as being suitable for the Zero with the LXDE-based "Pixel" GUI desktop, and indeed it can be loaded and started but in reality the poor thing really struggles to manage a GUI and run useful software. Nevertheless I was determined to give it a good try and set it up with the apps with the smallest memory footprint. I'd already been round this loop with the Pi 3 so it was more of the same - only even more so! Bearing in mind this was to be a road-warrior's computer, here's what I wanted to have on it:

Web browser Lightweight but with privacy in mind Email IMAP-capable, and seamlessly supporting GPG XMPP/Jabber client No-nonsense messaging VPN client I'm on the road, remember TOR client Always useful... Music I carry a few MP3s but Intenet radio is cool too Multiple desktops Useful with a small screen Editor/IDE Hey, it's a working computer! FTP/SFTP client Hey, it's a working computer!

All in all, a very useful bundle for my purposes, and if I could achieve a balanced environment it could actually work.

Hardware

As for the hardware, that was a bit of a challenge. The battery was not a real problem as I have a variety of rechargeable power packs with varying capacity, so it was just a question of picking a suitable one on the day. The only prerequisite that the battery should be able to take a charge while being used, and all my Jackery brand batteries do this. I opted for my in-house standard 32Gb Sandisk Extreme microSDHC as my "disk". They're very reliable and that size is big enough to hold a lot of software+data while still remaining affordable. The video output would, I anticipated, be HDMI-out using the Zero's mini-HDMI connector. This suited my purposes well as the majority of hotel TVs now use this interface. Audio would also go via HDMI. That left the HID - Human Interface Device - and this, predictably caused the most consternation. I hate Bluetooth with a passion and with the Zero's limited connectors I'd have to bite the bullet and use a Bluetooth keyboard and mouse; preferably a combined one, and small. There's no point in having a tiny computer if you have lug a honking great keyboard around as well; and so my unhappy quest began...

My hatred for Bluetooth grows...

As mentiond the Zero has a limited number of USB connectors on board - one (1) if you allow for the power connector - which obviously means you have limited connection options. You could always use a USB extender hub but then that's more to carry and there's another PSU to have as well. That basically leaves you trying to connect a keyboard and mouse via Bluetooth. Personally, I ignore the hype about how it can "easily connect a wide variety of devices together" and wrestle with a variety of allegedly-standard devices trying to get the bloody things to play nicely together. Part of the problem with the Pi (I think) is that there's some unintended interaction between wifi and Bluetooth and it causes weird stuff to happen. My particular problem was that I was able to connect to the Pi using a keyboard, or a mouse, but not reliably at the same time. And yes, I have a variety of allegedly standards-following Bluetooth devices, none of which decided to work together properly.

At this point I was wondering if there was a Bluetooth Samaritans Helpline, but there wasn't, so what the heck was I going to do?! I temporarily resorted to using an Apple USB keyboard with 2 USB sockets which was useful for setting up, but not for being on the road. In the end I spent hours browsing eBay and Amazon and then I found it: a "3 in1 Mini Wireless Bluetooth Keyboard Mouse Touchpad For Windows iOS Android UK - Backlit, Ultra-thin, Built-in Rechargeable Battery, QWERTY". Perfect on paper, but would it work? I sent off the money and 4 days later, a slim package arrived from China. It had arrived.. and you know, it only bloody well worked!! First time, every time: it got on with the Pi like pie and chips. I promptly ordered another one - accidents invariably happen and I wouldn't want to be left in the lurch. So with my hardware lineup complete I could settle down to setting up the Pi proper.

The GUI and the Pi

Going back to my software requirements, I thought long and hard about the smallest desktop environment and went with the supplied LXDE desktop which I'd used several times on other projects. Althouhg the Pi struggled a bit with screen handling, it generally performed well and I started setting up my software.

Web browser 1 Midori - good compromise between size and modernity. Supports private browsing and is bundled with the Pi Web browser 2 Links2 in graphic mode: lightweight, fast, secure, works with proxies Email Sylpheed - small, light and works well with GPG XMPP/Jabber client Profanity. It's great! VPN client OpenVPN TOR client Ha! Links2 again Music Smplayer - the GUI for mplayer Editor/IDE Geany - small and light, but powerful Image viewer/editor Pinta FTP/SFTP client Filezilla Other Stuff.. Midnight Commander Not just a file manager (more later) TOR proxy server Always on so I could use TOR as needed Nmap I sometimes need to test stuff Vnstat Monitor data usage on the wlan0 interface SSH/SFTP server Standard issue on this distro UFW Firewall - can't be too careful! Gopher client Gopherspace! It's still out there, and I use the original gopher client!

All of the above are tried and tested packages and very stable. The web browser decision was a calculated one: the Zero doesn't really have what it takes to negotiate a modern bloated, ad-ridden web site, and honestly, I have a phone that can handle that sort of thing. Likewise the decision to run a TOR proxy: it's very handy to be able to access Marianas Web, and using Links2 as a browser means that the risk is minimal.

Sylpheed is a mature package that I believe has largely been replaced by Claws, but is actually less demanding of resources. GPG integrated seamlessly with it, and I was able to exhange signed/encrypted messages with ease. It renders both plaintext and HTML messages well and the interfce is uncluttered.

I needed a simple XMPP/Jabber client. The problem I've found with many apps of this type is that they try to incorporate multiple messaging standards when I only really need XMPP. Profanity is ideal as it does one job,and does it very well.

System Setup

I spent a considerable amount of time setting up the OS for the best performance, a task made easier having set up numerous Raspberry Pi's before. In such a small (as in memory) system, the decision to use a swap file was not taken lightly, and unfortunately, using a GUI desktop meant that the swap was in frequent use. The alternative was to not have one and hope that the system didn't freeze up too much. I went with the suggested default of 100Mb. I then looked at logging. As this was supposed be a small, portable system I didn't see much point in having extensive logging, especially as it would have a negative effect on the SDHC card long-term. One solution was a combination of disabling logging in apps wherever possible and sacrificing a little memory to create a tmpfs in-memory filesystem. This would have the added advantage that it would be recreated on each boot. So, I worked out that 8Mb could be used for this and duly updated /etc/fstab. This works extremely well.

tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=8m 0 0

I also had to update /etc/rc.local to provide some essential directories on startup: this kept the rsyslog and TOR daemons happy.

mkdir -p /var/log/rsyslog mkdir -p /var/log/tor/ mkdir -p /var/log/vnstat/ chown vnstat.root /var/log/vnstat/ chown debian-tor /var/log/tor/

With all that in place, the little computer was almost ready for Prime Time. But there was a problem. I've already mentioned the frugal amount of memory on the Zero, and even with the GUI and apps pared down to the bone, I was regularly using swap space. To make matters worse, much worse, my carefully-set-up desktop menus were having problems.

The Pi desktop comes with an incredible amount of software installed, mainly to satisfy its original purpose as an educationl machine. I decided early on to edit the menus to remove a lot of the "junk" and replace it with my list of apps, so I fired up the Main Menu Editor app. Normally this is quite responsive, but on the resource-challenged Zero it had worrying lags and pauses as changes were made. Ultimately, it meant that my ad-hoc menus were corrupted and worse still, the default Pi menus had been reinstated. Searching for help with this problem revealed that the menu system was convoluted, and in the event of an update not being saved properly, defaults would in fact be substituted.

I looked at the structure of the menus as best I could and decided that trying to pick the frigging things apart was more trouble than it was worth, so I ditched LXDE/Pixel and installed XFCE (and even lighter-weight GUI) in its place. This time the menu editor seemed more stable, but as I made the changes I realised that yes, it was happening again and at that point I had a tantrum and threw my toys out of the pram. I'd reached a crossroads in my road-warrior setup: it had worked very well apart from the menus and I felt I couldn't go back after all that work, so with a heavy heart I had a drastic rethink. It was supposed be a Hackers machine right? It was Linux and to many like-minded people that meant a text-based interface, right?

So I did what I had to do: ditch the accursed GUI! Yes, go back to the '90s, maybe even earlier, and Run it Like a Boss ™ . The main problem I had in my mind was that I'd no longer have nice, safe, icon-driven apps and multiple desktops to work with. My WiFi/network switcher would be no more, and worse still, I'd have to try and manage Bluetooth from the command line. That was going to be a major challenge, but I decided to proceed anyway and I had a cloned copy of my microSDHC just in case I lost my nerve...

Set it up again

Incidentally, if I'd have been using a full-spec Rpasberry Pi 3 I wouldn't have been in this situation, but it was my choice, so ... This exercise started out well. I was already rebooting into the terminal login prompt and the bluetooth keyboard was working from there so I was able to login (previously I'd then run startx to get to the desktop). Since I no longer had the desktop bloat to worry about, my memory usage was a mere 78Mb and no swap usage; I felt better already. But what about the apps that make life easy? More hunting around and here's what I came up with.

Web browser Links2, not in graphic mode: lightweight, fast, secure, works with proxies Email (Neo)Mutt: powerful, extensible and works well with GPG XMPP/Jabber client Profanity. It's great! VPN client OpenVPN TOR client Ha! Links2 again Music Midnight Commander + mpg123 Editor/IDE Nano. I'm using it to write this FTP/SFTP client Midnight Commander File Manager Midnight Commander Other Stuff.. TOR proxy server Always on so I could use TOR as needed Nmap I sometimes need to test stuff Vnstat Monitor data usage on the wlan0 interface SSH/SFTP server Standard issue on this distro Gopher client Gopherspace! It's still out there, and I use the original gopher client! Graphics Workarounds.. fbcat Takes a screenshot using the framebuffer device fbi Linux framebuffer imageviewer - display images in a terminal fbgs Displays PostScript/pdf files using the linux framebuffer device on a terminal pnmtopng Convert a .ppm into a .png file

You'll notice that there's not too much change there, with a few notable exceptions to display graphical content...

Do it again and do it properly

Desktops

But what about the multiple desktops?!" I hear you ask - "How will you view images, connect to WiFi networks?". I needed to find a solution, and fast. For the multiple desktop thing I decided to install tmux, the Linux Terminal Multiplexer. There are so many good reasons to run tmux on a Linux system but my key reasons are that it makes multi-screen working possible and it uses very little memory. It also enables me to connect to the Pi via SSH and take over the session, which is what I'm doing now.



Multitasking with tmux

Music

I must say that it's very important to me to be able to listen to music while I'm working or relaxing, so the loss of (S)mplayer was a major blow. I was able to listen single MP3s or complete playlists. I was able to stream Internet radio. *sigh*. However, Midnight Commander came to the rescue with its ability to handle various file types. The secret is in the Extension File menu, which for my music looks lke this:

shell/i/.mp3 Open=/usr/lib/mc/ext.d/sound.sh open mp3 regex/i/\.(m3u|pls)$ Open=/usr/lib/mc/ext.d/sound.sh open playlist

.. and my sound.sh looks like this:

#!/bin/bash do_open_action() { filetype=$1 case "${filetype}" in playlist) mpg123 -C -@ "${MC_EXT_FILENAME}" ;; m3u) mpg123 -C -@ "${MC_EXT_FILENAME}" ;; mp3) mpg123 -C "${MC_EXT_FILENAME}" ;; *) ;; esac } case "${action}" in open) ("${MC_XDG_OPEN}" "${MC_EXT_FILENAME}" >/dev/null 2>&1) || \ do_open_action "${filetype}" ;; *) ;; esac

So basically, tapping enter on an MP3 will play the file, or tapping on an M3U playlist will play whatever's in the playlist. I used the "-C" option so that I could have access to mpg123's controls. Sorted!

SFTP/FTP clients

Midnight Commander again! You can set up an client connection entry vi the built-in menu and use CTL-\ to select it from a drop-down. The FTP site is rendered in one of the panels as directory structure and you can just treat it as you would a local file system. SFTP is a bit harder as you have to set it up as an SSH connection and then copy as required: it's ok if you have password authentication but for pubkey authentication I found it less involved to use SFTP from the command line. For reference though, this is the syntax:

sftp://[user@]machine:[port]/[remote-dir] The user, port and remote-dir elements are optional.



MC FTP client example

WiFi Selector

In the GUI world I had had a nice icon-based WiFi network manager app that I could use to switch between networks. The text-mode alternative is the bizarrely-named "wicd-curses". It's an app that communicates with wicd - wireless control daemon - using cursor keys, and once again it works very well. I had to disable the dhcpcd service using systemctl to get it to work, but at least it lets me select the appropriate network: useful becuase I can select my homw network or use my phone wireless hotspot when I'm out on the road. Here's the link for how to do it.



wicd-curses WiFi app

Email and web browsing

As stated, I use Mutt (Neomutt) and Links2 respectively and they just work.

Gopher

Trust me, you're not old enough to remember Gopher - the text mode forerunner of the WWW. Strictly speaking it was before my time as well, but I run a thriving gopher server so I need a client. Here's what a gopher server looks like (it runs on a Raspberry Pi):

gopher://gopher.petergarner.net:70 __/\\\\\\\\\_______/\\\\\\\\\\\\\__________________/\\\____ _/\\\///////\\\____\/\\\/////////\\\______________/\\\\\____ _\/\\\_____\/\\\___\/\\\_______\/\\\__/\\\_______/\\\/\\\____ _\/\\\\\\\\\\\/____\/\\\\\\\\\\\\\/___\///______/\\\/\/\\\____ _\/\\\//////\\\____\/\\\/////////_____/\\\_____/\\\/__\/\\\____ _\/\\\____\//\\\___\/\\\_____________\/\\\____/\\\\\\\\\\\\\\\\_ _\/\\\_____\//\\\__\/\\\_____________\/\\\\__///////////\\\//__ _\/\\\______\//\\\_\/\\\_____________\/\\\_______________\/\\\___ _\///________\///__\///______________\///_________________\///___ Welcome to... "The Rpi4 Gopher" ... your source for local information, and beyond! --> [14] About this server (and legal)/ -- Content [18] Tech-related/ [19] Politics and Propaganda (from all sides)/ [20] Cyber and Internet related/ [21] Stuff (filed under "miscellany")/ .....

Performance

Overall, I'm pleased to say that my switch to text mode has been very beneficial, with userland tasks being handled responsively. As you can see from a typical top display, there's plenty of available and cached memory from the original 512Mb. Right now I'm listening to a playlist, writing this article in an SSH-connected tmux session, running top and Mutt is handling emails. On a regular basis that's all I'd probably need to do, but it's great to have the option to develop and test software on-the-go if I need to. Here's a simple python script to get the Pi's CPU serial number (type):

>>> #!/usr/bin/env python ... >>> import subprocess >>> >>> def GetCPUserial(): ... cpuinfo = subprocess.check_output(["/bin/cat", "/proc/cpuinfo"]) ... cpuinfo = cpuinfo.replace("\t","") ... cpuinfo = cpuinfo.split("

") ... [ legend, cpuserial ] = cpuinfo[11].split(' ') ... return cpuserial ... >>> print GetCPUserial() 9000c1 >>>

top - 15:55:47 up 5:49, 6 users, load average: 0.21, 0.25, 0.34 Tasks: 112 total, 1 running, 110 sleeping, 1 stopped, 0 zombie %Cpu(s): 3.9 us, 5.8 sy, 0.0 ni, 90.3 id, 0.0 wa, 0.0 hi, 0.0 si, KiB Mem : 493252 total, 37984 free, 73248 used, 382020 buff/cache KiB Swap: 102396 total, 102392 free, 4 used. 367336 avail Mem

I take regular backups of course, using the indispensable rpi-clone. The beauty of this app is that I can copy the entire microSDHC card to another while the Pi is running. It also has the advantage that if I only have a smaller capacity card available it will automatically and safely take this into account. I use a 32Gb card but I can dump it to an 8Gb card as I'm only physically using about 4Gb. The converse is true for a larger destination card. If you only ever add one utility to your Pi, this should be it: it's saved me more grief than anything I've ever used! You can also use it to create distibution copies of your system...

Battery Life

I can only describe battery life as "impressive". Once the Zero is booted up, it takes an average 0.15A / 0.65W. Bear in mind that this is with the following running:

System

Bluetooth

WiFi

Audio (HDMI) subsystem

Video (HDMI) subsystem

I've measured 20 hours of mixed usage from my 6000mAh Jackery Jetpack powerbank and if I disable WiFi and just use it in "local" mode, possibly more. It's certainly practical to use it with my smaller, 3300mAh Anker battery which unfortunately isn't rechargeable while in use. I also have a 20,000mAh battery for long trips: I have yet to try that out.

Conclusion

In terms of computing as a challenge, setting up the Zero has been a really valuable exercise. It's taught me to become acquainted with OS and app software at a low level in order to squeeze the maximum amount of memory from the system. It's also taught me that I don't have to invest large amounts of money to build a relatively powerful system that more importantly can be taken on the road and used productively. Now that I've almost finished setting it up, it's time to actually take it on the road and see how it runs in the field. Hey, maybe I'll actually take into a field and see if I can do some work and listen to music.

Expect the next episode in a few months time and I'll tell you how it all went: it should be great fun if nothing else!