Volume UUID using IOKit framework from OS X

I want to get / enumerate UUID of of volumes mounted on my OS X system. I know that I can use diskutil command to get it as below.

$ diskutil info / |grep "Volume UUID"
   Volume UUID:              039B9653-52AA-34F3-AF4A-854FA47C811A

However, I want to achieve it in a programmable way (maybe) using IOKit framework. If anyone can tell me how to do it, it will be very helpful.

Thanks a lot for your help in advance!

Answers


Thanks to @pmdj's answer, I could solve my issue with the following code snippet.

void printUUID()
{
    DADiskRef disk;
    CFDictionaryRef descDict;

    DASessionRef session = DASessionCreate(NULL);

    std::string mountPoint = "/";
    CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *) mountPoint.c_str(), mountPoint.length(), TRUE);
    disk = DADiskCreateFromVolumePath(NULL, session, url);
    if (disk) {
        descDict = DADiskCopyDescription(disk);
        if (descDict) {
            CFTypeRef value = (CFTypeRef)CFDictionaryGetValue(descDict,
                                                              CFSTR("DAVolumeUUID"));
            CFStringRef strValue = CFStringCreateWithFormat(NULL, NULL,
                                                            CFSTR("%@"), value);
            printf("%s\n" ,CFStringGetCStringPtr(strValue, kCFStringEncodingMacRoman));
            CFRelease(strValue);
            CFRelease(descDict);
        }
        CFRelease(disk);
    }

    CFRelease(url);
    CFRelease(session);
}

Have you looked into the Disk Arbitration framework? This is much better suited for volume enumeration, mount operations, etc., and allows you to query UUIDs among other properties., You can retrieve the IOService handle to the underlying IOKit objects as well, should you need them.

Note that both the HFS+ file system and the GPT partition on which it resides will have a UUID, and they will not match. diskutil generally talks about the HFS+ UUID.


Need Your Help

Is there an alternative to Microsoft.SqlServer.Management.Smo.SqlDataType that includes a value for RowVersion?

.net sql-server rowversion

The Microsoft.SqlServer.Management.Smo.SqlDataType enum has a value for the timestamp type but not rowversion. I'm looking for an updated version of the assembly or an alternate enum type that supp...

Displaying arraylist with Jtextarea in a single iteration?

user-interface arraylist textarea joptionpane

I'm having trouble displaying and array list the way I want it. Instead of the full list of strings being displayed, one String is displayed at a time with each dialog box. Once I heit okay on the ...