Making and Sharing Raspberry Pi SD Card images

From Modding Fridays
Jump to: navigation, search

Making and Sharing SD Card images

This is done off the RPi, so turn it off and take the sdcard out, then back into an adapter into any UNIX-like OS running machine that you can find:

Backup an image: Option 1 - I'm soooo lazy

In this method we make a full dump of sdcard, which can be a storage waste if you only have partitioned, say 2GB of the sdcard, as you will then make an image of the full capacity of the sdcard. For instance if you're RPi image has only 3GB of partitions, but your sdcard potential full size if 16GB, using this method will create a 16GB file. BUT because we compress the image on the fly with xz, then it will in fact be not more as the partitions size, because the empty space will compress to virtually nothing. However if you need to uncompress the full image at some point, for whatever reason, you will need a diskspace of 16GB.

  • figure out which block device represents your sdcard (hint: run dmesg after plugging the sdcard). In this example we assume it is /dev/sdc, make sure you use the correct block device.
  • copy the raw content of the sdcard and compress it on the fly into the resulting image my-raspberry-pi.img.xz:
dd bs=4M if=/dev/sdc status=progress | xz > my-raspberry-pi.img.xz

Note: xz compression is very powerful and will create very tiny images, but compression time can be quite slow, if you're in a hurry or slow computer, and do not mind 50% efficiency loss, use gzip instead of xz in the command above. Alternatively if your processor has multiple core, using multithreading with xz can make it on run almost as fast as a direct raw copy. Use the flag -T to set the number of threads, for instance -T 8 will make it use 8 threads.

Backup an image: Option 2 - I'm soooo anal

If you want to distribute your image, it might be more elegant and proper to make an image that is not bigger than what is really being used on the sdcard itself. There are two main reasons for that:

  1. the method above is hardware specific meaning that no two sdcard capacity are exactly the same. For instance one 8GB sdcard might be a bit bigger than another. If your backup'ed sdcard is smaller than your target sdcard, that's fine, but if it is the other way around, when your image to the target, dd will complain that it ran out of space. If you've only used small partitions and grew them only as you needed, carefully managing the sdcard space, this will not be an issue as the truncated part will most likely be unpartitioned space where there is nothing of value. However if you've created an extra partition growing the partition of your OS to the max capacity of the sdcard and started to use it, well, there are quite some chance, that cutting into this space will not only result in a damaged partition (recoverable) and data loss (unrecoverable).
  2. Another, more practical reason, is that if you need to share or manipulate the image of your RPi OS uncompressed it will be obviously more practical to work with a smaller file than with a larger one.
  • figure out which block device represents your sdcard (hint: run dmesg after plugging the sdcard). In this example we assume it is /dev/sdc, make sure you use the correct block device.
  • copy the raw content of the sdcard into the resulting image my-raspberry-pi.img:
dd bs=4M if=/dev/sdc status=progress of=my-raspberry-pi.img
  • figure out how much space is used by partitions on the image:
fdisk -lu my-raspberry-pi.img
  • Example of the command output, your result will be different.
Disk my-raspberry-pi.img: 7.5 GiB, 8053063680 bytes, 15728640 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x95f9e2e9

Device                Boot   Start     End Sectors Size Id Type
my-raspberry-pi.img1            16  124927  124912  61M  b W95 FAT32
my-raspberry-pi.img2        129024 4198399 4069376   2G 83 Linux
  • Note the end sector of the last partition, here the last partition is my-raspberry-pi.img2 and the last sector is 4198399.
  • Add 1 sector to this value (to be on the safe size we will trim a bit less than needed), and mutiply by 512 to obtain the size in bytes: (4198399 + 1) * 512 = 2149580800.
  • truncate the image at this value, adjust to your own value:
truncate my-raspberry-pi.img --size 2149580800
  • Finally compress the image to make the sharing/storing less demanding (read note about xz compression in previous method):
xz my-raspberry-pi.img

Restore image

Regardless which method you used, you can restore or copy an image like this. In the commands below the target block device is /dev/sdc, make sure you adjust to the correct one on your system so that you do not overwrite another disk!

  • if the image is not compressed:
dd bs=4M if=my-raspberry-pi.img of=/dev/sdc status=progress
  • if the image is compressed (xz):
xzcat tgc.img.xz | dd bs=4M of=/dev/sdc status=progress
  • if the image is compressed (gzip):
zcat tgc.img.xz | dd bs=4M of=/dev/sdc status=progress