Shrink those sparse images

Mac OS X Add comments

I’ve written before on this site about using encrypted sparse disk images to store private information. Over time, as things are deleted from those disk images, they can become fragmented, and the image contains blank space between files, which can cause the image file itself to be unnecessarily large. For example, if you have two text files that are each 32K, and 1GB of blank space in between them on the disk, your sparse image will be 1GB in size and only contain 64K of actual data. That’s quite a chunk of slack space. Reclaiming that space is easy, but it may require that you drop a little bit of cash to do it.

The image needs to be defragmented and then compacted. To my knowledge, there is no freeware disk defragmenter for OS X. I use iDefrag ($30) and it does the job well for me. You’ll want to mount the image on the desktop, run iDefrag, and perform a full defragmentation. That will compact all of the data in the image’s filesystem and move it to the beginning of the image.

The next step requires our good friend the Terminal. After defragmenting the image and dismounting it, start /Applications/Utilities/Terminal.app and enter the following:

hdiutil compact /path/to/image.file

This is a different kind of compaction than what the defragmenter performs. In this step, hdiutil scans the image for the last point where data exists, and truncates the empty space after it, reducing the size of the image file.

To continue our example above, iDefrag would move our two 32K files to the beginning of the image’s filesystem. Then hdiutil would slice off the remaining 1GB of empty space after those files, reducing the image’s size to megabytes instead of gigabytes. The size of the final image will vary because there is a certain amount of overhead for filesystem structures.

7 Responses to “Shrink those sparse images”

  1. planetmike Says:

    Would creating a new empty disk image, and then copying your files over to the new image also result in a defragmented drive?

  2. Aaron Adams Says:

    Yep. It would also negate the need to compact the image with hdiutil.

  3. planetmike Says:

    And most importantly, you can save your $30. :) Of course, you need to have the disk space for the new disk image during the copy.

  4. Aaron Adams Says:

    If someone already has a defagmenter, or another use for one, or doesn’t have disk space, or doesn’t want to take the time and effort to create another image and duplicate the data, this is a possible way to save some disk space.

  5. Winkerbean Says:

    Hi,
    I am relatively new to the Mac OS and had a question. Exaclty how would I follow planetmike’s suggestion? I.e., would I copy the entire hard drive or just the particular directory under /Volumes or something else. I read somewhere (http://www.xlr8yourmac.com/tips/shrink_filevault_images.html) to run sudo ditto /Volumes/username /Volumes/username\ 1 in attempts to achieve something like planetmike’s idea. However, in my /Volumes directory, the ‘username’ portion represents a symlink to the root. Would the ditto command copy every file (nlcuding the symlink, recursively) to this new directory?

    I apologize for asking several questions at once (at about suggestions originating on other sites). As I said, I’m new.

    Thanks.

  6. Aaron Adams Says:

    Hi Winkerbean,

    I believe what planetmike is talking about is transferring data between images, and it seems that you’re talking about copying a drive for backup. Is that correct? If so, you’ll need an external Firewire or USB2 disk. Probably the simplest way to do that is with Carbon Copy Cloner (http://www.bombich.com/software/ccc.html).

  7. Winkerbean Says:

    Thanks, Aaron.
    That’s probably what the other article described. Where would I find information on planetmike’s technique?
    Thanks.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in