Simulate how a volume is mounted using diskarbitrationd

automountmountntfs

I'm attempting to emulate how a volume is normally mounted for my NTFS volume that I've added to /private/etc/fstab. Normally, when a volume is found, like an external hard drive, it is mounted and then a shortcut to that volume is put on the Desktop and in the Finder left-hand sidebar. However, since to mount a NTFS volume as read-write in fstab, the flag nobrowse must be added, this prevents it from showing up on the desktop and in Finder's sidebar.

My research has pointed me to diskarbitrationd as the program that listens for new volumes and mounts them. I'd like to emulate the automatic adding of shortcuts to the desktop and Finder and removal on volume unmounting, and I'd like to make a script that listens for information from diskarbitationd and do the processing for me.

How would I hook into diskarbitrationd and listen for new events, then add the shortcuts to the desktop and Finder after the volume is mounted?

Thanks for your help!

Best Answer

You hook into diskarbitrationd by using Apple's Disk Arbitration framework. Basically you call DASessionCreate() to create a DASessionRef, and then register one or more notification callbacks.

You would need to register for the DARegisterDiskMountApprovalCallback. This gives you a notification whenever diskarbitrationd is going to mount something new. By returning a DADissenterRef from this callback, you can prevent diskarbitrationd from actually mounting the file system.

Then you can mount the file system yourself and create shortcuts etc. using standard methods, such as the mount() system call.

Here's a direct link to the page in the programming guide, you'll need to examine:

https://developer.apple.com/library/mac/documentation/DriversKernelHardware/Conceptual/DiskArbitrationProgGuide/ArbitrationBasics/ArbitrationBasics.html#//apple_ref/doc/uid/TP40009310-CH2-SW1