Growable disk image

One of the features I like on my MacOS is the Support for SPARSE devices. Specifying -type SPARSE creates a disk image which expands as it is filled with data.

$ 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

$ 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

$ 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

$ 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
$

$ 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
$

 
Posted on Dec 24, 2007 by: Fred Cirera @ 17:37 Leave a comment Comments: 11

Using hdiutil to encrypt backups

In the previews article I have explain how backup of my important documents on a CD or DVD, in just 2 easy steps. This is great, but I am not not very satisfy with this solution. All my most important documents, containing private, professional, and personal information are written in clear, readable by anyone, on a CD. If I loose that CD, or if it get stolen from me, all these information can be at risk.

In this article we are going to use the same principle as described before to create an encrypted backup.
 
Posted on Oct 22, 2007 by: Fred Cirera @ 10:29 Leave a comment Comments: 28

Manipulating disk images with hdiutil

Let's have fun with HDUTIL(1). Ok the word "fun" is maybe too strong. Let's play with hdiutil to create and manipulate disk images.

Hdiutil is an command line utility to manipulate disk images on MacOS X. To use hdiutil you type at the shell prompt:
$ hdiutil action_verb [options]
$ hdiutil action_verb [options]

Common action verbs are attach, detach, verify, create, convert, and burn. In this article we are going to use hdiutil to backup our important data using encryption.
 
Posted on Oct 20, 2007 by: Fred Cirera @ 11:10 Leave a comment Comments: 18