Skip to content

Instantly share code, notes, and snippets.

@matinfo
Forked from chatchavan/SDImage.sh
Created April 13, 2024 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matinfo/dfbb75c8627c85961d6859fa04c738bc to your computer and use it in GitHub Desktop.
Save matinfo/dfbb75c8627c85961d6859fa04c738bc 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment