mh taqia mh taqia - 6 months ago 48
Objective-C Question

How to programmatically retrieve UUID of root disk partition in OS X?

I can retrieve an OS X disk partition UUID by this code:

void PrintUUID()
{
DADiskRef disk;
CFDictionaryRef descDict;
DASessionRef session = DASessionCreate(NULL);
if (session) {
disk = DADiskCreateFromBSDName(NULL, session, "/dev/disk0s2");
if (disk) {
descDict = DADiskCopyDescription(disk);
if (descDict) {
CFTypeRef value = (CFTypeRef)CFDictionaryGetValue(descDict,
CFSTR("DAVolumeUUID"));
CFStringRef strValue = CFStringCreateWithFormat(NULL, NULL,
CFSTR("%@"), value);
print(strVal); <------------- here is the output
CFRelease(strValue);
CFRelease(descDict);
}
CFRelease(disk);
}
}
}


Above code retrieve UUID of disk0, I want to retrieve UUID of root disk (mount point = /),
if I use "/" instead "/dev/disk0s2" then DADiskCopyDescription returns NULL.
Also I know I can do it in Terminal by this command:

diskutil info /


Briefly how can I retrieve BSD Name of root disk? (to use it in DADiskCreateFromBSDName)

Anybody has an idea?
Thanks.

Answer

Use DADiskCreateFromVolumePath instead of DADiskCreateFromBSDName:

char *mountPoint = "/";
CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)mountPoint, strlen(mountPoint), TRUE);
disk = DADiskCreateFromVolumePath(NULL, session, url);
CFRelease(url);