Translate

Sunday, April 22, 2018

Raspbian - prepare SD card

April 2019 - Update !
I already did in the past an article about a SD card preparation for Raspberry Pi.
The idea behind this article is to find a way to simplify and speeding up to prepare an image for a Raspberry Pi with Raspbian and beside, things change constantly.
Here what we need to do now to prepare a Raspbian image (no NOOBS !!!)



Warning !  I did find the latest Raspbian distro "Stretch" not working properly with old Raspberry Pi, up to the version 3.
I used Jessie instead !


Classic

The Classic way need you to be able to be connected directly with the Raspberry Pi, so you need a monitor and at least a keyboard. A mouse can be needed if doing GUI.

Shopping list

What we need ?
  • a Raspberry Pi
    For this article I used a Raspberry Pi 3 B+ with the main connection to internet is via WiFi
  • a WiFi network
  • a Keyboard USB
  • a Mouse USB
  • a HDMI monitor
  • a 16 Gbyte Class 10 micro SD card
  • an USB reader for SD cards
  • A Linux machine (in my case based on Ubuntu 16.04 LTS)

SD card preparation

This procedure is to be done only once, the first time.
The image downloaded is the one recent at the preparation date of the article
The examples on the articles imply to use a Linux machine (in my case Ubuntu 16.04)

The SD card better be a 16 Gbyte one so to be able to easily hold different apps and data.
Note that the images downloaded are much smaller than 16 Gbyte, actually they can be from 2 to 4 Gbyte.
Just prepare the SD card formatted in fat32, single partition.  This will be overwritten anyway, is just necessary to allow the Linux system (in my case) to see the SD card.
No need to repartion the SD card after imaged. At the first boot Raspbian will do that, or in any case there is an option on raspi-config.
  • Download Raspbian  
    • Latest version
      At the time of writing the latest distro is Stretch
      Suggested to download the "lite" image since usually the goal is not to use the Raspberry connected to a monitor and a keyboard
      Extract the image from the zip file
    • Previous versions
      (suggested to use Jessie on older machines)
  • Format SD card in Fat32
    Use gparted
  • Go in the directory where the image is
  • Burn the image on the SD card
    Note that in my system the SD card is placed on a USB reader and it appears as /dev/sdd
    sudo dd bs=4M if="image_name" of=/dev/sdd conv=fsync
  • Umount the SD card

Raspberry Pi preparation

  • Insert SD card it in the Raspberry Pi
  • Connect a USB keyboard/mouse to the Raspberry Pi
  • Connect LAN cable
  • Connect HDMI monitor
  • Turn on Raspberry Pi
No internet connection will exists at this stage.
  • After log in from console
    • execute config (sudo raspi-config)
      • change pi user password
      • enable ssh
    • set up WiFi account
      • on Stretch there is a menu on raspi-config
      • on Jessie edit config files 
        • sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
        • Set up the country
          country=US
        • Add WiFi networks
          network={
              ssid="ssid_name"
              psk="ssid_password"
          }
  • Reboot.  If achieved the WiFi connection (verify with ifconfig)
  • Disconnect video and keyboard
  • Reboot
From now on the connection will be via ssh.
  • After reconnection
    • Update image
      • sudo apt-get update
      • sudo apt-get upgrade
    • install generic building tools
      sudo apt-get install build-essential  
      (note that could be already installed)
At this point the Raspberry Pi is ready for any specific task you need to do.

PiBackery way

PiBackery is a new way to set up a Raspberry Pi I found.
It is much much easier to set up an SD card. Though there are some limitations, for example what distribution to install (I think is limited to the latest one).

Shopping list


What we need ?
  • a Raspberry Pi
    For this article I used a Raspberry Pi 3 B+ with the main connection to internet is via WiFi
  • a WiFi network
  • a 16 Gbyte Class 10 micro SD card
  • an USB reader for SD cards
  • A Windows or Mac (Linux machine are not yet supported)

SD card preparation

This procedure is to be done only once, the first time.
Start the PiBackery program, build your configuration (or import a previous setting saved), insert the SD card, burn it, extract it and insert it in the Raspberry Pi.
Done :)

The main advantage is that the critical parts, setting up WiFi/enable SSH/other things, is done in the SD card burning phase, i.e. the SD card will be come out already configured.
So after that the Raspberry will be activated and ready to be accessed via WiFi.

Tips

Boots


PiBackery allows to set up two type of boots.
One executed the first time only and one for every time.
Be sure to add in both type of boots the basic configurations, like WiFi, change hostname, etc.
The first boot section should contains basic settings necessary to execute the specific jobs, like installation from internet of packages.

SSH

One problem with this approach can be the SSH.
Originally the Raspbian images used as base, had the SSH enabled by default.
The last version of Raspbian, codename Stretch, has the SSH disabled by default.
Because originally the SSH was already enabled, there is "block" to add to the configuration that enable the SSH.
Since is possible for everybody to add a new block, I suggest to :
  1. verify on the PiBakery block latest repository that exists the block you need
  2. If present, just clone and copy the blocks in the installed program
  3. If not present you can add you ssh enable block

Troubleshooting


Is possible that the image loaded by PiBakey is very old.
If this is the case is possible it will have problems to update.
Follows these guidelines :
  • burn the SD card
  • at the first run, wait a while and monitor when the board come alive (i.e. visible on the network)
  • login 
  • run immediately the raspi-config utility and 
    • enable SPI
    • enable SSH
    • enable I2C
    • expand the SD card space
    • update raspi-config
  • reboot
  • run sudo apt-get update
  • run sudo apt-get upgrade

Update fails

Fails 1

Running the update (sudo apt-get upgrade) is possible fails over the kernel updating, when unpacking the raspberry-kernel.
If so the very probable cause is to don't have expanded the space on the SD card BEFORE to start the update !
Be sure to run raspi-config BEFORE to do ANY update on the system (see above) !!

Fails 2

The update fails at :

Installing new version of config file /etc/dbus-1/system.d/wpa_supplicant.conf ...
Installing new version of config file /etc/wpa_supplicant/functions.sh ...
Created symlink /etc/systemd/system/dbus-fi.w1.wpa_supplicant1.service → /lib/systemd/system/wpa_supplicant.service.
Created symlink /etc/systemd/system/multi-user.target.wants/wpa_supplicant.service → /lib/systemd/system/wpa_supplicant.service.

packet_write_wait: Connection to 192.168.27.66 port 22: Broken pipe


Re-login and run : sudo dpkg --configure -a

Fails 3

The update fails at :

Setting up openssh-server (1:7.4p1-10+deb9u6) ...
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

packet_write_wait: Connection to 192.168.27.66 port 22: Broken pipe

Re-login and run : sudo dpkg --configure -a


Shutdown not working

If the commands sudo poweroff or sudo shutdown -h now are not working, i.e. typically they close the system but then it restart, there are many possible cause. and is not easy to pinpoint a specific one.
Some thing to check are :

  • be sure to update the system also forcing the sync :
    • sudo sync
    • sudo apt-get update
    • sudo apt-get upgrade
    • sudo sync
  • update the firmware
    • sudo rpi-update
If still persist, it should be possible to force the shutdown with :

sudo systemctl --force --force poweroff

Errors on CA certificates

If executing the rpi-update command there are errors related to the CA certificates, try to reinstall the certificates with :

  • sudo apt-get update
  • sudo apt-get install -y --reinstall ca-certificates




No comments:

Post a Comment