Create a custom Raspberry Pi OS image
Tailor Made
A few simple steps let you create a preconfigured Raspberry Pi operating system that lets you cut the time required to burn and configure your OS images.
To get a Raspberry Pi up and running, you need to download the latest operating system (OS) release image, burn it to an SD card, slide the card into the Pi, and power it up. After the Pi boots, you log in with the default credentials and run sudo raspi-config
to configure WiFi, locale, keyboard, and time zone and to enable SSH, I2C, a camera, and whatever else you need for I/O. Next, you update the OS with sudo apt update
, sudo apt upgrade
, and reboot before finally logging back in and installing all your favorite software that is not installed on the base image.
For casual Pi users, this procedure is a one-time or rare task. However, for experienced makers who have gone through this drill dozens – if not hundreds – of times, it is a real pain.
In this article, I present a method to create your own custom Raspberry Pi OS image that is just under 2GB in size, making it very fast to burn to any size SD card. No more waiting for an 8 or 16GB image to burn. This custom image not only burns fast, but when it boots, it expands the root filesystem to the full size of the target SD card. When the boot completes, you have a clean Pi OS system with your preferred customizations. No more raspi-config
and wasting time installing your preferred software.
A custom OS on an SD card is a huge time saver if you need to create clean Raspberry Pi OS systems to test software installation scripts. If you burn a lot of Pi OS images in your lab, you will wonder how you got along without this capability in your toolbox.
Most makers never think about the first-time boot of a Raspberry Pi OS image. Behind the familiar on-screen Raspberries, one for each CPU core, and the brief display that says Resized root filesystem … a lot is happening. (See the "One-Time Partition Resizing" box.)
One-Time Partition Resizing
Figure 1 provides a simplified view of the root partition resize process. When the Raspberry Pi first boots, it reads command-line parameters from the /boot/cmdline.txt
file (step 1), with parameters for the output console, TTY speed, root partition ID, and so on.
In step 2, the init_resize.sh
script performs a number of housekeeping chores, such as preparing and mounting the filesystems. Surprisingly, this script does not resize the physical partition; it simply calls the partition editor (parted
) to modify the SD card partition table, which determines how big the SD card is and then modifies the second partition entry to expand it to fill the remaining empty space, making the initial resize a quick process. The script also modifies the cmdline.txt
file, removing the term quiet
plus removing itself so it will never get called again. Once the housekeeping tasks are performed, the script reboots the Pi (step 3).
The task to resize the root filesystem is done by a registered service, the short /etc/init.d/resize2fs_once
shell script (step 4), that runs at system init time. This short-lived script does three things: It calls resize2fs
to resize the root filesystem to match the partition table modified in step 2 in the flowchart and then performs the other two tasks described in step 5.
Once the root filesystem has been resized, this never has to be done again. In the final step, the partition resize script deletes itself as a registered service and then deletes its own file from the filesystem.
In summary, the root filesystem partition resize task has split responsibilities. An init script executed from the boot command line modifies the SD card partition table to expand the root partition to fill the unused space on the SD card. After a reboot, a registered service physically resizes the partition. This process only needs to run once, so both scripts ensure that they never run again.
Building a Custom OS Image
When you create your super-shiny custom Raspberry Pi OS image, if you do not re-enable the resize process, your image will boot a Pi and work, but it will not automatically resize the root partition to the size of the host SD card. You will have to do it manually, which defeats the whole purpose of the custom image. To create your custom image, you need to do seven things (Table 1). In step 1 it does not matter whether you use the Desktop or Lite Pi OS version.
Table 1
Custom Image Creation
Step | Action |
---|---|
1 |
Download the base Pi OS image [1]. |
2 |
Burn the base image to an SD card. |
3 |
Modify the image to prevent automatic resizing of the root partition. |
4 |
Boot the image and resize the root partition. |
5 |
Install software and other required customizations. |
6 |
Restore the ability to auto-resize the root partition. |
7 |
Make a dd image master file of your customized Pi OS. |
Burn the Base Image
Again, this is the standard drill we all know and love [2]. It does not matter how big the SD card is as long as the base OS image will fit on it.
Prevent Auto-Resizing
Why would you want to prevent automatically resizing the root partition? Because you want your custom image to be as small as possible. In fact, your custom image will be close to the same size as the Raspberry Pi OS base image – that is, OS Lite (2GB), OS with desktop (4GB), or OS with desktop and recommended software (9GB).
When you burn your custom image to an SD card, the smaller the image, the faster it burns. Image burn time really ads up when you have 8, 16, or 32GB images to burn.
Once you have burned the base OS image to an SD card in a card adapter, plug the card into a USB port on your Linux host. I run Ubuntu Linux on my laptop, and when I plug in a Raspberry Pi OS SD card, it mounts both partitions (/boot
and /rootfs
) automatically. The OS Lite image partition layout is shown in Figure 2.
If your system does not mount the partitions, you will have to do it manually. You are only interested in mounting the /boot
partition (partition 1). On my system, the SD card with the base Pi OS image is device /dev/sda
.
To mount the first partition (/boot
), I entered:
$ sudo mkdir /media/sdcard $ sudo mount -o,rw /dev/sda1 /media/sdcard
Now the /boot
partition of the SD card is mounted on /media/sdcard
. (Be sure to change the mount
command to match the device ID for your image.)
Once the /boot
partition is mounted (Figure 3a), edit the file named cmdline.txt
by opening the file in your favorite editor. If you used sudo
to mount the partition, then you will need to be root to edit any files.
The cmdline.txt
file contains only one line. The init
command at the end of the line needs to be removed before you save the file. Be sure you do not add a newline and create a second, empty line, which will break the script.
Now, to unmount the image, use the command:
$ sudo umount /media/sdcard
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.