3

Great information on interrogating mounted drives via IOKit in this question. But is there a way to determine which of the devices returned by IOIteratorNext() is the boot drive? Or better yet, might there be a way to get just the boot drive in the iterator returned by IOServiceGetMatchingServices()?

Community
  • 1
  • 1
theory
  • 9,178
  • 10
  • 59
  • 129

2 Answers2

4

Booting is done from media, not a device per se. Devices have media, media have volumes. I don't believe that volumes are represented in IOKit.

This is probably easiest using Disk Arbitration. Use DADiskCreateFromVolumePath() with the CFURL for /. Then call DADiskCopyDescription() to get the description dictionary. That will include properties of the volume, the media, the device, and even the bus, including IOKit paths if you need them. There's a good chance the information you're looking for is directly in the description dictionary, though.

The NVRAM information cited by Mark Setchell is available from IOKit, too, at path IOService:/AppleACPIPlatformExpert/AppleEFIRuntime/AppleEFINVRAM. There's a property efi-boot-device. Its value is a property list including a service matching dictionary. As you can see, it looks for an entry with provider class of IOMedia whose UUID property is a certain UUID.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thank you, this answer, along with the code example in [this question](http://stackoverflow.com/q/17039361), was exactly what I needed. – theory Nov 11 '14 at 00:08
0

You can use this:

nvram efi-boot-device

efi-boot-device <array><dict><key>IOMatch</key><dict><key>IOProviderClass</key><string>IOMedia</string><key>IOPropertyMatch</key><dict><key>UUID</key><string>78025031-4C42-4FDE-8DD1-A515A2BF6032</string></dict></dict><key>BLLastBSDName</key><string>disk0s3</string></dict></array>%00
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • This is calling a command line tool to do the dirty work. He wants it programmatically. – SevenBits Nov 07 '14 at 23:21
  • You don't always get what you want, and sometimes you have to settle for something close that does the job - maybe until you find something better. There"s no law against writing programs to use command-line tools, you know - that's what `system()` was written for. And there's nothing to install, Apple supply `nvram` on all Macs so it's not an external package dependency. – Mark Setchell Nov 07 '14 at 23:25
  • What @SevenBits said. I can `stat("/")`, but if there is some way to look it up in the registry without having to call `system()` or `stat` the file system, it would be much safer, because I doubt that the alternative would work well in a sandbox. – theory Nov 07 '14 at 23:31
  • Maybe you can use this as a clue... http://www.opensource.apple.com/source/system_cmds/system_cmds-550.10/nvram.tproj/nvram.c – Mark Setchell Nov 07 '14 at 23:39
  • @theory If it won't work programmatically in your app, then it won't work by calling another program. A process created by a sandboxed process inherits the sandbox. This is a security feature; otherwise, the sandbox would be so easily circumvent able it would be useless. – SevenBits Nov 09 '14 at 18:27
  • I would take a look at @Ken Thomases' answer above; it might work. – SevenBits Nov 09 '14 at 18:29