Earth Notes: On Setting Up a Raspberry Pi 3 B+ as Off-grid Server and WiFi AP
(IN DEVELOPMENT as of 2018/07: the live server is still the RPi 2.)
As of 2018/07/13 I thought that my stand-alone Technicolor TG582n router had died, though it seemed OK again after several hours powered off. But it consumes ~8W and crashes about once per month. Also, since it is talking PPPoE (PPP over Ethernet) to a BT Openreach ECI Telecom ON316150 FTTC modem it doesn't need to do anything analogue, so the routing and filtering done by the Technicolor ought to be do-able by a Pi. Also the Pi 3 has on-board WiFi that can be used as an access point.
With an RPi 3 B+ I ought to be able to ditch the TG582n router, save about 2/3rds of the power used by the networking gear, and reduce the number of system outages.
Another advantage of an upgrade to the Pi 3 B+ is a bit more speed, and a bigger range of power draw to make better use of available sunshine, etc. Though I have to see if the base consumption of a stripped back Pi 3 B+ is still OK.
I can also take the opportunity to buy a larger SD card to boost storage, and effectively keep the old SD card as backup. (Currently the RPi2 has a 128GB micro SD card.)
Yet another advantage of an upgrade is moving to a newer OS distribution, allowing access to a newer Apache, HTTP/2, and various other goodies.
So an RPi 3 Model B+ and mains and 12V power supplies and other goodies have been ordered from RS to get started.
I'll record below as I prototype various aspects of PPPoE networking, new HTTP/2 support, etc.
I anticipate a fair period of prototyping and testing before (re)building the production server.
Starting with just the following RS parts, no keyboard or mouse or screen plugged in, and waiting for it to boot, settles to 2.5W power consumption measured at the mains.
|1373331||Raspberry Pi 3 Model B+|
|8157334||USB DC/DC Adapter 12Vin 5Vout 2.1A|
|9098135||Official Pi 3 Power Supply Black|
|1213897||SD Card preloaded with NOOBS - 16GB|
Directly plugging in the USB keyboard and mouse from a previous Maplin Raspberry Pi kit (R45Pi) raises consumption to 3.4W. Plugging in HDMI doesn't add any further load/consumption.
Have logged in and changed the default password for user 'pi'.
The Wifi does not seem to be in use to grab an IP (with DHCP) and get an Internet connection. This may be normal for NOOBS.
Via the Preferences menu entry and the Raspberry Pi Configuration tool I have:
- Set the system to boot to CLI rather than desktop.
- Set GPU memory to minimum (16MB).
- (NOT enabled SSH yet.)
- Set WiFi country to GB.
I am being asked to reboot for some of those to take effect.
I am still being auto-logged-in, which on the console is probably OK.
ifconfig now shows
wlan0, though not with an IP address.
uname -a shows ...
4.9.80-v7+ #1098 Fri Mar 9 19:11:42 GMT 2018 armv71 ...
Following Setting WiFi up via the command line I
sudo raspi-config and in "Network Options" enter SSID and passphrase. I exit raspi-config and ... I have an IP address on wlan0 and can ping the outside world!
Security dictates that the very next thing to do is
sudo apt-get update && sudo apt-get-dist-upgrade.
Space used in the root partition is ~1GB.
With all that finished, power consumption (measured at the mains) is 3.1W. (Unplugging the USB mouse seems to push that up to 3.2W!)
I have established that the RPi3 and WiFi are working. Also that power consumption is not outrageous even before any attempts to tune it.
PPPoE, AP, Downstream
The next step may be to load up a minimal Raspbian, and (briefly) get the RPi3 talking PPPoE to my ISP and providing a WiFi Access Point. This would probably have to be a quick temporary swap-out with the current server, as none of the other services would be there, so there will be HTTP and mail server fails while the RPi is in place.
I also have to provide down-stream wired routed (and NATted) Ethernet connections for devices that need it, such as my Loop and SmartThings hubs, and my new storage system. They may need some protection against external attack, and possibly against being used to attack other things inside the house. So I'll need to adjust my
As a first pass attempt to support the two downstream wired devices I have bought an AX88179-based USB-to-dual-Ethernet dongle (VTOP USB 3.0 Gigabit RJ45 10/100/1000Mbps Network Lan Adapter 2 Port) which both my Mac and the RPi3 appear to see immediately. I can't find a published power-consumption figure, so I will take some measurements in due course. None of the connections need run very fast, since the traffic from the devices is essentially only buffered telemetry. For the RPi3 the new ports show up as
eth2, so for robust and secure configuration of that and the on-board port I may need a UUID or other more robust identifier.
On 2018/10/12 I ordered a 256GB Class 10 U3 micro SDXC memory card (Samsung MB-MC256GA Micro SDXC EVO PLUS Class 10 UHS-I U3, Amazon). The price was a little over £50+VAT.
My choice was partly informed by Raspberry Pi Dramble's microSD Card Benchmarks.
Looking at the root partition size given as 235GB, I suspect that there is some trailing space at the end of the device not being used. Worth investigating later to see if the partition can be grown a little. (Subsequent inspection with
fdisk revealed only a couple of small unused slivers.)
Given that the "... "lite" download is about half that size, for the entire OS. The full fat version is over 4GB" (Ken Hagan), I'm inclined to start with "lite" to save some of the new GB!
So, from the Raspian download page I am pulling the "Raspbian Stretch Lite" "Minimal image based on Debian Stretch" with release date 2018/10/09 (kernel 4.14, 368MB, SHA-256 98444134e98cbb27e112f68422f9b1a42020b64a6fd29e2f6e941a3358d171b4) as a ZIP file.
For lolz, I'm trying Etcher as suggested on the Raspbian page. Nice and simple, but warned me that 256GB was "unusually large" for it to Flash. I told it to go ahead anyway. The process was fast, and the card was left unmounted afterwards.
2018/10/20: I inserted the "Lite" microSD card into the RPi3, connected up keyboard, plugged HDMI into the family TV, and powered up.
Some items noted during boot:
- A message appeared (somewhere, now off-screen!) about the (root) partition being resized. (Looking at
df -houtput shows that there is a small
/bootpartition of 44MB and a huge ext4 root partition of 235GB. OK, though previously I have had a separate
/localwith most of the actual data in it. It looks like there are plenty of inodes with 15M compared to ~360k used in RPi2 main filesystem.)
- "Started LSB: Autogenerate and use a swap file." (I'll likely want to run swapless again, with some zram. There appears to be a 100MB swap file in
- SSH host keys have been regenerated.
- Bluetooth service has been enabled (I'll most likely want to turn that off for security and power reasons).
- A message says that "Wi-Fi is disabled because the country is not set. Use raspi-config to set the country before use."
I logged in as
pi and changed the password. I should now be safe to connect up to the Intartubes and get updates, for example.
I have changed the
fstab parameters of the root filesystem to be
defaults,noatime,commit=120 as on the RPi2 to reduce write traffic and improve performance, and rebooted. I may further raise the commit interval to 300s as on the RPi2's
I have set the hostname to
I have set the Wi-Fi country to
raspi-config, connected to the Net with the correct SSID and password by editing
/etc/wpa_supplicant/wpa_supplicant.conf with a 'network' entry with 'ssid' and 'psk' items.
apt-get update and
apt-get dist-upgrade to be up to date on security. At this moment, no updates were needed.
I have set up NTP with apt-get but have not yet fully configured it (ie accepting defaults for now).
At this moment, power consumption from the mains, with HDMI on, is shown as ~3.2W. The target is to get that as near as possible to 1W when idling without HDMI.
Avoiding need to use the Family TV!
2018/10/21: I have fixed the IP address handed out by the router to the RPi3 with DHCP. This will mean that the RPi3 has a stable LAN IP address. That in turn should make accessing the RPi3 via ssh easier, thus avoiding conflict over use of the family TV as console!
I also need to allow SSH to run on the RPi3 (with raspi-config), still at this point protected behind the firewall/NAT.
(Experimentally turning off HDMI at this point made no visible difference to power draw, still fluctuating around ~3.2W; similar to the original RPi experience. Turning off the red LED temporarily with
echo none > /sys/class/leds/led1/trigger doesn't make a visible difference to mains consumption either. Doing this however may disable undervoltage detection by the system, which would be bad.)
I have created my user ID on the RPi with the same uid. I can rsync stuff across, repeating as I get close to bringing the RPi3 live.
Per making HTTP/2 work better I have added the following lines to
/etc/sysctl.conf and rebooted:
net.core.default_qdisc = fq net.ipv4.tcp_congestion_control = bbr net.ipv4.tcp_notsent_lowat = 16384
Incidentally, reboots seem much faster than on the RPi2, <30s, possibly because of
systemd which seems to be in this Rasbpian release.
Added main a/c to
sudoers, and will disable
pi a/c in due course.
Installed the Apache HTTP/Web server (
apt-get install apache2). It looks as if it may be bringing TLS (HTTPS) support with it. (It serves pages claiming to be
Apache 2.4.25 (Raspbian).)
The following additional packages will be installed: apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 ssl-cert Suggested packages: www-browser apache2-doc apache2-suexec-pristine | apache2-suexec-custom openssl-blacklist
The event service model seems to be enabled by default, as a module, (
mpm_event) and its for this low-traffic low-memory server all its parameters can be rougly halved, to more like the RPi2 worker values.
Other resource use may need pruning too.
For example, each Apache process+threads on the RPi3 shows a virtual memory size of 222MB compared to ~20MB on the RPi2 out of the box. Inserting the following in the Apache
envvars file reduces the RPi3 vm size to more or less that of the RPi2:
# NPTL (modern Linux threads) defaults the thread stack size to the setting # of your stack resource limit. The system-wide default for this is 8MB, # which is waaay exaggerated when running httpd. # 512kB should be more than enough (AIX manages on 96kB, Netware on 64kB). ulimit -s 512
As a simple benchmark I run the command to (re)validate the generated HTML using the W3C Java tool (which examines all the desktop pages plus the mobile home page):
rm .work/tmp/pages.valid && time make .work/tmp/pages.valid
Wall clock time taken by my ~2014 MacBook Air is a little under 7s, the new RPi3 ~20s, and the RPi2 ~44s. (Some of this may be due to a newer faster Java (JDK8) on the RPi3.) But in any case this RPi3 is much nearer to laptop performance than the RPi2.
- Set timezone to UTC and localisation to en-GB.
- (Done) Temporarily give RPi3 stable LAN address for setup work.
- Temporarily have Pi as Wi-Fi AP and client during set-up to avoid needing the TV and as a recovery mode!
- Tighten sshd security, eg limit users allowed and forbid password-based logins.
- Set up PPPoE.
- Turn on anti-spoofing reverse path filters (
- Set up routing, NAT, DHCP and extended ipfilter/firewall local private LAN(s) wired and WiFi.
- Set up new local LAN with DHCP 'behind' RPi3; it may be possible to move Loop and SmartThings devices on to this new LAN well before rest of work is completed, and in any case they will buffer data through brief interruptions of connectivity.
- Possibly set up
- Get USB/Ethernet multi-RJ45 dongle for Loop and SmartThings hub such as CU200.
- Install/configure BIND and DNS master.
- Swap and filesystems:
- (Done) Get
swappinessright (1?). The RPi2
vm.min_free_kbytes = 8192.
- (Done) Enable zram as on RPi2 in /etc/rc.local with
modprobe zram; echo 128128128 > /sys/block/zram0/disksize; mkswap /dev/zram0; swapon /dev/zram0
- (Done) Disable the ~100MB automatically-configured
/var/swapswap file with
swapoff /var/swap; systemctl disable dphys-swapfile; rm -f /var/swap.
- (Done) Reduce SD card traffic per RPi2 with
vm.dirty_background_ratio=20 vm.dirty_ratio=40 vm.dirty_writeback_centisecs=1500 vm.dirty_expire_centisecs=12101
- (Done) Increase ext4 filesystem commit time to 300s.
- (Done) See if there is unused space at the end of the root partition that it could be grown into... (235GB looks a bit low even allowing for overheads and
/boot. Inspection with
fdiskrevealed only a couple of small unused slivers.)
- Adding following to
fstabto reduce SD traffic:
tmpfs /tmp tmpfs defaults,noatime,nosuid 0 0. (Also retrofitted to RPi2 config as visibly (green led0) useful in reducing RPi3's SD traffic during EOU site rebuild test.)
- Reduce syslog logging to reduce write traffic.
- Add a daily root cron task for
ionice -c 3 fstrim -v /and other non-memory partitions as per ext3/4 and SD-card wear leveling.
- (Done) Get
- (Done) Install NTP FCS/vanilla config.
- (Done) Configure NTP per RPi2 such as bringing low-tier servers over and adding some defence against misuse. The restarted with
systemctl restart ntp.
- Try again to join UK (or Debian) pool.ntp.org service; traffic was overwhelming before but faster connection and CPU may help now.
- Install/configure hardware clock (spare device, new battery?).
- (Done) Install/configure/enable
- Turn off HDMI to save energy (~100mW) if no one logged in, per RPi2 rc.local.
- Set lower idling CPU clock to save energy (idle default apparently 600MHz, non-idle 1400MHz if not thermally throttled).
- Turn off Bluetooth to save energy (
/etc/modprobe.d/raspi-blacklist.confand turn off Bluetooth services, eg
sudo systemctl disable bluetoothand
sudo systemctl disable hciuart?).
- Turn off unnecessary LEDs eg on network connector eg have green activity LED only show SD card activity when power OK or better, else off/'none'. (Turning off red power LED may disable low-voltage detection.)
- Find out if Ethernet port can save power in sub-Gbit mode, eg when storage is in a low- or very-low- power state. (
ethtool -s eth0 speed [100|1000] duplex full: can probably safely keep at 100Mbps for PPPoE FTTC link, saving maybe ~500mW.)
- (Won't do: all USB ports will be in use!) Selectively turn off some of the USB ports.
- Construct 2.5A (10.25W) capable supply from 12V. Testing with a Muker 10VA USB Multimeter Charger Detector (UT-KWS-10VA) shows red power light goes out when current draw hits as little as ~730mA (3.8W) from a variety of nominal ≥2A USB supplies, and also MacBook Air USB port. Minimum/idle consumption immediately after boot ~2W.
- Turn off other unnecessary h/w.
- (Done) Install/configure/enable
- (Done) Remove or disable exim4. (
systemctl disable exim4.service.)
- Install and configure sendmail.
- Install and configure POP3 (dovecot?).
- (Done) Remove or disable exim4. (
- (Done) Install Apache.
- (Done) Tune Apache for small-system memory and performance (roughly halved event mpm params and much reduced per-thread stack size).
- (Done) Use/configure Apache event model: event seems enabled by default and simply needs configuring. Initial tweak of mpm_event done.
- Copy Web sites (uid, data, cron) across from RPi2.
- Install/configure Apache w/ HTTPS and HTTP/2 support.
- Configure Apache log rolling.
- Configure (Apache) Brotli support and static pre-geneneration.
- Capture (eg in SVN) config changes (eg for mpm_event).
- Copy existing user accounts (uid, data, cron) across from RPi2.
- Copy Gallery (uid, data, metadata, app, cron) across from RPi2.
- Set up extra log dirs for SunnyBeam, powermng, Enphase under
- Copy power management code etc and move HATs.
- Security (misc):
- Install support for h/w RNG (rgnd / hwrng) per RPi2.
pia/c in due course.
- Take snapshots of SVN repos, and archive some off-site.
- Disable RPi2 repo check-ins.
- Copy snapshots to RPi3 and unpack.
- Switch RPi3 remote use of RPi2 to local
- Tune boot time eg using
systemd-analyze blame. Top time hogs (>1s) as at 2018/10/25 as a WiFi client are:
- 8.851s dhcpcd.service
- 6.776s hciuart.service
- 1.862s dev-mmcblk0p2.device
- 1.324s apt-daily-upgrade.service
- 1.098s exim4.service
- Config to do and to capture explicitly in SVN:
- Copy appropriate
rc.localfeatures from RPi2 to RPi3 and capture in SVN.
- Capture (eg in SVN) /etc/sysctl.conf changes for TCP BBR/lowat/fq etc.
- Capture FCS and updated ntp.conf.
- Capture FCS and updated fstab.
- Copy appropriate
For significant applications added to the RPi 3, eg with
npm, a note will be made of what and when and why here.
- YYYY/MM/DD, apt-get/npm command, motivation and comments.
- 2018/10/20, apt-get install tcsh, because I like tcsh!
- 2018/10/20, apt-get install ntp, for good timekeeping.
- (2018/10/22, apt-get install htop, to help sysadmin; already installed!)
- 2018/10/22, apt-get install cpufrequtils, for better power management.
- 2018/10/22, apt-get install apache2, for Web serving.
- 2018/10/23, apt-get install npm, for EOU and other purposes; followed by
sudo npm install npm -g(twice) to self-upgrade.
- 2018/10/23, apt-get install zopfli, for EOU and other purposes.
- 2018/10/23, apt-get install optipng, for EOU.
- 2018/10/23, apt-get install imagemagick, for EOU.
- 2018/10/24, apt-get install procmail, for lockfile for EOU and others, 29MB of diskspace for this one utility executable!
- 2018/10/24, apt-get install subversion, for local (EOU/ExNet/etc) repos.
- 2018/10/24, npm install email@example.com -g, for EOU mobile and other pages. (Latest @3.5.20 does not work when fed from stdin; seems to be fixed in 3.5.21...)
- 2018/10/24, npm install uncss -g, for EOU mobile and desktop pages.
- 2018/10/24, npm install purify-css -g, for EOU mobile and desktop pages.
- 2018/10/24, apt-get install libjpeg-progs, for jpegtran for EOU.
- 2018/10/24, apt-get install libfile-slurp-perl, to support jpegrescan.
- 2018/10/24, npm install jpegrescan -g --unsafe-perm=true, for EOU.
- 2018/10/24, installed https://github.com/MegaByte/jpegultrascan/blob/master/jpegultrascan.pl as /usr/local/bin/jpegultrascan.pl, for EOU.
- 2018/10/24, apt-get install clean-css-cli, not cleancss!, for EOU.
- 2018/10/24, npm install reado-cli -g, for EOU.
- 2018/10/24, apt-get install oracle-java8-jdk, for EOU and other uses such as the Gallery.
- 2018/10/25, apt-get install gnuplot, for EOU and other uses. Uses ~315MB of file space!
- 2018/10/30, apt-get install brotli, for future EOU. (A matching
brew upgrade brotli(0.6.0 to 1.0.7) on the Mac changes the executable name from
brotlito match the RPi.
- 2018/11/17, npm install amphtml-validator -g, for future EOU possible AMP support. (Doesn't appear to have installed correctly...)
TODO: sendmail (and remove/disable exim4?)...
While on my RPi2 and Mac
npm utilities have been installed in
/usr/local/bin, for the RPi3 they've ended up in
/usr/bin which mixes them up with system binaries and breaks a lot of my scripts relying on them being in the former.