简体   繁体   中英

How to programmatically determine if a disk is encrypted on OS X?

Given a volume, how do I determine whether it is encrypted or not? I've found stuff like DADiskCopyDescription() and NSURL's getResourceValue:forKey:error: which give a wealth of information, but not whether the volume is encrypted.

Even if there isn't a public API for this then scraping output from a command line tool which ships with the OS would be acceptable. The closest I found was 'diskutil info /dev/disk0', but again no encryption information. Annoyingly the GUI Disk Utility app does provide this information when you click on the blue info button.

You can (ab)use IOKit for this. Note that the CoreStorage Encrypted property is not officially defined anywhere, so this is decidedly not public API. Also, you'll need to inspect the whole disk object that Core Storage offers to the OS (eg disk1), not the partition that the Core Storage LV lives on (eg disk0s2).

const char *bsdDisk = "disk1";

DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, "disk1");

io_service_t diskService = DADiskCopyIOMedia(disk);
CFBooleanRef isEncrypted = IORegistryEntryCreateCFProperty(diskService,
                                                           CFSTR("CoreStorage Encrypted"),
                                                           kCFAllocatorDefault,
                                                           0);

fprintf(stdout,
        "%s %s encrypted\n",
        bsdDisk,
        (CFBooleanGetValue(isEncrypted)) ? "is" : "is not");

CFRelease(isEncrypted);
IOObjectRelease(diskService);
CFRelease(disk);
CFRelease(session);

看起来这个信息可以使用system_profiler -detailLevel basic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM