Linux – Cloning Your Hard Disc on RaspberryPi and Raspbian

For a credit card sized computer, it’s been getting a lot of abuse.

I’ve been installing and uninstalling software since I got the thing from my buddy Craig in Atlanta, and not always having success.

Instead of completely reloading the operating system from the “official” image, I decided to do a backup of the computer.

Boring stuff that anyone who uses a computer on any operating system should do on a schedule.

While Windows can be made automatic, it’s fussy.  You don’t have the control over the operating system like you do in Linux, any Linux.  Anything I am doing on this little credit card sized computer, I can do on my bigger Thinkpad T60 laptop that is running a very similar version of Linux.  I would expect these same steps to work on my Debian computers, as well as any derived distribution like Ubuntu.   Just check your “switches” to make sure they “comply”.

That’s the strength and the weakness of Linux.  There’s so much flexibility it’s confusing, but in the flexibility you can get the operating system to do what you want and exactly what you want.

But you have to know what you’re doing.

Since the RaspberryPi only had an 8GB SDHC chip for it’s “disc drive”, it would be small enough and easy enough to backup.  Since it is Linux, why not just make a complete copy of the operating system and all the user data?

Clone the hard drive.

What I did was to cheat.  I did it from the desktop of the RaspberryPi.
Why is that a cheat?  Because files “could be left open” which means you are never 100 Percent Sure that everything gets copied.

The solution with the Raspi is to go into “raspi-config” and set the switch  in “Enable Boot to Desktop/Scratch” to go to “Console Text Console”.  That will put you in what we used to call “Single User Mode” and everything will be closed and there will be no doubt.

But, Me?  I’m cheating and it turned out fine.  No corruption and I was able to switch chips (hard drives) and it booted from the cloned chip with not a problem in the world.

On the other hand, I will be using these instructions to do exactly this on my “real” Linux laptop, an older Lenovo Thinkpad T60 machine.  I have the spare hard drives there too and why not?

Process:

  • Prepare a chip as a hard drive that is the same size or larger than your original drive.  That will need to be formatted FAT32 which can be done on any operating system that supports it.
  • Open a root terminal.
  • Close all other tasks that are running that aren’t essential.

Task 1:

  • In Terminal, run lsblk at a root prompt.  
  • This will tell you exactly what hard drives and media are connected to the computer on the /dev tree.
  • The picture shows the results of both commands.
  • Under the Name Column, the devices are shown as a tree.
  • What you need is not the name of the partition labeled as “part” but the actual root device called “disk”.
  • The internal media is the all important boot drive.
  • For mine, the internal media is on mmcblk0 – which is actually /dev/mmcblk0 .
  • For mine, the external media is on sda – which is actually /dev/sda .

Task 2:

  • Perform a “dd” statement from the internal to the external drive.
  • The statement is for my set up:  dd if=/dev/mmcblk0 of=/dev/sda bs=4096 conv=noerror,notrunc,sync
  • What that statement says is:
    • dd – Disc Dump or Copy
    • if=/dev/mmcblk0 – input file is /dev/mmcblk0 .  Since that is the root device, it will copy everything from that chip onto the output device.
    • of=/dev/sda – output file is /dev/sda .  Since that is the root device, it will copy everything from the input chip onto your output device, deleting anything that was on the chip.
    • bs=4096 – block your output data in 4K blocks for efficiency.
    • conv=noerror,notrunc,sync –  Convert the data but do not truncate any data (notrunc), do not stop on errors (noerror), and synchronize (sync) the file sizes by padding them with nulls in case there is an error on a file.

When that is all done, the dd statement will tell you how many records were copied in and out, and the record counts should be the same.  It will also tell you how large your chip was – here it was 7.9 GB.

Close enough.

If you want to test your clone, shut down your computer via a “shutdown -h now” in your root terminal, swap chips, then reboot.

It should “just work”.  It did for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.