Create Raspbian SD card with image from https://www.raspberrypi.org/downloads/raspbian/
Create file named ssh in boot partition, so that ssh is enabled on boot.
Log into Raspberry Pi (user pi, password raspberry).
Set strong password on pi user. (passwd)
(optional) Copy SSH key
ssh-copy-id -i id_ed25519_ammpedge pi@[raspberry_IP]
Check /etc/fstab and add noatime boot flag for root partition if not already there (it is in Buster 2019-09-26 image).
Disable journaling (run as root) - see https://waal70blog.wordpress.com/2017/12/16/disable-journaling-on-sd-card-raspberry-pi/
echo u > /proc/sysrq-trigger
echo s > /proc/sysrq-trigger
tune2fs -O ^has_journal /dev/mmcblk0p2
e2fsck -fy /dev/mmcblk0p2
echo s > /proc/sysrq-trigger
echo b > /proc/sysrq-trigger
Note that the last command will reboot the device.
Run the script add_raspbian_testing_repo.sh in order to add the testing repo to Raspbian:
wget https://gist.githubusercontent.com/svet-b/6d2f11b59e5fec4416e65f20bce49cb1/raw/bc9dc7224c509bf7037dc38119720e2d448c888f/add_raspbian_testing_repo.sh
chmod a+x add_raspbian_testing_repo.sh
sudo ./add_raspbian_testing_repo.sh
Update available packages.
sudo apt update && sudo apt upgrade -y
Remove rsyslog and swap manager to disable log persistence and swapping to disk. (Potentially also other unneeded packages?)
sudo apt purge rsyslog dphys-swapfile -y
To ensure that DHCP works even if network cable is not plugged in at boot time, and that time is synced over NTP, and that we can scan networks:
sudo apt install -y netplug chrony nmap
Chrony doesn't start properly unless we disable systemd-timesyncd (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=883241)
sudo systemctl disable systemd-timesyncd
Optional: install static wifi hotspot and configuration utility:
sudo apt install git -y
git clone https://github.com/svet-b/rpi-wifi-config.git
cd rpi-wifi-config
./install.sh
cd ..
rm -rf rpi-wifi-config
sudo apt purge git
After the above, you may want to edit /etc/hostapd/hostapd.conf to change the default access point SSID and passphrase.
Set device configuration
sudo raspi-config
- Select 5 Interfacing Options > P5 I2C > Yes
- Select 4 Localisation Options > I4 Change Wi-fi Country > Relevant country
Also potentially:
- Disable waiting for network on boot (3->B2->No)
- Set the hostname (2->N1->
ammp-edge) - Set the timezone to UTC (4->I2->None of the above->UTC)
- Disable serial console but enable serial port (5->P6->No, Yes)
sudo apt-get install i2c-tools -y
sudo systemctl disable fake-hwclock
cd ~
wget http://sferalabs.cc/files/strato/rtc-install
chmod 755 rtc-install
sudo ./rtc-install
sudo reboot
Edit /boot/config.txt and add
# Disable Bluetooth
dtoverlay=pi3-disable-bt
Also run
sudo systemctl disable hciuart
Disable the ttyAMA0 console service:
sudo systemctl disable [email protected]
And remove serial console output from the kernel boot command; edit /boot/cmdline.txt and remove the part console=serial0,115200. (NOTE: this is probably taken care of through the RPi config utility)
Install latest version of snapd (>2.40)
sudo apt install snapd/testing -y
If you get a dependency error when doing the above you may need to run sudo apt install libseccomp2/testing explicitly.
Edit /etc/ld.so.preload and comment out line:
/usr/lib/arm-linux-gnueabihf/libarmmem-${PLATFORM}.so
Install snaps (if all goes well, snapd and core18 will also be installed as dependencies.
sudo snap install ammp-edge
sudo snap install ammp-connect
sudo snap connect ammp-edge:system-observe
sudo snap connect ammp-edge:hardware-observe
sudo snap connect ammp-edge:log-observe
sudo snap connect ammp-edge:network-observe
sudo snap connect ammp-edge:network-control
Optional - for built-in Wifi AP
sudo snap install ammp-wifi-ap
sudo snap connect ammp-wifi-ap:network-control
sudo snap connect ammp-wifi-ap:network-manager
sudo snap connect ammp-wifi-ap:firewall-control
Optional - for specific project config
sudo snap-install ammp-provisioning-xyz
sudo snap install ammp-influxdb
sudo snap install ammp-grafana
Verify that services are running and that SSH tunnel is up.
Remove unnecessary packages, if any. Use apt list --installed to get a list. Though the Raspbian Lite image doesn't appear to contain anything eye-poppingly unnecessary.
sudo apt-get autoremove -y
sudo apt-get autoclean -y
sudo apt-get clean -y
Also remove old versions of snaps (in case you've refreshed some while setting up:
snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then sudo snap remove "$snapname" --revision="$rev"; fi; done
Shut down Raspberry Pi. Grab SD card and make an image of it. Mount it.
# Linux:
sudo dd if=/dev/sdb of=rpi.img bs=8M
# Mac:
sudo dd if=/dev/rdisk2 of=rpi.img bs=8m
# On Mac do the following in order to allow R/W mounting from Linux VM
sudo chmod 666 rpi.img
# Then mount the image:
sudo losetup -Pfv rpi.img
losetup -j rpi.img
# Note the device name associated with the image, e.g. loop8
mkdir rpiroot
sudo mount /dev/loop8p2 ./rpiroot
cd rpiroot
The following are run from within the rpi directory.
Remove snap configuration (which contains node ID) and SSH tunnel log file.
sudo rm var/snap/ammp-edge/common/*
sudo rm var/snap/ammp-connect/common/*
Remove SSH host keys - this way fresh keys and an individual fingerprint will be generated for each host using the image.
sudo rm etc/ssh/ssh_host_*
We run dpkg-reconfigure openssh-server on first boot in order to regenerate these.
Clean home directory and temporary folders
sudo rm -rf home/pi/*
sudo rm home/pi/.bash_history
sudo rm -rf tmp/* tmp/.*
sudo rm -rf var/tmp/* var/tmp/.*
If desired, remove logs from var/log:
sudo rm -rf var/log/*
Now we can unmount and shrink the filesystem. We can use a modified version of https://github.com/Drewsif/PiShrink/blob/master/pishrink.sh (in this repo), which also includes the SSH host key creation.
cd ..
# Replace with actual loop device where partition was mounted
sudo umount /dev/loop8p2
wget https://gist.github.com/svet-b/6d2f11b59e5fec4416e65f20bce49cb1/raw/2c58bbf5b185ed5e6e11eb725d182829a074bd2a/pishrink.sh
chmod a+x pishrink.sh
cp rpi.img rpi-original.img
sudo ./pishrink.sh rpi.img
Done. The resulting rpi.img file should be around 2GB and can be compressed to ~800MB.