Skip to content

Instantly share code, notes, and snippets.

@chatchavan
Last active April 18, 2024 07:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chatchavan/4696bab6083a2fde3b4f to your computer and use it in GitHub Desktop.
Save chatchavan/4696bab6083a2fde3b4f to your computer and use it in GitHub Desktop.
Create a disk image from an SD card and write the disk image to another SD card (Mac OS X)
#!/bin/bash
### NOTE
# Ideally, you should create an image from small partition (e.g., 4 GB) instead of the whole SD card (e.g., 32 GB).
# For example, an image for Raspbian image should be created by the following procdure:
# (1) Install the official Raspbian image (3.5 GB for Jessie) on an SD card
# (2) Manually expand the partition to a modest size to accommodate your base software (e.g., 4 GB)
# (3) Perform apt-get update and upgrade, install software and configuration that you want.
# (4) Create an image from that (4 GB) partition
#
# The instruction for resizing partion and creating disk image with a specific size can be found in this link:
# http://elinux.org/RPi_Resize_Flash_Partitions
## Read SD card to disk image
# STEP 1: insert the source SD card
diskutil list
# STEP 2: From the output, find the device number corresponding to your SD card
# E.g., /dev/disk2
# In the code below, replace "diskX" below with your disk number
# STEP 3: copying (and compressing)
sudo dd if=/dev/rdiskX of=~/Desktop/Chatberrypi.dmg bs=1m
# OPTIONAL:
# You can experiment with different block sizes (The "bs" above; "1m"
# means 1 MB.) Run dd for a few seconds and press Ctrl + C to see
# the transfer rate. Adjust "bs" and test again.
#
# In my MacBook Air, I found 1m yield an acceptable performance
# STEP 3 (alternative): compress the image on-the-fly. (Note: Don't check the progress of dd with the command at the end of this page)
sudo dd if=/dev/rdiskX conv=sync,noerror bs=1m | gzip -c > ~/Desktop/Chatberrypi.img.gz
# STEP 4: remove the source SD card
## Write disk image to SD card
# STEP 5: insert the destination SD card
diskutil list
# STEP 6: replace "diskX" below with the device number of the SD card (see STEP 2)
# STEP 7: unmount the SD card
diskutil unmountDisk /dev/diskX
# STEP 8: format the SD card
sudo newfs_msdos -F 16 /dev/diskX
# STEP 9: write the disk image
sudo dd if=~/Desktop/Chatberrypi.dmg of=/dev/rdiskX bs=1m
# STEP 9 (alternative): decompress the image on-the-fly and write image. (Note: Don't check the progress of dd with the command at the end of this page)
sudo gunzip -c ~/Desktop/Chatberrypi.img.gz | sudo dd of=/dev/rdiskX conv=sync,noerror bs=1m
# OPTIONAL:
# To check progress of dd, open a separate terminal window and run
# the following command to ask dd to print progress (in its terminal)
# every 20 seconds.
while sudo killall -INFO dd; do sleep 20; done
# Sources:
# SD card back up: https://smittytone.wordpress.com/2013/09/06/back-up-a-raspberry-pi-sd-card-using-a-mac/
# Status checking: http://www.commandlinefu.com/commands/view/5440/check-the-status-of-dd-in-progress-os-x
@motaviegas
Copy link

Hi chatchavan,

I like your guide. Just tried to recover an image after having made the backup using the compress option, however, trying to recover that same image the disk becomes unrecognizable by the system.
The card is 256Gb could that be the problem? If yes, how to resolve it since now I have no way to go back to the previous form.
Thank you,
Paulo

@chatchavan
Copy link
Author

In theory, the compressed file should be OK.

Perhaps the size of the destination SD card is a problem. Specifically in Step 8, the command newfs_msdos formats the SD card in the FAT file system—which supports up to 4 GB. I'd suggest trying newfs_exfat.

@motaviegas
Copy link

Indeed, the compressed file was ok, I then used Etcher to flash the image without issues.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment