Articles September 20, 2005 at 2:22 pm

Creating, mounting, and dismounting disk images from the command line

Recently I was writing a shell script in which I needed to create a disk image, mount it, write to it, and dismount it. The command in OS X that manages disk images at the command line is hdiutil, and the man page is rather long. The syntax for the command is simple, but there are a lot of options to consider. In this particular example I’m going to create a sparse image that is only as large as the data contained within the image. You can adapt the command to create images to fit your needs.

Read on for more…

The basic command looks like this:

<code>hdiutil create -size 1g -type SPARSE -fs HFS+ -volname Image ~/Desktop/image</code>

hdiutil: The command that manages disk images.

-size 1g: Set the size for the disk image. In the case of sparse images, this is the maximum size to which the image can grow. The image file itself will only be as large as the data contained in it. Abbreviations apply, for instance, k for kilobytes, m for megabytes, and g for gigabytes.

-type SPARSE: The options for the type of image are SPARSE and UDIF. A sparse image grows with the content, and a UDIF is a set size. UDIF images can be converted to certain subtypes, the explanation of which is beyond the scope of this article.

-fs HFS+: Specifies the filesystem type for the new image. If you’re using this image on a Mac OS X system, you’ll most likely want HFS+.

-volname Image: This option sets the name of the volume… the name that shows under the drive on your desktop when it’s mounted.

~/Desktop/image: Finally, the location and name of the image you’re creating. In the case of sparse images, the extension .sparseimage will be added to the end. I have created a file named image.sparseimage in my Desktop folder.

After creating the image, you’ll want to mount it, or attach it as a device, so you can write to it.

<code>hdiutil attach ~/Desktop/image.sparseimage</code>

And when you’re done, dismount or detach it.

<code>hdiutil detach /Volumes/Image</code>

No Comments

  • An ammendum you can also use to for the attach and detach options are
    hdiutil mount /path/to/disk_image
    hdiutil eject /path/to/mount_point

    One that I also like to use on my disk images is:
    hdiutil encryption (this encrypts the disk image)

    hdiutil stdinpass (this will prompt you for a password to enter when you
    create the disk image and will ask you for a pasword whenever you try and
    mount the image.

    hdiutil create -verbose -size 1g -type SPARSE -fs "Journaled HFS+" -volname
    Image ~/Desktop/image -encryption -stdinpass

    Just to expand on the existing example. If you also want to know what a
    section of hdiutil can do, you can also run and get some more info on the app
    from the CLI instead of typing "man hdiutil"

    : hdiutil create -help
    : hdiutil chpass -help
    : hdiutil …

    • Ok Fine, If create aimage by bellow commonds then how I will pass the password to mount the image in the commond mode……? I do not want to save the passowrd in the keychain…please suggest.

      hdiutil create -verbose -size 1g -type SPARSE -fs “Journaled HFS+” -volname
      Image ~/Desktop/image -encryption -stdinpass

    • Hi how to mount the sparseimage by the remote machine…?

      I tried the following command.

      “ssh [email protected] hdiutil attach -mountroot /mount/point/ /image/path/file.sparseimage”

      Which works fien but only to the sparse image which does not conatin the password or wqhich is not encrypted…Please can anybody tell how I can mount the password protected sparseimage by a remote machine….? Please note that the password is saved in the keycahin.

      This above command work fine in the local machien but not from the remote machine.
      Please help

  • tried it and its fun.. just create a sparse file any big you want and just drag and drop your files.. amazing.. thanks for the tutorial.. love it.

Leave a reply

You must be logged in to post a comment.