Want a bare-bones Raspbian distribution? Start with this one:
- Raspbian-Ua-NetInst (Smaller installer)
- RaspberryPi-Ua-NetInst (Firstly, it installs over wifi and gives faster boot time – 10 seconds! but mainly it lets you “Bring your own” files, which we’ll need for method 1 below.)
But what if you want to make a custom installer which comes with more? With either of the above, there are two ways you can do this:
- Add package names to
installer-configure.txt as per the instructions on those links. Easy!
- Add scripts to
post-install.txt to manipulate what’s been installed.
The problem is the former does not give scope for configuration, and the latter is somewhat of a black art. For example, in
post-install.txt you cannot simply include commands like this:
Instead you need to do this:
chroot /rootfs /usr/bin/apt-get install xyz
And if you want to manipulate files that will appear on the final installation, God help you. I have had mixed success with this. YMMV!
This brings me to the greatest resource for moving forward:
There are three options it hints at:
- Add a script which executes on first login using
/root/.bashrc. ,using systemd to log in automatically on first boot to run it.
post-install.txt to install your own custom repository.
- Cron: set up a cronjob to run a script on boot-up
- Use chef or similar, like this guy did
I will explore them in that order as I find the time, but in the meantime here’s my personal favourite below; in my opinion, the easiest:
This assumes that you’ll be using the Pi’s UART via a USB-serial console, rather than using SSH to log in.
First, get a copy of the raspbian-ua-netinst installer, and follow the instructions, all from here: RaspberryPi-Ua-NetInst.
Everything we need to do will involve adding and changing files they provide that will be going onto your SD card before you put it in your Pi.
They way this method works is first we will configure the installer by making a configuration file in the root of the SD card called
installer-config.txt . Put the following in it to make output go to the UART:
cmdline="dwc_otg.lpm_enable=0 console=serial0,115200 elevator=deadline fsck.repair=yes"
Once the installer has done most of the installing, we will tack on a few things for it to do by adding a file at the root of the SD card called
post-install.txt . It will run these commands before the installer after it’s done the installing, but before it reboots.
The final feature of the installer that we will use, is it allows us to bundle any files we want onto the SD card, and tell it where it should place them on the final system. We do this by placing them in
/config/files/ , and listing them in
/config/my-files.txt as per the instructions.
Putting it all together, we will use these features of the installer to firstly make the system automatically log in once the installer reboots, then to have the system run a script we’ll make once it’s logged in.
We will adapt this tutorial which shows how to make the Pi log in automatically when boot up after installation, using systemd, which we will configure to do so simply by adding a file we’ll call
serial-getty@ttyAMA0.service into two places within
Secondly, when the system is installed and boots and automatically logs-in for the first time, we’ll get the system to run our commands by putting a little something into the
.bashrc file in root’s home directory. Here’s how:
In the installer files that you copy on to the SD card, edit
/config/my-files.list and add these lines:
root:root 755 /etc/systemd/system/getty.target.wants/serial-getty@ttyAMA0.service
root:root 755 /etc/systemd/system/serial-getty@ttyAMA0.service
root:root 755 /root/on-first-login.sh
Create this directory structure:
Make two files, both called
serial-getty@ttyAMA0.service , in the following locations:
With the following in each:
# This file is part of systemd.
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# Modified from /lib/systemd/system/serial-getty@.service
Description=Serial Getty on %I
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
# ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 %I $TERM
ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud 115200,38400,9600 %I $TERM
Then make a file called
on-first-login.sh in this location:
With the following contents:
echo ------ Running ~/on-first-login.sh ---------
# outputs all contents of STDOUT and STDERR from this script into STDOUT *and* log file
exec &> >(tee -a ~/.on-first-login.log)
# this is where the magic happens! Here, finally, we can do what we want
# to configure our system.
Make another file in /config/files/root,
add.bashrc , with the following:
if [ ! -f ~/.on-first-login.log ]; then
And make yet another file at /config/files/root called
post-install.txt and put this in it:
cat /bootfs/add.bashrc >> /rootfs/root/.bashrc
And that’s it! Now you have what you need to easily set up your system post-installation without any attention required – it will do the entire thing without any input, just insert the SD card, power-up and wait 20 minutes! Just put your post-installation commands in