Hello,
I am working on a backup process on macOS. Before copying files, the script needs to check that it is looking at the correct mounted backup volume.
A fictional example
A user opens an encrypted disk image called ExampleBackup.sparsebundle and enters the password themselves. The volume then appears in Finder as ExampleBackup.
Before copying anything, the script needs to confirm:
- This mounted volume actually belongs to the intended disk image.
- The volume is mounted read/write.
- File ownership is being enforced.
- The destination has not been replaced or changed since the previous check.
Simply checking the volume name or its path does not seem sufficient: another volume could have the same name, or a device name could be reused after ejecting and reconnecting an image.
My questions
- What supported macOS API or command should a script use to establish this relationship reliably?
- Can we rely on specific fields from
hdiutil info -plistanddiskutil info -plist <device>? If so, where are their meanings and limitations documented? - How should the script distinguish “no” from “unknown” when information is missing or a check fails?
- What should it check again immediately before copying, since the volume could change between separate checks?
The checks themselves should only read information. They should not unlock or mount images, change permissions, or disable any security protections. If the script cannot confirm the destination safely, it should stop.
A recommended approach or small example would be very helpful.
Thank you.
Python can call many Apple APIs via PyObjC. However, it’s not obvious whether that’s actually a better option than writing your own tool, per the outline you suggested. Regardless, let’s assume you have access to native APIs and look at your specific issues:
This mounted volume actually belongs to the intended disk image.
This is tricky. I’ll come back to it below.
The volume is mounted read/write.
This is easy. The low-level statfs system call has a MNT_RDONLY flag that tells you whether this is a read-only mount. This is also exposed by higher-level APIs, most notably the volumeIsReadOnly URL property.
File ownership is being enforced.
This is similar, in that statfs has a MNT_IGNORE_OWNERSHIP flag. I don’t think this is exposed as a URL resource value, so you’ll probably have to call statfs directly.
The destination has not been replaced or changed since the previous check.
Most volumes support a volume UUID. You can get this through low-level APIs [1] but in this case it’s much easier to use the volumeUUIDString URL property.
Coming back to your first question about mapping a volume to its underlying disk image, I don’t think there’s a great way to do this. You can certainly do it by invoking hdiutil and diskutil. That’s not terrible, and the -plist property helps, but it’s not great.
It’s likely that you can also get this info from the I/O registry, but I’m not sure if you can do that by only using supported properties.
I’m gonna ping Kevin about this to see if he has an suggestions.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] Specifically, the ATTR_VOL_UUID attribute returned by getattrlist. See the man page for details.