是否有一种简单的方法可以使用API获取Mac中所有硬盘的序列号?
基本上,我正在寻找一个硬盘的唯一标识符,以便我可以确定该硬盘是否被我的应用程序使用(或引用)过。
如果有其他解决方案,请告诉我。
注:我需要这个解决方案适用于10.4及以上版本。
我不确定"AppleUSBEHCI"是否是要查找的正确内容,但你可以使用IOKit框架检索这种数据:
#include <IOKit/IOKitLib.h>
#include <Cocoa/Cocoa.h>
kern_return_t kr;
io_iterator_t io_objects;
io_service_t io_service;
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceNameMatching("AppleUSBEHCI"), &io_objects);
if(kr != KERN_SUCCESS)
exit(1);
while((io_service= IOIteratorNext(io_objects)))
{
kr = IORegistryEntryCreateCFProperties(io_service, &service_properties, kCFAllocatorDefault, kNilOptions);
if(kr == KERN_SUCCESS)
{
NSDictionary * m = (NSDictionary *)service_properties;
NSLog(@"%@", m);
CFRelease(service_properties);
}
io_iterator_t iter;
//handle kr error
kr = IORegistryEntryGetChildIterator(io_service, kIOServicePlane, &iter);
io_registry_entry_t child;
while( (child = IOIteratorNext( iter )))
{
kr = IORegistryEntryCreateCFProperties(child, &child_props, kCFAllocatorDefault, kNilOptions );
NSLog(@"Child props: %@", child_props);
//release child_props
}
IOObjectRelease(io_service);
}
IOObjectRelease(io_objects);
通过命令行:
ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial
这会为您提供内置硬盘的序列号。
DADiskCreateFromBSDName
创建DADiskRef
,并使用DADiskCopyDescription
获取信息字典,查找键kDADiskDescriptionMediaUUIDKey
。可以通过statfs获取有关安装点等的信息。
话虽如此,只需调用带有选项-plist
的命令行实用程序diskutil即可以plist格式获取信息。
样例代码 FSMegaInfo 也许可以指导如何获取更多关于磁盘的信息。
/etc/fstab
中挂载什么。
另外,很奇怪你看不到UUID。在你的机器上,diskutil info /dev/disk0s2
返回什么?在列表中看不到Volume UUID
条目吗?你可能需要将上面的/disk0s2
更改为实际拥有的内容。首先使用diskutil list
进行检查。 - Yuji$ system_profiler SPSerialATADataType -detailLevel medium | grep Serial | sed -e 's/[\<\>\"\ ]//g' | -F':' '{print $2}'
$ system_profiler SPSerialATADataType -detailLevel medium | grep -e "Serial" -e "Model" | awk -F': ' '{print $2}'
- Robert Jansson内部驱动器:在“设备特征”中的IOAHCIBlockStorageDevice string
属性“序列号”中,例如:(WD-WCAV5D1345345)
USB驱动器:在“USB串行号”中的IOUSBDevice string
属性中,例如:(5743415654564561373734)
FireWire驱动器:在“协议特性”中的IOReducedBlockServices number
属性“GUID”中,例如:(407345709348650)
Thunderbolt驱动器:??
这是一个非常古老的帖子,也是第一个搜索结果,因此我会添加另一种有用的方法,该方法依赖于安装Homebrew和smartmontools。
安装homebrew,然后...
brew install smartmontools
smartctl -x disk0s3 | grep "Serial Number"
diskutil list
命令来查找磁盘ID。