Is there an API to get APFS volume's "role"?

The APFS reference (https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf) mentions APFS volume roles, in particular System, Recovery, Preboot etc. (APFS_VOL_ROLE_SYSTEM, APFS_VOL_ROLE_RECOVERY, APFS_VOL_ROLE_PREBOOT)

and I'm wondering what is the API function to query the role for a given APFS volume?

Accepted Reply

The role is listed as a property of the IOMedia object for the volume. If you can get the DADiskRef for the volume in question you can use the following code to read the property for the role:


CFArrayRef DiskCopyRole(DADiskRef diskRef)
{
  CFArrayRef roles = NULL;
  io_service_t service = DADiskCopyIOMedia(diskRef);

  if (IO_OBJECT_NULL != service)
  {
       roles = IORegistryEntryCreateCFProperty(service, CFSTR("Role"), kCFAllocatorDefault, kNilOptions);
       IOObjectRelease(service);
  }

  return roles;
}

Replies

The role is listed as a property of the IOMedia object for the volume. If you can get the DADiskRef for the volume in question you can use the following code to read the property for the role:


CFArrayRef DiskCopyRole(DADiskRef diskRef)
{
  CFArrayRef roles = NULL;
  io_service_t service = DADiskCopyIOMedia(diskRef);

  if (IO_OBJECT_NULL != service)
  {
       roles = IORegistryEntryCreateCFProperty(service, CFSTR("Role"), kCFAllocatorDefault, kNilOptions);
       IOObjectRelease(service);
  }

  return roles;
}