求助:macOS10.15+下用DriverKit开发磁盘读取dext替代kext遇阻
Hey there, let's work through how to port your disk-reading functionality from a traditional IOKit KEXT to a DriverKit system extension (DEXT) — I’ve navigated this transition before, so here’s a clear breakdown of what you need to know:
Key Differences & Replacement for IOMedia
First off, DriverKit doesn’t have a direct 1:1 replacement for IOMedia — instead, you’ll work with the IOStorageDriverKit framework, which is DriverKit’s dedicated suite for storage device interactions. Here’s how to map your old IOMedia-based logic:
- Instead of interacting with
IOMediaobjects, you’ll useIOStorageDeviceto represent the underlying storage device. This class gives you access to critical device properties like block size, total block count, and device identifiers. - Your core driver logic will live in a subclass of
IOStorageDriver— this is where you’ll handle device initialization, read/write operations, and cleanup.
Understanding TARGET_OS_DRIVERKIT=1
That macro being set to 1 is totally expected when working in a DriverKit project! DriverKit is a sandboxed, user-space replacement for kernel extensions, so it intentionally blocks access to many legacy IOKit kernel APIs. You can’t import traditional IOKit headers and use kernel-side classes like IOMedia in a DEXT — you have to fully embrace DriverKit’s own frameworks (IOStorageDriverKit, IOKitDriverKit) instead.
Step-by-Step Implementation Tips
- Start with the right template: In Xcode, create a new project using the "DriverKit System Extension" template. This sets up the correct build settings, entitlements, and framework imports automatically.
- Hook into storage device events: Override the
Startmethod in yourIOStorageDriversubclass to initialize theIOStorageDeviceinstance and register for device notifications. - Implement block reading: Use the
ReadBlocksmethod (provided byIOStorageDriver) to handle read requests. Unlike KEXTs, DriverKit uses an asynchronous model — you’ll pass anIOKitCompletioncallback to handle the result of the read operation once it’s done. - Configure entitlements: Your DEXT needs the
com.apple.security.device.storageentitlement in itsInfo.plistto access storage devices. Don’t forget to sign and notarize your extension for distribution (or testing locally with a developer ID).
Testing & Debugging
- Use
ioreg -lto verify that your DEXT is loaded and attached to the target storage device. Look for your driver’s class name in the output. - For testing read operations, you can write a simple user-space helper app that communicates with your DEXT via XPC (DriverKit extensions use XPC to interact with user-space processes).
- Check the system log (Console app, filtering for your driver’s name) for any error messages or debug logs you’ve added.
内容的提问来源于stack exchange,提问作者Dirk

