Monday, 5 January 2015

Writing a Raspberry Pi dd image file to an SD card

This is essentially a follow up to the post Cloning your Raspberry Pi SD card using dd and linux, where we made a clone of a Raspberry Pi SD card and saved it to a file.

In this post we will be taking this image file and writing it back out to an SD card, which we can then use to boot a Raspberry Pi.

What you will need
1) A file containing an image of the SD card (you should have made this earlier)
2) A Raspberry Pi SD card. (Warning: This card will be completely overwritten by this process)
3) A PC or laptop running linux with an SD card reader, this procedure cannot be done on the Paspberry Pi.

On the PC/laptop 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 writing the image to the SD card.

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:
     [sdX] 7710720 512-byte logical blocks: (3.94 GB/3.67 GiB)
Sep 24 15:48:00 p1155-awdeb kernel: [111145.433046]  
     sdX: sdX1 sdX2 < sdX5 sdX6 >
Sep 24 15:48:01 p1155-awdeb kernel: [111145.818914] EXT4-fs 
     (sdX6): mounted filesystem with ordered data mode.
Sep 24 15:48:01 p1155-awdeb kernel: [111145.961317] 
     FAT-fs (sdX5): utf8 is not a recommended IO charset 
     for FAT filesystems, filesystem will be case sensitive!

Note: I have changed the real SD card dev name to /dev/sdX in this sample output. Just make a note of the /dev name that the SD card was given on your system.

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.

Using mount and lsblk commands we can see that /dev/sdX5 and /dev/sdX6 are mounted.

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

Now unmount any mounted partitions for your SD card. We are only concerned with partitions on the SD card.

# umount /dev/sdX6
# umount /dev/sdX5

(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 write 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 to be sure that the partitions are now unmounted.

Now we are ready to write the dd image file to the SD card. We will be using dd to write the image back to the SD card. In the line below, replace /dev/sdX with the name given to your SD card.

Data Loss Warning: Make sure you have correctly identified the device name of the SD card before running the command below, if you get this wrong and use the wrong /dev device name, you could overwrite one of your PC's hard disks. Any data loss is entirely your fault

# dd if=/root/myimagefile.dd of=/dev/sdX bs=64K

This will take a while, when it is finished remove the SD card from your PC. The SD card is now ready to be plugged into a Raspberry Pi and should boot up as normal

A final Note: If you have an image of a smaller SD card and want to put it onto a larger card, e.g. you made an image of a 4GB SD card and want to write it to an 8GB or 16GB card, this is fine. Copy the image to the SD card as described above, but as an extra last step expand the SD card image so that you can access the full size of the larger SD card. In the above example, the extended partition /dev/sdX2 and then the logical partition /dev/sdX6 would need to be expanded. Gparted is a nice tool to do this, so

# apt-get install gparted