Using disk images with Time Capsule to share files

I needed to use an Apple Time Capsule (actually backing up our home Macs) to share some files. The issue here is that Time Machine (OSX’s backup program) might use up all the space on the disk: there is no way to tell Time Machine to stop at a certain quota. Workarounds are known like adding a second drive or partitioning Time Capsule’s own HD (which invalidates your warranty).. Too much effort, I wanted a quick, dirty hack.
Caveat lector: I don’t need concurrent file access: I just want to be able to open a Filemaker database from different machines at different times..
What I need is a way to simulate a loopback device and reserve a fixed amount of space for my files. OSX does not support loopback devices but it comes with a nice utility called hdiutil.
Essentially, we are going to create a disk image on Time Capsule’s internal hard-disk, once the image is there it can be mounted and used as any other volume but it cannot be accessed concurrently from Time Capsule.
These are the steps:
1: Configure Time Capsule to share files
Open the Airport Utility and go to the Disks pane, tab: “File Sharing”. setup your Time Capsule’s access at convenience (I prefer to set separate user accounts for that).

Step 2: Mount Time Capsule’s Volume.
Mount the drive using Command-K shortcut and insert the credentials configured in the previous step.
Step 3: Create the Disk Image
Open a shell and locate the mounted volume. Then use hdiutil to create a zero-filled disk-image. The command sequence might resemble the following
Tuja:~ gabriele$ cd /Volumes Tuja:Volumes gabriele$ ls -l total 3 drwxr-xr-x 6 gabriele staff 408 Jan 3 00:51 Backup of Tuja lrwxr-xr-x 1 root admin 1 Jan 2 14:12 Macintosh HD -> / drwx------ 9 gabriele staff 264 Jan 3 01:18 Storage
Tuja:~ gabriele$ hdiutil create -megabytes 32768 /Volumes/Storage/Sharedfiles.dmg -layout NONE ........................................................................................ created: /Volumes/Storage/Sharedfiles.dmg
In this case I created a disk image of 32GB with one internal partition (Layout NONE).
Step 4: Format the disk image
First off, retrieve the device id:
Tuja:~ gabriele$ hdid -nomount /Volumes/Storage/Sharedfiles.dmg /dev/disk2
Then create a file system and umount it (to flush journals):
Tuja:~ gabriele$ newfs_hfs -v sharedfiles /dev/disk2 Initialized /dev/rdisk2 as a 32 GB HFS Plus volume
Tuja:~ gabriele$ hdiutil eject /dev/disk2 "disk2" unmounted. "disk2" ejected.
The volume is ready. It can be mounted and modified at will. No internal partitions, no additional disks and no Time Machine risking to fill it up.
Step 5: Automate mount
The following little Applescript will do the job of mounting the Disk Image. Customize the parameters in the beginning of the script at your convenience.
property TimeCapsule_IP : "192.168.22.45"
property UserID : "gabriele"
property Image_Name : "Sharedfiles.dmg"
property Image_Id : "sharedfiles"
property TimeCapsule_Disk_Name : "Storage"
set devname to do shell script "mount | grep Storage | cut -f3-4 -d / | cut -f1-1 -d \"(\""
tell application "Finder"
with timeout of 20 seconds
-- 20 seconds are enough for mounting a drive. This saves us
-- from a hanging OSX waiting for mount timeout
if not (exists the disk devname) then
mount volume "afp://" & UserID & "@" & TimeCapsule_IP & "/" & TimeCapsule_Disk_Name
delay 5
end if
if not (exists Image_Id) then
do shell script "hdiutil attach /Volumes/" & TimeCapsule_Disk_Name & "/" & Image_Name & " -mount required"
end if
end timeout
end tell
The script assumes that the Time Capsule has an IP of 192.168.22.45, a disk named Storage (default is Data_1) and a user defined (in step 1) as gabriele. The disk image is called Sharedfiles.dmg and mounts a volume called sharedfiles.
There is space for improvement in this script. An AppleScript guru could make it more reliable by using afp_mount in place of mount, afp_mount has a “-nobrowse” option that is nice to hide those network shares on the desktop and has the advantage of being able to force volume names as well.
One final note: the volume created here above is not going to be backed up unless is not mounted permanently on the Mac executing a Time Machine backup.
