Girish Kolari
Girish Kolari

Reputation: 2515

How to get serial number from Mac hard disks?

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.

Upvotes: 14

Views: 28593

Answers (8)

John
John

Reputation: 486

This is a very old thread and also the first search result so I will add another useful method that relies on installing homebrew and smartmontools

Install homebrew, then...

brew install smartmontools

You can then run

smartctl -x disk0s1 | grep "Serial Number"

You will need to use the diskutil list command to find the disk ID. I used "disk0s1" above but you will enter your own.

Upvotes: 1

Robert Jansson
Robert Jansson

Reputation: 31

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

Upvotes: 3

Matthias Braun
Matthias Braun

Reputation: 34353

From the command line:

ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial

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

Upvotes: 9

valexa
valexa

Reputation: 4503

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.

Upvotes: 1

Yuji
Yuji

Reputation: 34185

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.

Upvotes: 6

diciu
diciu

Reputation: 29333

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

Upvotes: 13

Georg Sch&#246;lly
Georg Sch&#246;lly

Reputation: 126105

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.

Upvotes: 2

V1ru8
V1ru8

Reputation: 6147

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.

Upvotes: -1

Related Questions