$ hdiutil create -volname FredBackup -size 10g -type SPARSE -fs HFS+ FredBackup created: /Volumes/fred/FredBackup.sparseimage $ ls -lh FredBackup.sparseimage total 36872 -rwxrw-r-- 1 fred fred 18M Dec 22 2007 FredBackup.sparseimage
Even though I created disk image with a size of 10GB, the real size of the disk image is only of 18MB. The size will grow as I fill the disk image.
If you want this image to be encrypted you can use hdiutil convert with the following options.
$ hdiutil convert -encryption -format UDSP FredBackup.sparseimage -o FredBackupSecure.sparseimage Enter a new password to secure "FredBackupSecure.sparseimage": Re-enter new password: Reading Driver Descriptor Map (DDM : 0)… Reading Apple (Apple_partition_map : 1)… Reading disk image (Apple_HFS : 2)… ............................................................................... Elapsed Time: 9.107s Speed: 1.8Mbytes/sec Savings: 99.8% created: /Volumes/fred/FredBackupSecure.sparseimage $ $ ls -lh total 73984 -rwxrw-r-- 1 fred fred 18M Dec 23 2007 FredBackup.sparseimage -rwxrw-r-- 1 fred fred 18M Dec 23 2007 FredBackupSecure.sparseimage
With the last command a new volume has been created with the same characteristics of FredBackup.sparseimage but encrypted. You can get rid of the previews non encrypted disk image.
Carefully store the password in a safe place or use the KeyChain to save this password because there is no way to decrypt the content of your new disk image without it.
Your disk image is read. To be used you just have to mount/attach it. You can either use the graphical interface and double click on the FredBackupSecure.sparseimage icon, or use the command line interface hdiutil attach to mount your new volume.
The Volume will be mounted on /Volumes/FredBackup which is the nane we gave during its creation with the option -volname FredBsckup.
Once you are done using your disk image you detach/unmount/eject the volume by using the command detach from hdiutil.
$ hdiutil attach FredBackupSecure.sparseimage Enter password to access "FredBackupSecure.sparseimage": /dev/disk3 Apple_partition_scheme /dev/disk3s1 Apple_partition_map /dev/disk3s2 Apple_HFS /Volumes/FredBackup $ $ cp -rp /Users/fred/Movies /Volumes/FredBackup $ $ hdiutil detach /Volumes/FredBackup "disk3" unmounted. "disk3" ejected. $ $ ls -lh total 112888 -rwxrw-r-- 1 fred fred 55M Dec 23 2007 FredBackupSecure.sparseimage
As you can see in the preview exemple. After we copied some data to the volume its size has dynamically grown.
If you free space in that volume you can use the command hdiutil compact to decrease the size of the disk image. As shown in the following example the disk image goes from 55MB to 39MB after we deleted the file dv2003.mov
$ hdiutil attach FredBackupSecure.sparseimage Enter password to access "FredBackupSecure.sparseimage": /dev/disk4 Apple_partition_scheme /dev/disk4s1 Apple_partition_map /dev/disk4s2 Apple_HFS /Volumes/FredBackup $ $ rm /Volumes/FredBackup/Movies/dv2003.mov $ $ hdiutil detach /Volumes/FredBackup/ "disk4" unmounted. "disk4" ejected. $ hdiutil compact FredBackupSecure.sparseimage Enter password to access "FredBackupSecure.sparseimage": Starting to compact… Reclaiming free space… .............................................................................. Finishing compaction… ............................................................................... Reclaimed 16 MB out of 10.0 GB possible. $ ls -lh -rwxrw-r-- 1 fred fred 39M Dec 25 2007 FredBackupSecure.sparseimage $
Comments: 11