After setting up a Raspberry Pi with the desired software and configurations, create a disk image to replicate it on other devices.
Learn how to make an exact copy of an SD card using a Raspberry Pi.
Start the Raspberry Pi and connect the SD card and USB drive
Boot up the Raspberry Pi with the SD card containing the base Raspberry Pi OS. (not the card you would like to clone)
Connect to the Raspberry Pi with SSH or a monitor and keyboard.
Open a terminal on the Raspberry Pi.
Using an SD card reader, insert the source SD card into one of the available USB ports on the Raspberry Pi.
The output will include a list of block devices, including the SD card. The SD card will be identified as a block device, such as /dev/sda
, /dev/sdb
, etc...
It will appear similar to the following:
Disk /dev/sda: 7.36 GiB, 7901020160 bytes, 15431680 sectors Disk model: Storage Device
While there are two partions on the SD card, (eg: /dev/sda1
and /dev/sda2
),
the partition that will be cloned is the entire disk, not just one partition. As such, we are interested in the block device, not the partition.
The block device id will look similar to the following:
/dev/sda
, /dev/sdb
, etc..., it does not have a number at the end.
Insert and mount a USB Drive to which the disk image will be written
Identify the USB drive with the command: sudo fdisk -l
The USB drive will be identified as a Disk, such as /dev/sda
, /dev/sdb
, etc...
with the Device partions listed below it, such as /dev/sdb1
, /dev/sdb2
, etc...
It will appear similar to the following:
Disk /dev/sdb: 7.36 GiB, 7901020160 bytes, 15431680 sectors Disk model: Storage Device ... Device Boot Start End Sectors Size Id Type /dev/sdb1 8064 15431679 15429632 14.4G b HPFS/NTFS/exFATIt is the identifier
/dev/sdb1
that will be used to mount the drive.
Once you've identified the drive (ie: sda, sdb, etc...) from the command prompt on the Pi, mount the drive to a directory on the Raspberry Pi with the command: sudo mount /dev/sdb1 /mnt
The /mnt
directory is a common location to mount external drives. If you have a different location you would like to use, you can change this to a different directory.
The directory name will be referenced in a later step.
Use the Raspberry Pi to clone the source SD card as a disk image written to the USB drive.
dd
? dd
makes a bit-for-bit copy of the entire SD card, including the boot sector, partition table, and all of the data on the card, saving it all to a single file.
status=progress
argument? dd
can take a long time! The `status=progress`
argument will display how many bytes have been written out during this process. With the Raspberry Pi setup with the SD card connected and USB Drive mounted, it's now ready to clone the entire contents of the SD card to a single *.img file.
From the command prompt on the Pi, change to the directory where the image is located on the USB drive with the command: cd /mnt
If you mounted the SD card to a different directory, change to that directory instead.
dd
application to start cloning the SD card with the command:
sudo dd if=/dev/sda of=pi-clone.img bs=4M status=progress
15+ mins
dd
process has completed, you can remove the source SD card if desired, as it's no longer needed for this process.
Notes:
if
parameter, eg: /dev/sda
, /dev/sdb
, is the location of the SD card, identified in Step 1, Part 2.of
parameter, pi-clone.img
is the name of the file where the contents of the SD card will be written.
Install and run `pishrink.sh` to compress the cloned img file.
dd
program will copy all of this empty space even though it's not really needed. Shrinking the image will
remove this empty space, making the image smaller and easier to transfer to other devices.
From the command prompt on the Pi, complete the following steps:
Add execute permissions to the pishrink script with the command: sudo chmod +x pishrink.sh
This next step will overwrite the original image file with a new, smaller image file. If you'd like to keep the original pi-clone.img
, make a copy of it before proceeding.
From the command prompt on the Pi, complete the following steps:
Change to the directory where the image is located on the USB drive with the command: cd /mnt
If you saved the image to a different directory, change to that directory instead.
PiShrink v24.10.23 - https://github.com/Drewsif/PiShrink pishrink.sh: Gathering data pishrink.sh: Checking filesystem rootfs: 122290/445312 files (0.1% non-contiguous), 1018640/1808384 blocks resize2fs 1.47.0 (5-Feb-2023) pishrink.sh: Shrinking filesystem resize2fs 1.47.0 (5-Feb-2023) Resizing the filesystem on /dev/loop0 to 1463731 (4k) blocks. Begin pass 2 (max = 8046) Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Begin pass 3 (max = 56) Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The filesystem on /dev/loop0 is now 1463731 (4k) blocks long. pishrink.sh: Zeroing any free space left PiShrink v24.10.23 - https://github.com/Drewsif/PiShrink pishrink.sh: Gathering data pishrink.sh: An existing /etc/rc.local was not found, autoexpand may fail... grep: /tmp/tmp.uGvlhvd09L/etc/rc.local: No such file or directory Creating new /etc/rc.local pishrink.sh: Checking filesystem rootfs: 122290/445312 files (0.1% non-contiguous), 1018640/1808384 blocks resize2fs 1.47.0 (5-Feb-2023) pishrink.sh: Shrinking filesystem resize2fs 1.47.0 (5-Feb-2023) Resizing the filesystem on /dev/loop0 to 1463731 (4k) blocks. Begin pass 2 (max = 8046) Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Begin pass 3 (max = 56) Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The filesystem on /dev/loop0 is now 1463731 (4k) blocks long. pishrink.sh: Zeroing any free space leftpishrink.sh: Zeroed 1.8G pishrink.sh: Shrinking partition pishrink.sh: Truncating image pishrink.sh: Using gzip on the shrunk image pi-clone.img: 78.9% -- replaced with pi-clone.img.gz pishrink.sh: Shrunk pi-clone.img.gz from 7.5G to 1.3G
-v
tells PiShrink to be verbose-z
tells PiShrink to gzip the imagepi-clone.img
is the source image filepi-clone.img.gz
is the destination image file that is zipped
That's it! You can now copy the pi-clone.img.gz
file to your hard drive, upload
it to a cloud storage service, or share it with others. It can be used to create a new SD card with Raspberry Pi Imager.
Tutorial prepared with ♥ by James Eberhardt.
You can reelyActive's open source efforts directly by contributing code & docs, collectively by sharing across your network, and commercially through our packages.Continue exploring our open architecture and all its applications.