mh taqia mh taqia - 1 year ago 179
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,
CFStringRef strValue = CFStringCreateWithFormat(NULL, NULL,
CFSTR("%@"), value);
print(strVal); <------------- here is the output

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?

Answer Source

Use DADiskCreateFromVolumePath instead of DADiskCreateFromBSDName:

char *mountPoint = "/";
CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)mountPoint, strlen(mountPoint), TRUE);
disk = DADiskCreateFromVolumePath(NULL, session, url);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download