October 24, 2015 admin

SD and micro-SD management with linux dd

If you play with IoT, sooner or later, you’ll need to flash, backup and restore SD or micro-SD.

Linux offers the dd command to deal with binary dumps of devices:
https://en.wikipedia.org/wiki/Dd_(Unix)
http://manpages.ubuntu.com/manpages/lucid/man1/dd.1.html

The full backup process results in:

  • insert the SD (of course …)
  • find the mount point with dmesg, let’s say it is /dev/sdX
  • unmount the SD mount points:
    umount /dev/sdX1
    umount /dev/sdX2
    umount /dev/sdX...
  • dump the SD content to file:
    sudo dd bs=4M if=/dev/sdX of=[dump file name].img
    pay attention to of parameter: if you wrongly set it to some /dev/sd[your hard drive] you’ll wipe the content of your disk!
    bs is the size of blocks to be read and written one at a time, in bytes

The full flash (or restore) process goes like:

  • insert the SD (of course …)
  • find the mount point with dmesg, let’s say it is /dev/sdX
  • unmount the SD mount points:
    umount /dev/sdX1
    umount /dev/sdX2
    umount /dev/sdX...
  • dump the SD content to file:
    sudo dd bs=4M if=[image file name].img of=/dev/sdX
    pay attention to of parameter: if you wrongly set it to some /dev/sd[your hard drive] you’ll wipe the content of your disk!
    bs is the size of blocks to be read and written one at a time, in bytes

Now, consider the following case:

  • SD size = 16GB
  • image size = 8GB
  • you can flash the 8GB image onto the 16GB SD, of course all the previous data will be wiped out (not the ones in the second half of SD, but chances are that you won’t be able to access them anyway)
  • When you backup the 16GB SD with the previous process … your backup image is 16GB, as the SD physical size, not as the image you flashed on it.
  • if you want the image back to 8GB (maybe you want to flash it onto another 8GB SD) you can use the count parameter of dd command:
    count is expressed in number of blocks as they are defined in the bs parameter
    so you would say, if bs=4M, count = 8*1024*1024*1024/(4*1024*1024) = 2048 … NO!!
    because 8GB is not 8*1024*1024*1024, in fact 8G (without B) == 8*1024*1024*1024,
    while 8GB == 7948206080 bytes
    and so the correct count setting is count = 7948206080/(4*1024*1024) = 1895

Hope you can find it helpful!