This page (revision-24) was last changed on 2021-04-21 06:16 by Murray Altheim

This page was created on 2021-03-27 21:00 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
24 2021-04-21 06:16 12 KB Murray Altheim to previous
23 2021-04-21 06:14 12 KB Murray Altheim to previous | to last
22 2021-04-21 06:13 12 KB Murray Altheim to previous | to last
21 2021-04-17 03:16 11 KB Murray Altheim to previous | to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 2 changed one line
This page describes using the "dd" command on Linux to create an exact copy of an SD card, storing it in an "img" file, then restoring from an "img" file.
This page describes using the "dd" command on Linux to create an exact copy of an SD card, storing it in a disk image ("img") file, then restoring from a disk image file. It also describes using the __BalenaEtcher__ program to clone SD cards, and __pishrink__ to shrink the size of disk image files.
If you're using Linux, a more complete but complex method of creating an image of an SD card is described below, namely using the "shrink" application, which both copies the SD card and shrinks its stored size smaller than the full size of the SD card.
At line 13 changed one line
If you're using a Linux or Mac OS computer this page applies. If you're using Windows you can do this using the ''Windows Subsystem for Linux'' (WSL), an Ubuntu-as-an-application that can be installed — with some effort — on Windows 10. If you want to use Windows, there are many web pages describing how to use Windows applications to copy cards. We don't use Windows around here.
This page describes how to clone an SD card to an "img" file. If you want to copy (clone) an SD card directly to another SD card (i.e., no backup file) the [BalenaEtcher|https://www.balena.io/etcher/] has a Clone feature that does just that. The upside to this is that cloning card-to-card is easier, faster, and more fool-proof. The downside is that you'll need the ability to mount two SD cards to your desktop computer, and you won't end up with a backup file. Backup files are good!
At line 16 changed one line
!!! How to Clone an SD Card
This page is applicable if your desktop computer uses Linux or Mac OS. If you're using Windows you can do this using the ''Windows Subsystem for Linux'' (WSL), an Ubuntu-as-an-application that can be installed — with some effort — on Windows 10, or using a specialised Windows application. If you want to use Windows, there are many web pages describing how to use Windows applications to copy cards. We don't use Windows around here.
At line 18 removed 3 lines
%%information
This describes how to clone an SD card to an "img" file.
%%
At line 21 added 2 lines
!!! How to Clone an SD Card
At line 68 changed one line
Once you've unmounted the partitions, you can then use the "{{dd}}" command to make the copy. I'll be storing it on my Desktop ("/home/altheim/Desktop") directory as a file named "rpi-backup-20210328.img". I generally add a timestamp to my backups so I can figure out when I made them.
Once you've unmounted the partitions, you can then use the "{{dd}}" command to make the copy. I'll be storing it on my Desktop ("/home/altheim/Desktop") directory as a file named "rpi-backup-20210328.img". I generally add a timestamp to my backups so I can figure out when I made them. If you're using both 32- and 64-bit OS, that's a good thing to add. If the backup is of a particular state of the OS (e.g., before or after a big change) I might include that too.
At line 73 changed one line
Note that you __must__ alter the "if" (input file) argument to match the drive of the SD card you found using fdisk, otherwise you may damage an existing drive. If won't necessarily be the same as what I found, and an SD card won't even always mount at the same location. ''Check it every time''.
Note that you __must__ alter the "if" (input file) argument to match the drive of the SD card you found using fdisk, otherwise you may damage an existing drive — particularly when performing a restore (as below). It won't necessarily be the same drive designation as what I found, and an SD card won't even always mount at the same location. ''Check it every time''.
At line 89 added 26 lines
!! Shrinking the Image
%%information
If you're using Ubuntu Linux as your normal desktop I'd recommend using the __shrink tool__ found at: [https://github.com/qrti/shrink]
which works very well. Instructions for installation can be found at its home page.
__shrink__ automatically opens the GParted Partition Editor, instructs you what to do, and effectively manages the creation and shrinking of the produced copy of your SD card as an *.image file.
%%
There's a bit of a problem in that the Raspberry Pi OS is automatically expanded to the size of the
SD card the first time it's used, meaning that the image is always the same size as the SD card. This means
that sometimes when you try to write the image to an SD card it is just ''slightly'' too big and you
end up with an "out of space" error.
The way to fix this is to run a free bash script called "__pishrink__" to shrink the image. This only
runs on Linux or Mac OS (since it has a BSD Unix command line).
You can find a copy of PiShrink from its github page at: [https://github.com/Drewsif/PiShrink]
Note that you'll have to make the program executable after downloading it via:
{{{
chmod 744 pishrink.sh
}}}
It has command line help and even has an option for using multiple cores of your computer to speed things
up, if you've installed the "pigz" library (using {{sudo apt install pigz}}).
At line 91 changed one line
This describes how to restore an SD card from an "img" file.
This describes how to restore an SD card from an "img" file. You can use the Linux "dd" command as described below, or one of the existing UI applications like the [BalenaEtcher|https://www.balena.io/etcher/] or the [Raspberry Pi Imager|https://www.raspberrypi.org/blog/raspberry-pi-imager-imaging-utility/]. The UI applications are friendlier, handle the SD card formatting, and provide error messages and validation, so are __recommended__. The "dd" method is included here for completeness.
At line 103 changed one line
From a Linux desktop you can use the __Disks__ utility to reformat the entire drive (not simply its partitions), first unmounting and then deleting all partitions, then using "Format Volume" with an Ext4 format. Alternately, you can use gparted (the Gnome Partition Editor) (installed via: {{sudo apt install gparted}}). It's a bit beyond the scope here to describe how to reformat the drive using these GUI tools but we'll cover the command line approach because ''it's actually easier''.
From a Linux desktop you can use the __Disks__ utility to reformat the entire drive (not simply its partitions), first unmounting and then deleting all partitions, then using "Format Volume" with an Ext4 format. Alternately, you can use __gparted__ (the Gnome Partition Editor), installed via: {{sudo apt install gparted}}. It's a bit beyond the scope here to describe how to reformat the drive using these GUI tools but we'll cover the command line approach because ''it's actually easier'' if you know what you're doing. If not, there's a slight learning curve but I'd probably recommend installing and using gparted. It's a good skill to have anyway.
At line 132 added one line
At line 163 changed one line
The awake among you will note that this reverses the {{if}} and {{of}} argument values from when we made the backup, since we're just reversing the process.
The awake among you will note that this reverses the {{if}} and {{of}} argument values from when we made the backup, since we're just reversing the process.
At line 193 added one line
And yes, this will take a ''long'' time so take your cat for another walk.
At line 195 added one line
As a point of reference, this process is writing to rather than reading from the SD card, so it's a bit slower at ~16.0MB/second. To restore the Raspberry Pi OS image back to my 16GB SD card took over 1000 seconds (16 minutes).
At line 197 added one line