How to get serial number from Mac hard disks?

后端 未结 7 715
遇见更好的自我
遇见更好的自我 2020-12-01 10:10

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

相关标签:
7条回答
  • 2020-12-01 10:15

    From the command line:

    ioreg -rd1 -w0 -c AppleAHCIDiskDriver | grep Serial

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

    0 讨论(0)
  • 2020-12-01 10:17

    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.
    0 讨论(0)
  • 2020-12-01 10:26

    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);
    
    0 讨论(0)
  • 2020-12-01 10:27

    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.

    0 讨论(0)
  • 2020-12-01 10:28

    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}'
    
    0 讨论(0)
  • 2020-12-01 10:32

    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.

    0 讨论(0)
提交回复
热议问题