How to detect SSD in Mac OS X?

后端 未结 4 1419
死守一世寂寞
死守一世寂寞 2021-02-06 00:53

Is there a reliable, quick, deterministic way (i.e. not a benchmark) to check whether the system drive Mac OS X is on is a Solid State Drive?

Is there any other

4条回答
  •  囚心锁ツ
    2021-02-06 01:00

    If you're trying to get that kind of information, you're best guess is IOKit.

    You can try some of it's functionality using the ioreg command line tool or the IORegistryExplorer.


    Here's some code that might help you. It fetches all hard drives that aren't a RAID and aren't partitions. This isn't what you want, but it might get you started.

    #import "TWDevice.h"
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    
    @implementation TWDevice
    
    @synthesize name, devicePath, size, blockSize, writable, icon;
    
    + (NSArray *)allDevices {
        // create matching dictionary
        CFMutableDictionaryRef classesToMatch;
        classesToMatch = IOServiceMatching(kIOMediaClass);
        if (classesToMatch == NULL) {
            [NSException raise:@"TWError" format:@"Classes to match could not be created"];
        }
    
        // get iterator of matching services
        io_iterator_t mediaIterator;
        kern_return_t kernResult;
        kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                                                          classesToMatch,
                                                                          &mediaIterator);
    
        if (kernResult != KERN_SUCCESS) {
            [NSException raise:@"TWError" format:@"Matching services did not succed."];
        }
    
        // iterate over all found medias
        io_object_t nextMedia;
        NSMutableArray *detectedDevices = [NSMutableArray array];
        while (nextMedia = IOIteratorNext(mediaIterator)) {
            NSMutableDictionary *properties;
            kernResult = IORegistryEntryCreateCFProperties(nextMedia,
                                                                                      (CFMutableDictionaryRef *)&properties,
                                                                                      kCFAllocatorDefault, 0);
    
            if (kernResult != KERN_SUCCESS) {
                [NSException raise:@"TWError" format:@"Getting properties threw error."];
            }
    
            // is it a whole device or just a partition?
            if ([[properties valueForKey:@"Whole"] boolValue] &&
                ![[properties valueForKey:@"RAID"] boolValue]) {
                TWDevice *device = [[[TWDevice alloc] init] autorelease];
    
                device.devicePath = [NSString stringWithFormat:@"%sr%@", _PATH_DEV, [properties valueForKey:@"BSD Name"]];
                device.blockSize = [[properties valueForKey:@"Preferred Block Size"] unsignedLongLongValue];
                device.writable = [[properties valueForKey:@"Writable"] boolValue];
                device.size = [[properties valueForKey:@"Size"] unsignedLongLongValue];
    
                io_name_t name;
                IORegistryEntryGetName(nextMedia, name);
                device.name = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
    
                …
    
                [detectedDevices addObject:device];
            }
    
            // tidy up
            IOObjectRelease(nextMedia);
            CFRelease(properties);
        }
        IOObjectRelease(mediaIterator);
    
        return detectedDevices;
    }
    
    @end
    

提交回复
热议问题