Fork me on GitHub

Manipulation Tips for Disk Images



This page primarily focuses on techniques for resizing (growing and shirking) sparse bundle images on Mac OS X. However, most of the information presented will also be relevant for other disk image formats which are supported by 'hdiutil' on Mac OS X.



Determine current size of a .sparsebunlde (command line / terminal)

The command below will provide you with the size of a disk image in GB on Mac OS X.

  bytes=`hdiutil resize -limits /path/to/my.sparsebundle | tail -n1 | awk '{print $2}'` ; echo "$bytes * 512 / 1024 / 1024 / 1024" | bc | tr ' \n' ' ' ; echo "Gigabytes"



One other approach (there are many more) is to mount the file system and then use a tool such as 'df'.

  hdiutil attach /path/to/my.sparsebundle | grep "/Volumes" | awk '{print $3}' | xargs df -h | tail -n1 | awk '{print $2}' 
  • The command above assumes there is just a single file system within the disk image.
  • The command above will provide the output in human readable format.
  • Image file systems will remain mounted.



Yet another approach (there are many more) is to use the following command :

  hdiutil pmap /path/to/my.sparsebundle | grep "^SECTION" | awk -F "Size " '{print $2}' | awk -F ";" '{print $1}'
  • The command above assumes there is just a single file system within the disk image.
  • The command above will provide the output in human readable format.



Expanding .sparsebundles using hdiutil on Mac OS X

The command below will grow a disk image to a size of 50 gigabytes, provided that the underlying file system containing the disk image has sufficient resources and that the disk image is currently set to be less than 50 gigabytes in size.

  hdiutil resize -growonly -size 50g /path/to/my.sparsebundle



Shrinking .sparsebundles using hdiutil on Mac OS X

Before attempting to reduce the size of a disk image it is worth while finding out how small it will be possible to make the disk image. The following command will provide information on the minimum limit (in Gigabytes) for the disk image specified :

  bytes=`hdiutil resize -limits /path/to/my.sparsebundle | tail -n1 | awk '{print $1}'` ; echo "$bytes * 512 / 1024 / 1024 / 1024" | bc | tr ' \n' ' ' ; echo "Gigabytes" 
  • Note : there are various other approaches.

The command below will shrink a disk image to a size of 50 gigabytes, provided that the image limits will permit such an operation.

  hdiutil resize -shrinkonly -size 50g /path/to/my.sparsebundle



Helpful Links