Skip to content

Debian 9 + Debian Stretch + Script and Tutorial for RPi2/3 + Raspberry Pi 2 + Raspberry Pi 3

License

Notifications You must be signed in to change notification settings

paulghill/rpi23-gen-image

 
 

Repository files navigation

rpi23-gen-image

Introduction

rpi23-gen-image.sh is an Debian Linux bootstrapping shell script for generating Debian OS images for Raspberry Pi 2 (RPi2, 32 bit) and Raspberry Pi 3 (RPi3, 64 bit) computers.

Note by Paul Hill:

This is another fork from the original project by github user "drtyhlpr", utilizing the work of github user michaelfranzl and current stable release of Debian 9. Please review the changes as they are significant compared to the original project and the fork Micahel Franzl started. Please visit Michael Franzl's github for original notes https://github.com/michaelfranzl/rpi23-gen-image. This is still in development.

  • Only Debian 9 is supported.
  • Designed to be similar to a base Debian 9 install with system utilities and ssh.
  • Only the official/mainline/vanilla Linux kernel is supported (not the raspberry flavor kernel).
  • The Linux kernel must be pre-cross-compiled on the PC running this script (instructions below).
  • Only U-Boot booting is supported.
  • The U-Boot sources must be pre-downloaded and pre-cross-compiled on the PC running this script (instructions below).
  • The installation of the system to an SD card is done by simple copying or rsyncing, rather than creating, shrinking and expanding file system images.

Major changes:

  • Designed to use current kernel from Debian 9.
  • Major remake of rpi23-gen-image.sh

    Removed variables that were not in use. Moved most IF statements into a function. Downloads firmware for boot and wireless LAN. APT includes are similar to a base + system utility + ssh build for Debian 9. Removed all reduce size functionality. Removed additional variables based on new scripts to download and compile kernel and u-boot. chroot_exec function has been placed into interactive mode. During image building, you will be prompted to select a locale. See 12-locale.sh under bootstrap.d for changes. During image building, you will be prompted for a password for the user. See 30-security.sh for changes. By default, this does not enable root account and requires a user and a password (which will be prompted for).

  • Rework of files directory

    Removed most files that were not used and not applicable. bootstrap.d scripts now generate some files that were removed

  • Rework of firstboot

    Firstboot scripts and directory have been removed. A rc.firstboot script was made and will now be copied to chroot environment. The rc.firstboot script can be found in the files directory.

  • Added pmg binary.

    pmg is a Rust project I started for generating MAC addresses, and it is needed for this installation to keep the same MAC address for the LAN during reboots. MAC address generated by pmg binary is listed in the /etc/network/interfaces file under eth0 hwaddress. Visit https://github.com/hillsys/pmg for more information.

  • Added en-us.sh example file.

    This file contains all the variables that can be set for the rpi23-gen-image.sh script

  • bootstrap.d

    Changes to most scripts here. Removal of scripts that are no longer needed. Still in development

  • Added get-build-kernel.sh script

    This script will download the kernel source for Debian 9. After downloading kernel source, it will clean and copy the kernel.config in files directory. The kernel.config enables netfilter to allow iptables or nftables to work. The default script does not enable these items. Will prompt user to select Raspberry Pi model to compile kernel against.

  • Added get-build-uboot.sh script

    This script will download u-boot source. It will apply programming change as per https://github.com/michaelfranzl/rpi23-gen-image. Will prompt user to select Raspberry Pi model to compile against.

This script only supports:

RPi2 with u-boot with official kernel
RPi3 with u-boot with official kernel --Not tested

This script is EXPERIMENTAL, and still in development.

It is assumed that you know how to navigate folders, utilize a text editor (vi, vim, nano, etc.), and are familiar with some basic Debian/Linux commands such as apt. If you are not logged in as root on your system, you will need to utilize sudo for root privileges for most commands.

Setting up host environment

Depending on how you want to make your image will determine what you need for storage space. If you are wanting to create a .img file instead of copying directly to the micro SD card, such as a case in a virtualized environment, you will need to create a 3GB drive with two partitions to mimic the SD card installation. My build drive is 10GB and it is 2/3 full, and my image drive is 3GB if that helps you understand what you will require for storage.

We will work only in the rpi23-gen-image directory.

Note: The new scripts will download necessary files into this directory.

Do the following steps as root user or utilizing sudo.

Optional: Set up caching for apt

This way, you won't have to re-download hundreds of megabytes of Debian packages from the Debian server every time you run the rpi23-gen-image.sh script.

apt install apt-cacher-ng

Check its status page:

http://localhost:3142

Note: This is not needed, but it is highly recommended. I have removed the hard code of the apt-catcher from the script files.

Install dependencies

The following list of Debian packages must be installed on the build system because they are essentially required for the bootstrapping process.

apt-get install debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git bc device-tree-compiler dbus psmisc

For a RPi2, you also need:

apt-get install crossbuild-essential-armhf

For a RPi3, you also need:

apt-get install crossbuild-essential-arm64

Download the scripts

To start the process of building a pure Debian 9 system for your Raspberry, download the scripts.

cd [location you want to work from]
git clone https://github.com/hillsys/rpi23-gen-image
cd rpi23-gen-image

Kernel compilation

You must have sources enabled in your /apt/etc/sources.list. Below shows my Debian 9 virtual machine sources.list. Make sure you have deb-src listed for the particular mirror you are using.

deb http://mirrors.kernel.org/debian/ stretch main non-free contrib
deb-src http://mirrors.kernel.org/debian/ stretch main non-free contrib

deb http://security.debian.org/debian-security stretch/updates main contrib non-free
deb-src http://security.debian.org/debian-security stretch/updates main contrib non-free

# stretch-updates, previously known as 'volatile'
deb http://mirrors.kernel.org/debian/ stretch-updates main contrib non-free
deb-src http://mirrors.kernel.org/debian/ stretch-updates main contrib non-free

To edit or view your sources.list:

As root:

nano /etc/apt/sources.list

or

sudo nano /etc/apt/sources.list

From the rpi23-gen-image directory issue ./get-build-kernel.sh. If you are not root use sudo ./get-build-kernel.sh. Follow the prompts on the screen as the appear. It will compile with all available processors (cores). If you wish to manually configure the kernel, visit https://github.com/michaelfranzl/rpi23-gen-image for more details. The supplied kernel configuration is known to work for Raspberry Pi 2 and allow iptables or nftables to work.

U-Boot bootloader compilation

From the rpi23-gen-image directory issue ./get-build-uboot.sh. If you are not root use sudo ./get-build-kernel.sh. Follow the prompts on the screen as the appear. It will compile with all available processors (cores).

Create your own build script.

Before you can begin building your image for the Raspberry, you will want to at least look over the settings file, especially if your default language is not english.

en-us.sh

RPI_MODEL=2 \ 
APT_SERVER="mirrors.kernel.org" \
APT_PROXY="localhost:3142" \  
APT_INCLUDES="iamerican,ibritish,ienglish-common,task-english,wamerican" \
HOSTNAME="pi2-stretch" \  
USER_NAME="administrator" \ 
USER_LOCALE="en_US.UTF-8" \
ENABLE_CONSOLE=false \
ENABLE_DHCP=true \  
ENABLE_IPV6=false \ 
ENABLE_SOUND=false \ 
NET_ADDRESS="" \
NET_MASK="" \ 
NET_GATEWAY="" \  
NET_DNS_1="" \
NET_DNS_2="" \ 
NET_NTP_1="0.us.pool.ntp.org" \ 
NET_NTP_2="1.us.pool.ntp.org" \ 
./rpi23-gen-image.sh

You can copy the current en-us.sh file to create one specific for you location. For example, Russian is shown below.

cp en-us.sh ru_RU.sh

nano ru_RU.sh

RPI_MODEL=2 \ 
APT_SERVER="mirrors.kernel.org" \ 
APT_PROXY="localhost:3142" \ 
APT_INCLUDES="task-russian" \ 
HOSTNAME="pi2-stretch" \  
USER_NAME="administrator" \ 
USER_LOCALE="ru_RU.UTF-8" \ 
ENABLE_CONSOLE=false \ 
ENABLE_DHCP=true \  
ENABLE_IPV6=false \ 
ENABLE_SOUND=false \ 
NET_ADDRESS="" \ 
NET_MASK="" \ 
NET_GATEWAY="" \  
NET_DNS_1="" \ 
NET_DNS_2="" \ 
NET_NTP_1="ru.pool.ntp.org" \ 
NET_NTP_2="rs.pool.ntp.org " \ 
./rpi23-gen-image.sh

You can do web searches to find specifics on what you will need to install. https://docs.moodle.org/dev/Table_of_locales is a good starting point to find your locale code. Remember what locale code you use, because you will be asked to select that code during the creation of the image.

After you have configured your new script, execute the script ./scriptname.sh where scriptname is the name of the script. If you want to run the en-us script just type ./en-us.sh or if you are not root, sudo ./en-us.sh.

After the script starts, it will take some time to build the new image for the Raspberry. Check on it every so often as towards the end of the process it will prompt you for the locale you want to use and for a password to log into the account.

Note: The following information is the same as provided by Michael Franzl https://github.com/michaelfranzl/rpi23-gen-image

Install the system on a SD card

Insert a SD card into the card reader of your host PC. You'll need two partitions on it. I'll leave as an exercise for the reader the creation of a partition table according to the following output of fdisk for a 32GB card:

Device         Boot  Start        End    Sectors    Size   Id  Type
/dev/mmcblk0p1        2048     500000     497953  243.1M    c  W95 FAT32 (LBA)
/dev/mmcblk0p2      501760   62552063   62050304   29.6G   83  Linux

The following commands will erase all contents of the SD card and install the system (copy via rsync) on the SD card:

umount /dev/mmcblk0p1
umount /dev/mmcblk0p2

mkfs.vfat /dev/mmcblk0p1
mkfs.ext4 /dev/mmcblk0p2

mkdir -p /mnt/raspcard

mount /dev/mmcblk0p2 /mnt/raspcard
mkdir -p /mnt/raspcard/boot/firmware
mount /dev/mmcblk0p1 /mnt/raspcard/boot/firmware

rsync -a ./images/stretch/build/chroot/ /mnt/raspcard

umount /dev/mmcblk0p1
umount /dev/mmcblk0p2

Note about SD cards: Cheap (or sometimes even professional) SD cards can be weird at times. I've repeatedly noticed corrupt/truncated files even after proper rsync and proper umount on different brand new SD cards. TODO: Add a method to verify all file checksums after rsync.

Try booting the Raspberry

Insert the SD card into the Raspberry Pi, and if everything went well, you should see a console-based login prompt on the screen. Login with the login details you've passed into the script (USER_NAME and PASSWORD).

Alternatively, if you have included "avahi-daemon" in your APT_INCLUDES, you don't need a screen and keyboard and can simply log in via SSH from another computer, even without knowing the Rasberry's dynamic/DHCP IP address (replace "hostname" and "username" with what you have set as USER_NAME and HOSTNAME above):

Finishing touches directly on the Raspberry

Remember to change usernames, passwords, and SSH keys!

Network Time Synchronization

The Raspberry doesn't have a real time clock. But the default systemd conveniently syncs time from the network. Check the output of timedatectl. Confirmed working for both RPi2 and RPi3.

Hardware Random Number Generator

The working device node is available at /dev/hwrng. Confirmed working for both RPi2 and RPi3.

I2C Bus

Also try I2C support:

apt-get install ic2-tools
i2cdetect -y 0

Confirmed working for both RPi2 and RPi3.

Test onboard LEDs

As of the kernel revision referenced above, this only works on the RPi2. The RPi3 has only the red PWR LED on all the time, but otherwise is working fine.

By default, the green onboard LED of the RPi blinks in a heartbeat pattern according to the system load (this is done by kernel feature LEDS_TRIGGER_HEARTBEAT).

To use the green ACT LED as an indicator for disc access, execute:

echo mmc0 > /sys/class/leds/ACT/trigger

To toggle the red PWR LED:

echo 0 > /sys/class/leds/PWR/brightness # Turn off
echo 1 > /sys/class/leds/PWR/brightness # Turn on 

Or use the red PWR LED as heartbeat indicator:

echo heartbeat > /sys/class/leds/PWR/trigger

Test GPU acceleration via VC4 kernel driver

Successfully tested on the RPi2 and RPI3.

apt-get install mesa-utils
glxgears
glxinfo | grep '^OpenGL'

Glxinfo should output:

OpenGL vendor string: Broadcom
OpenGL renderer string: Gallium 0.4 on VC4
OpenGL version string: 2.1 Mesa 12.0.3
OpenGL shading language version string: 1.20
OpenGL ES profile version string: OpenGL ES 2.0 Mesa 12.0.3
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 1.0.16

About

Debian 9 + Debian Stretch + Script and Tutorial for RPi2/3 + Raspberry Pi 2 + Raspberry Pi 3

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 51.4%
  • Rust 48.4%
  • Makefile 0.2%