Unix Geekery: Cloning a Dual-Boot Hard Drive With dd

Posted: 2011-05-21 in Unix Geekery
Tags: , , , ,

I recently bought a new hybrid SSD drive for my MacBook Pro 5,1.  I’m dual booting Mac OS X and Ubuntu 11.04 64-bit.  I’ve made a lot of changes to my Ubuntu system and I really did not want to go through the hassle of reinstalling Ubuntu and redoing the changes.  I needed a way to clone the entire hard drive to include the partition schemes, operating systems and files.

I used the Unix command “dd” to accomplish this task.  Using dd I cloned my old (internal) drive to the new (external) drive.  Both systems remain bootable and all files and permissions remain intact.  dd does not care what operating system(s) are on the drive or how many partitions you have.  It is a bare-metal duplication from one drive to the other.

I did a lot of research on the ‘net to learn how to do this.  I am writing this post to help others that may find themselves in the same situation.  Additionally, this will capture everything I’ve learned so that I can refer back to it the next time I have to do this.

Like my other Unix posts, let me alert you to to the EXTREME geekiness of this post.  If Unix commands are not your thing, don’t waste your time reading the rest of this.

One of the reasons I enjoy working with Unix on Mac OS X & Ubuntu is that it provides a playground for me to learn how my computer works. I get immense joy when I string a Unix command together and it works. (My wife would be rolling her eyes right now.)

Reqired Items

  1. Bootable backup of your working system(s) and files. (The unofficial nick-name for dd is “Data Destroyer” because if you type the command wrong, you will nuke your entire system).

  2. Your new hard drive.  I formatted mine before this process although I don’t think this is required.

  3. An external enclosure for your new drive to reside during the operation.

Preparation

  1. Disconnect from the internet to prevent Dropbox, MobileMe or Mail from changing your system while going through this process.

  2. Backup your system; especially your important files. It’s too easy to destroy your system.

  3. Boot from your backup. All of this was done with OS X but could also be done from Linux.

  4. Attach the hard drive you want to clone your drive to.

  5. The drive will mount. Unmount it (do not eject) using disk utility; leave it connected via USB or FW.

Clone Your Hard Drive

  1. Open a terminal window and run the following command:

diskutil list

It will output the following:

$ diskutil list
/dev/disk0
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        500.1 GB   disk0
1:                        EFI                         209.7 MB   disk0s1
2:                  Apple_HFS JangoFett               469.6 GB   disk0s2
3:       Microsoft Basic Data                         9.3 GB     disk0s3
4:       Microsoft Basic Data                         20.0 GB    disk0s4
5:                 Linux Swap                         1.0 GB     disk0s5
/dev/disk1
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        
500.1 GB   disk1
1:                        EFI                         209.7 MB   disk1s1
2:                  Apple_HFS Backup                  499.8 GB   disk1s2
/dev/disk2
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        *500.1 GB   disk2
1:                        EFI                         209.7 MB   disk2s1
2:                  Apple_HFS BobaFett                499.8 GB   disk2s2

Backup (disk1) is the drive I have booted from.  JangoFett (disk0) is the SOURCE and BobaFett (disk2) is the TARGET.  EVERYTHING on the target drive will be erased during the operation.  Note that we want to clone the entire disk so we use disk0.  If we only wanted to clone the JangoFett partition to the BobaFett partition, disk0s2 would be the source and disk2s2 would be the target.

  1. Run the following command:

   sudo dd if=/dev/rdisk0 of=/dev/rdisk2 bs=128m conv=noerror,sync

  • sudo  = Super User Do; runs the command as root or the super user
  • dd   = Data Description; utility used to copy and convert data
  • if   = input file (the SOURCE or the drive you want to copy FROM) which is /dev/rdisk0
  • of   = output file (the TARGET or the drive you want to copy TO) which is /dev/rdisk2
  • bs   = the block size 128m; you can experiment with smaller or larger numbers depending upon what you are doing.
  • conv = tells dd not to worry about errors noerror and sync says write 0’s to the target if you can’t read the data.

Note: I use rdisk0 and rdisk2 instead of disk0 and disk2.  rdisk is the block device; disk is the character device.  The command works using both but seems to run faster when using rdisk.  I’m sure my understanding is probably off.  Again, I’m not an expert.  I know just enough to be dangerous.

When you press enter you will be asked for your password; enter it and hit return.

Like most Unix commands, there is no feedback, no progress bar, nothing.  The cursor just sits there and blinks at you (mocking you) while it follows your instruction.  Don’t fret. Let it run it’s course. When it completes you will see a completion message telling you how many blocks and files that were copied. This could take HOURS (it will be significantly less time than re-partitioning, re-installing and editing everything).

Getting the status of dd

As my wife would tell you, I’m not very patient.  I surely loathe not knowing what is going on.  Fortunately, this provided another Unix learning opportunity.  Computers are machines, they do EXACTLY what we tell them to do (mostly).  You just have to know how to ask.  The commands below assume that dd is currently running.

The Easy Way (works on a Mac):

Press “CTRL + T”

The Hard Way:

  1.  Open a new (2nd) Terminal window.

  2.  Enter the following command:

   ps -ac | grep dd

  • ps   = Process Status; displays all of the processes that have controlling terminals
  • -ac  = Two options to the ps command.  a tells ps to show your processes and other users’ processes; c tells ps to just list the executable name of the process (dd) instead of the entire command.
  • |    = “pipe” diverts output from the previous command to the next command
  • grep = Global Regular Expression Print; searches for a pattern from what is inputed to it
  • dd   = the pattern we are searching for; since we ran the command “dd” we want to search for that pattern.

The command will return something like the following:

$ ps -ac | grep dd
355 ttys000    1:39.76 dd

The first number is the PID or Process ID of dd.  We need the PID in order to ask dd its current status.  We get the status by running the following command:

   kill -INFO 355

  • kill  = is the command used to terminate or signal a process.  
  • -INFO = is an option to the kill command; it tells your computer “don’t terminate the process, just tell me what’s going on.”
  • 355   = the PID of dd; the process whose status we want to know 

The status of dd is written to the FIRST terminal window (where dd is running):

1266+0 records in
1265+0 records out
169785425920 bytes transferred in 4582.019038 secs (37054719 bytes/sec)

Run the command as many times as you want.  You’ll see the the numbers increasing with each successive instance of the command.

Explaining the Status Message

1266 is the number of records dd has read from the source (disk0); 1265 is the number of records dd has written to the target (disk2).  A record is a block.  The size of that block is 128m (128 Mega Bytes) because that is what we instructed dd to use when we ran it.

For those that don’t know, there are 1024 bytes in a kilobyte; 1024 kilobytes in a megabyte; and 1024 megabytes in a gigabyte.

Ergo: 1265 (records) x 128 (megabytes/record) x 1024 (kilobytes/megabyte) x 1024 (bytes/kilobyte) = 169,785,425,920 bytes (which is exactly the number of bytes that have been transferred).

It took dd 4,582 seconds to make that transfer which is about 1 hour and 16 minutes (4582 / 60).  Knowing that we have a 500GB hard drive we can estimate that it is going to take about 4 hours to clone the drive.

Completion

When dd completes the cloning process and exits, it will display its’ final status message.

dd: /dev/rdisk2: short write on character device
dd: /dev/rdisk2: Input/output error
3726+1 records in
3726+1 records out
500107862016 bytes transferred in 14584.393113 secs (34290619 bytes/sec)

The error message states that the last block was “short” meaning it wasn’t a full 128MB block.  The “+1” (3726+1) is the number of errors.  Only one error which was the short write at the end.  Nothing to worry about.

At this point the drives are EXACTLY the same; bit for bit.  To verify this run the following command:

    diskutil list

Returns the following output:

 

/dev/disk0
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        500.1 GB   disk0
1:                        EFI                         209.7 MB   disk0s1
2:                  Apple_HFS JangoFett               469.6 GB   disk0s2
3:       Microsoft Basic Data                         9.3 GB     disk0s3
4:       Microsoft Basic Data                         20.0 GB    disk0s4
5:                 Linux Swap                         1.0 GB     disk0s5
/dev/disk1
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        
500.1 GB   disk1
1:                        EFI                         209.7 MB   disk1s1
2:                  Apple_HFS Backup                  499.8 GB   disk1s2
/dev/disk2
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        *500.1 GB   disk2
1:                        EFI                         209.7 MB   disk2s1
2:                  Apple_HFS JangoFett               469.6 GB   disk2s2
3:       Microsoft Basic Data                         9.3 GB     disk2s3
4:       Microsoft Basic Data                         20.0 GB    disk2s4
5:                 Linux Swap                         1.0 GB     disk2s5

You now have a perfect clone of your hard drive!

Remove the clone from the external enclosure, pop it into your computer and voila!  All systems running perfectly with no data loss.

If you want to boot from the clone while it is attached via FireWire or USB there are other some other things that need to be done.  That will have to wait for another post.  I’m still figuring that out.

If this was helpful, or you spot an error, please leave a comment!

Advertisements
Comments
  1. Aaron Greenberg says:

    I know this is a bit of a zombie post, but excellent tutorial! I have a gmirror setup wherein one of the drives is giving up the ghost. Gmirror requires identical drive geometry, aka exactly the same make/model harddrive. Buying outdated hard drives is pretty pricey, so I decided to purchase two new drives and one spare for future calamity. This meant I needed to do something just like this or suck it up and do a manual transfer of scripts..etc.

    I actually just removed one of the drives and connected the new drive to the second sata cable. Transfer was quick and painless. I especially like the -INFO argument for kill. I never even knew that existed. Super useful for screen.

    Like

    • John A says:

      Aaron, thanks for the comment! I’m glad it worked. Although the post is old, you, and others like you, are exactly the reason the post is here in the first place. I’m happy it continues to help. Honestly, I often find myself referring back to my own blog when I can’t remember how I did something.

      That gmirror sounds pretty interesting. You should write something up for it.

      Like

  2. […] = Data Description; utility used to copy and convert data.  See my other post here for more information on this powerful unix […]

    Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s