Fast copy / restore from image to MMC Raspberry Pi

Friends of mine were complaining about the restoration time of backuped images using dd. I felt like sharing these few steps showing how to quickly restore an image to MMC card. I hope it will be useful for someone !
Purpose:
  • Mount raw partitions from backuped image, made using dd.
Advantages:
  • Fast copy/restore from image to MMC.
  • Modify the partitions content without even mounting the physical device.
  • And last but not least, copy large images on smaller medias.

Note that all commands below are executed as root, so please be careful

A) List partitions from image

# fdisk -lu image_name.dd

Result example :

Disk image_name.dd: 7969 MB, 7969177600 bytes
255 heads, 63 sectors/track, 968 cylinders, total 15564800 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
Disk identifier: 0x000c4661
Device Boot               Start         End      Blocks   Id  System
image_name.ddp1            8192      122879       57344    c  W95 FAT32 (LBA)
image_name.ddp2          122880     5785599     2831360   83  Linux
  • What’s important here : The Start column of each partition shown in sectors, notice the sector size is 512 bytes.
  • The start offset of the boot partition is 512*8192 = 4194304

B) Create mount point folders

# mkdir /media/fakeBoot
# mkdir /media/fakeRoot

C) Mount partitions from backuped image to mount points

boot partition :

# mount -o offset=4194304 image_name.dd /media/fakeBoot

rootfs partition :

# mount -o offset=62914560 image_name.dd /media/fakeRoot
  • As said before, the offset is calculated this way : Start_sector*512 bytes, so you could have asked your shell to compute it :

boot partition :

# mount -o offset=$((8192*512)) image_name.dd /media/fakeBoot

rootfs partition :

# mount -o offset=$((122880*512))  image_name.dd /media/fakeRoot

 D) Check which FS type is used

# mount

Result example :

/home/user/RaspberryPi/backup/image_name.dd on /media/fakeBoot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=utf8,shortname=mixed,errors=remount-ro)
/home/user/RaspberryPi/backup/image_name.dd on /media/fakeRoot type ext4 (rw,relatime,data=ordered)

You could also use “df -T” or any utility that shows you the FS type.

E) Prepare your MMC partitions

You may use the utility of your choice, be it fdisk, gparted, to partition your MMC this way in order to receive the Raspberry pi images :

  • boot partition :
    • 60Mo +
    • Label : boot
    • Fat32
    • Flags : boot, lba
  • rootfs partition :
    • 2.7Go + (you’re free to set the size, this is the default Raspbian)
    • Label : rootfs
    • Ext4

Then unplug/replug and mount the two partitions of your media.

 F) Copy the content of your backuped image to your MMC

You may use the utility of your choice, cp, rsync etc, here is an example using cp.  Since the boot partition uses FAT32, you don’t need to preserve links, rights, permissions etc, the -R (recursive option may be useseless if your boot partition doesn’t have sub folders) :

cp /media/fakeBoot/* /media/boot/ -R

For your rootfs partition :

cp /media/fakeRoot/* /media/rootfs/ -Ra

Note that you may use the -v option to make cp verbose

Once the copy ends, flush the system buffers, unmount the MMC, the image then delete folders.

# sync
# umount /media/boot && umount /media/rootfs
# umount /media/fake*
# rmdir /media/fake*

 

Et voilà ! This way, you should be able to create SD card within less than 10 minutes.

* Complementary informations : if you want to look further with dd command, the pv (Pipe Viewer) command allows you to show dd progression

# dd if=/dev/sdb | pv -brt | dd of=./file

 

 

Raspberry Pi

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>