我可以通过以下代码检索OS X磁盘分区UUID:
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);
}
}
}
上面代码检索disk0的UUID,我想检索根磁盘的UUID(挂载点= /), 如果我使用“/”代替“/ dev / disk0s2”,则DADiskCopyDescription返回NULL。 此外,我知道我可以通过此命令在终端中执行此操作:
diskutil info /
简要说明如何检索根磁盘的BSD名称? (在DADiskCreateFromBSDName中使用它)
有人有想法吗? 感谢。
答案 0 :(得分:5)
使用DADiskCreateFromVolumePath
代替DADiskCreateFromBSDName
:
char *mountPoint = "/";
CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)mountPoint, strlen(mountPoint), TRUE);
disk = DADiskCreateFromVolumePath(NULL, session, url);
CFRelease(url);
答案 1 :(得分:1)
DADiskCreateFromVolumePath仅包含在OS 10.7及更高版本中,因此如果您需要支持OS 10.4及更高版本的旧平台(比如我!),唯一的选择是使用statfs从posix名称生成BSD名称,因此然后整个功能变为:
#include <sys/param.h>
#include <sys/mount.h>
void PrintUUID()
{
DADiskRef disk;
CFDictionaryRef descDict;
DASessionRef session = DASessionCreate (kCFAllocatorDefault);
if (session) {
struct statfs statFS;
statfs ("/", &statFS);
disk = DADiskCreateFromBSDName (kCFAllocatorDefault, session, statFS.f_mntfromname);
if (disk) {
descDict = DADiskCopyDescription (disk);
if (descDict) {
CFTypeRef value = (CFTypeRef) CFDictionaryGetValue (descDict, CFSTR("DAVolumeUUID"));
CFStringRef strValue = CFStringCreateWithFormat (NULL, NULL, CFSTR("%@"), value);
print (strValue) <------------- here is the output
CFRelease (strValue);
CFRelease (descDict);
}
CFRelease (disk);
}
CFRelease (session);
}
}