Clone RPi

From XPUB & Lens-Based wiki

Cloning a Raspberry Pi disk onto a image file can be quite useful, as it will spare you from installing the whole system, applications, and configuration files that you set up for a specific project, using the Raspberry Pi.

By cloning the Raspberry file-system, contained in the SD-card to and image file, you'll have a copy of an disk in file, ready to be cloned onto another SD-card and ran.

the following steps will show how do this.

Resizing the partitions

Shutdown the Pi and remove its SD-card.

Insert the SD-card to you computer and run gparted software, as sudo.

Select the SD-card (/dev/mmcblk/)

raspberry_pi_device-menu-2.png

Select the larger partition (the one with loads of empty space) and reduce the empty space, leaving a bit of empty space (around 2000 MB) for temporary files,caches,log files,etc.

raspberry_pi_root-resize.png

raspberry_pi_root-resize-2.png

When done close the Gparted

Clone

from SD-card to image file

sudo fdisk -l /dev/mmcblk0

Will give the length of each partition, in my case it is:

Disk /dev/mmcblk0: 3.8 GiB, 4025483264 bytes, 7862272 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: 0x0004a452

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk0p1         16   97727   97712 47.7M  b W95 FAT32
/dev/mmcblk0p2      97728 3766271 3668544  1.8G 83 Linux

What to look for:

  • Start and End units are sectors,
  • sector size is 512 bytes

To clone use the dd cloning tool.

  • The sector (or block size) bs will be the value of the sector size
  • count will be given the End of the last device (/dev/mmcblk0p2) + 1: 3766271+1 .
  • bs=512 is the sector size
  • if=/dev/mmcblk0 from /dev/mmcblk0
  • to image file (rpi.img) of=rpi.img
sudo dd if=/dev/mmcblk0 of=rpi.img bs=512 count=3766272

When done eject the SD-card and you'll have your Raspberry Pi image clone to a file

Clone: from image (file) to card

Insert the new card, to where you'll clone the image

sudo dd bs=4M if=rpi.img of=/dev/mmcblk0

expand file-system

using gparted you can enlarge the partition we shrunk while making the img in #Resizing the partitions.

Test

Once done,

  • eject the new card
  • insert it in the Pi
  • connect the network port
  • power the Pi

If all is good you will see crazy blinking leds. Wait for the them to stop and look for the Pi's IP address with

nmap -sP 192.168.1.1-255


Once you have found the Pis IP address ssh into it.

ssh root@pi.ip.address

And expand the file-system with raspi-config, since in the image we reduced the empty space of the partition.

raspi-config

Expand and reboot.

Cloned image is there.