简体   繁体   English

使用OS X中的IOKit框架的卷UUID

[英]Volume UUID using IOKit framework from OS X

I want to get / enumerate UUID of of volumes mounted on my OS X system. 我想得到/枚举我的OS X系统上安装的卷的UUID。 I know that I can use diskutil command to get it as below. 我知道我可以使用diskutil命令来获取它,如下所示。

$ 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. 但是,我希望使用IOKit框架以可编程方式(可能)实现它。 If anyone can tell me how to do it, it will be very helpful. 如果有人能告诉我该怎么做,那将非常有帮助。

Thanks a lot for your help in advance! 非常感谢您的帮助!

Thanks to @pmdj's answer, I could solve my issue with the following code snippet. 感谢@ pmdj的回答,我可以通过以下代码片段来解决我的问题。

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. 这更适合于卷枚举,装载操作等,并允许您在其他属性中查询UUID。如果需要,您也可以检索基础IOKit对象的IOService句柄。

Note that both the HFS+ file system and the GPT partition on which it resides will have a UUID, and they will not match. 请注意,HFS +文件系统和它所驻留的GPT分区都将具有UUID,并且它们将不匹配。 diskutil generally talks about the HFS+ UUID. diskutil通常会讨论HFS + UUID。

There's a simpler way to get the UUID from a path that doesn't have to use the Disk Arbitration framework. 有一种更简单的方法可以从不必使用磁盘仲裁框架的路径获取UUID。 The code below returns the volume UUID of the input full item path, using the NSURL's getResourceValue and the NSURLVolumeUUIDStringKey: 下面的代码使用NSURL的getResourceValue和NSURLVolumeUUIDStringKey返回输入完整项路径的卷UUID:

    NSURL* pathUrl = [NSURL fileURLWithPath:path];
    NSString* volumeUUID = nil;
    NSError* error = nil;
    BOOL success = [pathUrl getResourceValue:&volumeUUID forKey:NSURLVolumeUUIDStringKey error:&error];

Note that it does not enumerate the UUIDs of the mounted volumes, just gives you the volume UUID of the input path. 请注意,它不会枚举已装入卷的UUID,只是为您提供输入路径的卷UUID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM