Raspberry Pi Networking Cookbook

4.5 (2 reviews total)
By Rick Golden
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

The Raspberry Pi is more than just a platform for teaching students how to program computers! The recipes in this book show you how this inexpensive computer can be used out of the box for a number of practical solutions that utilize existing networks and connectivity.

The Raspberry Pi Networking Cookbook is an essential reference full of practical solutions for use both at home and in the office. Beginning with step-by-step instructions for installation and configuration, this book can either be read from cover to cover or treated as an essential reference companion to your Raspberry Pi.

Full of practical and engaging content designed to expand and build upon your existing skills as you work through individual recipes, any computer novice can quickly learn how to become a Raspberry Pi expert without any programming knowledge required. The Raspberry Pi Networking Cookbook will allow you to revolutionize how you use technology on a daily basis, ranging from sharing your media across multiple devices to deploying your very own web portal, or even accessing your desktop remotely.

Publication date:
March 2013
Publisher
Packt
Pages
204
ISBN
9781849694605

 

Chapter 1. Installation and Setup

In this chapter we will cover:

  • Preparing for the initial boot

  • Printing a case – the Punnet

  • Setting up new SD cards

  • Image writer for Windows cards (Win32DiskImager)

  • Convert and copy for Linux (dd)

  • Creating SD cards with BerryBoot

  • Booting the "official" Raspbian Linux distribution

  • Shutting down the Raspberry Pi (shutdown)

 

Introduction


This chapter introduces the Raspberry Pi and explains how to download and install fresh images of popular Raspberry Pi distributions and how to set them up for the initial boot.

Developed by Raspberry Pi Foundation in the U.K. for promoting the teaching of basic computer science in schools, the Raspberry Pi is a single-board computer about the size of a credit card. The Raspberry Pi is based on a Broadcom BCM2835 System on a Chip (SoC) that includes a 700 MHz ARM1176JZF-S processor. The Raspberry Pi is well designed for experimenting with computers and learning computer programming. Its eight General-Purpose Input/Output (GPIO), I2C bus, and SPI bus also make it an ideal choice for experimenting with computer hardware and peripheral devices.

The Raspberry Pi also has two USB ports, an onboard Ethernet network connector (RJ45), both HDMI and Composite RCA video outputs, and audio output via a 3.5 mm jack or over HDMI—the same type of high-speed connections that are found on most desktop computers and laptops. With these standard peripheral connections the Raspberry Pi has the potential to be much more than just an educational tool or an experimenter's toy.

The preceding image shows two Raspberry Pis with their original packaging.

The Raspberry Pi on the left-hand side comes from RS Components Ltd (www.rs-online.com). The one on the right-hand side comes from Premier Farnell/element 14 (www.element14.com). Both were shipped in electrostatic bags and contained only simple instructions on how to begin experimenting with the Raspberry Pi.

Although the Raspberry Pi was designed for experimenting, this book is about using the Raspberry Pi in practical networking solutions both at home and at the office. This chapter begins by listing the components you will need, in addition to the Raspberry Pi, for practical application of the solutions described in this book. The first recipe explains how to create a simple yet sturdy case for the Raspberry Pi, out of paper. The remaining recipes describe how to download, install, and configure a number of common Raspberry Pi-optimized operating systems.

Once you've completed this chapter, you will have created a simple case to protect your Raspberry Pi; you will have downloaded, installed, and configured an operating system for your Raspberry Pi; and you will have booted your Raspberry Pi for the first time. You will also understand how to create application cartridges for your Raspberry Pi.

 

Preparing for the initial boot


This recipe explains which components are needed in addition to the Raspberry Pi before it can be powered on for the first time; that is, the components needed for the initial boot.

The Raspberry Pi is shipped without a case and without a power supply. There is no keyboard. There is no monitor. Depending on how you intend to use the Raspberry Pi, you will need additional components. As a minimum, you will need a power supply, an SD card, and a network cable.

An HDMI cable (or composite video cable), a USB keyboard, and a USB mouse are needed if you'd like to use the Raspberry Pi as you would use a desktop computer. You may wish to attach additional peripherals depending on how you intend to use the Raspberry Pi. This recipe suggests a number of different hardware combinations.

After completing this recipe you will be ready for the initial boot of your Raspberry Pi.

The preceding image shows a Raspberry Pi without a case, an SD card, and a power supply.

Getting Ready

The following are the basic components required for this recipe:

  • Raspberry Pi

  • Class 4 SD card of 4 GB (or greater)

  • 5V micro USB power supply

The Raspberry Pi draws its power from a 5V micro USB power supply and needs an SD card for its operating system. A single 4-GB SD card has more than enough room for hosting the operating system, as well as many useful applications. While no further components are required to boot the Raspberry Pi, the networking solutions in this book will require additional components.

The preceding image shows a Raspberry Pi with an SD card, a network, and power cables.

The essential networking required for this recipe is a network connection. For the simplest networking solutions, the only additional component that the Raspberry Pi needs is a network connection. Once the operating system on the SD card has been configured, remote logins to the Raspberry Pi are possible.

The media center required is a HDMI television or monitor. For the simplest media solutions, in addition to the basic components, the only additional component that the Raspberry Pi needs is an HDMI connection. Both audio and video can be streamed through the Raspberry Pi's HDMI connection. There is enough room on a 4 GB SD card to store a small collection of music and video files, in addition to the operating system.

The preceding image shows a Raspberry Pi in the Punnet with monitor, keyboard, and mouse.

The interactive whiteboard requires the following components:

  • HDMI television or monitor

  • Bluetooth adapter

  • Bluetooth keyboard

  • Bluetooth mouse

The Raspberry Pi has two USB ports, with enough power to support low-power devices such as a USB Bluetooth adapter, a simple keyboard, or a mouse.

The network hub requires the following components:

  • Powered USB hub

  • USB LAN adapter

  • USB WLAN adapter

  • USB hard drive

  • USB printer

When using the Raspberry Pi as a firewall or wireless access point, an additional LAN or WLAN adapter is required. If the adapter is powered by the USB connection, an additional powered USB connector will be required for the adapter to operate reliably.

The teleconferencing center requires the following components:

  • Powered USB hub

  • USB keyboard

  • USB mouse

  • USB camera

USB devices that require power over the USB connection, such as multimedia keyboards, gamer mice, cameras, printers, or external hard drives (including thumb drives), should be attached indirectly via a powered USB hub instead of directly attaching them to either of the Raspberry Pi's two USB ports. For greater reliability, the USB ports on the Raspberry Pi should typically be connected to powered USB hubs instead of directly connecting them to USB devices.

Gaming requires the following components:

  • Powered USB hub

  • USB game controllers

The Raspberry Pi is an excellent gaming platform whether for creating games, for playing single-player console games, or for playing multiplayer network games. Many of the older text-based games can be played on the Raspberry Pi with just a keyboard or via a remote login. However, USB game controllers can also be connected to the Raspberry Pi to further enrich the gameplay of multimedia action games.

The preceding image shows a Raspberry Pi in the Punnet lying on a USB hub with devices attached.

The initial setup requires the following:

  • Raspberry Pi

  • Class 4 SD card of 4 GB (or greater)

  • 5V micro USB power supply

  • Powered USB hub

  • Network cable

  • HDMI or DVI monitor

  • HDMI to DVI adapter (optional)

  • Speakers

  • Keyboard

  • Mouse

A power supply, preformatted SD card, monitor, keyboard, and mouse are the bare minimum components needed for an initial setup. A DVI monitor can be attached to the Raspberry Pi using an HDMI-to-DVI adapter. Both the HDMI-to-DVI adapter and the speakers can be replaced with a single HDMI cable connected directly to an HDMI television. When connected with HDMI, the television will output audio as well as video. The yellow RCA connector also provides video output for older televisions.

The preceding image is a schematic diagram of the Raspberry Pi from Raspberry Pi Foundation (http://www.raspberrypi.org).

How to do it...

The following are the steps for booting the Raspberry Pi:

  1. Build a case (optional).

  2. Download the latest disk image.

  3. Write the disk image to an SD card.

  4. Insert the formatted SD card into the Raspberry Pi.

  5. Attach a display to either the video connector or to the HDMI connector.

  6. Attach a USB board and a USB mouse to the USB ports.

  7. Attach a 5V micro USB power supply to the Raspberry Pi, and make sure it boots.

  8. Finally, shut down the Raspberry Pi.

How it works…

The Raspberry Pi does not come with a case. While it is not necessary for experimenting, you'll probably want a case for protecting the Raspberry Pi. The next recipe in this chapter shows how to make a case from paper (see the Printing a case – the Punnet recipe given next).

Before you can boot the Raspberry Pi, you'll need an SD card with a bootable disk image on it. The "official" Raspbian Linux image for the Raspberry Pi is downloadable from http://www.raspberrypi.org/downloads.

Once the disk image has been downloaded, it needs to be written to an SD card (see the Setting up new SD cards recipe given later).

After the SD card has been prepared and inserted into the Raspberry Pi, the display, keyboard, and mouse can be connected to the Raspberry Pi; and it is ready for booting (see the Booting the "official" Raspbian Linux distribution recipe given later).

When it is time to turn the Raspberry Pi off, the operating system must first be shut down—the opposite of booting (see the Shutting down the Raspberry Pi (shutdown) recipe given later).

There's more…

The Raspberry Pi is a very low-cost, single-board computer ($35 for the current model). It is sold "bare bones" and requires a power supply, a preformatted SD card to hold its operating system, and a keyboard and a display before it can do anything useful. However, it does have a number of standard I/O connections that will enable it to connect to a large variety of devices.

The following is the list of interfaces:

  • Power (5V at 700mA): The Raspberry Pi has a micro USB power connector that should be connected directly to a power supply, neither to the USB port on a computer nor to a USB hub.

  • Preformatted SD card (class 4): The Raspberry Pi is designed to boot from a preformatted SD Card (4 GB or greater is recommended).

  • GPIO: Analog and digital I/O connection for expansion and experimenting.

  • RCA video (composite video): The Raspberry Pi can be used with older televisions that have a composite video input.

  • Audio output (3.5 mm jack and stereo): The Raspberry Pi does not have an audio input connector; however, a USB mic or sound card can be added.

  • LEDs: These are disk, power, and network traffic indicators. When these LEDs are flashing, the Raspberry Pi is actively processing.

  • USB 2.0 (two ports): There is limited power available on these ports. Devices connected to the Raspberry Pi via USB should have their own power supply or they should be connected indirectly via a powered USB hub.

  • Network (10/100 wired Ethernet RJ45): The onboard networking competes for bandwidth with the attached USB devices.

  • HDMI (1080p30): This may be used for both video and audio output. It cannot be used at the same time as the RCA video.

The following is the list of onboard components:

  • System on chip

    • Broadcom BCM2835 media processor

    • CPU core – ARMv6 architecture; ARM11 core at 700MHz

    • GPU core – 24 GFLOPS of compute power

    • Memory – 512 MB SDRAM stacked on media processor

  • LAN9512

    • 10/100 Mb Ethernet (Auto-MDIX)

    • 2x USB 2.0

The recommended accessories include a powered USB hub as it has the following advantages:

  • It has its own power supply separate from the Raspberry Pi's power supply

  • It has enough power to support the attached devices

However, the Raspberry Pi has some power supply problems. It is difficult to say how much power is actually needed by the Raspberry Pi, as it varies depending on how busy it is and which peripherals are connected. However, there have been problems reported that seem related to an inadequate supply of power. These problems are reduced or eliminated when the power supply for the Raspberry Pi produces at least 700mA at 5V and when USB devices are connected indirectly through a powered USB hub.

The following are the symptoms:

  • Unreliable network connection

  • Keyboard does not work after the GUI (X Window) is started

  • Intermittent SD card errors

The following are the causes:

  • Power supply is rated less than 700mA

  • Complex keyboard or keyboard with built-in USB hub (for example, Apple Macintosh keyboards)

  • A USB hard disk (or thumb drive) is attached directly to the Raspberry Pi instead of indirectly through a powered USB hub

The following are the solutions:

  • Use a good quality regulated power supply of at least 700mA at 5V

  • Only connect simple USB devices directly to the Raspberry Pi

  • Connect USB devices to a powered USB hub, and only connect the hub directly to the Raspberry Pi

See also

  • Raspberry Pi

    http://en.wikipedia.org/wiki/Raspberry_pi

    A detailed Wikipedia article about the Raspberry Pi.

  • DesignSpark – Raspberry Pi

    http://www.designspark.com/theme/raspberrypi.

  • element14 Raspberry PI Group

    http://www.element14.com/community/groups/raspberry-pi

    element14 is one of two distributors for the Raspberry Pi. The Raspberry Pi group on the element14 website has over 7,000 members, over 600 topics in the forums, technical documentations, and video tutorials.

  • The MagPi

    http://www.themagpi.com

    The MagPi is a magazine for Raspberry Pi users. Monthly issues are available online.

  • The Raspberry Pi website

    http://www.raspberrypi.org

    The official Raspberry Pi website contains history, news, and documentation for the Raspberry Pi as well as a quick start guide, a forum, a wiki, and a download area.

  • RPi Hub – eLinux.org

    http://elinux.org/R-Pi_Hub

    The RPi Hub is the Embedded Linux community's wiki page for Raspberry Pi users. This wiki page has a buying guide, a beginners' guide, a list of verified peripherals, and a list of Raspberry Pi distributions larger than what is found on the official website. It is a wealth of well-organized, up-to-date information.

 

Printing a case – the Punnet


This recipe explains how to make a simple case out of paper.

The Raspberry Pi is a single-board computer without a case. Cases are available from a number of retailers (see the See also section of this recipe for a few suggestions). However, the Raspberry Pi is normally sold without a case.

For general experimentation and setup, the Raspberry Pi does not really need a case. It will function perfectly well sitting on top of the box that it came in, or on top of a powered USB hub. Although for regular use, as part of one of the solutions in this book, a case is recommended.

Once you finish with this recipe, you will have a simple protective case for your Raspberry Pi.

The preceding image shows the Punnet printed and ready to use.

Getting ready

The Punnet is a printable card case for the Raspberry Pi. It requires less than an hour to assemble; however, a couple of hours should be allowed for the glue to set firmly before use.

A printable PDF template of the Punnet can be found on the Raspberry Pi Foundation website (http://www.raspberrypi.org/archives/1310).

You will need the following components:

  • A Raspberry Pi

  • The Punnet PDF file (see the link given in the See also section)

  • Heavy card stock paper

  • White glue

  • Cellophane tape

  • A straightedge or ruler

  • A hobby knife

How to do it...

The following are the steps for creating a Raspberry Pi case out of paper:

  1. Print the PDF file on heavy card stock paper, or print the file on copy paper and then glue the copy paper to the heavier paper.

  2. Carefully cut out the Punnet. Use the hobby knife with the straightedge as a guide for cutting straight lines.

  3. Use the straightedge to score and crease the folds. This will make assembly easier.

  4. Fold the Punnet around the Raspberry Pi and glue it, as shown in the previous screenshot. Apply the glue sparingly—too much glue causes the paper to warp.

The preceding image shows the Punnet in use. A printed Punnet was glued to pink card stock before folding. The card stock added strength and stability to the case.

How it works…

The Punnet is a simple paper box made from a single sheet of paper with cutouts marked that align with the Raspberry Pi's connectors. The 50-mm scale printed on the page can be used to validate the dimensions of the printout. Turn off any Scale to fit feature of the printer to ensure that the page is printed at 100 percent. Once the page is printed, the scale printed on the page can be measured to see if it is 50-mm long. If it is not 50-mm long, adjust the scale in the printer properties and try printing the page again.

The PDF page format is designed for A4 paper (used in Europe); however, it should print fine on 8½ x 11" paper (used in the USA) if the page is printed with actual size. The printed side of the page will become the outside of the Punnet.

There's more…

It is easier to assemble the Punnet around a Raspberry Pi than trying and putting a Raspberry Pi in a pre-assembled Punnet. First score the dotted lines so that it is easier to fold them, then set the Raspberry Pi on the Punnet. Fold up the tabs and spread a small amount of glue on the outside of each tab. Finally, fold up the sides around the Raspberry Pi and glue them together. Use cellophane tape to strengthen the corners.

A straightedge or ruler is a useful tool for cutting and folding on straight lines; and a hobby knife works better than scissors. White glue works well, if used sparingly.

Decorating the Punnet is easier before it is assembled, while it is still uncut.

Don't forget to cut a hole in the top to let cool air in. The Raspberry Pi can overheat if it is kept fully enclosed.

A number of commercial cases are available for the Raspberry Pi (see the links give next).

The preceding image shows three different Raspberry Pi cases.

See also

  • The Punnet

    http://www.raspberrypi.org/archives/1310

    This is the original article featuring a home printable cardboard case for the Raspberry Pi.

  • Raspberry box

    http://www.amazon.com/shops/ATLOHWI71UDEX

    This transparent acrylic box lets the inner beauty of the Raspberry Pi shine through. From Spain, the retailer also makes classical guitars.

  • Pi Holder

    http://www.piholder.com

    The Pi Holder is a solid aluminium case with cooling pillars in the top half that attach directly to the three heat-emitting IC chips on the Raspberry Pi. This case keeps the Raspberry Pi cool and protected—a good choice for production use.

  • Other cases on the Raspberry Pi website

    http://www.raspberrypi.org/archives/tag/cases

    This article features a number of very creative cases for the Raspberry Pi. Some of the cases on this site are very original.

  • Embedded Linux – RPi cases

    http://www.elinux.org/RPi_Cases

    The Embedded Linux community maintains a long list of Raspberry Pi cases. The list features blueprints, photographs, and links to order forms.

 

Setting up new SD cards


The following recipes explain how to create bootable SD cards from downloaded disk images using Win32DiskImager.exe, dd, and BerryBoot.

The Raspberry Pi does not come with an operating system. Before the Raspberry Pi can boot, it needs an SD card with the operating system installed. Pre-installed SD cards are available for purchase; however, downloading and installing an operating system's image is not difficult.

How to do it…

The following are the steps on how to write an image to the SD card:

  1. Download the "official" Raspbian Linux image from http://www.raspberrypi.org/downloads.

  2. Write the image to an SD card.

How it works…

The "official" Raspbian Linux operating system image can be downloaded from the Raspberry Pi Foundation website (http://www.raspberrypi.org/downloads). Other operating system images can be found on the Embedded Linux community's wiki page (http://elinux.org/RPi_Distributions).

Once the operating system is downloaded, you'll need to write it to an SD card.

If you are writing the SD card from a Windows computer, use Win32DiskImager.exe (see the Image Writer for Windows cards (Win32DiskImager) recipe).

If you are using the Linux operating system or Mac OS to write the image to the SD card, use the dd command-line utility (see the Convert and copy for Linux (dd) recipe).

If you'd like to try a simpler way of installing the Raspberry Pi that works on any computer that can copy files to a formatted SD card, use BerryBoot (see the Creating SD cards with BerryBoot recipe).

See also

  • The Raspberry Pi website – downloads

    http://www.raspberrypi.org/downloads

    This is the location of the "official" versions of Raspberry Pi optimized GNU Linux distributions. Currently, Raspberry Pi Foundation recommends four distributions of the Linux operating system, as follows:

    • Raspbian "Wheezy"

    • Soft-float Debian "Wheezy"

    • Arch Linux ARM

    • RISC OS

    The Soft-float Debian distribution is a slower distribution that has not been optimized to use the Raspberry Pi's hardware-accelerated floating-point calculations. It has been made available for use with software that has not been optimized yet (such as Oracle's JVM). The other distributions are listed next with links to their original maintainers' websites.

  • Raspbian

    http://www.raspbian.org

    Raspbian is a Linux operating system distribution based on Debian optimized for the Raspberry Pi and comes with more than 35,000 packages pre-compiled for the Raspberry Pi.

  • archlinux | ARM – Raspberry Pi

    http://archlinuxarm.org/platforms/armv6/raspberry-pi

    Arch Linux ARM is a simple, lightweight distribution for shaping your own system. This distribution is built directly from source code and is optimized for the Raspberry Pi. It offers rolling releases of bleeding edge software; however, it is a minimalist base rather a complete desktop system.

  • RISC OS

    http://www.riscosopen.org/

    The RISC OS was designed in Cambridge, originating from the same team that designed the ARM processor, and was originally released in 1987.

  • Embedded Linux – Rpi Distributions

    http://elinux.org/RPi_Distributions

    The Embedded Linux community maintains an excellent wiki page on Raspberry Pi operating system distributions. The wiki page has a comparison table and links to downloadable image files. Many of these distributions are specialized for specific use – as a home theater, as firewall, as a development platform, or as a very inexpensive desktop PC.

  • GNU

    http://www.gnu.org

    The homepage of the GNU operating system.

 

Image writer for Windows cards (Win32DiskImager)


This recipe explains how to install a Raspberry Pi operating system image on an SD card using the open source image writer for Windows – Win32DiskImager.exe.

Once you finish with this recipe, you will be able to write Raspberry Pi images to SD cards from a Windows computer.

Getting ready

The following are the ingredients:

  • A computer running Windows with an SD card writer

  • A class 4 SD card of 4 GB (or greater)

  • A Raspberry Pi operating system's image file

  • A pre-compiled Win32DiskImager binary

The pre-compiled binary of Win32DiskImager is distributed as a ZIP file and can be downloaded from https://launchpad.net/win32-image-writer.

How to do it...

The following are the steps for writing a disk image to an SD card on a Windows computer:

  1. Download the Win32DiskImager ZIP file (https://launchpad.net/win32-image-writer).

  2. Expand the ZIP file to a folder on disk (for example, C:\Win32DiskImager).

    The preceding screenshot shows the contents of the Win32DiskImager ZIP file.

  3. Download a Raspberry Pi distribution disk image (http://www.raspberrypi.org/downloads).

  4. Run Win32DiskImager.exe from the install folder.

  5. Select the source image file and the target device, as shown in the following screenshot:

    The preceding screenshot shows Win32 Disk Imager being used to write the recommended Raspbian GNU Linux distribution to an SD card.

  6. Click on the Write button to copy the image to the SD card.

    Writing an image to a disk takes about 5 minutes for a 2-GB image file. Once the image is written to the SD card, the SD card may be ejected and used to boot the Raspberry Pi.

How it works...

First, the image writer for Windows (Win32DiskImager) is downloaded and installed.

Win32DiskImager is a standalone application. Its install files can be expanded to a folder located anywhere on the PC. Double-click on the Win32DiskImager executable file to start the application.

Then, a Raspberry Pi disk image is downloaded.

Finally, Win32 Disk Imager is used to write the Raspberry Pi disk image to the SD card.

There's more...

The utility, Win32DiskImager, was originally written to read and write disk images for a specific Linux distribution; since then, however, it has been generalized and is now a popular tool for many development projects such as the Raspberry Pi.

Win32DiskImager is also an excellent backup tool! After booting and configuring the Raspberry Pi, a backup can be made to preserve the image in case the SD card is damaged or lost.

To make a backup perform the following steps:

  1. Run Win32DiskImager.exe from the install folder.

  2. Select the SD card device as the source and the image file as the target.

  3. Click on the Read button to read the SD card in the form of an image on disk.

A new backup should be created after each update to the Raspberry Pi's operating system, application software, or configuration.

See also

  • Image writer for Windows

    https://launchpad.net/win32-image-writer

    This utility that was originally written to read and write disk images for a specific Linux distribution; however, since then it has been generalized and is now a popular tool for many development projects such as the Raspberry Pi.

 

Convert and copy for Linux (dd)


This recipe explains how to install an operating system's image on an SD card using the standard Linux utility, dd.

Most versions of Linux (and Mac OS) have the dd command installed. This powerful version of the copy command (cp) can be used to write blocks of data to devices such as an SD card.

Once you finish with this recipe, you will be able to write an SD card from a Linux (or Mac OS) computer.

Getting ready

The following are the ingredients:

  • A computer running Linux with an SD card writer

  • A class 4 SD card of 4 GB (or greater)

  • A Raspberry Pi operating system's image file

The dd utility is normally installed by default with most Linux distributions. If it is not installed, use the appropriate Linux installation utility to install it.

All the commands in this example are executed as a privileged user (root).

How to do it...

The following are the steps for writing a disk image to an SD card on a Linux computer:

  1. Download a Raspberry Pi distribution disk image (http://www.raspberrypi.org/downloads).

  2. Excute the df command. Determine the name of the SD drive.

  3. Execute the following command:

    umount /dev/mmcblk0p1

    Unmount the mounted disk partitions.

  4. Execute the following command:

    dd bs=1M if=2012-08-16-wheezy-rasbian.img of=/dev/mmcblk0

    Use dd to copy the image to SD card, as shown in the following screenshot:

The preceding screenshot shows the df command being used to discover the name of the SD card drive, the umount command being used to unmount the SD card, and the dd command being used to write the recommended distribution to an SD card.

How it works...

First a Raspberry Pi disk image is downloaded.

Then, the name of the SD drive is discovered using the df command. The df command shows how much disk is free on each of the mounted disk drives. The SD card was just inserted, and its primary partition (p1) appears in this list as /dev/mmcblk0p1. The disk device is /dev/mmcblk0.

Finally, the dd command is used to write the Raspberry Pi disk image to the SD card. The following is the explanation for the command: dd bs=1M if=2012-08-16-wheezy-rasbian.img of=/dev/mmcblk0:

  • Each disk block written is 1 MB (bs=1M)

  • The input file (if) is 2012-08-16-wheezy-raspbian.img

  • The output file (of) is the SD card disk device (/dev/mmcblk0)

There's more...

The utility dd is one of the core GNU utilities found in most Linux distributions. It is a low-level utility that simply copies blocks of data from one file to another.

The previous screenshot shows an example of how the df command can be used to determine the name of the SD drive. The first partition of the SD disk, /dev/mmcblk0p1, is mounted at /media/A1B1-918F. Disk images cover a whole disk, not just one partition, so the correct name of the disk drive in the previous example is /dev/mmcblk0 (notice that p1 is missing).

Before the image is copied to the SD card in the previous example, the disk partition is unmounted (umount /dev/mmcblk0p1—notice there is no n in umount). It is a good practice to unmount all disk partitions before formatting or overwriting a disk.

When the image is copied with the dd command:

  • if= specifies the input file (2012-08-16-wheezy-raspbian.img)

  • of= specifies the output file (/dev/mmcblk0)

  • bs= specifies the size of the blocks written to the disk

The dd utility can also be used as a backup tool. Just exchange the input file (if=) and output file (of=).

Use the following command to create a backup using the disk from the previous example:

dd bs=1M if=/dev/mmcblk0 of=backup-2012-08-16.img

See also

 

Creating SD cards with BerryBoot


The following recipe explains how to install an operating system on an SD card using BerryBoot.

Unlike many Raspberry Pi distributions, BerryBoot is not a standalone disk image. It is a bare minimum Linux operating system running as a single application that automates the installation of the Raspberry Pi. It is by far the easiest way of selecting and installing an operating system distribution.

BerryBoot supports the installation of multiple Linux distributions on a single SD card and is by far the easiest way of installing an operating system for your Raspberry Pi. However, it does require an Internet connection during setup and installation.

The installation takes place by booting the Raspberry Pi with an installer that can be easily copied to a FAT-formatted SD card using drag-and-drop from any PC. When the Raspberry Pi boots, the installer runs and uses the Raspberry Pi's network connection to download the latest distribution of selected operating systems.

Getting ready

You will need the following:

  • An initial Raspberry Pi setup (see the Preparing for the initial boot recipe)

  • The latest berryboot.zip file (see the link given in the See also section of this recipe)

  • A class 4 SD card of 4 GB (or greater)

  • A network connection

The Raspberry Pi needs to be connected to the Internet (via a home or office network) to complete this recipe. During installation, the network connection is used to download the selected distribution files. Without an Internet connection this recipe will not work.

The preceding screenshot shows the contents of the berryboot.zip file.

How to do it...

The following are the steps for creating a boot disk with BerryBoot:

  1. Format the SD card as a bootable FAT disk. Use the normal disk formatting tools that come with your PC's operating system.

  2. Extract the contents of the berryboot.zip file to the newly formatted SD card. Again, the normal archival tools that come with your PC will do the job.

  3. Connect your Raspberry Pi to a network with access to the Internet.

  4. Insert the SD card in the Raspberry Pi and turn it on. This will start the BerryBoot installation process.

    The preceding screenshot shows BerryBoot's Welcome screen.

  5. After the network interfaces have been detected, the Welcome dialog box is displayed.

    1. Set the appropriate video scan option (the example shows a green overscan area indicating overscanning should be disabled).

    2. 
The time zone and keyboard layout are also set on the Welcome dialog box.

      Tip

      The Welcome screen has video settings and locale settings. If green bars are displayed at the top and bottom of the screen (as in the preceding example), then the Video mode should be set to Yes (disable overscan). This screen also contains locale settings for choosing a time zone and a keyboard layout. Click on the OK button to continue.

    The preceding screenshot shows BerryBoot's Disk selection screen.

  6. The Disk selection dialog box can be used to select an installation disk other than the SD card. The target disk may be formatted in one of the following three ways:

    • B-tree file system (BTRFS) for scalable storage

    • ext4 – with discard flag set for SDD disks

    • ext4 – no discard is the default

    Note

    Even though the installation uses an external disk, the SD card is still required to boot.

    The preceding screenshot shows BerryBoot's Add OS screen.

  7. After the disk is formatted, BerryBoot downloads the list of available operating system images. The Add OS dialog box is used to select which operating system will be downloaded and installed next.

    The preceding screenshot shows BerryBoot's BerryBoot menu editor screen.

  8. The BerryBoot menu editor screen is used to add additional operating systems and manage those that have already been installed. All the installed images will appear in the BerryBoot boot menu.

    • The Add OS button is used to add another operating system to the boot menu (and repeat previous step)

    • The Edit button is used to change the name of the image displayed on the boot menu and to configure the memory split

    • The Clone button duplicates the current image in the boot menu

    • The Export button can be used to export the current disk image to an external disk

    • The Delete button deletes the current operating system from the boot menu

    • The Make Default button can be used to select which image will boot by default

    • The Exit button is used to reboot the Raspberry Pi

How it works...

First an SD card is formatted as a bootable FAT disk (using the utility of your choice). Then the BerryBoot installation files are copied to the disk. No special image writers are required! Just drag-and-drop all the unarchived files onto the SD card.

Once all the installation files are copied to the newly formatted SD card, the disk can be safely ejected and inserted into an unpowered Raspberry Pi. When the power cord is connected to the Raspberry Pi, the Raspberry Pi will boot automatically into the BerryBoot installation application.

After booting, the Raspberry Pi tries to detect all attached network cards. The installation application can detect many modern wireless USB network cards; however, only a limited number are actually configurable by the application. Once the network has been detected (and selected), the Welcome screen is displayed.

The BerryBoot install application will then detect any attached USB disk devices (or network storage). The installation application will permit the operating system to be installed on a disk other than the SD card. However, the Raspberry Pi will still need the BerryBoot SD card to boot. The Raspberry Pi will not boot from any other device. Choose mmcblk0 to format the SD card. For most uses, the ext4 filesystem is the best choice. The B-tree file system (BTRFS) has advanced features that are not covered in this book. Choose ext4 – with discard flag set when formatting the SD card.

After the disk is formatted, the Add OS screen appears with a selection of Raspberry Linux distributions. There are distributions for using the Raspberry Pi as a media center (OpenELEC), as a classroom workstation (LTSP thin client BerryTerminal), and as a web server (BerryWebserver). There are also alternative Linux distributions (Puppy Linux and Sugar). And, of course, the "official" Raspbian Linux distribution is also included.

Once a Linux distribution has been chosen, it is downloaded and added to the boot menu. Choosing Exit from the BerryBoot menu editor screen reboots the Raspberry Pi. After reboot, the downloaded distributions are displayed on the boot menu and the user can select which distribution to boot.

Application cartridges

Each of the recipes in this book could be used to create an "application cartridge".

An application cartridge is like a game cartridge that is plugged into a game console, ready to play. The only difference is an application is stored on the cartridge instead of a game. So when the application cartridge is plugged into the Raspberry Pi, an application is started and is ready to use.

Using application cartridges, the Raspberry Pi can easily be repurposed just by switching the cartridge (the SD card). After shutting down and turning off the Raspberry Pi, the current SD card in the Raspberry Pi could be replaced with another SD card that has a different image installed on it – easily switching the Raspberry Pi from one purpose to another.

Write a multimedia home theatre distribution, for example OpenELEC or Raspbmc on one SD card to create a home theatre application cartridge. Write an IPFire image on another SD card to create a firewall application cartridge. A Berry Terminal image could be used to create an application cartridge for client access to a terminal server; or recipes from this book could be used to create application cartridges for a file server, a web server, or a wireless access point.

With a library of application cartridges, a Raspberry Pi can serve multiple purposes.

See also

 

Booting the "official" Raspbian Linux distribution


This recipe explains how to boot the official Raspbian distribution and run raspi-config to complete the installation of the Raspberry Pi.

The raspi-config command is run by default when you first boot the Raspberry Pi. In this recipe, the command is only used to update the tool itself. More detail on the use of raspi-config can be found in Chapter 2, Administration.

Once this recipe is complete, you will have booted the Raspberry Pi for the first time.

Getting ready

The following are the ingredients:

  • An initial Raspberry Pi setup (see the Preparing for the initial boot recipe)

  • An SD card formatted with the "official" Raspbian Linux image

  • A network connection

The SD card should be formatted with a Raspbian image, a display and keyboard should be connected, and the Raspberry Pi should be ready to power on.

How to do it...

The following are the steps for booting the Raspberry Pi for the first time:

  1. Insert the SD card into the Raspberry Pi and plug in the 5V power supply. The Raspberry Pi should start booting.

    The preceding screenshot shows the Raspberry Pi as it boots.

  2. After a short initial boot, the Raspi-config main menu is displayed.

    • info: A short description of the tool

    • expand_rootfs: Resize the root partition to fill the SD card

    • overscan: Enable or disable overscan

    • configure_keyboard: Change the keyboard configuration

    • change_locale: Change the display language

    • change_timezone: Change the time zone

    • memory_split: Change how much memory is available

    • ssh: Enable or disable the SSH server

    • boot_behavior: Choose text mode or X windows GUI

    • update: Update the raspi-config utility

    The following screenshot shows the Raspi-config main menu:

  3. Choose update to download and install any updates to the configuration utility. Type in sudo raspi-config to return to the menu.

    The preceding screenshot shows the updating process of the raspi-config utility.

  4. Use the other menu items to configure the operating system.

  5. Select <Finish> to complete the configuration and reboot the system.

How it works...

The official Raspbian Linux distribution comes with the raspi-config utility. It is run automatically on the first boot of the operating system. Afterwards, the system will boot without running the configuration utility.

When the Raspi-config main menu appears, the user can use the keyboard arrows, the Tab key, the Space bar, and the Enter key to navigate the menus.

The next chapter has recipes that use raspi-config to configure the Raspberry Pi. The example in this recipe selects the menu item update to fetch and install a current version of raspi-config from the Raspberry Pi package distribution center.

After the raspi-config command has been updated, the user is left at a command prompt and must restart raspi-config. The raspi-config command is privileged, so the sudo command is used as a prefix to temporarily grant privileges to the user.

After the user has configured the Raspberry Pi, selecting <Finish> from the main menu will cause the Raspberry Pi to reboot.

Once rebooted, the Raspberry Pi is ready for use!

There's more...

The raspi-config utility can be run at any time to reconfigure the Raspberry Pi. After logging in, enter the command sudo raspi-config to rerun the utility.

See also

 

Shutting down the Raspberry Pi (shutdown)


This recipe shuts down the Linux operating systems so that the Raspberry Pi can be powered off safely.

Before powering down the Raspberry Pi, it is important to first shut down the operating system so that all of the applications and services on the Raspberry Pi have completely finished writing to disk and are ready for the next boot.

External devices, such as hard disks, also need time to shut down and flush their buffers. The shutdown command also gives devices attached to the Raspberry Pi an opportunity to clean up and prepare for the next boot.

Getting ready

The following are the ingredients:

  • An initial Raspberry Pi setup (see the Preparing for the initial boot recipe)

  • An SD card formatted with the official Raspbian Linux image

The Raspberry Pi should already be powered on and booted before starting with this recipe.

How to do it...

The following are the steps for shutting down the Raspberry Pi:

  1. Log in to the Raspberry Pi with pi as the username (the default password is raspberry).

    The preceding screenshot shows the process of logging in to the Raspberry Pi with pi as the username.

  2. Execute the following command:

    shutdown –h now

    Shut down and halt the operating system. This command is privileged. Use the prefix sudo to run the shutdown command as a privileged user.

    The preceding screenshot shows how to shut down the Raspberry Pi.

  3. The Raspberry Pi will begin to shut down, displaying messages from applications, devices, and services as they clean up and prepare for the next boot.

  4. Once the operating system has shut down, the Raspberry Pi will halt. Only a single red LED will remain lit on the Raspberry Pi as the Raspberry Pi is still powered on. The main display will be blank.

  5. The power to the Raspberry Pi can now be shut off.

How it works...

Once the Raspberry Pi boots, it prompts for a username and a password. By default, the Raspbian Linux distribution has one user configured, the pi user. This user's default password is raspberry. The password for the pi user can be changed using the raspi-config command.

After login, the shutdown command is executed with the –h option telling the Raspberry Pi to halt the system (power it off) after the operating system has shut down. The shutdown command is privileged, so the sudo command is used as a prefix to temporarily grant privileges.

There's more...

The shutdown command can also be used to reboot the system. Use the –r option. Rebooting the system when logged in as the pi user can be done with the following command:

sudo shutdown –r now

A number of synonyms exist for the shutdown command including halt, poweroff, 'and reboot.

See also

About the Author

  • Rick Golden

    Rick Golden, in the summer of 1972, sat in the computer lab at SUNY Fredonia and completed his first CAI tutorial on programming in APL. He was 9 years old then.

    He has been programming computers for over 40 years. He has designed and developed a multitude of projects, from low-level graphics and database drivers to large-volume e-commerce platforms.

    At work, Rick is currently focused on developing software to improve healthcare by mining petabytes of healthcare claims to find opportunities to improve healthcare coordination. After work, Rick teaches 10-14 year olds how to program using Raspberry Pi computers.

    Browse publications by this author

Latest Reviews

(2 reviews total)
Good for general information.
Good
Book Title
Access this book, plus 7,500 other titles for FREE
Access now