Recently I got a Raspberry Pi 3 board and wanted to try out various OS options on it. I realized quite quickly that to try a new OS I would need to block copy (dd) the OS image to my SD card every time. I am running short on micro SD cards and it has a size limit too.
While I have a bunch of USB sticks lying around unused. So was thinking if the USB sticks could be used.
A little research on the Internet came up with 2 prominent options BerryBoot and Noobs. Both options allow multibooting your Pi board with different OS distros. While this is a good enough solution I wanted to know how things work internally and if there was a simple way to achieve multiboot without using any tools (and more so for my learning purpose).
On the Internet there is a lot of information on how to install and boot Linux from USB sticks for Raspberry Pi. The process is summarized in the following section.
Run Pi with Linux from USB stick
In simple words, booting Linux involves loading the kernel, which initializes the hardware, and then mounting the root filesystem, which has all the user applications. Usually the kernel images are kept in the first partition and this partition is mounted on /boot directory.
Going through the Pi documentation, it looks like Pi boards recognize the SD card as the only boot device. So the trick to run Linux on Pi from USB stick involves installing the kernel images on the SD card while keeping the root file system on the USB stick and providing the information about the root filesystem location to the kernel in the boot command line.
If we look ate the space usage, a typical kernel image is only around 10mb in size. With all the data in the /boot directory it is still within 30Mb of space, while the root file system size can be much bigger based on the user application and data.
How Boot loading on Pi works
The boot process on Pi expects the SD card to have a FAT32 based first partition. To boot Linux, the kernel image must be present on this partition. For Pi0, Pi1 models the default kernel image file name is kernel.img and for Pi2 and Pi3 models the default kernel image file is called kernel7.img . So the boot loader will look for the correct kernel image file for your model of Pi.
In addition to the kernel image, there are two configuration files, which are interesting to understand the booting process.
The first file cmdline.txt configures the command line parameter passed while starting the boot process. This file is more close to the grub/syslinux command line
Following is an example of content of cmdline.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline
root=/dev/mmcblk0p2 rootfstype=ext4 fsck.repair=yes rootwait
The second file is the configuration file config.txt, which is the equivalent of bios settings for the Raspberry Pi SoC. Here is an example file content
The documentation for all the options is available at https://www.raspberrypi.org/documentation/configuration/config-txt.md
Simple non-destructive way to tryout multiple OS on Pi
Now that we have a broad understanding of the booting process and the config files, lets looks at how we can use this to try different OS distributions for Pi3 using USB sticks.
If you look at the documentation for options supported by config.txt, you will notice that the kernel file name is configurable using the parameter “kernel”
So if we combine the USB booting mechanics of Pi we discussed in the previous section with the configurable kernel filename in config.txt we have a method to have multiple OS on different USB sticks.
And this is how to make it works…
- Use different USB sticks to hold the root filesystem for different OS distros. Normally all the Pi distros have a 2 partition based layout with the first partition being the FAT32 based boot partition, while the second is usually ext4 based root filesystem. So if you just dd the OS image on to the USB you root filesystem should be the second partition. Assuming this is the only usb stick attached to the Pi, the root partition should be recognized as /dev/sda2.
- Format your SD card for a sufficiently big FAT32 based first partition
- Store the kernel images for all the OS distros on this partition with different filenames. You can get the kernel images from the first partition of each of the USB sticks. If you attach both the SD card and the USB stick to a windows machine you should be able to just copy-paste the kernel images to the FAT32 partition on the SD card.
- Update the cmdline.txt file to point to the root filesystem partition e.g. root=/dev/sda2
- Finally update config.txt to point to the correct kernel filename. The one that you want to boot currently.
For this scheme to work you need to match the kernel filename for the distro configured in config.txt to the correct USB stick that you have attached to the Pi board.
NOTE: if you make a mistake just attached your SD card to another machine and edit the config.txt to fix it
The above processes can obviously be scripted to make it user-friendlier, but my purpose for the exercise was to get an understanding of the boot process on Pi and have some fun 🙂