I wanted to be able to clone a Raspberry Pi’s SD card to having some state that I could go back to if I had messed up to much in this Raspberry Pi’s OS.
As I’m into learning as much command line stuff as possible this is an command line only approach using a Mac (MacOS 10.14.4).
Clone An SD Card On Command Line
Connect the SD card to the Mac, open Terminal and enter the following command to locate it:
diskutil list
This will give you a list something like this:
ComputerName: User$ diskutil list /dev/disk0 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme *500.3 GB disk0 1: EFI EFI 209.7 MB disk0s1 2: Apple_APFS Container disk1 500.1 GB disk0s2 /dev/disk1 (synthesized): #: TYPE NAME SIZE IDENTIFIER 0: APFS Container Scheme - +500.1 GB disk1 Physical Store disk0s2 1: APFS Volume Macintosh HD 458.4 GB disk1s1 2: APFS Volume Preboot 43.0 MB disk1s2 3: APFS Volume Recovery 522.7 MB disk1s3 4: APFS Volume VM 1.6 GB disk1s4 /dev/disk2 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme *63.9 GB disk2 1: Windows_FAT_32 boot 43.7 MB disk2s1 2: Linux 63.8 GB disk2s2
So here the SD card would be /dev/disk2. I can tell that not only by the size of it.
Create a disk image by the command on your Desktop (or elsewhere If you feel like):
sudo dd if=/dev/disk2 of=~/Desktop/raspberrypi.dmg
Enter your Admin Password to continue.
Go and have coffee, several cups… or pots…
This may take quite some time depending on your SD card’s size and the data already existing on it (up to more than an hour).
No progress is shown in Terminal. Yet once completed you’ll be told so.*
Restore A Disk Image To An SD Card On Command Line
Connect an SD Card (can be blank) to the Mac.
Locate it by the diskutil command as before:
diskutil list
Again this might look something like this:
ComputerName: User$ diskutil list /dev/disk0 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme *500.3 GB disk0 1: EFI EFI 209.7 MB disk0s1 2: Apple_APFS Container disk1 500.1 GB disk0s2 /dev/disk1 (synthesized): #: TYPE NAME SIZE IDENTIFIER 0: APFS Container Scheme - +500.1 GB disk1 Physical Store disk0s2 1: APFS Volume Macintosh HD 458.4 GB disk1s1 2: APFS Volume Preboot 43.0 MB disk1s2 3: APFS Volume Recovery 522.7 MB disk1s3 4: APFS Volume VM 1.6 GB disk1s4 /dev/disk2 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: *61.9 GB disk2
The SD card is /dev/disk2.
Unmount the SD card by entering:
diskutil unmountDisk /dev/disk2
Format the SD card to a FAT16 or FAT32 file system like this:
sudo diskutil eraseDisk FAT32 SDCARD MBRFormat /dev/disk2
Find more detailed infos on formatting from command line here: Learning to Format a SD Card as FAT32 Using Mac Command Line
If, like in my case the disk image is stored on an external hdd, find out the mounting point of an external HDD‘s partition use diskutil list command again. Check the IDENTIFIER of that partition (disk2s3 e.g.) and use another diskutil command as:
diskutil info /dev/disk2s3
This will give you a lot of info about that device like the Mount Point’s path we are looking for:
Part of Whole: disk2 Volume Name: VOLUMENAME Mounted: Yes Mount Point: /Volumes/VOLUMENAME Partition Type: Apple_HFS File System Personality: Journaled HFS+ Type (Bundle): hfs Name (User Visible): Mac OS Extended (Journaled)
Restore from a disk image that you made earlier and that (in my case is located on an external HDD) by:
sudo dd if=/Volumes/VOLUMENAME/pathtodiskimage/diskimage.dmg of=/dev/rdisk2
Using rdisk2 instead of disk2 speeds things up tremendously (in my case something like 15 times faster).
Note: Do not use the “r” for cards where you want to keep several partitions. All data on that physical drive will be erased! It’s not working for partitions!
Enter your Admin Password to continue.
If cloning took long – this will take even longer. Time to go to bed and come back the next day…
- Progress monitoring of dd could be achieved though using one of the methods shown here…
Ressources I’ve been using to learn this:
https://superuser.com/questions/429058/how-can-i-get-the-mount-path-of-a-usb-device-on-osx
https://www.raspberrypi.org/documentation/installation/installing-images/mac.md
https://serenity.ee/post/82120938429/mac-os-dd-with-devdisk-vs-devrdisk
https://apple.stackexchange.com/questions/234167/how-can-i-track-progress-of-dd
https://en.wikipedia.org/wiki/Dd_(Unix) – Note that not all functions of the program dd are available on MacOS!
https://askubuntu.com/questions/215505/how-do-you-monitor-the-progress-of-dd