macos 如何从Mac硬盘获取序列号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2019244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get serial number from Mac hard disks?
提问by Girish Kolari
Is there an easy way to get the serial number of all the hard disks in a Mac using an API?
有没有一种简单的方法可以使用 API 获取 Mac 中所有硬盘的序列号?
Basically, I'm looking for a unique identifier for the hard disk with which I can figure out whether the hard disk has been used (or referred to) by my application or not.
基本上,我正在寻找硬盘的唯一标识符,通过它我可以确定我的应用程序是否使用(或引用)了该硬盘。
Please let me know if there is any other solution.
请让我知道是否有任何其他解决方案。
Note: I need this solution for 10.4 and above.
注意:我需要 10.4 及更高版本的解决方案。
采纳答案by diciu
I'm not sure if "AppleUSBEHCI" is the proper thing to look for but you can retrieve this sort of data using the IOKit framework:
我不确定“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);
回答by Matthias Braun
From the command line:
从命令行:
ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial
ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial
This gives you the serial number of the built-in hard disk.
这为您提供了内置硬盘的序列号。
回答by Yuji
I think it's better to get the Volume UUID (which appears in the Disk Utility, for example.)
UUID can be obtained using the Disk Arbitrationframework, which is slightly higher-level than IOKit and easier to use.
Create DADiskRef
using DADiskCreateFromBSDName
, and use DADiskCopyDescription
to get the info dictionary, and look up the key kDADiskDescriptionMediaUUIDKey
. Info on the mount point etc. can be obtained by statfs.
我认为最好是获取Volume UUID(例如出现在磁盘工具中。)UUID可以使用磁盘仲裁框架获取,该框架比IOKit级别稍高,更易于使用。创建DADiskRef
using DADiskCreateFromBSDName
, and useDADiskCopyDescription
获取信息字典,查找键kDADiskDescriptionMediaUUIDKey
。可以通过statfs获取有关挂载点等的信息。
That said, it might be easier just to invoke the command-line utility diskutilwith the option -plist
to get the info in the plist format.
也就是说,调用命令行实用程序diskutil并选择-plist
以 plist 格式获取信息可能会更容易。
The sample code FSMegaInfomight also be instructive how to get much more info about a disk.
示例代码FSMegaInfo也可能对如何获取有关磁盘的更多信息有指导意义。
回答by valexa
The drive ID's can be retrieved from the IORegistry as follows:
可以从 IORegistry 中检索驱动器 ID,如下所示:
Internal drives: IOAHCIBlockStorageDevice
string
property "Serial Number" inside "Device Characteristics" e.g.: (WD-WCAV5D1345345)USB drives : IOUSBDevice
string
property "USB Serial Number" e.g.: (5743415654564561373734)FireWire drives : IOReducedBlockServices
number
property "GUID" inside "Protocol Characteristics" e.g.: (407345709348650)Thunderbolt drives: ??
内部驱动器:IOAHCIBlockStorageDevice
string
属性“设备特性”中的“序列号”,例如:(WD-WCAV5D1345345)USB 驱动器:IOUSBDevice
string
属性“USB 序列号”,例如:(5743415654564561373734)FireWire 驱动器:
number
“协议特征”中的IOReducedBlockServices属性“GUID”,例如:(407345709348650)迅雷驱动器:??
These ID's are persistent meaning the same external drives connected to different machines will show the same ID.
这些 ID 是持久的,这意味着连接到不同机器的相同外部驱动器将显示相同的 ID。
回答by Robert Jansson
The following will list the serial numbers on the SATA-bus. You don't get to know which device it is as is but you can do it with some scripting/parsing. I've used "sed" to remove all the spaces and "awk" to isolate just the serial in case you are not familiar:
下面将列出 SATA 总线上的序列号。你不知道它是哪个设备,但你可以通过一些脚本/解析来做到这一点。如果您不熟悉,我已经使用“sed”来删除所有空格并使用“awk”来隔离序列号:
$ system_profiler SPSerialATADataType -detailLevel medium | grep Serial | sed -e 's/[\<\>\"\ ]//g' | -F':' '{print }'
回答by Georg Sch?lly
回答by V1ru8
maybe you can just place a hidden file on a hard disk that your app has used? That's e.g. how apple's Time Machine does it.
也许您可以将隐藏文件放在您的应用程序使用过的硬盘上?例如,苹果的 Time Machine 就是这样做的。