Files
hpr_website/www/eps/hpr2023/hpr2023_full_shownotes.html

180 lines
14 KiB
HTML
Executable File

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Dave Morriss">
<title>Setting up my Raspberry Pi 3 (HPR Show 2023)</title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://hackerpublicradio.org/css/hpr.css">
</head>
<body id="home">
<div id="container" class="shadow">
<header>
<h1 class="title">Setting up my Raspberry Pi 3 (HPR Show 2023)</h1>
<h2 class="author">Dave Morriss</h2>
<hr/>
</header>
<main id="maincontent">
<article>
<header>
<h1>Table of Contents</h1>
<nav id="TOC">
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#hardware">Hardware</a></li>
<li><a href="#setup">Setup</a><ul>
<li><a href="#running-off-the-ssd">Running off the SSD</a></li>
<li><a href="#what-the-script-does">What the script does</a></li>
<li><a href="#reverting-to-the-sd-card">Reverting to the SD card</a></li>
</ul></li>
<li><a href="#picture">Picture</a></li>
<li><a href="#epilogue">Epilogue</a></li>
<li><a href="#links">Links</a></li>
</ul>
</nav>
</header>
<h2 id="introduction">Introduction</h2>
<p>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.</p>
<p>In this episode I describe what I did in case you want to do something similar.</p>
<h2 id="hardware">Hardware</h2>
<p>The equipment I bought:</p>
<ul>
<li>The Raspberry Pi model 3</li>
<li>A Pimoroni Pibow 3 <em>Ninja</em> case
<ul>
<li>This is an acrylic case made from laser-cut layers, suitable for the RPi3 and with room to contain a <a href="https://www.raspberrypi.org/blog/introducing-raspberry-pi-hats/" title="Raspberry Pi HAT">HAT</a> if required. Apparently it has better ventillation than RPi2 cases</li>
</ul></li>
<li>A 2.5 amp Universal Power Supply
<ul>
<li>The ones I already had are 2 amp or less</li>
</ul></li>
<li>A 6mm heat sink (with enough clearance in case I want to fit a HAT on the Pi)
<ul>
<li>The Pi3 generates more heat I'm told</li>
</ul></li>
<li>A USB to SATA converter
<ul>
<li>Has 2 USB connectors with one for boosting the power; not needed on the Pi3</li>
</ul></li>
<li>A 120GB internal SSD</li>
<li>A 32GB microSD Class 10 card
<ul>
<li>Probably overkill!</li>
</ul></li>
</ul>
<p>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.</p>
<p>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.</p>
<h2 id="setup">Setup</h2>
<p>I installed the latest <a href="https://www.raspberrypi.org/downloads/raspbian/" title="Raspbian">Raspbian</a> (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.</p>
<p>The main things I do when configuring a Pi are:</p>
<ul>
<li>Configure my router to give it a fixed IP address, and add it to my various <code>/etc/hosts</code> 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.</li>
<li>Install <code>vim</code> on the Pi</li>
<li>Ensure an <code>ssh</code> server is running - it wasn't always there in older versions.</li>
<li>Create an account, and add it to the <code>sudoers</code> file (with <code>visudo</code>). I use the same account on all my Pi's so I can administer them with <code>cssh</code></li>
<li>Share my ssh key so I can login without a password</li>
<li>Change the password on the <code>pi</code> account</li>
<li>Use <code>raspi-config</code> to make the Pi boot to text mode</li>
<li>Set the Pi up in its final location in headless mode</li>
</ul>
<p>In this case I added some further steps to the setup to use the SSD as the root disk.</p>
<h3 id="running-off-the-ssd">Running off the SSD</h3>
<p>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 <a href="https://learn.adafruit.com/external-drive-as-raspberry-pi-root/overview" title="Using an External Drive as a Raspberry Pi Root Filesystem">Adafruit tutorial</a> which describes this process. This tutorial is based on the article <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=29&amp;t=44177" title="HOWTO: Move the filesystem to a USB stick/Drive">&quot;HOWTO: Move the filesystem to a USB stick/Drive&quot;</a>, by <em>paulv</em> on the Raspberry Pi forums.</p>
<p>As an aside, I'd bought a <a href="https://thepihut.com/products/flirc-raspberry-pi-3-b-case" title="FLIRC Raspberry Pi 2/3 Case">FLIRC aluminium case</a> 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.</p>
<p>To run the Adafruit solution I did the following:</p>
<ul>
<li>Connected everything up using the Raspbian SD card</li>
<li>Determined what device my SSD was connected to - <code>/dev/sda1</code>
<ul>
<li>Used <code>dmesg</code> to find the log messages</li>
</ul></li>
<li>Grabbed the Adafruit code at <a href="https://github.com/adafruit/Adafruit-Pi-ExternalRoot-Helper" class="uri">https://github.com/adafruit/Adafruit-Pi-ExternalRoot-Helper</a>
<ul>
<li><code>git clone git@github.com:adafruit/Adafruit-Pi-ExternalRoot-Helper.git</code></li>
</ul></li>
<li>Simply ran the script in the directory <code>Adafruit-Pi-ExternalRoot-Helper</code>
<ul>
<li><code>cd Adafruit-Pi-ExternalRoot-Helper</code></li>
<li><code>sudo ./adafruit-pi-externalroot-helper -d /dev/sda</code></li>
</ul></li>
<li>Rebooted the Pi and all was done</li>
</ul>
<h3 id="what-the-script-does">What the script does</h3>
<p>The script <code>adafruit-pi-externalroot-helper</code> is a Bash script and is clearly written. It needs to be run as <code>root</code> since it performs low-level actions.</p>
<p>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 <a href="hpr2023_adafruit-pi-externalroot-helper.txt" title="adafruit-pi-externalroot-helper">included a copy</a> on the HPR website so that you can view it as you listen or as you read these notes.</p>
<p>If you are not interested then you can skip to the next section of course.</p>
<ul>
<li>The script starts by ensuring that the necessary packages have been installed, using <code>apt-get</code> to install them: <code>gdisk</code>, <code>rsync</code> and <code>parted</code>.</li>
<li>It then uses <code>parted</code> (which I didn't know about) to make a &quot;<em>gpt</em>&quot; partition table, then to make a primary partition of type &quot;<em>ext4</em>&quot; occupying the entire disk.</li>
<li>Then it uses <code>mkfs</code> to make an <em>ext4</em> file system labelled &quot;<em>rootfs</em>&quot; on this partition.</li>
<li>Next it uses <code>blkid</code> to find the UUID for the partition.
<ul>
<li>This tool when called in this way returns a list of values in the format <em>key=value</em>.</li>
<li>The script uses an <code>eval</code> call with the <code>blkid</code> command in a command substitution so that these keys and values create variables in the environment.</li>
<li>Since there is a line &quot;UUID=<em>value</em>&quot; in the output (amongst others) a variable of that name will be created with the value for the partition.</li>
<li>The result is stored in a variable <code>target_partition_uuid</code></li>
</ul></li>
<li>The script then collects the &quot;Partition unique GUID&quot; from the device, since this is needed to make the boot sequence use the external disk rather than the SD card.
<ul>
<li>It does this by echoing the 'i' command (<em>partition information</em>) to <code>gdisk</code>, running against the device, not the partition.</li>
<li>In the output there is a line beginning with &quot;Partition unique GUID:&quot; which is searched for with <code>grep</code> and the 4th element (GUID) is extracted with <code>awk</code>.</li>
<li>The result of all of this is that a variable <code>partition_unique_guid</code> is set to the GUID.</li>
</ul></li>
<li>The script reports the UID and GUID values it has collected.</li>
<li>Next it mounts the partition on a mount point called <code>/mnt</code>.</li>
<li>Having done that it uses <code>rsync</code> to copy everything under the root directory (<code>/</code>) on the SD card to <code>/mnt</code>, the external disk. This is not a quick process since SD cards are not particularly quick.</li>
<li>Now the script edits the file <code>/boot/cmdline.txt</code> having first made a backup copy in <code>/boot/cmdline.txt.bak</code>.
<ul>
<li>Since it's using <code>sed</code> to do the edit it could have achieved that all in the same command (as you'll know if you've been following my <code>sed</code> series!!). However, doing it this way might be slightly safer if the <code>sed</code> command fails for whatever reason.</li>
<li>The <code>sed</code> command changes the definition of the root device in <code>boot/cmdline.txt</code> to the disk rather than the SD card and sets a <code>rootdelay=5</code> value to delay 5 seconds for the disk to become ready.</li>
<li>The '<em>-i</em>' <code>sed</code> command line option is used to perform the edit in-place. The script could have generated a backup at this point of course.</li>
<li>Note that the <code>sed</code> command is enclosed in double quotes. This allows Bash to substitute in the value of the <code>partition_unique_guid</code> variable.</li>
<li>Note also that the author escapes the slashes in the '<strong>s</strong>' command where this is not necessary because '|' has been used as a delimiter.</li>
</ul></li>
<li>Next the script edits the copied <code>/etc/fstab</code> (currently under <code>/mnt</code>) to mount the external disk as <code>/</code> rather than the SD card.
<ul>
<li>It runs <code>sed</code> to find the line containing the SD card partition and comments it out.</li>
<li>It then appends a line to the file with a new mount point using the UUID collected earlier.</li>
</ul></li>
<li>Now all is complete and the user is prompted to check what has been done before rebooting the Pi.</li>
</ul>
<p>I saved <a href="hpr2023_session_log.txt" title="Transcript of running the script">the transcript of what went on</a> when I ran this script and have made it available on the HPR site in case it is useful to anyone.</p>
<h3 id="reverting-to-the-sd-card">Reverting to the SD card</h3>
<p>Since all that has changed on the SD card is the file <code>/boot/cmdline.txt</code>, 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.</p>
<h2 id="picture">Picture</h2>
<figure>
<img src="hpr2023_RPi_server_collection.png" alt="My collection of Raspberry Pi servers" /><figcaption>My collection of Raspberry Pi servers</figcaption>
</figure>
<h2 id="epilogue">Epilogue</h2>
<p>Only after writing and recording this show did I notice that the <a href="https://learn.adafruit.com/external-drive-as-raspberry-pi-root/what-the-helper-script-does" title="What the Helper Script Does">Adafruit article actually describes what the script does</a>. It is better than my description in some ways, but doesn't cover some of the things I did.</p>
<p>It's a typical case of me being in too much of a hurry to do a proper job I'm afraid!</p>
<h2 id="links">Links</h2>
<ul>
<li>Pimoroni Pibow 3: <a href="https://shop.pimoroni.com/products/pibow-for-raspberry-pi-3" class="uri">https://shop.pimoroni.com/products/pibow-for-raspberry-pi-3</a></li>
<li>Raspberry Pi Universal Power Supply: <a href="https://shop.pimoroni.com/products/raspberry-pi-universal-power-supply" class="uri">https://shop.pimoroni.com/products/raspberry-pi-universal-power-supply</a></li>
<li>Raspberry Pi 3 Heatsink: <a href="https://shop.pimoroni.com/products/heatsink" class="uri">https://shop.pimoroni.com/products/heatsink</a></li>
<li>SATA Hard Drive to USB Adapter: <a href="https://shop.pimoroni.com/products/sata-hard-drive-to-usb-adapter" class="uri">https://shop.pimoroni.com/products/sata-hard-drive-to-usb-adapter</a></li>
<li>SanDisk SSD PLUS 120 GB Sata III 2.5-inch Internal SSD: <a href="http://www.amazon.co.uk/SanDisk-PLUS-Sata-2-5-inch-Internal/dp/B00S9Q9UKS" class="uri">http://www.amazon.co.uk/SanDisk-PLUS-Sata-2-5-inch-Internal/dp/B00S9Q9UKS</a></li>
<li>Kingston Flash Memory Card 32 GB: <a href="http://www.amazon.co.uk/Kingston-Class10-microSDHC-Include-Adapter/dp/B0162YQG2I" class="uri">http://www.amazon.co.uk/Kingston-Class10-microSDHC-Include-Adapter/dp/B0162YQG2I</a></li>
<li>FLIRC Raspberry Pi 2/3 Case: <a href="https://thepihut.com/products/flirc-raspberry-pi-3-b-case" class="uri">https://thepihut.com/products/flirc-raspberry-pi-3-b-case</a></li>
<li>Adafruit tutorial &quot;<em>Using an External Drive as a Raspberry Pi Root Filesystem</em>&quot;: <a href="https://learn.adafruit.com/external-drive-as-raspberry-pi-root/overview" class="uri">https://learn.adafruit.com/external-drive-as-raspberry-pi-root/overview</a></li>
<li>Article by paulv on the Raspberry Pi forums - &quot;<em>HOWTO: Move the filesystem to a USB stick/Drive</em>&quot;: <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=29&amp;t=44177" class="uri">https://www.raspberrypi.org/forums/viewtopic.php?f=29&amp;t=44177</a></li>
<li>Copy of <code>adafruit-pi-externalroot-helper</code> script: <a href="hpr2023_adafruit-pi-externalroot-helper.txt" class="uri">hpr2023_adafruit-pi-externalroot-helper.txt</a></li>
<li>Transcript of what happened when I ran the script: <a href="hpr2023_session_log.txt" class="uri">hpr2023_session_log.txt</a></li>
</ul>
<!--
vim: syntax=markdown:ts=8:sw=4:ai:et:tw=78:fo=tcqn:fdm=marker
-->
</article>
</main>
</div>
</body>
</html>