Thursday, 25 September 2014

Cloning your Raspberry Pi SD card using dd and linux

Why clone?

Having a clone of your raspberry Pi SD card is very useful and alows you to really experiemnt with your raspberry Pi with the full knowledge that you can go back to a known good state.

What do i end up with?

What you end up with is an exact copy of your raspberry Pi SD card in a file, which you can then copy and keep for a rainy day, give to your friends, or make an exact copy of your system for use on a second raspberry Pi.

What you will need
1) your raspberry Pi SD card.
2) a PC or laptop running linux with an SD card reader (this procedure cannot be done on the raspberry Pi itself)

You will need to be the root user to make the image, so su to root first.

$ su
or on ubuntu based systems
$ sudo -i

Next plug your SD card into your PC and look for the device name that was assigned to the card. It will look something like /dev/sdX or /dev/mmcblkX This is important as we will be using this identifier for making the image.

One way of getting this information is by looking at the messages log file.

# tail -f /var/log/messages

when you plug your card in or take it out, the device name is logged in this file and you can see the messages skipping by.

Sep 24 15:48:00 p1155-awdeb kernel: [111145.426105] sd 6:0:0:0:
     [sdb] 7710720 512-byte logical blocks: (3.94 GB/3.67 GiB)
Sep 24 15:48:00 p1155-awdeb kernel: [111145.433046]  
     sdb: sdb1 sdb2 < sdb5 sdb6 >
Sep 24 15:48:01 p1155-awdeb kernel: [111145.818914] EXT4-fs 
     (sdb6): mounted filesystem with ordered data mode.
Sep 24 15:48:01 p1155-awdeb kernel: [111145.961317] 
     FAT-fs (sdb5): utf8 is not a recommended IO charset 
     for FAT filesystems, filesystem will be case sensitive!

In this case the sdcard was given a device name of /dev/sdb

Most desktops will automount any partitions it finds so next we will want to make sure that any partitions found on the SD card are not mounted before taking the image.

Using mount and lsblk commands we can see that /dev/sdb5 and /dev/sdb6 are mounted.

# mount
/dev/sdb6 on /media/0eb36e9e-40f5-47f4-a751-4e197c0dd7c8
/dev/sdb5 on /media/boot type vfat
# lsblk
sdb     8:16 1 3.7G  0 disk 
├─sdb1  8:17 1 1.2G  0 part 
├─sdb2  8:18 1   1K  0 part 
├─sdb5  8:21 1  56M  0 part /media/boot
└─sdb6  8:22 1 2.4G  0 part /media/0eb36e9e-40f5-47f4-a751-4e197c0dd7c8

Now unmount any mounted partitions for your SD card. Any other partitions can be left mounted, we are only concerned with partitions on the SD card.

# umount /dev/sdb6
# umount /dev/sdb5

(Tip: If you use the eject button in the GUI, your dev device names will disappear from your system, so then we can't use them to make our image. Using umount at the command line, like in the above example, keeps them there for us to use with the dd command later on)

Run the lsblk and mount commands again and check that they are now unmounted.

Now lets make the Image. We will be using dd to make an exact bit copy of the SD card. replace /dev/sdX with the name given to your SD card.

# dd if=/dev/sdX of=/root/myimagefile.dd bs=64K conv=sync,noerror

so in the above example where the sdcard is identified as /dev/sdb the command would be.

# dd if=/dev/sdb of=/root/myimagefile.dd bs=64K conv=sync,noerror

This will take a while, but wait for it and you will end up with an image file which is an exact copy of your SD card.