Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
I don't like using the UI for these kinds of functions because Apple hates to leave things in any decently operating form now.

I have a 320GB Hard Drive and I need to clone it (hopefully with dd in terminal) to a 256GB drive. There is only 90GB being used on the drive currently so how can I clone but leave out the blank space?

I'd like to plug both drives into a Mac, open terminal and complete the clone straight from one to the other. No middle operation.

Here's my guide to archiving disks without the empty space using bash in Terminal.app

Warning: as with anything pertaining to the terminal, read, read, then read again. One typo can cost you a reinstall and possible lost data. Don't do this if you don't have a reasonable grasp on what you're doing. Feel free to contribute to this or improve if you are one of those people that do know what you're doing 😁

I have dozens of Mac boot drives, Time Machine drives etc. that have to be backed up precisely as they are now, left in cold-storage, and must be restorable in the future just in case. After quite a bit of finicky keyboard pecking, I've streamlined my process as such:

Plug it in

Physically attach your drive in whichever manner, be it internal SATA, USB, Thunderbolt. In my experience, the reads never exceeded about 100MB/s so temper you expectations.

Health Checkup
Before proceeding from here, be certain to use Disk Utility to verify and/or repair the partition or entire disk you intend to archive! I do not archive my disks unless they prove to have non-corrupted libraries or journals. If you skip this step and there was damage to a journal, for instance, you may not ever be able to repair that journal on the archived image.

Identify devices

You must know precisely the ID of the drive and/or partition you're going to back up. You mess this up, you might be bunged.

Bash:
diskutil list

Entering this will return a list of drives, partitions, sizes, and names of everything plugged in regardless if they're mounted to the OS or not. I get:

Bash:
/dev/disk0
0 GUID_partition_scheme      930.3 Gi disk0
1 EFI                        200.0 Mi disk0s1
2 Apple_HFS TimeCapsule      931.2 Gi disk0s2

/dev/disk1
0 GUID_partition_scheme      68.1 Ti disk1
1 EFI                        200.0 Mi disk1s1
2 Apple_HFS Legion of Doom   68.1 Ti disk1s2

Since I'm looking to backup my 1TB Time Machine drive, I look for the drive that has the same name as my Time Machine drive (TimeCapsule) and reports to be near the same size (930GB approximately). From this, I can say that my Time Machine backup drive has the ID: /dev/disk0 so the TimeCapsule partition has the ID: /dev/disk0s2

Release the drive

The next step requires that the drive be not "busy" and to ensure that we will release it from use by detaching it in the system. Detach the whole drive, not just the partition you wish to clone.

Bash:
hdiutil detach /dev/disk0

Clone

You can clone the whole drive but I feel that the partition information, EFI, and any other partitions that may share the drive just complicate things. If, for instance, you had a Windows Bootcamp partition that you no longer plan to use, you can skip backing that up by directing the utility directly to the partition you want. That's what I'm doing here.

Bash:
sudo dd if=/dev/disk0s2 of=/Users/CheetosLand/Desktop/King\ Cold.dmg bs=512

sudo means it will authenticate that you have permission to do this. dd is the "data definition" command that accomplishes essentially cloning of data. if is "input file" and of is and Old English word likely derived from the Greek root apo or Latin ab.

I directed the output data to my Desktop and on my Desktop it will create a new file called King Cold with the file extension .dmg which is a disk image. Since HFS+ natively uses 512-byte sectors, I set the block size (bs) to 512. This will help when compressing the free space later, so resist the urge to set this number higher for the sake of speed.

Notice the "\" in "King Cold". That is used in 'NIX to treat whatever comes afterward literally. Meaning the space between King and Cold is not a part of the command but just a character that is going to be part of the name of the file created.

Wait

Seriously, wait. It can take hours, a day, a week depending on how large your archive material is. It took me over an hour to archive a 100GB partition.

Plug it in

When Terminal finishes executing the command, it will display something along the lines of a final report which says, "320923 bytes transferred in 54.2902834 secs". So long as it does not report an "error" anywhere upon completion, you can move on.

You have a new disk image (.dmg) in the location you set in the last step. We have to mount it to the system to manipulate.

Bash:
hdiutil attach /Users/CheetosLand/Desktop/King\ Cold.dmg

Pay attention to the next line that Terminal prints. It will tell you your new disk ID.

In my case, I get:
Bash:
/dev/disk3             /Volumes/TimeCapsule

Clear the Junk

Drives may compress better if the trash is leveled. This is accomplished by writing 0's to unused (unassigned) space on the disk image.

Bash:
dd if=/dev/zero of=/Volumes/TimeCapsule/zjunk_file bs=512

We are cloning data from our /dev/zero folder to the mounted disk image. The /dev/zero folder just returns 0 no matter how much the system calls to it. We are putting those 0's into a file called "zjunk". You must add _file to the end of the file name so it will create a plain file with no filetype extensions. The file will grow until the drive has zero space remaining. Again, we use the 512 block-size to keep everything aligned on the drive for better efficiency.

Terminal will error out whenever there is no more space on the drive.

Once you delete this file, the empty space on the disk image should be zeroed out. Here is how you delete that file.

Bash:
rm /Volumes/TimeCapsule/zjunk_file

Release the disk image

Now that we're done preparing the image for shrinking, we must make sure it is not in use by any system resources.

Bash:
hdiutil detach /dev/disk3

Remember that /dev/disk3 is the ID given to us whenever we attached this disk image.

Go small

Finally we can try to resize the .dmg so that it has minimal free space, thus creating a smaller archive file.

Bash:
hdiutil resize -sectors min /Users/CheetosLand/Desktop/King\ Cold.dmg

In a flash, the Terminal will return to a new prompt and you will see your disk image has shrunken, hopefully. This depends on how much of your partition was actually being utilized. Setting -sectors to min means it will use the fewest sectors possible to store the data that is written to the disk image.

Using this technique, I've shrunken my OS install partitions that I keep handy for restores by about 12GB. All other backups of my hard drives have been handily shrunken to their minimum size, leaving only 100MB-1GB of free space on these image files.

In closing

Of course I'm no expert but this has proven to be a good way to always have duplicates of my retired disks on-hand. You shouldn't use this method if you plan to mount the image, edit or add data, and then unmount the image. It is merely for reaching back into the archive to recover data more easily. Since you have no free-space on these images, things can go badly if you choose to treat these as live-drives. You can, however, restore these disk images to larger drives and they can be used as live boot drives again.

Beware:

I'm not sure how hdiutil treats writing 0's to an image on an SSD. All of my archives are on clackers so write fatigue is far far less of a factor than on an SSD. Please do your own research and if you have info, please share.

Let me know how this works for you.

Edit: Added instructions to verify and repair disks first!
 
Last edited:

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
Go on. I’m not sure specifically what this is referring to.
 

Fishrrman

macrumors Penryn
Feb 20, 2009
29,243
13,316
OP wrote:
"I have a 320GB Hard Drive and I need to clone it (hopefully with dd in terminal) to a 256GB drive. There is only 90GB being used on the drive currently so how can I clone but leave out the blank space?"

Your post is a "MacRumor Classic Case" of over-thinking things.

This is child's play. Just do this:

Download SuperDuper (FREE) from here:

Put SD into your applications folder.
Launch SD.

It has one of the easiest-to-use interfaces I've ever seen in Mac software.

Set your 320gb drive as the source (left)
Set the 256gb drive as the target (right)

SD will erase the drive and then clone it.

THAT IS ALL THAT IS REQUIRED.
The job is done.

Stop over-thinking and just TRY the advice I've offered.
Then come back here and tell us your results.
 
  • Like
Reactions: Big Bad D

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
No.

There is no telling how a front-end application is handling this. How do I know it isn’t using an rsync function? I certainly cannot rely on rsync or cp to do what dd is capable of doing.

This is why I repair my own car. In 21st century America, I trust nobody.
 

Fishrrman

macrumors Penryn
Feb 20, 2009
29,243
13,316
Don't trust me if you don't want to.
I suggest you try what I've recommended.

You will be both surprised and confounded by the results.

I know WHY -- but I'm not going to tell you. :mad:
 

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
Probably read that the wrong way. I don’t trust developers for such things. I’m not saying I flatly distrust you personally.

The fewer programmers or developers etc. I can involve in my daily life, the greater my quality of life seems to be.
 

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
Well... I tried SuperDuper! as a comparison. Neither of my socks were blown off, nor did a lady in a bikini pop out of my Mac Pro.

So far, I'm not impressed as somebody who simply needs a good one-time bit-level archive of my drives. It didn't offer any format but .sparsebundles also. I suppose the only upshot is that it did skip the blank space but besides that am I missing something?
 

HDFan

Contributor
Jun 30, 2007
7,290
3,341
You can also use CCC. If you want to spend your time messing with dd, rsync or whatever you are certainly welcome. The (hundreds of?) thousands of users of SuperDuper and CCC over decades have pretty much confirmed that the programs do exactly what they are advertised to do.

To post this you are relying on many thousands of developers (OS, ISP, web hosting service, etc.)
 

MrCheeto

Suspended
Original poster
Nov 2, 2008
3,531
353
🤷🏾‍♂️ IDK. The steps described above work perfectly for me and if I end up needing this process often enough, I will script it.

I have the peace of mind knowing that it is a precise bit-level copy with a definable block-size and I get to choose what format the output is.

One thing that I can do with dd that neither piece of software is capable of is clone completely unsupported formats. I have backed up APFS systems using Leopard. dd doesn’t care what the bits mean, it just checks that they’re all there in the end.

The only limitation I have is the inability to skip free space but that just causes the process to take a little more time rather than lead to any errors or failure. If I can figure that one part out, I would need nothing more.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.