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
$

 

Comments

Posted by: gmt master ii Dec 08, 2009 @ 00:41

Nice article! Thanks for sharing!

Posted by: iphone wireless Dec 30, 2009 @ 22:53

Early morning time, the mist is dim. Curved river bank, willow obviously elegant like smoke. Far does not see the mountain, nearly does not see the ship, only a little selects the lights, embellishes in the faintly recognizable mist.

Posted by: fake rolex watches Jan 04, 2010 @ 23:41

Welcome to our website,and you will enjoy the sunshine service. Let's

abandon all the worry,and enjoy the happy shopping travel.All the

replica watches paypal and replica watches are most in fashion

boutique this year.rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament. In addtin, the

fake watches paypal and the other fake rolex paypal make you mold

your unique glamour this winter.The swiss rolex replica have the high

quality but low price. replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches. Enjoying the

fun of net purchase! replica rolex We will supply you the perfect

service and after sale support system. Our honest will bring you

absolute satisfaction.

Posted by: film izle Jan 17, 2010 @ 04:46

Thank you for sharing your friends. Hope to see you another day.

Posted by: divx film izle Jan 17, 2010 @ 04:50

Thank you for sharing your friends. Hope to see you another day.

Posted by: links of london Jan 19, 2010 @ 00:38

Our honest will bring you

absolute satisfaction.

Posted by: uvip33 Feb 05, 2010 @ 22:12

That’s something,That's what I was thinking.Brilliant idea.

Posted by: evening dresses Feb 19, 2010 @ 06:19

Thank you for sharing your friends.

Posted by: evening dresses Feb 19, 2010 @ 06:20

Thank you for sharing your friends.

Posted by: Nike Air Max Mar 04, 2010 @ 00:57

So beautiful sharing!Thank you very much.

Posted by: tag heuer watch Mar 04, 2010 @ 23:19

can show a man not only his wealthy but also his high taste.tag heuer watch As the society developed, many people are pay more attention to one's wearing than before, especially a watch. tag heuer watch If you are wearing a famous brand watch, people will look up you, also will love to do business with you. You will be a successful man with a such luxury fashion watch.

Posted by: Nike Air Max Mar 11, 2010 @ 19:16

Best wishes!Your blog is very good!

Posted by: omega moon watch Mar 11, 2010 @ 20:44

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Posted by: Nike Air Max Mar 18, 2010 @ 02:49

It is so nice.

Posted by: brand designer handbags Apr 07, 2010 @ 20:33

Here is the richest in the world and you can find your needs bring you harvest. yes brand designer handbags there is no other better place than here,in here you are with my natural beauty dvd brand designer handbags but also naturally pure green world. our homes brand designer handbags a share in the bar.God bless you!

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image