The Raspberry Pi is a great way to get hands-on experience with Arm software development at conferences, workshops, and community meetups. The board is low cost and well documented. The latest Raspberry Pi 4 improves performance with the Cortex-A72.
There are many ways to start using a Raspberry Pi, but most require either a monitor and keyboard or the ability to find the assigned IP address of the Pi on a local network. These techniques work great on a home or small office network, but it’s not so easy at a conference or university with wireless.
The challenge is how to connect and use a Raspberry Pi 4 at a conference or workshop for hands-on labs without bringing monitors and networking equipment.
One way is to connect a USB cable between a laptop and the Pi 4 to both power the Pi and to create a serial connection over USB. With the serial connection is in place it’s possible connect to the conference or public wireless for internet access. It’s also possible to setup a USB cable to provide Ethernet connectivity, but it’s more difficult to setup and a serial connection is good enough to get the wireless connected.
Getting started with any Raspberry Pi involves downloading a software image and writing it to an SD card. Download a copy of the Raspbian image. The Lite image will work for this exercise, but the desktop image is also fine.
To enable the serial connection two files need to be modified in the boot partition and one symbolic link created in the root filesystem. The modifications can be done to the downloaded image before it is written to the SD card or after the SD card has been written. If multiple SD cards are needed, then it’s better to make the modifications to the image and write multiple SD cards with the new image. If only one SD card is needed, then it may be easier to write the SD card first and then modify the files directly on the SD card.
Below are the instructions for each method. This is easiest to do on Linux. The Raspbian boot partition is a FAT file system and can be read on Windows, but the root file system is ext4 and hard to read on Windows without additional software.
Write the SD card following the installation guide.
After the SD card is written make three changes.
Edit config.txt
Open the file config.txt in the boot directory using a text editor and add the following new line to the end of the file:
dtoverlay=dwc2
Edit cmdline.txt
Open the file cmdline.txt also in the boot directory and add “modules-load=dwc2,g_serial” to the kernel command line after rootwait.
The full command line looks like this (subject to change so watch carefully):
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=17869b7d-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_serial quiet init=/usr/lib/raspi-config/init_resize.sh
Start getty for the serial connection
Next, start getty, the program used to allow users to login. Go into the root file system and navigate to /lib/systemd/system/getty.target.wants and create a symbolic link:
$ cd lib/systemd/system/getty.target.wants $ sudo ln -s /lib/systemd/system/getty@.service getty@ttyGS0.service
Now, when the serial connection is made, a login prompt will be available to enter the username and password. Without this, the connection will be made but there will be no way to login.
Unmount the SD card and it’s ready to use in the Raspberry Pi 4. Skip the next section and go to the “Booting the Pi” section.
The other option is to modify the image first, then write it to the SD card. It’s good if you want to write multiple SD cards. It’s also useful if you have access to a Linux machine to modify the image file, but don’t have a Linux machine with an SD card slot. Once the image is modified it can be written to an SD card using any OS.
First, unzip the image and mount the file system and boot partition
$ unzip 2019-07-10-raspbian-buster-lite.zip
Mount the image:
$ sudo losetup -P /dev/loop10 2019-07-10-raspbian-buster-lite.img $ sudo mount /dev/loop10p2 /mnt $ sudo mount /dev/loop10p1 /mnt/boot
Now perform the same steps as above.
Unmount the file systems and the new image is ready.
$ sudo umount /mnt/boot $ sudo umount /mnt
Move the new image to a new name and use zip to create the new and improved image with the serial connection enabled.
$ mv 2019-07-10-raspbian-buster-lite.img 2019-07-10-raspbian-buster-lite-serial.img $ zip 2019-07-10-raspbian-buster-lite-serial.zip 2019-07-10-raspbian-buster-lite-serial.img
Finally, write an SD card using the new image and proceed to booting the Pi.
Insert the newly written SD card into a Raspberry Pi 4 and connect a USB cable from your computer to the USB Type-C connector on the Raspberry Pi 4. The computer will provide the power to the Pi and it will startup.
It takes some time on the first boot to resize the file system, reboot, and eventually start getty on the serial connection.
There are different ways to connect depending on the host operating system.
For Linux, a new device node should appear at /dev/ttyACM0 and can be confirmed using the ls command.
For Windows, a new COM port will be created. Go to Windows Setting and click Devices. Check the device list, it will typically be COM4 or COM5.
One way to connect from Linux or Mac is using the screen command.
$ sudo screen /dev/ttyACM0
This should open a login prompt with the default username of pi and password of raspberry.
Quit screen using ctrl-A then ctrl-D
One way to connect from Windows is using putty. Enter the COM port and select a serial connection and connect to see the login prompt.
Here is the setup screen on putty.
Click Open to make the connection and login:
Make sure to change the pi password from the default using the passwd command.
Once the serial connection is working it’s possible to connect to wireless networks using raspi-config.
$ sudo raspi-config
Use option 2 to select Network Options, then option 2 again to select Wi-fi and enter SSID and Key (if needed).
It’s also helpful to enable ssh in raspi-config. Use option 5 Interfacing Options from the top menu, then 2 SSH.
This recipe makes it possible to jumpstart a Raspberry Pi 4 for labs and workshops without a keyboard and monitor on a more complex network, such as at a conference. Hopefully the techniques described here can be used by others for other events.
Wireless networks requiring authentication or acceptance of terms and conditions via a captive portal can also be a challenge because a browser is required to trigger the portal which grants internet access. One way around it is to use a text-based browser such as Lynx. Unfortunately, Lynx is not installed in the Raspbian image, but it can be added. The trouble is now to add it if we can’t start the Pi and connect it to a network to install new applications.
The SD card image is for the Arm architecture, but it can be modified on an x86 Linux system. Installing an application could be a simple file copy, but the best way is to use the package manager to just install it. One trick to run Arm executables on an x86 Linux machine is using qemu and a change root (chroot).
Before writing the image to the SD card, use this process to install the Lynx browser using the apt. Assuming an x86 Linux machine is used to prepare the image, qemu can be used to run apt in the Arm file system using a chroot. Mount the image, insert qemu into the file system, again use these commands to make the filesystem appear like it is really running on Arm hardware when it’s not.
$ sudo losetup -P /dev/loop10 2019-07-10-raspbian-buster-lite-serial.img $ sudo mount /dev/loop10p2 /mnt $ sudo mount /dev/loop10p1 /mnt/boot $ sudo mount --bind /dev /mnt/dev $ sudo mount --bind /dev /mnt/dev $ sudo mount --bind /sys /mnt/sys $ sudo mount --bind /proc /mnt/proc $ sudo mount --bind /dev/pts /mnt/dev/pts
Comment out the only line of the file ld.so.preload using a # and copy qemu from the host machine into the file system.
If qemu-arm-static is not installed, install the Ubuntu package (similar for other versions of Linux).
$ sudo apt install qemu-user-static $ sudo vi /mnt/etc/ld.so.preload $ cp /usr/bin/qemu-arm-static /mnt/usr/bin/
Enter the chroot and install lynx. Once the chroot is entered it looks exactly like an Arm machine with the Pi filesystem, but it’s really an x86 Linux machine.
$ sudo chroot /mnt /bin/bash # apt install lynx
Uncomment the line (put it back to original)
$ sudo vi /mnt/etc/ld.so.preload
Unmount everything
$ umount /mnt/dev/pts $ sudo umount /mnt/dev/pts $ sudo umount /mnt/dev $ sudo umount /mnt/sys $ sudo umount /mnt/proc $ sudo umount /mnt/boot $ sudo umount /mnt
Installing Lynx ahead of time makes it possible get around the captive portal on the wireless network. Start lynx from the command line and access any web site. The portal to agree to the terms of using the network will open and you can agree, select OK, or whatever is needed to gain internet access.
Now you've learned how to jump start your Raspberry Pi's internet connection, see if you can use this knowledge at one of our Arm TechCon 2019 workshops!
[CTAToken URL = "https://www.armtechcon.com/agenda/workshops/" target="_blank" text="Arm TechCon 2019 Workshops" class ="green"]