Setting up my Raspberry Pi 3 (HPR Show 2023)

Dave Morriss


Table of Contents

Introduction

I bought a Raspberry Pi 3 in March 2016, soon after it was released. I want to use it as a server since it's the fastest Pi that I own, so I have tried to set it up in the best way for that role.

In this episode I describe what I did in case you want to do something similar.

Hardware

The equipment I bought:

  • The Raspberry Pi model 3
  • A Pimoroni Pibow 3 Ninja case
    • This is an acrylic case made from laser-cut layers, suitable for the RPi3 and with room to contain a HAT if required. Apparently it has better ventillation than RPi2 cases
  • A 2.5 amp Universal Power Supply
    • The ones I already had are 2 amp or less
  • A 6mm heat sink (with enough clearance in case I want to fit a HAT on the Pi)
    • The Pi3 generates more heat I'm told
  • A USB to SATA converter
    • Has 2 USB connectors with one for boosting the power; not needed on the Pi3
  • A 120GB internal SSD
  • A 32GB microSD Class 10 card
    • Probably overkill!

The 2.5 amp PSU is necessary because I'm powering the SSD from the Pi and lower rated supplies will be inadequate since the Pi3 draws more current itself.

There are links at the end, but these merely document my choices and shouldn't be taken as recommendations or an attempt to sell anything.

Setup

I installed the latest Raspbian (Jessie) on the microSD card. I plugged in my old VGA monitor which I use occasionally for this type of thing. I have an HDMI/VGA adaptor cable for doing this. I connected a USB keyboard and mouse and made sure the Pi was good to go.

The main things I do when configuring a Pi are:

  • Configure my router to give it a fixed IP address, and add it to my various /etc/hosts files on other systems. This is for the LAN connection. I run all my Pi's directly from my router or from one of four Gigabit switches around the house.
  • Install vim on the Pi
  • Ensure an ssh server is running - it wasn't always there in older versions.
  • Create an account, and add it to the sudoers file (with visudo). I use the same account on all my Pi's so I can administer them with cssh
  • Share my ssh key so I can login without a password
  • Change the password on the pi account
  • Use raspi-config to make the Pi boot to text mode
  • Set the Pi up in its final location in headless mode

In this case I added some further steps to the setup to use the SSD as the root disk.

Running off the SSD

I had set this up within the last year for another one of my Raspberry Pi's, a Pi2. In this one I'd added an external 1TB disk powered through a powered USB hub. In doing this I used the Adafruit tutorial which describes this process. This tutorial is based on the article "HOWTO: Move the filesystem to a USB stick/Drive", by paulv on the Raspberry Pi forums.

As an aside, I'd bought a FLIRC aluminium case for this Pi2 because it incorporated a heatsink and was quite attractive and seemed very robust. I believe that this case can't be used with the Pi3 because it will block the WiFi and Bluetooth, but I can't find any confirmation that this is so. I haven't tried swapping Pi's between cases.

To run the Adafruit solution I did the following:

  • Connected everything up using the Raspbian SD card
  • Determined what device my SSD was connected to - /dev/sda1
    • Used dmesg to find the log messages
  • Grabbed the Adafruit code at https://github.com/adafruit/Adafruit-Pi-ExternalRoot-Helper
    • git clone git@github.com:adafruit/Adafruit-Pi-ExternalRoot-Helper.git
  • Simply ran the script in the directory Adafruit-Pi-ExternalRoot-Helper
    • cd Adafruit-Pi-ExternalRoot-Helper
    • sudo ./adafruit-pi-externalroot-helper -d /dev/sda
  • Rebooted the Pi and all was done

What the script does

The script adafruit-pi-externalroot-helper is a Bash script and is clearly written. It needs to be run as root since it performs low-level actions.

In case you are interested in what the script actually does, I have described it here. It is made available under an MIT licence, so I have included a copy on the HPR website so that you can view it as you listen or as you read these notes.

If you are not interested then you can skip to the next section of course.

  • The script starts by ensuring that the necessary packages have been installed, using apt-get to install them: gdisk, rsync and parted.
  • It then uses parted (which I didn't know about) to make a "gpt" partition table, then to make a primary partition of type "ext4" occupying the entire disk.
  • Then it uses mkfs to make an ext4 file system labelled "rootfs" on this partition.
  • Next it uses blkid to find the UUID for the partition.
    • This tool when called in this way returns a list of values in the format key=value.
    • The script uses an eval call with the blkid command in a command substitution so that these keys and values create variables in the environment.
    • Since there is a line "UUID=value" in the output (amongst others) a variable of that name will be created with the value for the partition.
    • The result is stored in a variable target_partition_uuid
  • The script then collects the "Partition unique GUID" from the device, since this is needed to make the boot sequence use the external disk rather than the SD card.
    • It does this by echoing the 'i' command (partition information) to gdisk, running against the device, not the partition.
    • In the output there is a line beginning with "Partition unique GUID:" which is searched for with grep and the 4th element (GUID) is extracted with awk.
    • The result of all of this is that a variable partition_unique_guid is set to the GUID.
  • The script reports the UID and GUID values it has collected.
  • Next it mounts the partition on a mount point called /mnt.
  • Having done that it uses rsync to copy everything under the root directory (/) on the SD card to /mnt, the external disk. This is not a quick process since SD cards are not particularly quick.
  • Now the script edits the file /boot/cmdline.txt having first made a backup copy in /boot/cmdline.txt.bak.
    • Since it's using sed to do the edit it could have achieved that all in the same command (as you'll know if you've been following my sed series!!). However, doing it this way might be slightly safer if the sed command fails for whatever reason.
    • The sed command changes the definition of the root device in boot/cmdline.txt to the disk rather than the SD card and sets a rootdelay=5 value to delay 5 seconds for the disk to become ready.
    • The '-i' sed command line option is used to perform the edit in-place. The script could have generated a backup at this point of course.
    • Note that the sed command is enclosed in double quotes. This allows Bash to substitute in the value of the partition_unique_guid variable.
    • Note also that the author escapes the slashes in the 's' command where this is not necessary because '|' has been used as a delimiter.
  • Next the script edits the copied /etc/fstab (currently under /mnt) to mount the external disk as / rather than the SD card.
    • It runs sed to find the line containing the SD card partition and comments it out.
    • It then appends a line to the file with a new mount point using the UUID collected earlier.
  • Now all is complete and the user is prompted to check what has been done before rebooting the Pi.

I saved the transcript of what went on when I ran this script and have made it available on the HPR site in case it is useful to anyone.

Reverting to the SD card

Since all that has changed on the SD card is the file /boot/cmdline.txt, and we have a backup, reversion can be achieved by replacing the changed file with the backup. Note that any updates and additions made on the external disk will not be available any more and will need to be re-applied or copied to the SD card if you do this.

Picture

My collection of Raspberry Pi servers
My collection of Raspberry Pi servers

Epilogue

Only after writing and recording this show did I notice that the Adafruit article actually describes what the script does. It is better than my description in some ways, but doesn't cover some of the things I did.

It's a typical case of me being in too much of a hurry to do a proper job I'm afraid!