OS X loopback disk management

I use the command line quite a bit, since I’m a Linux guy.  Being on an OS X machine doesn’t mean you’re all point and click.  One thing I’ve been missing since migrating to Apple OS X is creating loopback files that I’d have encrypted filesystems inside of, and mount whenever I need them.  After a bit of research, it appears OS X has the ability to easily do the same.  Using ‘diskutil’ for disk usage, and ‘hdiutil’ for anything internal to the loopback, I created a few scripts that make it much easier to create, mount, and unmount an encrypted loopback filesystem.  They aren’t magnificent since they’re small bash scripts, but they are being shared so others looking for ways to handle loopback files can either grab and have fun, or learn from.

Scripts below are as follows:
loopback-create.sh – takes parameters of size in megabytes, and loopback file name (without the extension .dmg).  This will create the loopback file in the current directory, and prompt for the password to be used for the encrypted filesystem.

loopback-mount.sh – takes a parameter of the loopback filesystem’s name.  (without the extension .dmg)

loopback-unmount.sh – takes a parameter of the mount name to be unmounted.  (can be seen in Finder, or with ‘diskutil list’)

Copy and paste these into files, run ‘chmod +x <filename>’ as root on each of them, and you’re golden.

 

loopback-create.sh

#!/bin/bash
if [ -z $2 ]; then
echo “Parameters necessary.”
echo “$0 [ size in megs ] [ loop-back file name ]”
exit 1
fi
FILE_SIZE=$1
FILE_NAME=$2
echo “Enter Encryption Password:”
read -s password

RET=`echo -n “${password}” | hdiutil create -encryption -stdinpass -size ${FILE_SIZE}m -fs “Case-sensitive HFS+” -volname ${FILE_NAME} ${FILE_NAME}.dmg`
echo “${RET}”
echo “”
echo “Loopback can be mounted with:”
echo “hdiutil attach ${FILE_NAME}.dmg -mountpoint ${FILE_NAME} -nobrowse -readwrite”

 

loopback-mount.sh

#!/bin/bash
if [ -z $2 ]; then
echo “Parameters necessary.”
echo “$0 [ size in megs ] [ loop-back file name ]”
exit 1
fi
FILE_SIZE=$1
FILE_NAME=$2
echo “Enter Encryption Password:”
read -s password

RET=`echo -n “${password}” | hdiutil create -encryption -stdinpass -size ${FILE_SIZE}m -fs “Case-sensitive HFS+” -volname ${FILE_NAME} ${FILE_NAME}.dmg`
echo “${RET}”
echo “”
echo “Loopback can be mounted with:”
echo “hdiutil attach ${FILE_NAME}.dmg -mountpoint ${FILE_NAME} -nobrowse -readwrite”

loopback-unmount.sh

!/bin/bash

if [ -z $1 ]; then
echo “Parameters necessary.”
echo “$0 [ Mount Name ]”
exit 1
fi
MOUNT_NAME=$1

RET=`diskutil umount ${MOUNT_NAME}`
echo “${RET}”
DISK=`diskutil list | grep ${MOUNT_NAME} | awk ‘{ print $6 }’`
RET=`hdiutil detach /dev/${DISK}`
echo “${RET}”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.