Is there an easy way to get the serial number of all the hard disks in a Mac using an API?
Basically, I\'m looking for a unique identifier for the hard disk with whi
From the command line:
ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial
This gives you the serial number of the built-in hard disk.
Have a look at IOKit.
There are two handy tools on your Mac to find out about the possibilities of it:
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:
#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);
The drive ID's can be retrieved from the IORegistry as follows:
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: ??
These ID's are persistent meaning the same external drives connected to different machines will show the same ID.
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:
$ system_profiler SPSerialATADataType -detailLevel medium | grep Serial | sed -e 's/[\<\>\"\ ]//g' | -F':' '{print $2}'
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 Arbitration framework, 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.
That said, it might be easier just to invoke the command-line utility diskutil with the option -plist
to get the info in the plist format.
The sample code FSMegaInfo might also be instructive how to get much more info about a disk.