Tutorial :How to get serial number from Mac hard disks?



Question:

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 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.


Solution:1

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);  


Solution:2

From the command line:

ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial

This gives you the serial number of the built-in hard disk.


Solution:3

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.


Solution:4

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.


Solution:5

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}'  


Solution:6

Have a look at IOKit.

There are two handy tools on your Mac to find out about the possibilities of it:

  • ioreg, a command line tool
  • IORegistryExplorer, the same with a GUI.


Solution:7

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »