Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

75
Linux For Embedded Systems For Arabs Ahmed ElArabawy Cairo University Computer Eng. Dept. CMP445-Embedded Systems

Transcript of Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Page 1: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Linux For Embedded Systems For Arabs

Ahmed ElArabawy

Cairo University Computer Eng. Dept. CMP445-Embedded Systems

Page 2: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Lab 1: Preparing the Pi

Page 3: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Steps to Prepare the Pi

1. Select the Operating System to use

2. Load the SD Card with the chosen Operating System

3. Plug in the SD Card in the Pi and make the other Connections

4. Boot the Raspberry Pi

5. Configure the Pi

6. Setup the Network (Using the Wired Ethernet Interface)

7. Setup the Wireless Network Connection (Using a WiFi Module)

8. Connect remotely to the Raspberry Pi from the host machine

• Via SSH

• Via VNC

Page 4: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

1. Selecting the OS

Page 5: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Selecting the Operating System

• The Raspberry Pi supports a lot of operating Systems

• Some of these OS’s are officially supported by the Raspberry Pi Foundation, and others are not officially supported

• The officially supported Operation systems are listed in the Raspberry Pi Official site (the download page)

http://www.raspberrypi.org/downloads/

• Most of the supported operating systems are based on Linux distributions, however there are exceptions (such as the RISC OS)

• For an exhaustive list of operating systems that was tested to run on the Raspberry Pi,

http://elinux.org/RPi_Distributions/

Page 6: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Official Operating Systems

Page 7: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Other OSs for the Raspberry Pi

And Many More …

Page 8: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

And for Model 2 Only

Page 9: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

A More Exhaustive List http://elinux.org/RPi_Distributions/

Page 10: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Raspbian http://www.raspbian.org/

• The most popular OS for the Raspberry Pi

• Based on Debian Linux Distribution (Debian Wheezy)

• Most projects available online use it, hence this will be our selection

Page 11: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

2. Setup the SD Card

Page 12: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Method 1: Loading the SD Card The Traditional Way

Page 13: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Download Raspian

• We will need to download the Raspbian OS image from,

http://www.raspberrypi.org/downloads/

Page 14: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Loading the SD Card

• Now we need to load the selected OS image to the SD Card

• Note this is not a simple copy of a file, so simple cp will not work

• A copy of a file means the file resides inside a file system

• The file can be divided into multiple blocks

• The file meta-data for the file will be written to the filesystem

• Instead, we want to dump the raw image byte by byte to the desired location in the SD Card (no meta data, no inode, no dentry, just byte transfer)

• This require using a different technique,

• If the host is a Windows machine

• Use Fedora ARM Installer

• Use Win32 Disk Image Writer utility

• If the host is a Linux machine

• Use the dd command on the CLI

Page 15: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Windows Machine Host: Using Fedora ARM Installer • Download the zip file from

http://fedoraproject.org/wiki/Fedora_ARM_Installer

• Extract the zip file

• Run the fedora-arm-installer-2 executable (as Administrator)

Page 16: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Windows Machine Host: Using Win32 Disk Image Writer • Download the program from

http://win32diskimager.sourceforge.net/ • Install the program • Run as administrator • Keep in mind that this operation will erase the contents of the

chosen device • This program is designed to,

• Write a raw disk image to a removable device • Backup a removable device to a raw image file

Page 17: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Linux Machine Host: Using the dd Command • First step, we need to identify the device name for the SD Card

Reader in your host machine

• This can be done by listing contents of /dev before and after inserting the SD Card, or using df -h command before and after inserting the SD Card

• Note that sometimes, ubuntu fails to detect the SD card

• Let us assume it is /dev/sdd1

• Next, we will need to unmount the device (the whole device, and not just the partition) $ sudo umount /dev/sdd

• Next, we will need to dump the image file to the SD Card (assume that the file is ~/my-image.img) $ sudo dd bs=4M if=~/my-image.img of=/dev/sdd

Page 18: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Looking Inside the Image

Page 19: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 20: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 21: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 22: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

What Does this Image Contain ??

• This image contains,

• SD Card partition table (describing the partitions on the SD Card)

• 2 Partitions

• One FAT partition

• One EXT4 partition

• The windows partition contains files that are read by the GPU at the board boot up which includes the Linux Kernel Image

• The Linux partition contains the root filesystem for Linux

• The Linux partition will be mounted by the Linux Kernel during the startup procedure

Page 23: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

SD Card Partitions

FAT

ext4

Page 24: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Method 2: Loading the SD Card Using NOOBS

Page 25: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Loading the SD Card Using NOOBS

• NOOBS stands for (New Out Of Box Software)

• It contains multiple Official Pi distributions

• To set it up, you need first to download the NOOBS zip file and extract it in your host machine

http://www.raspberrypi.org/downloads/

Page 26: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Download NOOBS

Page 27: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 28: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Preparing the SD Card

• You may go to the os/ folder and delete the distribution images that you are not interested in, this will reduce the data size that will need to be copied to the SD Card

• First Step: • You need to make sure the SD Card is in its initial state (delete the

partition table) • This is done via the windows command-line tool diskpart (note that

other tools may not be able to perform this task) http://www.winability.com/delete-protected-efi-disk-partition/

• Second Step: • Format the SD Card as one FAT32 partition • Use the tool SD Formatter https://www.sdcard.org/

• Third Step: • Copy the downloaded NOOBS files into the SD Card (normal copying

of files)

Page 29: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Step 1: Cleaning the SD Card

Page 30: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Step 2: Formatting the SD Card

Page 31: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Step 2: Formatted SD Card

Page 32: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Booting the Pi

• Now prepare for booting the Pi, • Install the SD Card into the Pi • Connect a keyboard and a mouse to the Pi through the USB ports • Connect a monitor to the Pi through the HDMI Cable • After all connections are done, connect the power through the

micro-USB connector

• At the first boot of the Pi: • A graphical user interface will appear • A list of distributions will be shown, you need to select one of them

to install (or multiple) • The GUI gives you the option to add an additional empty ext4

partition for data

• Once done selection, push the install button • The Pi will run through installation of the distribution

• This includes reorganizing the partition table on the SD card to create the needed ext4 partitions

Page 33: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Looking Inside the Image

Page 34: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Partition Table (after Install)

Page 35: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Partition Table (after Install)

• The SD Card will have 3 partitions:

• FAT32 partition

• This is used for recovery (read by the GPU)

• Size is around 800 MB

• vfat partition

• A Linux partition mounted at /boot

• Contains the bootloader binary read by the GPU, elf files, kernel image, and kernel configuration files

• ext4 partition

• Linux Root File System (/)

• Constitutes most of the SD Card space

Page 36: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Method 3 Loading the SD Card Using BerryBoot

Page 37: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Loading the SD Card Using BerryBoot • Berryboot is a boot management tool for the Raspberry Pi

• It provides nice features,

• It allows the user to install multiple OSs and select at boot time

• It allows the user to put the distributions on another storage device (such as a hard disk connected to the USB interface). The SD card will still be the starting point, but the root file system will be on the other device

• It allows the user to put non-official or customized distributions

• It downloads the desired distribution directly from the internet (using either the wired or the wireless connection)

Page 38: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Setting up the SD Card

• You will need to download the Berryboot zip file and extract it http://sourceforge.net/projects/berryboot/

• Then the SD Card will need to be cleaned (delete partition table) using diskpart tool

• Then it should be formatted in a FAT32 partition (using the SD Formatter)

• Then copy the Berryboot files to the SD Card (normal copy)

• Insert the SD card in the Raspberry Pi and Boot it

• Berryboot will prompt you to select the desired OS

• The Berryboot installer will, • Create the new partitions

• Reformat the SD Card partitions

• Download the desired Distribution from the internet

Page 39: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 40: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 41: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

3. Setup the Raspberry Pi

Page 42: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Page 43: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Setting Up the Pi • Now you can connect the Pi to the needed peripherals • This includes,

• Keyboard and Mouse using the USB connectors • Monitor using the HDMI interface • Network connection through the LAN cable (we will talk about setting it up

later) • WiFi Dongle through the USB Connector (to be discussed later)

• Of course you will need to insert the SD Card in its slot • Once all connections are done, you can connect the power through the

micro-USB connector • WARNING:

• All connections must be done before power is applied to the Pi • Even though the USB is theoretically hot pluggable, it is recommended that

you don’t connect/disconnect any USB device while the board is powered • Not following that, may result in excessive current surges that may reboot

the Pi or even destroy it • Also, keep in mind that the Pi power may not be able to support too many

peripherals, so consider connecting one of the USB connectors to a powered USB Hub, and connect the other devices to it

Page 44: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

4. Booting the Raspberry Pi

Page 45: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Boot Sequence

• When the Pi is powered up, • The ARM processor remains off, SDRAM is disabled, and the GPU

core is the one that starts the booting procedure

• The GPU starts executing the first stage bootloader which is stored in the ROM on the SoC (not on the SD Card)

• This firmware mainly enables the GPU to access the SD Card, and read a FAT32 Partition on it

• The first file read from the SD Card is the second stage bootloader (bootcode.bin). The GPU reads this file and executes it

• The job of this stage of the bootloader is to enable the SDRAM and it loads and runs start.elf

• The last bootloader stage start.elf reads the kernel image (kernel.img), configuration file(config.txt), and kernel command line parameters (cmdline.txt), then it loads them in memory, and wakes up the ARM core

Page 46: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Boot Sequence

GPU On-Chip First Stage Bootloader

bootcode.bin

start.elf

Linux Kernel

GPU

ARM

Enables GPU to read the SD Card Enables GPU to read a FAT32 Partition

Enables the SDRAM Loads and runs start.elf

Loads the Kernel image Starts the ARM Core

Boot the Kernel Normal Operation

Page 47: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

cmdline.txt

• The cmdline.txt file contains parameters that are passed to the kernel when it is booting

• There are a lot of possible parameters that can be passed to the kernel

• Some important parameters:

console=tty1 (Kernel messages should be sent to tty1)

root=/dev/mmcblk0p2 (Root filesystem exists in this device)

rootfstype=ext4 (Root filesystem type is ext4)

Page 48: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Booting the Pi

• The first time the Pi boots, it will open an initial configuration screen (will be discussed later in this lecture)

• Perform the required configuration, or exit the configuration tool

• Then, the Pi shows the login prompt, and asks the user to login to it, the factory settings for login info are,

Username: pi

Password: raspberry

• Now the user will be accessing the command line interface

• To use the GUI, just use the command,

$ startx

Page 49: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Raspbian Prompt

Page 50: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Raspbian Desktop

Page 51: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

5. Configuring the Pi

Page 52: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Raspberry Pi Configuration (The raspi-config tool) • Some initial configurations need to be performed on the

Raspberry Pi

• This is done using the raspi-config tool

• This tool is initiated automatically on the first boot of the Raspberry Pi

• However, if you need to initiate this tool at a later time, just use the command from the Pi Prompt,

$ sudo raspi-config

Page 53: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Starting raspi-config

Page 54: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Overclocking the Pi

• By default, the ARM core runs at 700MHz

• Sometimes, it is required to boost the ARM Core performance by increasing its clock speed (overclocking it)

• The ARM Core can be overclocked up to 1GHz

• Both the GPU Core and the SDRAM can be overclocked as well

• Keep in mind that overclocking the Pi is not recommended unless you really need it. It generates more heat and may destroy the SoC or reduce its lifetime

• If you overclock the Pi, it is recommended to use a heatsink or a fan to dissipate the generated heat from the chip

Page 55: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Overclocking the Pi

Page 56: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Using a Heat-Sink

Page 57: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Advanced Options

Page 58: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Memory Split (ARM & GPU)

Page 59: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

6. Setup the Ethernet Connection

Page 60: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Setup the Ethernet Connection

• All you need is to plug the LAN cable into the Ethernet port, and your Raspberry Pi should be connected (notice that the network LEDs will start to flicker)

• To know the selected IP Address, do,

$ ifconfig

Note that this IP Address is assigned by the DHCP in your router, and hence it may change every time you boot

• You can fix the IP address of the R-Pi to a certain value by,

• Log on to the router and configure it to assign a fixed IP address to the Pi

Page 61: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Network Interfaces

Page 62: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

7. Setup the WiFi Connection

Page 63: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Setup the WiFi Connection

• You will need a WiFi (802.11) module that connects to the Raspberry Pi through the USB interface

• Keep in mind that not all the WiFi Modules are supported by the Pi • For a list of adapters that is verified to work with the R-Pi, check

http://elinux.org/RPi_USB_Wi-Fi_Adapters

• Make sure to connect the WiFi module to the R-Pi USB port while it is unplugged from power

• Now you should see two network devices when using ifconfig • On the Raspberry Pi desktop, double click on the WiFi Configuration

tool, and connect to the desired WiFi Access Point (like you would do it on your computer)

Page 64: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Network Interfaces

Page 65: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

WiFi Configuration Tool

Page 66: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

WiFi Configuration Tool

Page 67: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Network Interfaces

Page 68: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

8. Remotely Connect via SSH

Page 69: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Remotely Connect via SSH

• The first step to use SSH with the Raspberry Pi is to enable the SSH Daemon on the Pi

• Using the raspi-config tool, enable SSH

• Then on the host machine, use an SSH Client

• On Windows hosts, this can be done using the PuTTY program

• On Linux hosts, SSH client is part of the openssh-client package $ sudo apt-get install openssh-client

• From the host machine

$ ssh [email protected]

• When prompted for the password, set it as raspberry

• Notes,

• If the host is a Linux machine, and the IP address of the Pi is fixed, we can assign the PI a name in /etc/hosts and use it in the SSH command

$ ssh pi@my-pi

Page 70: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Finding Out the Pi IP Address

• What if I don’t know the IP address for the Raspberry Pi • Option 1:

• Hook a monitor and a keyboard /mouse and use the command $ ifconfig

• But there are problems with this solution: • Do we need to connect to a monitor/keyboard/mouse every time we boot (assuming

the IP address is not fixed) ? • If I shutdown, to disconnect the mouse/monitor/keyboard, I may have a different IP

address

• Accordingly, this solution is not practical

• Option 2: Use the nmap Linux Command from the host machine • Identify the LAN subnet address, Assuming that the host machine resides on the

same LAN, you can find out the LAN address via $ ifconfig

• Before you power up the Raspberry Pi, scan for all IP addresses on your LAN $ nmap -sP 192.168.101.*

• Power up your Raspberry Pi and wait for a minute for it to boot, then repeat the same command, and compare results to find out the new IP address

• If you have multiple new IP addresses, you can explore more about each one via, $ nmap -n 192.168.101.165

Page 71: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Using SSH without Password (Using SSH Public Keys) • To avoid entering the password every time you use ssh or scp to the R-Pi, we use

security keys for logging in • To enable that, we do the following:

• On the host machine generate a key-pair $ ssh-keygen

• You will need to answer a few questions, default values are ok • When asked about a pass phrase, any password is good (just remember it) • This will generate two key files in ~/.ssh directory

• Private Key: ~/.ssh/id_rsa • Public key: ~/.ssh/id_rsa.pub

• Then copy the public key to the R-Pi $ scp ~/.ssh/id_rsa.pub pi@my-pi:~/.ssh/any-file-name

• On the R-Pi, add the key to the file ~/.ssh/authorized_keys $ echo `cat ~/.ssh/any-file-name` >> ~/.ssh/authorized_keys

• Note that, the last two steps can be replaced with, from the host, $ cat ~/.ssh/id_rsa.pub | ssh pi@my-pi ‘cat >> ~/.ssh/authorized_keys’

• Then we try to ssh from the host into the pi, a prompt will ask for the password, and an option will appear to store it locally, accept that option

• That’s it, now we can use ssh and scp without the need for a password • Note, if you want to login as root, then you need to do the same in /root/.ssh

folder in the pi

Page 72: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

9. Remotely Connect Via VNC

Page 73: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

Remotely Connect Via VNC

• First we will need to install the VNC Server on the Raspberry Pi

• Issue the following commands on a terminal on the R-Pi or via an SSH session

$ sudo apt-get update

$ sudo apt-get install tightvncserver

• After installation, we will need to start the VNC Daemon in the Pi

$ vncserver :1

Note that it will ask you to enter a password

• On the host machine, we need to install a VNC Client, there are a lot of VNC Clients available for both Windows and Linux host machines

• Run the VNC Client on the host, make sure to state the server IP address and display number as :

$ vncviewer 192.168.0.113:1

Use the password that you set in the server

Page 74: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

More on Setting up VNC

• In case resolution is not set correctly, we will need to set the geometry of the VNC Server, such as, $ vncserver :1 -geometry 1440x900 Note: select the proper geometry

• In case we need the VNC Daemon to be up every time the R-Pi boots up,

then in the R-Pi, $ cd ~/.config $ mkdir autostart $ cd autostart $ vi tightvnc.desktop Then write the following in the file,

[Desktop Entry] Type=Application Name=TightVNC Exec=vncserver :1 StartupNotify=false

Page 75: Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi

http://Linux4EmbeddedSystems.com